Skip to content

Commit 95aa4fc

Browse files
graydonhopeGraydon Hope
andauthored
Fixes the issue where socket ci would exit with code 0 even when blocking alerts were found. (#986)
This is the expected behaviour based on our docs: https://docs.socket.dev/docs/socket-ci#non-zero-exit-code Co-authored-by: Graydon Hope <[email protected]>
1 parent abeeb18 commit 95aa4fc

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export async function outputScanReport(
8787
return
8888
}
8989

90+
if (!scanReport.data.healthy) {
91+
// When report contains healthy: false, process should exit with non-zero code.
92+
process.exitCode = 1
93+
}
94+
9095
// I don't think we emit the default error message with banner for an unhealthy report, do we?
9196
// if (!scanReport.data.healthy) {
9297
// logger.fail(failMsgWithBadge(scanReport.message, scanReport.cause))

src/commands/scan/output-scan-report.test.mts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22

3-
import { toJsonReport, toMarkdownReport } from './output-scan-report.mts'
3+
import {
4+
outputScanReport,
5+
toJsonReport,
6+
toMarkdownReport,
7+
} from './output-scan-report.mts'
48
import { SOCKET_WEBSITE_URL } from '../../constants.mjs'
59

610
import type { ScanReport } from './generate-report.mts'
711

12+
const { mockGenerateReport } = vi.hoisted(() => ({
13+
mockGenerateReport: vi.fn(),
14+
}))
15+
16+
vi.mock('./generate-report.mts', () => ({
17+
generateReport: mockGenerateReport,
18+
}))
19+
820
describe('output-scan-report', () => {
921
describe('toJsonReport', () => {
1022
it('should be able to generate a healthy json report', () => {
@@ -135,6 +147,71 @@ describe('output-scan-report', () => {
135147
`)
136148
})
137149
})
150+
151+
describe('outputScanReport exit code behavior', () => {
152+
const originalExitCode = process.exitCode
153+
154+
beforeEach(() => {
155+
process.exitCode = undefined
156+
vi.clearAllMocks()
157+
})
158+
159+
afterEach(() => {
160+
process.exitCode = originalExitCode
161+
})
162+
163+
it('sets exit code to 1 when report is unhealthy', async () => {
164+
mockGenerateReport.mockReturnValue({
165+
ok: true,
166+
data: getUnhealthyReport(),
167+
})
168+
169+
await outputScanReport(
170+
{
171+
ok: true,
172+
data: { scan: [], securityPolicy: {} },
173+
} as any,
174+
{
175+
orgSlug: 'test-org',
176+
scanId: 'test-scan',
177+
includeLicensePolicy: false,
178+
outputKind: 'json',
179+
filepath: '-',
180+
fold: 'none',
181+
reportLevel: 'error',
182+
short: false,
183+
},
184+
)
185+
186+
expect(process.exitCode).toBe(1)
187+
})
188+
189+
it('does not set exit code when report is healthy', async () => {
190+
mockGenerateReport.mockReturnValue({
191+
ok: true,
192+
data: getHealthyReport(),
193+
})
194+
195+
await outputScanReport(
196+
{
197+
ok: true,
198+
data: { scan: [], securityPolicy: {} },
199+
} as any,
200+
{
201+
orgSlug: 'test-org',
202+
scanId: 'test-scan',
203+
includeLicensePolicy: false,
204+
outputKind: 'json',
205+
filepath: '-',
206+
fold: 'none',
207+
reportLevel: 'error',
208+
short: false,
209+
},
210+
)
211+
212+
expect(process.exitCode).toBeUndefined()
213+
})
214+
})
138215
})
139216

140217
function getHealthyReport(): ScanReport {

0 commit comments

Comments
 (0)