diff --git a/packages/cpu-prof/src/lib/cpu/load-cpu-profiles.unit.test.ts b/packages/cpu-prof/src/lib/cpu/load-cpu-profiles.unit.test.ts index 8a45b7b..b8eb2ab 100644 --- a/packages/cpu-prof/src/lib/cpu/load-cpu-profiles.unit.test.ts +++ b/packages/cpu-prof/src/lib/cpu/load-cpu-profiles.unit.test.ts @@ -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', () => { @@ -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({ diff --git a/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.integration.test.ts b/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.integration.test.ts index e618f03..fb2a2bd 100644 --- a/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.integration.test.ts +++ b/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.integration.test.ts @@ -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, @@ -34,7 +34,8 @@ describe('runWithCpuProf', () => { let logger: { log: ReturnType }; 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 }); @@ -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( diff --git a/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.ts b/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.ts index 8a9cfa1..2ab9804 100644 --- a/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.ts +++ b/packages/cpu-prof/src/lib/cpu/run-with-cpu-prof.ts @@ -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,