Skip to content
Prev Previous commit
Next Next commit
test: add ${pid} placeholder test for --cpu-prof-name
  • Loading branch information
haramj committed Jul 16, 2025
commit 09a2458bde1c393535736d95fea6cf143f6d0533
5 changes: 3 additions & 2 deletions src/inspector_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,9 @@ static std::string ReplacePlaceholders(const std::string& pattern) {
for (const auto& [placeholder, getter] : kPlaceholderMap) {
size_t pos = 0;
while ((pos = result.find(placeholder, pos)) != std::string::npos) {
result.replace(pos, placeholder.length(), getter());
pos += getter().length();
const std::string value = getter();
result.replace(pos, placeholder.length(), value);
pos += value.length();
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/sequential/test-cpu-prof-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const fixtures = require('../common/fixtures');
common.skipIfInspectorDisabled();

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');

const tmpdir = require('../common/tmpdir');
Expand Down Expand Up @@ -41,3 +43,36 @@ const {
assert.deepStrictEqual(profiles, [file]);
verifyFrames(output, file, 'fibonacci.js');
}

// --cpu-prof-name with ${pid} placeholder
{
tmpdir.refresh();
// eslint-disable-next-line no-template-curly-in-string
const profName = 'CPU.${pid}.cpuprofile';
const dir = tmpdir.path;

const output = spawnSync(process.execPath, [
'--cpu-prof',
'--cpu-prof-interval',
kCpuProfInterval,
'--cpu-prof-name',
profName,
fixtures.path('workload', 'fibonacci.js'),
], {
cwd: dir,
env
});

if (output.status !== 0) {
console.error(output.stderr.toString());
}

assert.strictEqual(output.status, 0);

const expectedFile = path.join(dir, `CPU.${output.pid}.cpuprofile`);
assert.ok(fs.existsSync(expectedFile), `Expected file ${expectedFile} not found.`);

verifyFrames(output, expectedFile, 'fibonacci.js');

fs.unlinkSync(expectedFile);
}
Loading