|
| 1 | +import * as fs from "fs"; |
| 2 | + |
| 3 | +import test from "ava"; |
| 4 | + |
| 5 | +import { |
| 6 | + getRecordingLogger, |
| 7 | + LoggedMessage, |
| 8 | + setupTests, |
| 9 | +} from "../testing-utils"; |
| 10 | + |
| 11 | +import { |
| 12 | + fixInvalidNotifications, |
| 13 | + getToolNames, |
| 14 | + SarifLocation, |
| 15 | + type SarifFile, |
| 16 | +} from "."; |
| 17 | + |
| 18 | +setupTests(test); |
| 19 | + |
| 20 | +test("getToolNames", (t) => { |
| 21 | + const input = fs.readFileSync( |
| 22 | + `${__dirname}/../../src/testdata/tool-names.sarif`, |
| 23 | + "utf8", |
| 24 | + ); |
| 25 | + const toolNames = getToolNames(JSON.parse(input) as SarifFile); |
| 26 | + t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]); |
| 27 | +}); |
| 28 | + |
| 29 | +function createMockSarifWithNotification( |
| 30 | + locations: SarifLocation[], |
| 31 | +): SarifFile { |
| 32 | + return { |
| 33 | + runs: [ |
| 34 | + { |
| 35 | + tool: { |
| 36 | + driver: { |
| 37 | + name: "CodeQL", |
| 38 | + }, |
| 39 | + }, |
| 40 | + invocations: [ |
| 41 | + { |
| 42 | + toolExecutionNotifications: [ |
| 43 | + { |
| 44 | + locations, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + ], |
| 49 | + }, |
| 50 | + ], |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +const stubLocation: SarifLocation = { |
| 55 | + physicalLocation: { |
| 56 | + artifactLocation: { |
| 57 | + uri: "file1", |
| 58 | + }, |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +test("fixInvalidNotifications leaves notifications with unique locations alone", (t) => { |
| 63 | + const messages: LoggedMessage[] = []; |
| 64 | + const result = fixInvalidNotifications( |
| 65 | + createMockSarifWithNotification([stubLocation]), |
| 66 | + getRecordingLogger(messages), |
| 67 | + ); |
| 68 | + t.deepEqual(result, createMockSarifWithNotification([stubLocation])); |
| 69 | + t.is(messages.length, 1); |
| 70 | + t.deepEqual(messages[0], { |
| 71 | + type: "debug", |
| 72 | + message: "No duplicate locations found in SARIF notification objects.", |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test("fixInvalidNotifications removes duplicate locations", (t) => { |
| 77 | + const messages: LoggedMessage[] = []; |
| 78 | + const result = fixInvalidNotifications( |
| 79 | + createMockSarifWithNotification([stubLocation, stubLocation]), |
| 80 | + getRecordingLogger(messages), |
| 81 | + ); |
| 82 | + t.deepEqual(result, createMockSarifWithNotification([stubLocation])); |
| 83 | + t.is(messages.length, 1); |
| 84 | + t.deepEqual(messages[0], { |
| 85 | + type: "info", |
| 86 | + message: "Removed 1 duplicate locations from SARIF notification objects.", |
| 87 | + }); |
| 88 | +}); |
0 commit comments