Skip to content

Commit e32c499

Browse files
committed
upload-lib: Unit test filterAlertsByDiffRange
1 parent 8d77149 commit e32c499

File tree

4 files changed

+223
-2
lines changed

4 files changed

+223
-2
lines changed

lib/upload-lib.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
3+
"version": "2.1.0",
4+
"runs": [{
5+
"tool": {
6+
"driver": {
7+
"name": "LGTM.com",
8+
"organization": "Semmle",
9+
"version": "1.24.0-SNAPSHOT",
10+
"rules": [{
11+
"id": "js/unused-local-variable",
12+
"name": "js/unused-local-variable",
13+
"shortDescription": {
14+
"text": "Unused variable, import, function or class"
15+
},
16+
"fullDescription": {
17+
"text": "Unused variables, imports, functions or classes may be a symptom of a bug and should be examined carefully."
18+
},
19+
"defaultConfiguration": {
20+
"level": "note"
21+
},
22+
"properties": {
23+
"tags": ["maintainability"],
24+
"kind": "problem",
25+
"precision": "very-high",
26+
"name": "Unused variable, import, function or class",
27+
"description": "Unused variables, imports, functions or classes may be a symptom of a bug\n and should be examined carefully.",
28+
"id": "js/unused-local-variable",
29+
"problem.severity": "recommendation"
30+
}
31+
}]
32+
}
33+
},
34+
"results": [{
35+
"ruleId": "js/unused-local-variable",
36+
"ruleIndex": 0,
37+
"message": {
38+
"text": "Unused variable foo."
39+
},
40+
"locations": [{
41+
"physicalLocation": {
42+
"artifactLocation": {
43+
"uri": "main.js",
44+
"uriBaseId": "%SRCROOT%",
45+
"index": 0
46+
},
47+
"region": {
48+
"startLine": 2,
49+
"startColumn": 7,
50+
"endColumn": 10
51+
}
52+
}
53+
}],
54+
"partialFingerprints": {
55+
"primaryLocationLineHash": "39fa2ee980eb94b0:1",
56+
"primaryLocationStartColumnFingerprint": "4"
57+
}
58+
}],
59+
"columnKind": "utf16CodeUnits",
60+
"properties": {
61+
"semmle.formatSpecifier": "2.1.0",
62+
"semmle.sourceLanguage": "java"
63+
}
64+
},
65+
{
66+
"tool" : {
67+
"driver" : {
68+
"name" : "CodeQL command-line toolchain",
69+
"organization" : "GitHub",
70+
"semanticVersion" : "2.0.0",
71+
"rules" : [ {
72+
"id" : "js/unused-local-variable",
73+
"name" : "js/unused-local-variable",
74+
"shortDescription" : {
75+
"text" : "Unused variable, import, function or class"
76+
},
77+
"fullDescription" : {
78+
"text" : "Unused variables, imports, functions or classes may be a symptom of a bug and should be examined carefully."
79+
},
80+
"defaultConfiguration" : {
81+
"level": "note"
82+
},
83+
"properties" : {
84+
"tags" : [ "maintainability" ],
85+
"kind" : "problem",
86+
"precision" : "very-high",
87+
"name" : "Unused variable, import, function or class",
88+
"description" : "Unused variables, imports, functions or classes may be a symptom of a bug\n and should be examined carefully.",
89+
"id" : "js/unused-local-variable",
90+
"problem.severity" : "recommendation"
91+
}
92+
},
93+
{
94+
"id": "js/inconsistent-use-of-new",
95+
"name": "js/inconsistent-use-of-new",
96+
"shortDescription": {
97+
"text": "Inconsistent use of 'new'"
98+
},
99+
"fullDescription": {
100+
"text": "If a function is intended to be a constructor, it should always be invoked with 'new'. Otherwise, it should always be invoked as a normal function, that is, without 'new'."
101+
},
102+
"defaultConfiguration": {
103+
"level": "note"
104+
},
105+
"properties": {
106+
"tags": [
107+
"reliability",
108+
"correctness",
109+
"language-features"
110+
],
111+
"kind": "problem",
112+
"precision": "very-high",
113+
"problem.severity": "warning"
114+
}
115+
} ]
116+
}
117+
},
118+
"artifacts" : [ {
119+
"location" : {
120+
"uri" : "main.js",
121+
"uriBaseId" : "%SRCROOT%",
122+
"index" : 0
123+
}
124+
},
125+
{
126+
"location": {
127+
"uri": "src/promiseUtils.js",
128+
"uriBaseId": "%SRCROOT%",
129+
"index": 1
130+
}
131+
},
132+
{
133+
"location": {
134+
"uri": "src/LiveQueryClient.js",
135+
"uriBaseId": "%SRCROOT%",
136+
"index": 2
137+
}
138+
},
139+
{
140+
"location": {
141+
"uri": "src/ParseObject.js",
142+
"uriBaseId": "%SRCROOT%",
143+
"index": 3
144+
}
145+
} ],
146+
"results" : [ {
147+
"ruleId" : "js/unused-local-variable",
148+
"ruleIndex" : 0,
149+
"message" : {
150+
"text" : "Unused variable foo."
151+
},
152+
"locations" : [ {
153+
"physicalLocation" : {
154+
"artifactLocation" : {
155+
"uri" : "main.js",
156+
"uriBaseId" : "%SRCROOT%",
157+
"index" : 0
158+
},
159+
"region" : {
160+
"startLine" : 2,
161+
"startColumn" : 7,
162+
"endColumn" : 10
163+
}
164+
}
165+
} ],
166+
"partialFingerprints" : {
167+
"primaryLocationLineHash" : "39fa2ee980eb94b0:1",
168+
"primaryLocationStartColumnFingerprint" : "4"
169+
}
170+
}],
171+
"newlineSequences" : [ "\r\n", "\n", "", "" ],
172+
"columnKind" : "utf16CodeUnits",
173+
"properties" : {
174+
"semmle.formatSpecifier" : "sarif-latest"
175+
}
176+
}
177+
]
178+
}

