|
19 | 19 | * - src/commands/outputScanReport.mts (implementation) |
20 | 20 | */ |
21 | 21 |
|
22 | | -import { describe, expect, it } from 'vitest' |
| 22 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
23 | 23 |
|
24 | 24 | import { |
| 25 | + outputScanReport, |
25 | 26 | toJsonReport, |
26 | 27 | toMarkdownReport, |
27 | 28 | } from '../../../../src/commands/scan/output-scan-report.mts' |
28 | 29 | import { SOCKET_WEBSITE_URL } from '../../../../src/constants/socket.mts' |
29 | 30 |
|
30 | 31 | import type { ScanReport } from '../../../../src/commands/scan/generate-report.mts' |
31 | 32 |
|
| 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 | + |
32 | 53 | describe('output-scan-report', () => { |
33 | 54 | describe('toJsonReport', () => { |
34 | 55 | it('should be able to generate a healthy json report', () => { |
@@ -159,6 +180,71 @@ describe('output-scan-report', () => { |
159 | 180 | `) |
160 | 181 | }) |
161 | 182 | }) |
| 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 | + }) |
162 | 248 | }) |
163 | 249 |
|
164 | 250 | function getHealthyReport(): ScanReport { |
|
0 commit comments