|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 |
|
3 | | -import { toJsonReport, toMarkdownReport } from './output-scan-report.mts' |
| 3 | +import { |
| 4 | + outputScanReport, |
| 5 | + toJsonReport, |
| 6 | + toMarkdownReport, |
| 7 | +} from './output-scan-report.mts' |
4 | 8 | import { SOCKET_WEBSITE_URL } from '../../constants.mjs' |
5 | 9 |
|
6 | 10 | import type { ScanReport } from './generate-report.mts' |
7 | 11 |
|
| 12 | +const { mockGenerateReport } = vi.hoisted(() => ({ |
| 13 | + mockGenerateReport: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('./generate-report.mts', () => ({ |
| 17 | + generateReport: mockGenerateReport, |
| 18 | +})) |
| 19 | + |
8 | 20 | describe('output-scan-report', () => { |
9 | 21 | describe('toJsonReport', () => { |
10 | 22 | it('should be able to generate a healthy json report', () => { |
@@ -135,6 +147,71 @@ describe('output-scan-report', () => { |
135 | 147 | `) |
136 | 148 | }) |
137 | 149 | }) |
| 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 | + }) |
138 | 215 | }) |
139 | 216 |
|
140 | 217 | function getHealthyReport(): ScanReport { |
|
0 commit comments