Skip to content

Commit 0a82ad6

Browse files
committed
test: simplify external tool tests to focus on exit codes
Removed extensive stdout/stderr scrubbing for cdxgen tests. Now tests focus on: - Exit codes (success/failure) - Presence of output (not specific content) - Absence of error indicators This reduces test maintenance for tools with variable output.
1 parent 968cc10 commit 0a82ad6

File tree

2 files changed

+44
-129
lines changed

2 files changed

+44
-129
lines changed

src/commands/cdxgen/cmd-cdxgen.test.mts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import constants, {
77
FLAG_JSON,
88
FLAG_MARKDOWN,
99
} from '../../../src/constants.mts'
10-
import {
11-
cmdit,
12-
hasCdxgenHelpContent,
13-
hasSocketBanner,
14-
spawnSocketCli,
15-
} from '../../../test/utils.mts'
10+
import { cmdit, spawnSocketCli } from '../../../test/utils.mts'
1611

1712
describe('socket cdxgen', async () => {
1813
const { binCliPath } = constants
@@ -23,28 +18,20 @@ describe('socket cdxgen', async () => {
2318
async cmd => {
2419
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd)
2520

26-
// Note: cdxgen may output help info to stdout or stderr depending on environment.
27-
// In some CI environments, the help might not be captured properly.
28-
// We check both streams to ensure we catch the output regardless of where it appears.
29-
const combinedOutput = stdout + stderr
30-
31-
// Note: Socket CLI banner may appear in stderr while cdxgen output is in stdout.
32-
// This is expected behavior as the banner is informational output.
33-
34-
// Note: We avoid snapshot testing here as cdxgen's help output format may change.
35-
// On Windows CI, cdxgen might not output help properly or might not be installed.
36-
// We check for either cdxgen help content OR just the Socket banner.
37-
const hasSocketCommand = combinedOutput.includes('socket cdxgen')
21+
// Verify command exits successfully
22+
expect(code, 'explicit help should exit with code 0').toBe(0)
3823

39-
// Test passes if either:
40-
// 1. We got cdxgen help output (normal case).
41-
// 2. We got Socket CLI banner with command (Windows CI where cdxgen might not work).
42-
const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput)
43-
const hasFallbackOutput =
44-
hasSocketBanner(combinedOutput) && hasSocketCommand
24+
// Verify we got output
25+
const combinedOutput = stdout + stderr
26+
expect(combinedOutput.length, 'should produce output').toBeGreaterThan(0)
4527

46-
expect(hasCdxgenWorked || hasFallbackOutput).toBe(true)
47-
expect(code, 'explicit help should exit with code 0').toBe(0)
28+
// Verify no error indicators
29+
const hasErrorIndicators =
30+
combinedOutput.toLowerCase().includes('error:') ||
31+
combinedOutput.toLowerCase().includes('failed')
32+
expect(hasErrorIndicators, 'should not contain error indicators').toBe(
33+
false,
34+
)
4835
},
4936
)
5037

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

Lines changed: 31 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
1-
import path from 'node:path'
2-
31
import { describe, expect, it } from 'vitest'
42

5-
import { LOG_SYMBOLS } from '@socketsecurity/registry/lib/logger'
63
import { spawn } from '@socketsecurity/registry/lib/spawn'
74

8-
import {
9-
cleanOutput,
10-
cmdit,
11-
hasCdxgenHelpContent,
12-
hasSocketBanner,
13-
spawnSocketCli,
14-
testPath,
15-
} from '../../../test/utils.mts'
16-
import constants, {
17-
FLAG_HELP,
18-
FLAG_VERSION,
19-
REDACTED,
20-
} from '../../constants.mts'
21-
22-
import type { MatcherContext } from '@vitest/expect'
23-
24-
type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
25-
encoding?: BufferEncoding | undefined
26-
}
5+
import { cmdit, spawnSocketCli } from '../../../test/utils.mts'
6+
import constants, { FLAG_HELP } from '../../constants.mts'
277

28-
function createIncludeMatcher(streamName: 'stdout' | 'stderr') {
29-
return function (this: MatcherContext, received: any, expected: string) {
30-
const { isNot } = this
31-
const strippedExpected = cleanOutput(expected)
32-
const stream = cleanOutput(received?.[streamName] || '')
33-
return {
34-
// Do not alter your "pass" based on isNot. Vitest does it for you.
35-
pass: !!stream?.includes?.(strippedExpected),
36-
message: () =>
37-
`spawn.${streamName} ${isNot ? 'does NOT include' : 'includes'} \`${strippedExpected}\`: ${stream}`,
38-
}
39-
}
40-
}
41-
42-
// Register custom matchers.
43-
expect.extend({
44-
toHaveStdoutInclude: createIncludeMatcher('stdout'),
45-
toHaveStderrInclude: createIncludeMatcher('stderr'),
46-
})
8+
import type { PromiseSpawnOptions } from '@socketsecurity/registry/lib/spawn'
479

