|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +import test, { ExecutionContext } from "ava"; |
| 5 | +import * as sinon from "sinon"; |
| 6 | + |
| 7 | +import { |
| 8 | + AnalysisConfig, |
| 9 | + AnalysisKind, |
| 10 | + CodeQuality, |
| 11 | + CodeScanning, |
| 12 | +} from "./analyses"; |
| 13 | +import { getRunnerLogger } from "./logging"; |
| 14 | +import { createFeatures, setupTests } from "./testing-utils"; |
| 15 | +import { UploadResult } from "./upload-lib"; |
| 16 | +import * as uploadLib from "./upload-lib"; |
| 17 | +import { findAndUpload, uploadSarif, UploadSarifResults } from "./upload-sarif"; |
| 18 | +import * as util from "./util"; |
| 19 | + |
| 20 | +setupTests(test); |
| 21 | + |
| 22 | +const findAndUploadMacro = test.macro({ |
| 23 | + exec: async ( |
| 24 | + t: ExecutionContext<unknown>, |
| 25 | + sarifFiles: string[], |
| 26 | + analysis: AnalysisConfig, |
| 27 | + sarifPredicate: (name: string) => boolean, |
| 28 | + sarifPath: (tempDir: string) => string = (tempDir) => tempDir, |
| 29 | + expectedResult: UploadResult | undefined, |
| 30 | + ) => { |
| 31 | + await util.withTmpDir(async (tempDir) => { |
| 32 | + sinon.stub(uploadLib, "uploadSpecifiedFiles").resolves(expectedResult); |
| 33 | + const logger = getRunnerLogger(true); |
| 34 | + const features = createFeatures([]); |
| 35 | + |
| 36 | + for (const sarifFile of sarifFiles) { |
| 37 | + fs.writeFileSync(path.join(tempDir, sarifFile), ""); |
| 38 | + } |
| 39 | + |
| 40 | + const stats = fs.statSync(sarifPath(tempDir)); |
| 41 | + const actual = await findAndUpload( |
| 42 | + logger, |
| 43 | + features, |
| 44 | + sarifPath(tempDir), |
| 45 | + stats, |
| 46 | + "", |
| 47 | + analysis, |
| 48 | + sarifPredicate, |
| 49 | + ); |
| 50 | + |
| 51 | + t.deepEqual(actual, expectedResult); |
| 52 | + }); |
| 53 | + }, |
| 54 | + title: (providedTitle = "") => `findAndUpload - ${providedTitle}`, |
| 55 | +}); |
| 56 | + |
| 57 | +test( |
| 58 | + "no matching files", |
| 59 | + findAndUploadMacro, |
| 60 | + ["test.json"], |
| 61 | + CodeScanning, |
| 62 | + CodeScanning.sarifPredicate, |
| 63 | + undefined, |
| 64 | + undefined, |
| 65 | +); |
| 66 | + |
| 67 | +test( |
| 68 | + "matching files for Code Scanning with directory path", |
| 69 | + findAndUploadMacro, |
| 70 | + ["test.sarif"], |
| 71 | + CodeScanning, |
| 72 | + CodeScanning.sarifPredicate, |
| 73 | + undefined, |
| 74 | + { |
| 75 | + statusReport: {}, |
| 76 | + sarifID: "some-id", |
| 77 | + }, |
| 78 | +); |
| 79 | + |
| 80 | +test( |
| 81 | + "matching files for Code Scanning with file path", |
| 82 | + findAndUploadMacro, |
| 83 | + ["test.sarif"], |
| 84 | + CodeScanning, |
| 85 | + CodeScanning.sarifPredicate, |
| 86 | + (tempDir) => path.join(tempDir, "test.sarif"), |
| 87 | + { |
| 88 | + statusReport: {}, |
| 89 | + sarifID: "some-id", |
| 90 | + }, |
| 91 | +); |
| 92 | + |
| 93 | +const uploadSarifMacro = test.macro({ |
| 94 | + exec: async ( |
| 95 | + t: ExecutionContext<unknown>, |
| 96 | + sarifFiles: string[], |
| 97 | + sarifPath: (tempDir: string) => string = (tempDir) => tempDir, |
| 98 | + expectedResult: UploadSarifResults, |
| 99 | + ) => { |
| 100 | + await util.withTmpDir(async (tempDir) => { |
| 101 | + const logger = getRunnerLogger(true); |
| 102 | + const testPath = sarifPath(tempDir); |
| 103 | + const features = createFeatures([]); |
| 104 | + const uploadSpecifiedFiles = sinon.stub( |
| 105 | + uploadLib, |
| 106 | + "uploadSpecifiedFiles", |
| 107 | + ); |
| 108 | + |
| 109 | + for (const analysisKind of Object.keys(expectedResult)) { |
| 110 | + uploadSpecifiedFiles |
| 111 | + .withArgs( |
| 112 | + sinon.match.any, |
| 113 | + sinon.match.any, |
| 114 | + sinon.match.any, |
| 115 | + features, |
| 116 | + logger, |
| 117 | + analysisKind === AnalysisKind.CodeScanning |
| 118 | + ? CodeScanning |
| 119 | + : CodeQuality, |
| 120 | + ) |
| 121 | + .resolves(expectedResult[analysisKind as AnalysisKind]); |
| 122 | + } |
| 123 | + |
| 124 | + for (const sarifFile of sarifFiles) { |
| 125 | + fs.writeFileSync(path.join(tempDir, sarifFile), ""); |
| 126 | + } |
| 127 | + |
| 128 | + const stats = fs.statSync(testPath); |
| 129 | + const actual = await uploadSarif(logger, features, testPath, stats, ""); |
| 130 | + |
| 131 | + t.deepEqual(actual, expectedResult); |
| 132 | + }); |
| 133 | + }, |
| 134 | + title: (providedTitle = "") => `uploadSarif - ${providedTitle}`, |
| 135 | +}); |
| 136 | + |
| 137 | +test( |
| 138 | + "SARIF file", |
| 139 | + uploadSarifMacro, |
| 140 | + ["test.sarif"], |
| 141 | + (tempDir) => path.join(tempDir, "test.sarif"), |
| 142 | + { |
| 143 | + "code-scanning": { |
| 144 | + statusReport: {}, |
| 145 | + sarifID: "code-scanning-sarif", |
| 146 | + }, |
| 147 | + }, |
| 148 | +); |
| 149 | + |
| 150 | +test( |
| 151 | + "JSON file", |
| 152 | + uploadSarifMacro, |
| 153 | + ["test.json"], |
| 154 | + (tempDir) => path.join(tempDir, "test.json"), |
| 155 | + { |
| 156 | + "code-scanning": { |
| 157 | + statusReport: {}, |
| 158 | + sarifID: "code-scanning-sarif", |
| 159 | + }, |
| 160 | + }, |
| 161 | +); |
| 162 | + |
| 163 | +test( |
| 164 | + "Code Scanning files", |
| 165 | + uploadSarifMacro, |
| 166 | + ["test.json", "test.sarif"], |
| 167 | + undefined, |
| 168 | + { |
| 169 | + "code-scanning": { |
| 170 | + statusReport: {}, |
| 171 | + sarifID: "code-scanning-sarif", |
| 172 | + }, |
| 173 | + }, |
| 174 | +); |
| 175 | + |
| 176 | +test( |
| 177 | + "Code Quality file", |
| 178 | + uploadSarifMacro, |
| 179 | + ["test.quality.sarif"], |
| 180 | + (tempDir) => path.join(tempDir, "test.quality.sarif"), |
| 181 | + { |
| 182 | + "code-quality": { |
| 183 | + statusReport: {}, |
| 184 | + sarifID: "code-quality-sarif", |
| 185 | + }, |
| 186 | + }, |
| 187 | +); |
| 188 | + |
| 189 | +test( |
| 190 | + "Mixed files", |
| 191 | + uploadSarifMacro, |
| 192 | + ["test.sarif", "test.quality.sarif"], |
| 193 | + undefined, |
| 194 | + { |
| 195 | + "code-scanning": { |
| 196 | + statusReport: {}, |
| 197 | + sarifID: "code-scanning-sarif", |
| 198 | + }, |
| 199 | + "code-quality": { |
| 200 | + statusReport: {}, |
| 201 | + sarifID: "code-quality-sarif", |
| 202 | + }, |
| 203 | + }, |
| 204 | +); |
0 commit comments