Skip to content

Commit e858d15

Browse files
author
Graydon Hope
committed
fix: return non-zero exit code when scan report data is not healthy
Fixes the issue where socket ci would exit with code 0 even when blocking alerts were found. This is the expected behaviour based on our docs: https://docs.socket.dev/docs/socket-ci#non-zero-exit-code
1 parent 9cc003b commit e858d15

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

packages/cli/src/commands/scan/output-scan-report.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export async function outputScanReport(
9191
return
9292
}
9393

94+
if (!scanReport.data.healthy) {
95+
// When report contains healthy: false, process should exit with non-zero code.
96+
process.exitCode = 1;
97+
}
98+
9499
// I don't think we emit the default error message with banner for an unhealthy report, do we?
95100
// if (!scanReport.data.healthy) {
96101
// logger.fail(failMsgWithBadge(scanReport.message, scanReport.cause))

packages/cli/test/unit/commands/scan/output-scan-report.test.mts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,37 @@
1919
* - src/commands/outputScanReport.mts (implementation)
2020
*/
2121

22-
import { describe, expect, it } from 'vitest'
22+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2323

2424
import {
25+
outputScanReport,
2526
toJsonReport,
2627
toMarkdownReport,
2728
} from '../../../../src/commands/scan/output-scan-report.mts'
2829
import { SOCKET_WEBSITE_URL } from '../../../../src/constants/socket.mts'
2930

3031
import type { ScanReport } from '../../../../src/commands/scan/generate-report.mts'
3132

33+
const { mockGenerateReport, mockLogger } = vi.hoisted(() => ({
34+
mockGenerateReport: vi.fn(),
35+
mockLogger: { log: vi.fn(), fail: vi.fn(), dir: vi.fn() },
36+
}))
37+
38+
vi.mock('../../../../src/commands/scan/generate-report.mts', async () => {
39+
const actual =
40+
await vi.importActual<
41+
typeof import('../../../../src/commands/scan/generate-report.mts')
42+
>('../../../../src/commands/scan/generate-report.mts')
43+
return {
44+
...actual,
45+
generateReport: mockGenerateReport,
46+
}
47+
})
48+
49+
vi.mock('@socketsecurity/lib-internal/logger', () => ({
50+
getDefaultLogger: () => mockLogger,
51+
}))
52+
3253
describe('output-scan-report', () => {
3354
describe('toJsonReport', () => {
3455
it('should be able to generate a healthy json report', () => {
@@ -159,6 +180,71 @@ describe('output-scan-report', () => {
159180
`)
160181
})
161182
})
183+
184+
describe('outputScanReport exit code behavior', () => {
185+
const originalExitCode = process.exitCode
186+
187+
beforeEach(() => {
188+
process.exitCode = undefined
189+
vi.clearAllMocks()
190+
})
191+
192+
afterEach(() => {
193+
process.exitCode = originalExitCode
194+
})
195+
196+
it('sets exit code to 1 when report is unhealthy', async () => {
197+
mockGenerateReport.mockReturnValue({
198+
ok: true,
199+
data: getUnhealthyReport(),
200+
})
201+
202+
await outputScanReport(
203+
{
204+
ok: true,
205+
data: { scan: [], securityPolicy: {} },
206+
} as any,
207+
{
208+
orgSlug: 'test-org',
209+
scanId: 'test-scan',
210+
includeLicensePolicy: false,
211+
outputKind: 'json',
212+
filepath: '-',
213+
fold: 'none',
214+
reportLevel: 'error',
215+
short: false,
216+
},
217+
)
218+
219+
expect(process.exitCode).toBe(1)
220+
})
221+
222+
it('does not set exit code when report is healthy', async () => {
223+
mockGenerateReport.mockReturnValue({
224+
ok: true,
225+
data: getHealthyReport(),
226+
})
227+
228+
await outputScanReport(
229+
{
230+
ok: true,
231+
data: { scan: [], securityPolicy: {} },
232+
} as any,
233+
{
234+
orgSlug: 'test-org',
235+
scanId: 'test-scan',
236+
includeLicensePolicy: false,
237+
outputKind: 'json',
238+
filepath: '-',
239+
fold: 'none',
240+
reportLevel: 'error',
241+
short: false,
242+
},
243+
)
244+
245+
expect(process.exitCode).toBeUndefined()
246+
})
247+
})
162248
})
163249

164250
function getHealthyReport(): ScanReport {

0 commit comments

Comments
 (0)