|
1 | 1 | import * as path from "path"; |
2 | 2 |
|
| 3 | +import * as github from "@actions/github"; |
3 | 4 | import * as toolcache from "@actions/tool-cache"; |
4 | 5 | import test, { ExecutionContext } from "ava"; |
5 | 6 | import * as sinon from "sinon"; |
6 | 7 |
|
7 | 8 | import * as actionsUtil from "./actions-util"; |
| 9 | +import * as api from "./api-client"; |
8 | 10 | import { Feature, FeatureEnablement } from "./feature-flags"; |
9 | 11 | import { getRunnerLogger } from "./logging"; |
10 | 12 | import * as setupCodeql from "./setup-codeql"; |
| 13 | +import * as tar from "./tar"; |
11 | 14 | import { |
12 | 15 | LINKED_CLI_VERSION, |
13 | 16 | LoggedMessage, |
14 | 17 | SAMPLE_DEFAULT_CLI_VERSION, |
15 | 18 | SAMPLE_DOTCOM_API_DETAILS, |
| 19 | + checkExpectedLogMessages, |
16 | 20 | createFeatures, |
17 | 21 | getRecordingLogger, |
18 | 22 | initializeFeatures, |
@@ -268,6 +272,63 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow |
268 | 272 | }); |
269 | 273 | }); |
270 | 274 |
|
| 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 | + |
271 | 332 | test("getCodeQLSource correctly returns latest version from toolcache when tools == toolcache", async (t) => { |
272 | 333 | const loggedMessages: LoggedMessage[] = []; |
273 | 334 | const logger = getRecordingLogger(loggedMessages); |
|
0 commit comments