From 466a4f00eb612b2af75149e4cb03a47eac705b9c Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Sun, 15 Feb 2026 17:13:52 +0000 Subject: [PATCH] Add unit test for `tools: nightly` --- src/setup-codeql.test.ts | 61 ++++++++++++++++++++++++++++++++++++++++ src/setup-codeql.ts | 6 ++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/setup-codeql.test.ts b/src/setup-codeql.test.ts index 3046b6ff5..e502bfb05 100644 --- a/src/setup-codeql.test.ts +++ b/src/setup-codeql.test.ts @@ -1,18 +1,22 @@ import * as path from "path"; +import * as github from "@actions/github"; import * as toolcache from "@actions/tool-cache"; import test, { ExecutionContext } from "ava"; import * as sinon from "sinon"; import * as actionsUtil from "./actions-util"; +import * as api from "./api-client"; import { Feature, FeatureEnablement } from "./feature-flags"; import { getRunnerLogger } from "./logging"; import * as setupCodeql from "./setup-codeql"; +import * as tar from "./tar"; import { LINKED_CLI_VERSION, LoggedMessage, SAMPLE_DEFAULT_CLI_VERSION, SAMPLE_DOTCOM_API_DETAILS, + checkExpectedLogMessages, createFeatures, getRecordingLogger, initializeFeatures, @@ -268,6 +272,63 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow }); }); +test("getCodeQLSource correctly returns nightly CLI version when tools == nightly", async (t) => { + const loggedMessages: LoggedMessage[] = []; + const logger = getRecordingLogger(loggedMessages); + const features = createFeatures([]); + + const expectedDate = "30260213"; + const expectedTag = `codeql-bundle-${expectedDate}`; + + // Ensure that we consistently select "zstd" for the test. + sinon.stub(process, "platform").value("linux"); + sinon.stub(tar, "isZstdAvailable").resolves({ + available: true, + foundZstdBinary: true, + }); + + const client = github.getOctokit("123"); + const listReleases = sinon.stub(client.rest.repos, "listReleases"); + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + listReleases.resolves({ + data: [{ tag_name: expectedTag }], + } as any); + sinon.stub(api, "getApiClient").value(() => client); + + await withTmpDir(async (tmpDir) => { + setupActionsVars(tmpDir, tmpDir); + const source = await setupCodeql.getCodeQLSource( + "nightly", + SAMPLE_DEFAULT_CLI_VERSION, + SAMPLE_DOTCOM_API_DETAILS, + GitHubVariant.DOTCOM, + false, + features, + logger, + ); + + // Check that the `CodeQLToolsSource` object matches our expectations. + const expectedVersion = `0.0.0-${expectedDate}`; + const expectedURL = `https://github.com/dsp-testing/codeql-cli-nightlies/releases/download/${expectedTag}/${setupCodeql.getCodeQLBundleName("zstd")}`; + t.deepEqual(source, { + bundleVersion: expectedDate, + cliVersion: undefined, + codeqlURL: expectedURL, + compressionMethod: "zstd", + sourceType: "download", + toolsVersion: expectedVersion, + } satisfies setupCodeql.CodeQLToolsSource); + + // Afterwards, ensure that we see the expected messages in the log. + checkExpectedLogMessages(t, loggedMessages, [ + "Using the latest CodeQL CLI nightly, as requested by 'tools: nightly'.", + `Bundle version ${expectedDate} is not in SemVer format. Will treat it as pre-release ${expectedVersion}.`, + `Attempting to obtain CodeQL tools. CLI version: unknown, bundle tag name: ${expectedTag}`, + `Using CodeQL CLI sourced from ${expectedURL}`, + ]); + }); +}); + test("getCodeQLSource correctly returns latest version from toolcache when tools == toolcache", async (t) => { const loggedMessages: LoggedMessage[] = []; const logger = getRecordingLogger(loggedMessages); diff --git a/src/setup-codeql.ts b/src/setup-codeql.ts index e003717c7..08dd1a407 100644 --- a/src/setup-codeql.ts +++ b/src/setup-codeql.ts @@ -55,7 +55,9 @@ function getCodeQLBundleExtension( } } -function getCodeQLBundleName(compressionMethod: tar.CompressionMethod): string { +export function getCodeQLBundleName( + compressionMethod: tar.CompressionMethod, +): string { const extension = getCodeQLBundleExtension(compressionMethod); let platform: string; @@ -196,7 +198,7 @@ export function convertToSemVer(version: string, logger: Logger): string { return s; } -type CodeQLToolsSource = +export type CodeQLToolsSource = | { codeqlTarPath: string; compressionMethod: tar.CompressionMethod;