From cc17bed958d92a2496ae827d9874a3590c7e503c Mon Sep 17 00:00:00 2001 From: Kasper Svendsen Date: Mon, 27 Oct 2025 09:46:16 +0100 Subject: [PATCH] Move diff-range computation tests --- src/analyze.test.ts | 200 ---------------------- src/diff-informed-analysis-utils.test.ts | 203 ++++++++++++++++++++++- 2 files changed, 202 insertions(+), 201 deletions(-) diff --git a/src/analyze.test.ts b/src/analyze.test.ts index 154cd2300..b6880b43d 100644 --- a/src/analyze.test.ts +++ b/src/analyze.test.ts @@ -4,7 +4,6 @@ import * as path from "path"; import test from "ava"; import * as sinon from "sinon"; -import * as actionsUtil from "./actions-util"; import { CodeQuality, CodeScanning } from "./analyses"; import { runQueries, @@ -13,7 +12,6 @@ import { addSarifExtension, } from "./analyze"; import { createStubCodeQL } from "./codeql"; -import { exportedForTesting } from "./diff-informed-analysis-utils"; import { Feature } from "./feature-flags"; import { KnownLanguage } from "./languages"; import { getRunnerLogger } from "./logging"; @@ -131,204 +129,6 @@ test("status report fields", async (t) => { }); }); -function runGetDiffRanges(changes: number, patch: string[] | undefined): any { - sinon - .stub(actionsUtil, "getRequiredInput") - .withArgs("checkout_path") - .returns("/checkout/path"); - return exportedForTesting.getDiffRanges( - { - filename: "test.txt", - changes, - patch: patch?.join("\n"), - }, - getRunnerLogger(true), - ); -} - -test("getDiffRanges: file unchanged", async (t) => { - const diffRanges = runGetDiffRanges(0, undefined); - t.deepEqual(diffRanges, []); -}); - -test("getDiffRanges: file diff too large", async (t) => { - const diffRanges = runGetDiffRanges(1000000, undefined); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 0, - endLine: 0, - }, - ]); -}); - -test("getDiffRanges: diff thunk with single addition range", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,6 +50,8 @@", - " a", - " b", - " c", - "+1", - "+2", - " d", - " e", - " f", - ]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 53, - endLine: 54, - }, - ]); -}); - -test("getDiffRanges: diff thunk with single deletion range", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,8 +50,6 @@", - " a", - " b", - " c", - "-1", - "-2", - " d", - " e", - " f", - ]); - t.deepEqual(diffRanges, []); -}); - -test("getDiffRanges: diff thunk with single update range", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,7 +50,7 @@", - " a", - " b", - " c", - "-1", - "+2", - " d", - " e", - " f", - ]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 53, - endLine: 53, - }, - ]); -}); - -test("getDiffRanges: diff thunk with addition ranges", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,7 +50,9 @@", - " a", - " b", - " c", - "+1", - " c", - "+2", - " d", - " e", - " f", - ]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 53, - endLine: 53, - }, - { - path: "/checkout/path/test.txt", - startLine: 55, - endLine: 55, - }, - ]); -}); - -test("getDiffRanges: diff thunk with mixed ranges", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,7 +50,7 @@", - " a", - " b", - " c", - "-1", - " d", - "-2", - "+3", - " e", - " f", - "+4", - "+5", - " g", - " h", - " i", - ]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 54, - endLine: 54, - }, - { - path: "/checkout/path/test.txt", - startLine: 57, - endLine: 58, - }, - ]); -}); - -test("getDiffRanges: multiple diff thunks", async (t) => { - const diffRanges = runGetDiffRanges(2, [ - "@@ -30,6 +50,8 @@", - " a", - " b", - " c", - "+1", - "+2", - " d", - " e", - " f", - "@@ -130,6 +150,8 @@", - " a", - " b", - " c", - "+1", - "+2", - " d", - " e", - " f", - ]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 53, - endLine: 54, - }, - { - path: "/checkout/path/test.txt", - startLine: 153, - endLine: 154, - }, - ]); -}); - -test("getDiffRanges: no diff context lines", async (t) => { - const diffRanges = runGetDiffRanges(2, ["@@ -30 +50,2 @@", "+1", "+2"]); - t.deepEqual(diffRanges, [ - { - path: "/checkout/path/test.txt", - startLine: 50, - endLine: 51, - }, - ]); -}); - -test("getDiffRanges: malformed thunk header", async (t) => { - const diffRanges = runGetDiffRanges(2, ["@@ 30 +50,2 @@", "+1", "+2"]); - t.deepEqual(diffRanges, undefined); -}); - test("resolveQuerySuiteAlias", (t) => { // default query suite names should resolve to something language-specific ending in `.qls`. for (const suite of defaultSuites) { diff --git a/src/diff-informed-analysis-utils.test.ts b/src/diff-informed-analysis-utils.test.ts index 1125f18fd..eeb06cd1b 100644 --- a/src/diff-informed-analysis-utils.test.ts +++ b/src/diff-informed-analysis-utils.test.ts @@ -4,7 +4,10 @@ import * as sinon from "sinon"; import * as actionsUtil from "./actions-util"; import type { PullRequestBranches } from "./actions-util"; import * as apiClient from "./api-client"; -import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils"; +import { + shouldPerformDiffInformedAnalysis, + exportedForTesting, +} from "./diff-informed-analysis-utils"; import { Feature, Features } from "./feature-flags"; import { getRunnerLogger } from "./logging"; import { parseRepositoryNwo } from "./repository"; @@ -183,3 +186,201 @@ test( }, false, ); + +function runGetDiffRanges(changes: number, patch: string[] | undefined): any { + sinon + .stub(actionsUtil, "getRequiredInput") + .withArgs("checkout_path") + .returns("/checkout/path"); + return exportedForTesting.getDiffRanges( + { + filename: "test.txt", + changes, + patch: patch?.join("\n"), + }, + getRunnerLogger(true), + ); +} + +test("getDiffRanges: file unchanged", async (t) => { + const diffRanges = runGetDiffRanges(0, undefined); + t.deepEqual(diffRanges, []); +}); + +test("getDiffRanges: file diff too large", async (t) => { + const diffRanges = runGetDiffRanges(1000000, undefined); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 0, + endLine: 0, + }, + ]); +}); + +test("getDiffRanges: diff thunk with single addition range", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,6 +50,8 @@", + " a", + " b", + " c", + "+1", + "+2", + " d", + " e", + " f", + ]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 53, + endLine: 54, + }, + ]); +}); + +test("getDiffRanges: diff thunk with single deletion range", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,8 +50,6 @@", + " a", + " b", + " c", + "-1", + "-2", + " d", + " e", + " f", + ]); + t.deepEqual(diffRanges, []); +}); + +test("getDiffRanges: diff thunk with single update range", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,7 +50,7 @@", + " a", + " b", + " c", + "-1", + "+2", + " d", + " e", + " f", + ]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 53, + endLine: 53, + }, + ]); +}); + +test("getDiffRanges: diff thunk with addition ranges", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,7 +50,9 @@", + " a", + " b", + " c", + "+1", + " c", + "+2", + " d", + " e", + " f", + ]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 53, + endLine: 53, + }, + { + path: "/checkout/path/test.txt", + startLine: 55, + endLine: 55, + }, + ]); +}); + +test("getDiffRanges: diff thunk with mixed ranges", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,7 +50,7 @@", + " a", + " b", + " c", + "-1", + " d", + "-2", + "+3", + " e", + " f", + "+4", + "+5", + " g", + " h", + " i", + ]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 54, + endLine: 54, + }, + { + path: "/checkout/path/test.txt", + startLine: 57, + endLine: 58, + }, + ]); +}); + +test("getDiffRanges: multiple diff thunks", async (t) => { + const diffRanges = runGetDiffRanges(2, [ + "@@ -30,6 +50,8 @@", + " a", + " b", + " c", + "+1", + "+2", + " d", + " e", + " f", + "@@ -130,6 +150,8 @@", + " a", + " b", + " c", + "+1", + "+2", + " d", + " e", + " f", + ]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 53, + endLine: 54, + }, + { + path: "/checkout/path/test.txt", + startLine: 153, + endLine: 154, + }, + ]); +}); + +test("getDiffRanges: no diff context lines", async (t) => { + const diffRanges = runGetDiffRanges(2, ["@@ -30 +50,2 @@", "+1", "+2"]); + t.deepEqual(diffRanges, [ + { + path: "/checkout/path/test.txt", + startLine: 50, + endLine: 51, + }, + ]); +}); + +test("getDiffRanges: malformed thunk header", async (t) => { + const diffRanges = runGetDiffRanges(2, ["@@ 30 +50,2 @@", "+1", "+2"]); + t.deepEqual(diffRanges, undefined); +});