From a876e23075cbbc1aa1f5a0a1adc3e71e9e00edac Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Tue, 22 Sep 2026 13:17:05 +0100 Subject: [PATCH] Add tests for `determineCheckoutPath` --- src/analyze.test.ts | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/analyze.test.ts b/src/analyze.test.ts index 7523d239b..88e592507 100644 --- a/src/analyze.test.ts +++ b/src/analyze.test.ts @@ -11,9 +11,11 @@ import { resolveQuerySuiteAlias, addSarifExtension, diffRangeExtensionPackContents, + determineCheckoutPath, } from "./analyze"; import { createStubCodeQL } from "./codeql"; import { Feature } from "./feature-flags"; +import * as gitUtils from "./git-utils"; import { BuiltInLanguage } from "./languages"; import { getRunnerLogger } from "./logging"; import { @@ -21,12 +23,98 @@ import { setupActionsVars, createFeatures, createTestConfig, + callee, } from "./testing-utils"; import * as uploadLib from "./upload-lib"; import * as util from "./util"; setupTests(test); +test.serial( + "determineCheckoutPath - logs when checkout_path is not in a work tree", + async (t) => { + const expectedPath = "/checkout/path"; + const target = callee(determineCheckoutPath) + .withActions((actions) => { + sinon + .stub(actions, "getRequiredInput") + .withArgs("checkout_path") + .returns("/checkout/path"); + }) + .withArgs(createTestConfig({})); + sinon.stub(gitUtils, "getGitRoot").resolves(undefined); + + await target + .logs(t, "is not in the work tree of a git repository") + .passes(t.is, expectedPath); + }, +); + +test.serial( + "determineCheckoutPath - logs when checkout_path is not a repo root", + async (t) => { + const expectedPath = "/checkout/path"; + const target = callee(determineCheckoutPath) + .withActions((actions) => { + sinon + .stub(actions, "getRequiredInput") + .withArgs("checkout_path") + .returns("/checkout/path"); + }) + .withArgs(createTestConfig({})); + sinon.stub(gitUtils, "getGitRoot").resolves("/checkout"); + + await target + .logs(t, "is not the root of the repository") + .passes(t.is, expectedPath); + }, +); + +test.serial( + "determineCheckoutPath - logs when checkout_path is not the same as repo root in config", + async (t) => { + const expectedPath = "/checkout/path"; + const target = callee(determineCheckoutPath) + .withActions((actions) => { + sinon + .stub(actions, "getRequiredInput") + .withArgs("checkout_path") + .returns("/checkout/path"); + }) + .withArgs(createTestConfig({ repositoryRoot: "/some/other/path" })); + sinon.stub(gitUtils, "getGitRoot").resolves("/checkout/path"); + + await target + .logs(t, "does not match that found by the 'codeql-action/init' step") + .passes(t.is, expectedPath); + }, +); + +test.serial( + "determineCheckoutPath - doesn't log any of the messages when all is as expected", + async (t) => { + const expectedPath = "/checkout/path"; + const target = callee(determineCheckoutPath) + .withActions((actions) => { + sinon + .stub(actions, "getRequiredInput") + .withArgs("checkout_path") + .returns("/checkout/path"); + }) + .withArgs(createTestConfig({ repositoryRoot: "/checkout/path" })); + sinon.stub(gitUtils, "getGitRoot").resolves("/checkout/path"); + + await target + .notLogs( + t, + "is not in the work tree of a git repository", + "is not the root of the repository", + "does not match that found by the 'codeql-action/init' step", + ) + .passes(t.is, expectedPath); + }, +); + /** * Checks the status report produced by the analyze Action. *