Skip to content

Commit 466a4f0

Browse files
committed
Add unit test for tools: nightly
1 parent 817d568 commit 466a4f0

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/setup-codeql.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import * as path from "path";
22

3+
import * as github from "@actions/github";
34
import * as toolcache from "@actions/tool-cache";
45
import test, { ExecutionContext } from "ava";
56
import * as sinon from "sinon";
67

78
import * as actionsUtil from "./actions-util";
9+
import * as api from "./api-client";
810
import { Feature, FeatureEnablement } from "./feature-flags";
911
import { getRunnerLogger } from "./logging";
1012
import * as setupCodeql from "./setup-codeql";
13+
import * as tar from "./tar";
1114
import {
1215
LINKED_CLI_VERSION,
1316
LoggedMessage,
1417
SAMPLE_DEFAULT_CLI_VERSION,
1518
SAMPLE_DOTCOM_API_DETAILS,
19+
checkExpectedLogMessages,
1620
createFeatures,
1721
getRecordingLogger,
1822
initializeFeatures,
@@ -268,6 +272,63 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow
268272
});
269273
});
270274

275+
test("getCodeQLSource correctly returns nightly CLI version when tools == nightly", async (t) => {
276+
const loggedMessages: LoggedMessage[] = [];
277+
const logger = getRecordingLogger(loggedMessages);
278+
const features = createFeatures([]);
279+
280+
const expectedDate = "30260213";
281+
const expectedTag = `codeql-bundle-${expectedDate}`;
282+
283+
// Ensure that we consistently select "zstd" for the test.
284+
sinon.stub(process, "platform").value("linux");
285+
sinon.stub(tar, "isZstdAvailable").resolves({
286+
available: true,
287+
foundZstdBinary: true,
288+
});
289+
290+
const client = github.getOctokit("123");
291+
const listReleases = sinon.stub(client.rest.repos, "listReleases");
292+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
293+
listReleases.resolves({
294+
data: [{ tag_name: expectedTag }],
295+
} as any);
296+
sinon.stub(api, "getApiClient").value(() => client);
297+
298+
await withTmpDir(async (tmpDir) => {
299+
setupActionsVars(tmpDir, tmpDir);
300+
const source = await setupCodeql.getCodeQLSource(
301+
"nightly",
302+
SAMPLE_DEFAULT_CLI_VERSION,
303+
SAMPLE_DOTCOM_API_DETAILS,
304+
GitHubVariant.DOTCOM,
305+
false,
306+
features,
307+
logger,
308+
);
309+
310+
// Check that the `CodeQLToolsSource` object matches our expectations.
311+
const expectedVersion = `0.0.0-${expectedDate}`;
312+
const expectedURL = `https://github.com/dsp-testing/codeql-cli-nightlies/releases/download/${expectedTag}/${setupCodeql.getCodeQLBundleName("zstd")}`;
313+
t.deepEqual(source, {
314+
bundleVersion: expectedDate,
315+
cliVersion: undefined,
316+
codeqlURL: expectedURL,
317+
compressionMethod: "zstd",
318+
sourceType: "download",
319+
toolsVersion: expectedVersion,
320+
} satisfies setupCodeql.CodeQLToolsSource);
321+
322+
// Afterwards, ensure that we see the expected messages in the log.
323+
checkExpectedLogMessages(t, loggedMessages, [
324+
"Using the latest CodeQL CLI nightly, as requested by 'tools: nightly'.",
325+
`Bundle version ${expectedDate} is not in SemVer format. Will treat it as pre-release ${expectedVersion}.`,
326+
`Attempting to obtain CodeQL tools. CLI version: unknown, bundle tag name: ${expectedTag}`,
327+
`Using CodeQL CLI sourced from ${expectedURL}`,
328+
]);
329+
});
330+
});
331+
271332
test("getCodeQLSource correctly returns latest version from toolcache when tools == toolcache", async (t) => {
272333
const loggedMessages: LoggedMessage[] = [];
273334
const logger = getRecordingLogger(loggedMessages);

src/setup-codeql.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ function getCodeQLBundleExtension(
5555
}
5656
}
5757

58-
function getCodeQLBundleName(compressionMethod: tar.CompressionMethod): string {
58+
export function getCodeQLBundleName(
59+
compressionMethod: tar.CompressionMethod,
60+
): string {
5961
const extension = getCodeQLBundleExtension(compressionMethod);
6062

6163
let platform: string;
@@ -196,7 +198,7 @@ export function convertToSemVer(version: string, logger: Logger): string {
196198
return s;
197199
}
198200

199-
type CodeQLToolsSource =
201+
export type CodeQLToolsSource =
200202
| {
201203
codeqlTarPath: string;
202204
compressionMethod: tar.CompressionMethod;

0 commit comments

Comments
 (0)