Skip to content

Commit 540115d

Browse files
committed
refactor: pass checkoutPath as param and fix docs for relative path semantics
1 parent 1443f58 commit 540115d

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

src/analyze.test.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as path from "path";
44
import test from "ava";
55
import * as sinon from "sinon";
66

7-
import * as actionsUtil from "./actions-util";
87
import { CodeQuality, CodeScanning, RiskAssessment } from "./analyses";
98
import {
109
runQueries,
@@ -162,17 +161,16 @@ test("addSarifExtension", (t) => {
162161
});
163162

164163
test("diffRangeExtensionPackContents", (t) => {
165-
sinon
166-
.stub(actionsUtil, "getRequiredInput")
167-
.withArgs("checkout_path")
168-
.returns("/checkout/path");
169-
const output = diffRangeExtensionPackContents([
170-
{
171-
path: "main.js",
172-
startLine: 10,
173-
endLine: 20,
174-
},
175-
]);
164+
const output = diffRangeExtensionPackContents(
165+
[
166+
{
167+
path: "main.js",
168+
startLine: 10,
169+
endLine: 20,
170+
},
171+
],
172+
"/checkout/path",
173+
);
176174

177175
const expected = fs.readFileSync(
178176
`${__dirname}/../src/testdata/pr-diff-range.yml`,

src/analyze.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export async function setupDiffInformedQueryRun(
265265

266266
export function diffRangeExtensionPackContents(
267267
ranges: DiffThunkRange[],
268+
checkoutPath: string,
268269
): string {
269270
const header = `
270271
extensions:
@@ -281,7 +282,7 @@ extensions:
281282
// uses forward slashes as the path separator, so on Windows we need to
282283
// replace any backslashes with forward slashes.
283284
const filename = path
284-
.join(getRequiredInput("checkout_path"), range.path)
285+
.join(checkoutPath, range.path)
285286
.replaceAll(path.sep, "/");
286287

287288
// Using yaml.dump() with `forceQuotes: true` ensures that all special
@@ -350,7 +351,10 @@ dataExtensions:
350351
`,
351352
);
352353

353-
const extensionContents = diffRangeExtensionPackContents(ranges);
354+
const extensionContents = diffRangeExtensionPackContents(
355+
ranges,
356+
getRequiredInput("checkout_path"),
357+
);
354358
const extensionFilePath = path.join(diffRangeDir, "pr-diff-range.yml");
355359
fs.writeFileSync(extensionFilePath, extensionContents);
356360
logger.debug(

src/diff-informed-analysis-utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function getDiffInformedAnalysisBranches(
7171
}
7272

7373
export interface DiffThunkRange {
74+
/** Relative path from the repository root, using forward slashes as separators. */
7475
path: string;
7576
startLine: number;
7677
endLine: number;
@@ -112,8 +113,9 @@ export function readDiffRangesJsonFile(
112113
*
113114
* @param branches The base and head branches of the pull request.
114115
* @param logger
115-
* @returns An array of tuples, where each tuple contains the absolute path of a
116-
* file, the start line and the end line (both 1-based and inclusive) of an
116+
* @returns An array of tuples, where each tuple contains the relative path of a
117+
* file (relative to the repository root, as returned by the GitHub compare API),
118+
* the start line and the end line (both 1-based and inclusive) of an
117119
* added or modified range in that file. Returns `undefined` if the action was
118120
* not triggered by a pull request or if there was an error.
119121
*/

src/upload-lib.test.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,21 +1090,24 @@ function runFilterAlertsByDiffRange(
10901090
return uploadLib.filterAlertsByDiffRange(getRunnerLogger(true), input);
10911091
}
10921092

1093-
test("filterAlertsByDiffRange filters out alerts outside diff-range", (t) => {
1094-
const input = sarif.readSarifFile(
1095-
`${__dirname}/../src/testdata/valid-sarif.sarif`,
1096-
);
1097-
const actualOutput = runFilterAlertsByDiffRange(input, [
1098-
{
1099-
path: "main.js",
1100-
startLine: 1,
1101-
endLine: 3,
1102-
},
1103-
]);
1093+
test.serial(
1094+
"filterAlertsByDiffRange filters out alerts outside diff-range",
1095+
(t) => {
1096+
const input = sarif.readSarifFile(
1097+
`${__dirname}/../src/testdata/valid-sarif.sarif`,
1098+
);
1099+
const actualOutput = runFilterAlertsByDiffRange(input, [
1100+
{
1101+
path: "main.js",
1102+
startLine: 1,
1103+
endLine: 3,
1104+
},
1105+
]);
11041106

1105-
const expectedOutput = sarif.readSarifFile(
1106-
`${__dirname}/../src/testdata/valid-sarif-diff-filtered.sarif`,
1107-
);
1107+
const expectedOutput = sarif.readSarifFile(
1108+
`${__dirname}/../src/testdata/valid-sarif-diff-filtered.sarif`,
1109+
);
11081110

1109-
t.deepEqual(actualOutput, expectedOutput);
1110-
});
1111+
t.deepEqual(actualOutput, expectedOutput);
1112+
},
1113+
);

0 commit comments

Comments
 (0)