Skip to content

Commit 63d8a66

Browse files
committed
Dry up cdxgen tests
1 parent e043159 commit 63d8a66

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

src/commands/manifest/cdxgen-integration.test.mts

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,32 @@ import { LOG_SYMBOLS } from '@socketsecurity/registry/lib/logger'
77
import { spawn } from '@socketsecurity/registry/lib/spawn'
88
import { stripAnsi } from '@socketsecurity/registry/lib/strings'
99

10-
import { testPath } from '../../../test/utils.mts'
10+
import { cleanOutput, testPath } from '../../../test/utils.mts'
1111
import constants from '../../constants.mts'
1212

1313
type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
1414
encoding?: BufferEncoding | undefined
1515
}
1616

17+
function createIncludeMatcher(streamName: 'stdout' | 'stderr') {
18+
return function (received: any, expected: string) {
19+
const { isNot } = this
20+
const strippedExpected = cleanOutput(expected)
21+
const stream = cleanOutput(received?.[streamName] || '')
22+
return {
23+
// Do not alter your "pass" based on isNot. Vitest does it for you.
24+
pass: !!stream?.includes?.(strippedExpected),
25+
message: () =>
26+
`spawn.${streamName} ${isNot ? 'does NOT include' : 'includes'} \`${strippedExpected}\`: ${stream}`,
27+
}
28+
}
29+
}
30+
31+
expect.extend({
32+
toHaveStdoutInclude: createIncludeMatcher('stdout'),
33+
toHaveStderrInclude: createIncludeMatcher('stderr'),
34+
})
35+
1736
describe('Socket manifest cdxgen command', async () => {
1837
const { binCliPath } = constants
1938

@@ -26,44 +45,30 @@ describe('Socket manifest cdxgen command', async () => {
2645
},
2746
}
2847

29-
it.skipIf(constants.WIN32 && constants.ENV.CI)(
30-
'should forwards known commands to cdxgen',
31-
{
32-
// Increase timeout for CI environments where cdxgen downloads can be slow.
33-
timeout: 60_000,
34-
},
35-
async () => {
36-
for (const command of ['-h', '--help']) {
37-
// eslint-disable-next-line no-await-in-loop
38-
const output = await spawn(
39-
constants.execPath,
40-
[binCliPath, 'manifest', 'cdxgen', command],
41-
spawnOpts,
42-
)
43-
expect(
44-
output.stdout.includes('CycloneDX Generator'),
45-
'forwards commands to cdxgen',
46-
).toBe(true)
47-
}
48-
},
49-
)
50-
5148
describe('command forwarding', async () => {
52-
expect.extend({
53-
toHaveStderrInclude(received, expected) {
54-
const { isNot } = this
55-
const strippedExpected = stripAnsi(expected)
56-
const stderr = received?.stderr
57-
return {
58-
// do not alter your "pass" based on isNot. Vitest does it for you
59-
pass: stderr?.includes?.(strippedExpected) ?? false,
60-
message: () =>
61-
`spawn.stderr ${isNot ? 'does NOT include' : 'includes'} \`${strippedExpected}\`: ${stderr}`,
49+
it.skipIf(constants.WIN32 && constants.ENV.CI)(
50+
'should forward known flags to cdxgen',
51+
{
52+
// Increase timeout for CI environments where cdxgen downloads can be slow.
53+
timeout: 60_000,
54+
},
55+
async () => {
56+
for (const command of ['-h', '--help']) {
57+
// eslint-disable-next-line no-await-in-loop
58+
await expect(
59+
() =>
60+
spawn(
61+
constants.execPath,
62+
[binCliPath, 'manifest', 'cdxgen', command],
63+
spawnOpts,
64+
),
65+
// @ts-ignore toHaveStdoutInclude is defined above.
66+
).resolves.toHaveStdoutInclude('CycloneDX Generator')
6267
}
6368
},
64-
})
69+
)
6570

66-
it('should not forward -u to cdxgen', async () => {
71+
it('should not forward an unknown short flag to cdxgen', async () => {
6772
const command = '-u'
6873
await expect(
6974
() =>
@@ -72,13 +77,13 @@ describe('Socket manifest cdxgen command', async () => {
7277
[binCliPath, 'manifest', 'cdxgen', command],
7378
spawnOpts,
7479
),
75-
// @ts-ignore toHaveStderrInclude is defined above
80+
// @ts-ignore toHaveStderrInclude is defined above.
7681
).rejects.toHaveStderrInclude(
7782
`${LOG_SYMBOLS.fail} Unknown argument: ${command}`,
7883
)
7984
})
8085

81-
it('should not forward --unknown to cdxgen', async () => {
86+
it('should not forward an unknown flag to cdxgen', async () => {
8287
const command = '--unknown'
8388
await expect(
8489
() =>
@@ -93,7 +98,7 @@ describe('Socket manifest cdxgen command', async () => {
9398
)
9499
})
95100

96-
it('should not forward multiple unknown commands to cdxgen', async () => {
101+
it('should not forward multiple unknown flags to cdxgen', async () => {
97102
await expect(
98103
() =>
99104
spawn(

0 commit comments

Comments
 (0)