Skip to content

Commit 6e0b087

Browse files
committed
Add some tests for findAndUpload and uploadSarif
1 parent 9f452fa commit 6e0b087

File tree

2 files changed

+199
-1
lines changed

2 files changed

+199
-1
lines changed

src/upload-sarif.test.ts

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

src/upload-sarif.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function findAndUpload(
6262
}
6363

6464
// Maps analysis kinds to SARIF IDs.
65-
type UploadSarifResults = Partial<
65+
export type UploadSarifResults = Partial<
6666
Record<analyses.AnalysisKind, upload_lib.UploadResult>
6767
>;
6868

0 commit comments

Comments
 (0)