Skip to content

Commit 9e98037

Browse files
author
Graydon Hope
committed
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 9e98037

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-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: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@
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 } = vi.hoisted(() => ({
34+
mockGenerateReport: vi.fn(),
35+
}))
36+
37+
vi.mock('../../../../src/commands/scan/generate-report.mts', () => ({
38+
generateReport: mockGenerateReport,
39+
}))
40+
3241
describe('output-scan-report', () => {
3342
describe('toJsonReport', () => {
3443
it('should be able to generate a healthy json report', () => {
@@ -159,6 +168,71 @@ describe('output-scan-report', () => {
159168
`)
160169
})
161170
})
171+
172+
describe('outputScanReport exit code behavior', () => {
173+
const originalExitCode = process.exitCode
174+
175+
beforeEach(() => {
176+
process.exitCode = undefined
177+
vi.clearAllMocks()
178+
})
179+
180+
afterEach(() => {
181+
process.exitCode = originalExitCode
182+
})
183+
184+
it('sets exit code to 1 when report is unhealthy', async () => {
185+
mockGenerateReport.mockReturnValue({
186+
ok: true,
187+
data: getUnhealthyReport(),
188+
})
189+
190+
await outputScanReport(
191+
{
192+
ok: true,
193+
data: { scan: [], securityPolicy: {} },
194+
} as any,
195+
{
196+
orgSlug: 'test-org',
197+
scanId: 'test-scan',
198+
includeLicensePolicy: false,
199+
outputKind: 'json',
200+
filepath: '-',
201+
fold: 'none',
202+
reportLevel: 'error',
203+
short: false,
204+
},
205+
)
206+
207+
expect(process.exitCode).toBe(1)
208+
})
209+
210+
it('does not set exit code when report is healthy', async () => {
211+
mockGenerateReport.mockReturnValue({
212+
ok: true,
213+
data: getHealthyReport(),
214+
})
215+
216+
await outputScanReport(
217+
{
218+
ok: true,
219+
data: { scan: [], securityPolicy: {} },
220+
} as any,
221+
{
222+
orgSlug: 'test-org',
223+
scanId: 'test-scan',
224+
includeLicensePolicy: false,
225+
outputKind: 'json',
226+
filepath: '-',
227+
fold: 'none',
228+
reportLevel: 'error',
229+
short: false,
230+
},
231+
)
232+
233+
expect(process.exitCode).toBeUndefined()
234+
})
235+
})
162236
})
163237

164238
function getHealthyReport(): ScanReport {

0 commit comments

Comments
 (0)