Skip to content

Commit 27b8789

Browse files
committed
Add --yes to cdxgen
1 parent 236bd85 commit 27b8789

File tree

3 files changed

+92
-99
lines changed

3 files changed

+92
-99
lines changed

src/commands/cdxgen/cmd-cdxgen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const yargsConfig = {
6666
recurse: ['r'],
6767
'resolve-class': ['c'],
6868
type: ['t'],
69-
version: ['v']
69+
version: ['v'],
70+
yes: ['y']
7071
},
7172
array: [
7273
{ key: 'author', type: 'string' },
@@ -90,7 +91,10 @@ const yargsConfig = {
9091
'required-only',
9192
'server',
9293
'validate',
93-
'version'
94+
'version',
95+
// The --yes flag and -y alias map to the corresponding flag and alias of npx.
96+
// https://docs.npmjs.com/cli/v7/commands/npx#compatibility-with-older-npx-versions
97+
'yes'
9498
],
9599
string: [
96100
'api-key',

src/commands/cdxgen/run-cyclonedx.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'
1010
import constants from '../../constants'
1111
import shadowBin from '../../shadow/npm/bin'
1212

13-
const { NPM, NPX, PNPM } = constants
13+
const { NPM, NPX, PACKAGE_LOCK, PNPM, YARN } = constants
1414

1515
const nodejsPlatformTypes = new Set([
1616
'javascript',
@@ -25,18 +25,20 @@ const nodejsPlatformTypes = new Set([
2525

2626
export async function runCycloneDX(yargv: any) {
2727
let cleanupPackageLock = false
28+
const yesArgs = yargv.yes ? ['--yes'] : []
2829
if (
29-
yargv.type !== 'yarn' &&
30+
yargv.type !== YARN &&
3031
nodejsPlatformTypes.has(yargv.type) &&
3132
existsSync('./yarn.lock')
3233
) {
33-
if (existsSync('./package-lock.json')) {
34+
if (existsSync(`./${PACKAGE_LOCK}`)) {
3435
yargv.type = NPM
3536
} else {
3637
// Use synp to create a package-lock.json from the yarn.lock,
3738
// based on the node_modules folder, for a more accurate SBOM.
3839
try {
3940
await shadowBin(NPX, [
41+
...yesArgs,
4042
4143
'--',
4244
'--source-file',
@@ -48,13 +50,14 @@ export async function runCycloneDX(yargv: any) {
4850
}
4951
}
5052
await shadowBin(NPX, [
53+
...yesArgs,
5154
'@cyclonedx/[email protected]',
5255
'--',
5356
...argvToArray(yargv)
5457
])
5558
if (cleanupPackageLock) {
5659
try {
57-
await fs.rm('./package-lock.json')
60+
await fs.rm(`./${PACKAGE_LOCK}`)
5861
} catch {}
5962
}
6063
const fullOutputPath = path.join(process.cwd(), yargv.output)

test/socket-cdxgen.test.ts

Lines changed: 79 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -20,105 +20,91 @@ const spawnOpts: PromiseSpawnOptions = {
2020
cwd: npmFixturesPath
2121
}
2222

23-
describe(
24-
'Socket cdxgen command',
25-
{
26-
// Skip until we think of how to handle the output test.
27-
skip: true
28-
},
29-
async () => {
30-
// Lazily access constants.rootBinPath.
31-
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
23+
describe('Socket cdxgen command', async () => {
24+
// Lazily access constants.rootBinPath.
25+
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)
3226

33-
it(
34-
'should forwards known commands to cdxgen',
35-
{
36-
// Takes ~10s in CI
37-
timeout: 20_000
38-
},
39-
async () => {
40-
for (const command of ['-h', '--help']) {
41-
// eslint-disable-next-line no-await-in-loop
42-
const ret = await spawn(
43-
// Lazily access constants.execPath.
44-
constants.execPath,
45-
[entryPath, 'cdxgen', command],
46-
spawnOpts
47-
)
48-
expect(
49-
ret.stdout.includes('cdxgen'),
50-
'forwards commands to cdxgen'
51-
).toBe(true)
52-
}
27+
it(
28+
'should forwards known commands to cdxgen',
29+
{
30+
// Takes ~10s in CI
31+
timeout: 20_000
32+
},
33+
async () => {
34+
for (const command of ['-h', '--help']) {
35+
// eslint-disable-next-line no-await-in-loop
36+
const ret = await spawn(
37+
// Lazily access constants.execPath.
38+
constants.execPath,
39+
[entryPath, 'cdxgen', '--yes', command],
40+
spawnOpts
41+
)
42+
expect(
43+
ret.stdout.includes('cdxgen'),
44+
'forwards commands to cdxgen'
45+
).toBe(true)
5346
}
54-
)
47+
}
48+
)
5549

56-
describe(
57-
'command forwarding',
58-
{
59-
// Skip until we think of how to handle the output test.
60-
skip: true
61-
},
62-
async () => {
63-
expect.extend({
64-
toHaveStderrStartWith(received, expected) {
65-
const { isNot } = this
66-
return {
67-
// do not alter your "pass" based on isNot. Vitest does it for you
68-
pass: received?.stderr?.startsWith?.(expected) ?? false,
69-
message: () =>
70-
`spawn.stderr did${isNot ? ' not' : ''} start with \`${expected}\`: ${received?.stderr}`
71-
}
72-
}
73-
})
50+
describe('command forwarding', async () => {
51+
expect.extend({
52+
toHaveStderrInclude(received, expected) {
53+
const { isNot } = this
54+
return {
55+
// do not alter your "pass" based on isNot. Vitest does it for you
56+
pass: received?.stderr?.includes?.(expected) ?? false,
57+
message: () =>
58+
`spawn.stderr ${isNot ? 'does NOT include' : 'includes'} \`${expected}\`: ${received?.stderr}`
59+
}
60+
}
61+
})
7462

75-
it('should not forward -u to cdxgen', async () => {
76-
const command = '-u'
77-
await expect(
78-
() =>
79-
spawn(
80-
// Lazily access constants.execPath.
81-
constants.execPath,
82-
[entryPath, 'cdxgen', command],
83-
spawnOpts
84-
)
85-
// @ts-ignore -- toHaveStderrStartWith is defined above
86-
).rejects.toHaveStderrStartWith(
87-
`${LOG_SYMBOLS.fail} Unknown argument: ${command}`
63+
it('should not forward -u to cdxgen', async () => {
64+
const command = '-u'
65+
await expect(
66+
() =>
67+
spawn(
68+
// Lazily access constants.execPath.
69+
constants.execPath,
70+
[entryPath, 'cdxgen', '-y', command],
71+
spawnOpts
8872
)
89-
})
73+
// @ts-ignore -- toHaveStderrInclude is defined above
74+
).rejects.toHaveStderrInclude(
75+
`${LOG_SYMBOLS.fail} Unknown argument: ${command}`
76+
)
77+
})
9078

91-
it('should not forward --unknown to cdxgen', async () => {
92-
const command = '--unknown'
93-
await expect(
94-
() =>
95-
spawn(
96-
// Lazily access constants.execPath.
97-
constants.execPath,
98-
[entryPath, 'cdxgen', command],
99-
spawnOpts
100-
)
101-
// @ts-ignore -- toHaveStderrStartWith is defined above
102-
).rejects.toHaveStderrStartWith(
103-
`${LOG_SYMBOLS.fail} Unknown argument: ${command}`
79+
it('should not forward --unknown to cdxgen', async () => {
80+
const command = '--unknown'
81+
await expect(
82+
() =>
83+
spawn(
84+
// Lazily access constants.execPath.
85+
constants.execPath,
86+
[entryPath, 'cdxgen', '--yes', command],
87+
spawnOpts
10488
)
105-
})
89+
// @ts-ignore -- toHaveStderrInclude is defined above
90+
).rejects.toHaveStderrInclude(
91+
`${LOG_SYMBOLS.fail} Unknown argument: ${command}`
92+
)
93+
})
10694

107-
it('should not forward multiple unknown commands to cdxgen', async () => {
108-
await expect(
109-
() =>
110-
spawn(
111-
// Lazily access constants.execPath.
112-
constants.execPath,
113-
[entryPath, 'cdxgen', '-u', '-h', '--unknown'],
114-
spawnOpts
115-
)
116-
// @ts-ignore -- toHaveStderrStartWith is defined above
117-
).rejects.toHaveStderrStartWith(
118-
`${LOG_SYMBOLS.fail} Unknown arguments: -u, --unknown`
95+
it('should not forward multiple unknown commands to cdxgen', async () => {
96+
await expect(
97+
() =>
98+
spawn(
99+
// Lazily access constants.execPath.
100+
constants.execPath,
101+
[entryPath, 'cdxgen', '-y', '-u', '-h', '--unknown'],
102+
spawnOpts
119103
)
120-
})
121-
}
122-
)
123-
}
124-
)
104+
// @ts-ignore -- toHaveStderrInclude is defined above
105+
).rejects.toHaveStderrInclude(
106+
`${LOG_SYMBOLS.fail} Unknown arguments: -u, --unknown`
107+
)
108+
})
109+
})
110+
})

0 commit comments

Comments
 (0)