From bb3b5834eb8b9270f12d6a5e181acfc54057d9c6 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 2 Dec 2026 23:02:35 -0500 Subject: [PATCH] test(manifest): isolate the offline cdxgen launcher --- src/commands/cdxgen/cmd-cdxgen.test.mts | 15 +----- .../manifest/cmd-manifest-cdxgen.test.mts | 31 +++++++----- test/utils.mts | 49 ++++++++++++++++--- 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/src/commands/cdxgen/cmd-cdxgen.test.mts b/src/commands/cdxgen/cmd-cdxgen.test.mts index 2077b467b..35e8ca68f 100644 --- a/src/commands/cdxgen/cmd-cdxgen.test.mts +++ b/src/commands/cdxgen/cmd-cdxgen.test.mts @@ -3,27 +3,14 @@ import { describe, expect } from 'vitest' import constants, { FLAG_CONFIG, FLAG_DRY_RUN, - FLAG_HELP, FLAG_JSON, FLAG_MARKDOWN, } from '../../../src/constants.mts' -import { cmdit, spawnCdxgenHelp, spawnSocketCli } from '../../../test/utils.mts' +import { cmdit, spawnSocketCli } from '../../../test/utils.mts' describe('socket cdxgen', async () => { const { binCliPath } = constants - cmdit( - ['cdxgen', FLAG_HELP, FLAG_CONFIG, '{}'], - `should support ${FLAG_HELP}`, - async cmd => { - const { code, stderr, stdout } = await spawnCdxgenHelp(cmd) - expect(code).toBe(0) - expect(stdout + stderr).toContain('CycloneDX Generator') - expect(stdout + stderr).toContain('cdxgen [command]') - expect(stderr).toContain('`socket cdxgen`') - }, - ) - cmdit( ['cdxgen', FLAG_DRY_RUN, FLAG_CONFIG, '{}'], 'should handle dry-run without path', diff --git a/src/commands/manifest/cmd-manifest-cdxgen.test.mts b/src/commands/manifest/cmd-manifest-cdxgen.test.mts index 5c4c588fa..9ea8a9ed0 100644 --- a/src/commands/manifest/cmd-manifest-cdxgen.test.mts +++ b/src/commands/manifest/cmd-manifest-cdxgen.test.mts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import { cleanOutput, cmdit, spawnCdxgenHelp } from '../../../test/utils.mts' -import constants, { FLAG_HELP } from '../../constants.mts' +import constants, { FLAG_CONFIG, FLAG_HELP } from '../../constants.mts' import { LOG_SYMBOLS } from '../../utils/logger.mts' import { spawn } from '../../utils/spawn.mts' @@ -43,18 +43,23 @@ describe('socket manifest cdxgen', async () => { } describe('command forwarding', async () => { - for (const flag of ['-h', FLAG_HELP]) { - cmdit( - ['manifest', 'cdxgen', flag], - 'forwards help to cdxgen', - async cmd => { - const { code, stderr, stdout } = await spawnCdxgenHelp(cmd) - expect(code).toBe(0) - expect(stdout + stderr).toContain('CycloneDX Generator') - expect(stdout + stderr).toContain('cdxgen [command]') - expect(stderr).toContain('`socket manifest cdxgen`') - }, - ) + for (const { args, banner } of [ + { + args: ['manifest', 'cdxgen', '-h'], + banner: '`socket manifest cdxgen`', + }, + { + args: ['cdxgen', FLAG_HELP, FLAG_CONFIG, '{}'], + banner: '`socket cdxgen`', + }, + ]) { + cmdit(args, 'forwards help to cdxgen', async cmd => { + const { code, stderr, stdout } = await spawnCdxgenHelp(cmd) + expect(code, `cdxgen help exited ${code}\n${stdout}\n${stderr}`).toBe(0) + expect(stdout + stderr).toContain('CycloneDX Generator') + expect(stdout + stderr).toContain('cdxgen [command]') + expect(stderr).toContain(banner) + }) } it('should not forward an unknown short flag to cdxgen', async () => { diff --git a/test/utils.mts b/test/utils.mts index a6befe4c0..c436ac678 100644 --- a/test/utils.mts +++ b/test/utils.mts @@ -1,8 +1,16 @@ -import { mkdtemp, rm, symlink, writeFile } from 'node:fs/promises' +import { + mkdir, + mkdtemp, + readFile, + rm, + symlink, + writeFile, +} from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { fileURLToPath } from 'node:url' +import cmdShim from 'cmd-shim' import { it } from 'vitest' import constants, { @@ -145,12 +153,39 @@ export function cleanOutput(output: string): string { export async function spawnCdxgenHelp(args: string[]) { const cwd = await mkdtemp(path.join(os.tmpdir(), 'socket-cdxgen-help-')) try { - // Outside the pnpm workspace, npx can run the pinned devDependency locally. - await writeFile(path.join(cwd, 'package.json'), '{"private":true}') - await symlink( - path.resolve(testPath, '../node_modules'), - path.join(cwd, 'node_modules'), - 'junction', + // pnpm links this direct devDependency into the workspace's node_modules + // with either a local or a global virtual store. + const packageDirectory = path.resolve( + testPath, + '../node_modules/@cyclonedx/cdxgen', + ) + const packageMetadata = JSON.parse( + await readFile(path.join(packageDirectory, 'package.json'), 'utf8'), + ) as { version: string; bin: Record } + const packagePath = path.join(cwd, 'node_modules/@cyclonedx/cdxgen') + const binDirectory = path.join(cwd, 'node_modules/.bin') + const cdxgenEntry = packageMetadata.bin['cdxgen'] + if (!cdxgenEntry) { + throw new Error('@cyclonedx/cdxgen does not declare its cdxgen binary') + } + // Pin the fixture's dependency to the version already installed in the workspace. + await writeFile( + path.join(cwd, 'package.json'), + JSON.stringify({ + private: true, + dependencies: { '@cyclonedx/cdxgen': packageMetadata.version }, + }), + ) + // Create the @cyclonedx scope directory for the package link. + await mkdir(path.dirname(packagePath), { recursive: true }) + // Create the fixture's .bin directory where npm looks for cdxgen. + await mkdir(binDirectory, { recursive: true }) + // Link the installed package into the fixture so npm can run it offline. + await symlink(packageDirectory, packagePath, 'junction') + // Generate a launcher here because pnpm's Windows launcher uses workspace-relative paths. + await cmdShim( + path.join(packagePath, cdxgenEntry), + path.join(binDirectory, 'cdxgen'), ) return await spawnSocketCli(constants.binCliPath, args, { cwd,