Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/cpu-prof/src/lib/cpu/load-cpu-profiles.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { vol } from 'memfs';
import { join } from 'node:path';
import { loadCpuProfiles, type CpuProfileFilePath } from './load-cpu-profiles';

describe('loadCpuProfiles', () => {
Expand All @@ -18,10 +19,14 @@ describe('loadCpuProfiles', () => {
testDir
);

const expectedProfile1Path =
`${testDir}/${profile1Name}` as CpuProfileFilePath;
const expectedProfile2Path =
`${testDir}/${profile2Name}` as CpuProfileFilePath;
const expectedProfile1Path = join(
testDir,
profile1Name
) as CpuProfileFilePath;
const expectedProfile2Path = join(
testDir,
profile2Name
) as CpuProfileFilePath;

await expect(loadCpuProfiles(testDir)).resolves.toStrictEqual([
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
afterAll,
} from 'vitest';
import { runWithCpuProf } from './run-with-cpu-prof';
import { join } from 'node:path';
import { access, readdir, rm, mkdir } from 'node:fs/promises';
import { join, sep } from 'node:path';
import { access, readdir, rm, mkdir, writeFile } from 'node:fs/promises';

vi.mock('ansis', () => ({
green: (s: string) => s,
Expand All @@ -34,7 +34,8 @@ describe('runWithCpuProf', () => {
let logger: { log: ReturnType<typeof vi.fn> };
let originalEnv: NodeJS.ProcessEnv;
let cpuProfDir: string;
const root = join(process.cwd(), 'tmp', 'run-with-cpu-prof');
const originalCwd = process.cwd();
const root = join(originalCwd, 'tmp', 'run-with-cpu-prof');

beforeAll(async () => {
await rm(profilesDir, { recursive: true, force: true });
Expand All @@ -52,10 +53,58 @@ describe('runWithCpuProf', () => {
afterEach(async () => {
vi.restoreAllMocks();
process.env = originalEnv;
process.chdir(originalCwd);
await rm(profilesDir, { recursive: true, force: true });
await mkdir(profilesDir, { recursive: true });
});

it.runIf(process.allowedNodeEnvironmentFlags.has('--cpu-prof')).each([
{ name: 'the default profiles directory', directory: undefined },
{ name: 'an explicit absolute directory', directory: ['_dev', 'profiles'] },
{
name: 'a directory containing spaces',
directory: ['_dev', 'workspace with spaces', 'profiles'],
},
{
name: 'a directory with a trailing separator',
directory: ['_dev', 'trailing', 'profiles', ''],
},
])(
'should collect main and child profiles in $name',
async ({ directory }) => {
const scriptPath = join(profilesDir, 'profile.cjs');
await writeFile(
scriptPath,
`const { spawnSync } = require('node:child_process');
const child = spawnSync(process.execPath, ['--eval', '1 + 1'], { cwd: __dirname });
process.exitCode = child.status ?? 1;
`
);
process.chdir(profilesDir);
const cpuProfDir = directory
? join(profilesDir, ...directory) + (directory.at(-1) === '' ? sep : '')
: undefined;
const outputDir = cpuProfDir ?? join(profilesDir, 'profiles');

await expect(
runWithCpuProf(
'node',
{ _: [`"${scriptPath}"`] },
{ cpuProfDir, flagMain: true },
logger
)
).resolves.toEqual({ code: 0 });

const files = await readdir(outputDir);
expect(files.filter((file) => file.endsWith('.cpuprofile'))).toHaveLength(
2
);
expect(
files.filter((file) => file.startsWith('MAIN-CPU--'))
).toHaveLength(1);
}
);

it.skip('should profile a executable like "npm -v"', async () => {
cpuProfDir = join(root, 'npm-v');
await expect(
Expand Down
2 changes: 1 addition & 1 deletion packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function runWithCpuProf(
// Construct the environment variables for executeProcess
const envWithNodeOptions = {
...env,
NODE_OPTIONS: nodeOptionsString,
NODE_OPTIONS: nodeOptionsString.replaceAll('\\', '\\\\'),
};
const result = await executeProcess({
command,
Expand Down