|
2 | 2 |
|
3 | 3 | import {NoopReporter} from '../../src/reporters/index.js'; |
4 | 4 | import {run as buildRun} from './_helpers.js'; |
| 5 | +import * as auditModule from '../../src/cli/commands/audit.js'; |
5 | 6 | import {run as audit} from '../../src/cli/commands/audit.js'; |
6 | 7 | import {promisify} from '../../src/util/promise.js'; |
| 8 | +import * as lockfileModule from '../../src/lockfile/index.js'; |
| 9 | +import * as installModule from '../../src/cli/commands/install.js'; |
7 | 10 |
|
8 | 11 | const path = require('path'); |
9 | 12 | const zlib = require('zlib'); |
@@ -163,6 +166,47 @@ test('calls reporter auditSummary with correct data for private package', () => |
163 | 166 | }); |
164 | 167 | }); |
165 | 168 |
|
| 169 | +describe('returns semantic exit codes', () => { |
| 170 | + beforeAll(() => { |
| 171 | + // mock unrelated stuff |
| 172 | + jest.spyOn(lockfileModule.default, 'fromDirectory').mockImplementation(jest.fn()); |
| 173 | + jest.spyOn(installModule, 'Install').mockImplementation(() => { |
| 174 | + return { |
| 175 | + fetchRequestFromCwd: jest.fn(() => { |
| 176 | + return {}; |
| 177 | + }), |
| 178 | + resolver: { |
| 179 | + init: jest.fn(), |
| 180 | + }, |
| 181 | + linker: { |
| 182 | + init: jest.fn(), |
| 183 | + }, |
| 184 | + }; |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + const exitCodeTestCases = [ |
| 189 | + [0, {}, 'zero when no vulnerabilities'], |
| 190 | + [1, {info: 77}, '1 for info'], |
| 191 | + [2, {low: 77}, '2 for low'], |
| 192 | + [4, {moderate: 77}, '4 for moderate'], |
| 193 | + [8, {high: 77}, '8 for high'], |
| 194 | + [16, {critical: 77}, '16 for critical'], |
| 195 | + [17, {info: 55, critical: 77}, 'different categories sum up'], |
| 196 | + ]; |
| 197 | + exitCodeTestCases.forEach(([expectedExitCode, foundVulnerabilities, description]) => { |
| 198 | + test(description, async () => { |
| 199 | + jest.spyOn(auditModule.default.prototype, 'performAudit').mockImplementation(() => { |
| 200 | + return foundVulnerabilities; |
| 201 | + }); |
| 202 | + const configMock: any = {}; |
| 203 | + const reporterMock: any = {}; |
| 204 | + const exitCode = await audit(configMock, reporterMock, {}, []); |
| 205 | + expect(exitCode).toEqual(expectedExitCode); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
166 | 210 | test.concurrent('sends correct dependency map to audit api for workspaces.', () => { |
167 | 211 | const expectedApiPost = { |
168 | 212 | dependencies: { |
|
0 commit comments