Skip to content
Merged
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
15 changes: 1 addition & 14 deletions src/commands/cdxgen/cmd-cdxgen.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
31 changes: 18 additions & 13 deletions src/commands/manifest/cmd-manifest-cdxgen.test.mts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 () => {
Expand Down
49 changes: 42 additions & 7 deletions test/utils.mts
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down Expand Up @@ -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<string, string> }
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,
Expand Down
Loading