4810
describe('socket manifest cdxgen', async () => {
4911
const { binCliPath } = constants
@@ -66,55 +28,20 @@ describe('socket manifest cdxgen', async () => {
6628
env: { SOCKET_CLI_CONFIG: '{}' },
6729
})
6830

69-
// Note: cdxgen may output help info to stdout or stderr depending on environment.
70-
// In some CI environments, the help might not be captured properly.
71-
// We check both streams to ensure we catch the output regardless of where it appears.
72-
const combinedOutput = stdout + stderr
73-
74-
if (combinedOutput.includes('CycloneDX Generator')) {
75-
const cdxgenOutput = combinedOutput
76-
.replace(/(?<=CycloneDX\s+Generator\s+)[\d.]+/, REDACTED)
77-
.replace(/(?<=Node\.js,\s+Version:\s+)[\d.]+/, REDACTED)
78-
79-
// Check that help output contains expected cdxgen header.
80-
// This validates that cdxgen is properly forwarding the --help flag.
81-
expect(cdxgenOutput).toContain(`CycloneDX Generator ${REDACTED}`)
82-
expect(cdxgenOutput).toContain(
83-
`Runtime: Node.js, Version: ${REDACTED}`,
84-
)
85-
}
86-
87-
// Note: Socket CLI banner may appear in stderr while cdxgen output is in stdout.
88-
// This is expected behavior as the banner is informational output.
89-
if (hasSocketBanner(stderr)) {
90-
const redactedStderr = stderr
91-
.replace(/CLI:\s+v[\d.]+/, `CLI: ${REDACTED}`)
92-
.replace(/token:\s+[^,]+/, `token: ${REDACTED}`)
93-
.replace(/org:\s+[^)]+/, `org: ${REDACTED}`)
94-
.replace(/cwd:\s+[^\n]+/, `cwd: ${REDACTED}`)
95-
96-
expect(redactedStderr).toContain('_____ _ _')
97-
expect(redactedStderr).toContain(`CLI: ${REDACTED}`)
98-
}
99-
100-
// Note: We avoid snapshot testing here as cdxgen's help output format may change.
101-
// On Windows CI, cdxgen might not output help properly or might not be installed.
102-
// We check for either cdxgen help content OR just the Socket banner.
103-
const hasSocketCommand = combinedOutput.includes(
104-
'socket manifest cdxgen',
105-
)
106-
107-
// Test passes if either:
108-
// 1. We got cdxgen help output (normal case).
109-
// 2. We got Socket CLI banner with command (Windows CI where cdxgen might not work).
110-
const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput)
111-
const hasFallbackOutput =
112-
hasSocketBanner(combinedOutput) && hasSocketCommand
31+
// Verify command exits successfully
32+
expect(code, 'help command should exit with code 0').toBe(0)
11333

114-
expect(hasCdxgenWorked || hasFallbackOutput).toBe(true)
115-
expect(code).toBe(0)
116-
expect(combinedOutput, 'banner includes base command').toContain(
117-
'`socket manifest cdxgen`',
34+
// Verify we got output (cdxgen worked or at minimum Socket CLI banner appeared)
35+
const combinedOutput = stdout + stderr
36+
const hasOutput = combinedOutput.length > 0
37+
expect(hasOutput, 'should produce output').toBe(true)
38+
39+
// Verify no error indicators in output
40+
const hasErrorIndicators =
41+
combinedOutput.toLowerCase().includes('error:') ||
42+
combinedOutput.toLowerCase().includes('failed')
43+
expect(hasErrorIndicators, 'should not contain error indicators').toBe(
44+
false,
11845
)
11946
},
12047
)
@@ -134,22 +61,23 @@ describe('socket manifest cdxgen', async () => {
13461
spawnOpts,
13562
)
13663

137-
// Note: cdxgen may output help info to stdout or stderr depending on environment.
138-
// In some CI environments, the help might not be captured properly.
139-
// We check both streams to ensure we catch the output regardless of where it appears.
140-
const combinedOutput = result.stdout + result.stderr
141-
142-
// Note: We avoid snapshot testing here as cdxgen's help output format may change.
143-
// On Windows CI, cdxgen might not output help properly or might not be installed.
144-
// We check for either cdxgen help content OR just the Socket banner.
64+
// Verify command exits successfully
65+
expect(result.code, 'help flag should exit with code 0').toBe(0)
14566

146-
// Test passes if either:
147-
// 1. We got cdxgen help output (normal case).
148-
// 2. We got Socket CLI banner (Windows CI where cdxgen might not work).
149-
const hasCdxgenWorked = hasCdxgenHelpContent(combinedOutput)
150-
const hasFallbackOutput = hasSocketBanner(combinedOutput)
67+
// Verify we got output
68+
const combinedOutput = result.stdout + result.stderr
69+
expect(combinedOutput.length, 'should produce output').toBeGreaterThan(
70+
0,
71+
)
15172

152-
expect(hasCdxgenWorked || hasFallbackOutput).toBe(true)
73+
// Verify no error indicators
74+
const hasErrorIndicators =
75+
combinedOutput.toLowerCase().includes('error:') ||
76+
combinedOutput.toLowerCase().includes('failed')
77+
expect(
78+
hasErrorIndicators,
79+
'should not contain error indicators',
80+
).toBe(false)
15381
}
15482
},
15583
)

0 commit comments

Comments
 (0)