diff --git a/src/commands/cdxgen/cmd-cdxgen.test.mts b/src/commands/cdxgen/cmd-cdxgen.test.mts index 35c4bcc38..2077b467b 100644 --- a/src/commands/cdxgen/cmd-cdxgen.test.mts +++ b/src/commands/cdxgen/cmd-cdxgen.test.mts @@ -7,12 +7,7 @@ import constants, { FLAG_JSON, FLAG_MARKDOWN, } from '../../../src/constants.mts' -import { - cmdit, - hasCdxgenHelpContent, - hasSocketBanner, - spawnSocketCli, -} from '../../../test/utils.mts' +import { cmdit, spawnCdxgenHelp, spawnSocketCli } from '../../../test/utils.mts' describe('socket cdxgen', async () => { const { binCliPath } = constants @@ -21,33 +16,11 @@ describe('socket cdxgen', async () => { ['cdxgen', FLAG_HELP, FLAG_CONFIG, '{}'], `should support ${FLAG_HELP}`, async cmd => { - const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd) - - // Note: cdxgen may output help info to stdout or stderr depending on environment. - // In some CI environments, the help might not be captured properly. - // We check both streams to ensure we catch the output regardless of where it appears. - const combinedOutput = stdout + stderr - - // Note: Socket CLI banner may appear in stderr while cdxgen output is in stdout. - // This is expected behavior as the banner is informational output. - - // Note: We avoid snapshot testing here as cdxgen's help output format may change. - // On Windows CI, cdxgen might not output help properly or might not be installed. - // We check for either cdxgen help content OR just the Socket banner. - const hasSocketCommand = combinedOutput.includes('socket cdxgen') - - // Test passes if either: - // 1. We got cdxgen help output (normal case). - // 2. We got Socket CLI banner with command (Windows CI where cdxgen might not work). - const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput) - const hasFallbackOutput = - hasSocketBanner(combinedOutput) && hasSocketCommand - - expect(hasCdxgenWorked || hasFallbackOutput).toBe(true) - expect( - process.platform === 'win32' ? [0, 1, 127] : [0], - 'help should return normally or tolerate a missing Windows DLX command', - ).toContain(code) + 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`') }, ) diff --git a/src/commands/manifest/cmd-manifest-cdxgen.test.mts b/src/commands/manifest/cmd-manifest-cdxgen.test.mts index e8e1fb313..5c4c588fa 100644 --- a/src/commands/manifest/cmd-manifest-cdxgen.test.mts +++ b/src/commands/manifest/cmd-manifest-cdxgen.test.mts @@ -1,20 +1,7 @@ -import path from 'node:path' - import { describe, expect, it } from 'vitest' -import { - cleanOutput, - cmdit, - hasCdxgenHelpContent, - hasSocketBanner, - spawnSocketCli, - testPath, -} from '../../../test/utils.mts' -import constants, { - FLAG_HELP, - FLAG_VERSION, - REDACTED, -} from '../../constants.mts' +import { cleanOutput, cmdit, spawnCdxgenHelp } from '../../../test/utils.mts' +import constants, { FLAG_HELP } from '../../constants.mts' import { LOG_SYMBOLS } from '../../utils/logger.mts' import { spawn } from '../../utils/spawn.mts' @@ -56,106 +43,19 @@ describe('socket manifest cdxgen', async () => { } describe('command forwarding', async () => { - cmdit( - ['manifest', 'cdxgen', FLAG_HELP], - `should support ${FLAG_HELP}`, - async cmd => { - const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, { - // Need to pass it on as env because --config will break cdxgen. - env: { SOCKET_CLI_CONFIG: '{}' }, - }) - - // Note: cdxgen may output help info to stdout or stderr depending on environment. - // In some CI environments, the help might not be captured properly. - // We check both streams to ensure we catch the output regardless of where it appears. - const combinedOutput = stdout + stderr - - if (combinedOutput.includes('CycloneDX Generator')) { - const cdxgenOutput = combinedOutput - .replace(/(?<=CycloneDX\s+Generator\s+)[\d.]+/, REDACTED) - .replace(/(?<=Node\.js,\s+Version:\s+)[\d.]+/, REDACTED) - - // Check that help output contains expected cdxgen header. - // This validates that cdxgen is properly forwarding the --help flag. - expect(cdxgenOutput).toContain(`CycloneDX Generator ${REDACTED}`) - expect(cdxgenOutput).toContain( - `Runtime: Node.js, Version: ${REDACTED}`, - ) - } - - // Note: Socket CLI banner may appear in stderr while cdxgen output is in stdout. - // This is expected behavior as the banner is informational output. - if (hasSocketBanner(stderr)) { - const redactedStderr = stderr - .replace(/CLI:\s+v[\d.]+/, `CLI: ${REDACTED}`) - .replace(/token:\s+[^,]+/, `token: ${REDACTED}`) - .replace(/org:\s+[^)]+/, `org: ${REDACTED}`) - .replace(/cwd:\s+[^\n]+/, `cwd: ${REDACTED}`) - - expect(redactedStderr).toContain('_____ _ _') - expect(redactedStderr).toContain(`CLI: ${REDACTED}`) - } - - // Note: We avoid snapshot testing here as cdxgen's help output format may change. - // On Windows CI, cdxgen might not output help properly or might not be installed. - // We check for either cdxgen help content OR just the Socket banner. - const hasSocketCommand = combinedOutput.includes( - 'socket manifest cdxgen', - ) - - // Test passes if either: - // 1. We got cdxgen help output (normal case). - // 2. We got Socket CLI banner with command (Windows CI where cdxgen might not work). - const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput) - const hasFallbackOutput = - hasSocketBanner(combinedOutput) && hasSocketCommand - - expect(hasCdxgenWorked || hasFallbackOutput).toBe(true) - expect( - process.platform === 'win32' ? [0, 1, 127] : [0], - 'help should return normally or tolerate a missing Windows DLX command', - ).toContain(code) - expect(combinedOutput, 'banner includes base command').toContain( - '`socket manifest cdxgen`', - ) - }, - { timeout: 60_000 }, - ) - - it.skipIf(process.platform === 'win32')( - 'should forward known flags to cdxgen', - { - // Increase timeout for CI environments where cdxgen downloads can be slow. - timeout: 60_000, - }, - async () => { - for (const command of ['-h', FLAG_HELP]) { - // eslint-disable-next-line no-await-in-loop - const result = await spawn( - constants.execPath, - [binCliPath, 'manifest', 'cdxgen', command], - spawnOpts, - ) - - // Note: cdxgen may output help info to stdout or stderr depending on environment. - // In some CI environments, the help might not be captured properly. - // We check both streams to ensure we catch the output regardless of where it appears. - const combinedOutput = result.stdout + result.stderr - - // Note: We avoid snapshot testing here as cdxgen's help output format may change. - // On Windows CI, cdxgen might not output help properly or might not be installed. - // We check for either cdxgen help content OR just the Socket banner. - - // Test passes if either: - // 1. We got cdxgen help output (normal case). - // 2. We got Socket CLI banner (Windows CI where cdxgen might not work). - const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput) - const hasFallbackOutput = hasSocketBanner(combinedOutput) - - expect(hasCdxgenWorked || hasFallbackOutput).toBe(true) - } - }, - ) + 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`') + }, + ) + } it('should not forward an unknown short flag to cdxgen', async () => { const command = '-u' diff --git a/test/utils.mts b/test/utils.mts index f804e44ec..a6befe4c0 100644 --- a/test/utils.mts +++ b/test/utils.mts @@ -1,3 +1,4 @@ +import { mkdtemp, rm, symlink, writeFile } from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { fileURLToPath } from 'node:url' @@ -6,8 +7,6 @@ import { it } from 'vitest' import constants, { COANA_CLI_VERSION, - FLAG_HELP, - FLAG_VERSION, SOCKET_CLI_API_TOKEN_VARIABLES, } from '../src/constants.mts' import { type SpawnOptions, spawn } from '../src/utils/spawn.mts' @@ -143,35 +142,27 @@ export function cleanOutput(output: string): string { ) } -/** - * Check if output contains cdxgen help content. - * Used to verify cdxgen command executed with help flag. - */ -export function hasCdxgenHelpContent(output: string): boolean { - // Check for various cdxgen help indicators. - // Must have cdxgen or CycloneDX AND at least one help flag indicator. - const hasCdxgenMention = - output.includes('CycloneDX') || output.includes('cdxgen') - const hasHelpFlags = - output.includes(FLAG_HELP) || - output.includes(FLAG_VERSION) || - // cdxgen-specific flags. - output.includes('--output') || - output.includes('--type') - - return hasCdxgenMention && hasHelpFlags -} - -/** - * Check if output contains the Socket CLI banner. - * The banner appears as ASCII art in the stderr output. - * Note: The banner contains either '*' (when --config is used) or '.' (when no config is used). - */ -export function hasSocketBanner(output: string): boolean { - // Check for Socket banner ASCII art lines. - // The banner is always printed as a complete block, never partial. - // Just check for the most distinctive first line. - return output.includes('_____ _ _') +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', + ) + return await spawnSocketCli(constants.binCliPath, args, { + cwd, + env: { + SOCKET_CLI_CONFIG: '{}', + npm_config_offline: 'true', + npm_config_cache: path.join(cwd, '.npm-cache'), + }, + }) + } finally { + await rm(cwd, { recursive: true, force: true }) + } } export type TestCollectorOptions = Exclude[1], undefined>