src/upload-lib.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import { HTTPError } from "@actions/tool-cache";
66
import test from "ava";
77
import * as sinon from "sinon";
88

9+
import * as actionsUtil from "./actions-util";
910
import * as analyses from "./analyses";
1011
import { AnalysisKind, CodeQuality, CodeScanning } from "./analyses";
1112
import * as api from "./api-client";
13+
import * as diffUtils from "./diff-informed-analysis-utils";
1214
import { getRunnerLogger, Logger } from "./logging";
1315
import { setupTests } from "./testing-utils";
1416
import * as uploadLib from "./upload-lib";
15-
import { GitHubVariant, initializeEnvironment, withTmpDir } from "./util";
17+
import {
18+
GitHubVariant,
19+
initializeEnvironment,
20+
SarifFile,
21+
withTmpDir,
22+
} from "./util";
1623

1724
setupTests(test);
1825

@@ -960,3 +967,34 @@ for (const analysis of [CodeScanning, CodeQuality]) {
960967
});
961968
});
962969
}
970+
971+
function runFilterAlertsByDiffRange(
972+
input: SarifFile,
973+
diffRanges: diffUtils.DiffThunkRange[],
974+
): SarifFile {
975+
sinon
976+
.stub(actionsUtil, "getRequiredInput")
977+
.withArgs("checkout_path")
978+
.returns("/checkout/path");
979+
sinon.stub(diffUtils, "readDiffRangesJsonFile").returns(diffRanges);
980+
return uploadLib.filterAlertsByDiffRange(getRunnerLogger(true), input);
981+
}
982+
983+
test("filterAlertsByDiffRange filters out alerts outside diff-range", (t) => {
984+
const input = uploadLib.readSarifFile(
985+
`${__dirname}/../src/testdata/valid-sarif.sarif`,
986+
);
987+
const actualOutput = runFilterAlertsByDiffRange(input, [
988+
{
989+
path: "/checkout/path/main.js",
990+
startLine: 1,
991+
endLine: 3,
992+
},
993+
]);
994+
995+
const expectedOutput = uploadLib.readSarifFile(
996+
`${__dirname}/../src/testdata/valid-sarif-diff-filtered.sarif`,
997+
);
998+
999+
t.deepEqual(actualOutput, expectedOutput);
1000+
});

src/upload-lib.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,10 @@ export class InvalidSarifUploadError extends Error {
11341134
}
11351135
}
11361136

1137-
function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
1137+
export function filterAlertsByDiffRange(
1138+
logger: Logger,
1139+
sarif: SarifFile,
1140+
): SarifFile {
11381141
const diffRanges = readDiffRangesJsonFile(logger);
11391142
if (!diffRanges?.length) {
11401143
return sarif;

0 commit comments

Comments
 (0)