Skip to content

Commit 6123416

Browse files
committed
Merge remote-tracking branch 'origin/main' into henrymercer/parallel-unit-tests
2 parents 8e17ec9 + a6594f9 commit 6123416

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

src/analyze-action-env.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ test("analyze action with RAM & threads from environment variables", async (t) =
2828
// it a bit to 20s.
2929
t.timeout(1000 * 20);
3030
await util.withTmpDir(async (tmpDir) => {
31-
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
32-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
33-
process.env["GITHUB_API_URL"] = "https://api.github.com";
31+
setupActionsVars(tmpDir, tmpDir);
3432
sinon
3533
.stub(statusReport, "createStatusReportBase")
3634
.resolves({} as statusReport.StatusReportBase);
@@ -54,7 +52,6 @@ test("analyze action with RAM & threads from environment variables", async (t) =
5452
const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput");
5553
optionalInputStub.withArgs("expect-error").returns("false");
5654
sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion);
57-
setupActionsVars(tmpDir, tmpDir);
5855
mockFeatureFlagApiEndpoint(200, {});
5956

6057
// When there are no action inputs for RAM and threads, the action uses

src/analyze-action-input.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ setupTests(test);
2626
test("analyze action with RAM & threads from action inputs", async (t) => {
2727
t.timeout(1000 * 20);
2828
await util.withTmpDir(async (tmpDir) => {
29-
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
30-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
31-
process.env["GITHUB_API_URL"] = "https://api.github.com";
29+
setupActionsVars(tmpDir, tmpDir);
3230
sinon
3331
.stub(statusReport, "createStatusReportBase")
3432
.resolves({} as statusReport.StatusReportBase);
@@ -51,7 +49,6 @@ test("analyze action with RAM & threads from action inputs", async (t) => {
5149
optionalInputStub.withArgs("expect-error").returns("false");
5250
sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion);
5351
sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true);
54-
setupActionsVars(tmpDir, tmpDir);
5552
mockFeatureFlagApiEndpoint(200, {});
5653

5754
process.env["CODEQL_THREADS"] = "1";

src/init-action-post-helper.test.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { parseRepositoryNwo } from "./repository";
1515
import {
1616
createFeatures,
1717
createTestConfig,
18+
DEFAULT_ACTIONS_VARS,
1819
makeVersionInfo,
20+
setupActionsVars,
1921
setupTests,
2022
} from "./testing-utils";
2123
import * as uploadLib from "./upload-lib";
@@ -28,8 +30,7 @@ setupTests(test);
2830

2931
test.serial("init-post action with debug mode off", async (t) => {
3032
return await util.withTmpDir(async (tmpDir) => {
31-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
32-
process.env["RUNNER_TEMP"] = tmpDir;
33+
setupActionsVars(tmpDir, tmpDir);
3334

3435
const gitHubVersion: util.GitHubVersion = {
3536
type: util.GitHubVariant.DOTCOM,
@@ -62,8 +63,7 @@ test.serial("init-post action with debug mode off", async (t) => {
6263

6364
test.serial("init-post action with debug mode on", async (t) => {
6465
return await util.withTmpDir(async (tmpDir) => {
65-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
66-
process.env["RUNNER_TEMP"] = tmpDir;
66+
setupActionsVars(tmpDir, tmpDir);
6767

6868
const uploadAllAvailableDebugArtifactsSpy = sinon.spy();
6969
const printDebugLogsSpy = sinon.spy();
@@ -343,11 +343,7 @@ test.serial(
343343
"saves overlay status when overlay-base analysis did not complete successfully",
344344
async (t) => {
345345
return await util.withTmpDir(async (tmpDir) => {
346-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
347-
process.env["GITHUB_RUN_ID"] = "12345";
348-
process.env["GITHUB_RUN_ATTEMPT"] = "1";
349-
process.env["GITHUB_JOB"] = "analyze";
350-
process.env["RUNNER_TEMP"] = tmpDir;
346+
setupActionsVars(tmpDir, tmpDir);
351347
// Ensure analyze did not complete successfully.
352348
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
353349

@@ -403,9 +399,9 @@ test.serial(
403399
builtOverlayBaseDatabase: false,
404400
job: {
405401
checkRunId: undefined,
406-
workflowRunId: 12345,
407-
workflowRunAttempt: 1,
408-
name: "analyze",
402+
workflowRunId: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ID),
403+
workflowRunAttempt: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ATTEMPT),
404+
name: DEFAULT_ACTIONS_VARS.GITHUB_JOB,
409405
},
410406
},
411407
"fourth arg should be the overlay status recording an unsuccessful build attempt with job details",
@@ -418,8 +414,7 @@ test.serial(
418414
"does not save overlay status when OverlayAnalysisStatusSave feature flag is disabled",
419415
async (t) => {
420416
return await util.withTmpDir(async (tmpDir) => {
421-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
422-
process.env["RUNNER_TEMP"] = tmpDir;
417+
setupActionsVars(tmpDir, tmpDir);
423418
// Ensure analyze did not complete successfully.
424419
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
425420

@@ -456,8 +451,7 @@ test.serial(
456451

457452
test.serial("does not save overlay status when build successful", async (t) => {
458453
return await util.withTmpDir(async (tmpDir) => {
459-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
460-
process.env["RUNNER_TEMP"] = tmpDir;
454+
setupActionsVars(tmpDir, tmpDir);
461455
// Mark analyze as having completed successfully.
462456
process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY] = "true";
463457

@@ -495,8 +489,7 @@ test.serial(
495489
"does not save overlay status when overlay not enabled",
496490
async (t) => {
497491
return await util.withTmpDir(async (tmpDir) => {
498-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
499-
process.env["RUNNER_TEMP"] = tmpDir;
492+
setupActionsVars(tmpDir, tmpDir);
500493
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
501494

502495
sinon.stub(util, "checkDiskUsage").resolves({
@@ -582,9 +575,8 @@ async function testFailedSarifUpload(
582575
config.dbLocation = "path/to/database";
583576
}
584577
process.env["GITHUB_JOB"] = "analyze";
585-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
586-
process.env["GITHUB_WORKSPACE"] =
587-
"/home/runner/work/codeql-action/codeql-action";
578+
process.env["GITHUB_REPOSITORY"] = DEFAULT_ACTIONS_VARS.GITHUB_REPOSITORY;
579+
process.env["GITHUB_WORKSPACE"] = "/tmp";
588580
sinon
589581
.stub(actionsUtil, "getRequiredInput")
590582
.withArgs("matrix")

src/setup-codeql.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ test.serial(
373373
sinon.stub(api, "getApiClient").value(() => client);
374374

375375
await withTmpDir(async (tmpDir) => {
376-
setupActionsVars(tmpDir, tmpDir);
377-
process.env["GITHUB_EVENT_NAME"] = "dynamic";
376+
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
378377

379378
const source = await setupCodeql.getCodeQLSource(
380379
undefined,
@@ -428,8 +427,7 @@ test.serial(
428427
.returns(latestVersionPath);
429428

430429
await withTmpDir(async (tmpDir) => {
431-
setupActionsVars(tmpDir, tmpDir);
432-
process.env["GITHUB_EVENT_NAME"] = "dynamic";
430+
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
433431

434432
const source = await setupCodeql.getCodeQLSource(
435433
"toolcache",

src/status-report.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ import { BuildMode, ConfigurationError, withTmpDir, wrapError } from "./util";
2525
setupTests(test);
2626

2727
function setupEnvironmentAndStub(tmpDir: string) {
28-
setupActionsVars(tmpDir, tmpDir);
28+
setupActionsVars(tmpDir, tmpDir, {
29+
GITHUB_EVENT_NAME: "dynamic",
30+
GITHUB_RUN_ATTEMPT: "2",
31+
GITHUB_RUN_ID: "100",
32+
});
2933

3034
process.env[EnvVar.ANALYSIS_KEY] = "analysis-key";
31-
process.env["GITHUB_EVENT_NAME"] = "dynamic";
32-
process.env["GITHUB_REF"] = "refs/heads/main";
33-
process.env["GITHUB_REPOSITORY"] = "octocat/HelloWorld";
34-
process.env["GITHUB_RUN_ATTEMPT"] = "2";
35-
process.env["GITHUB_RUN_ID"] = "100";
36-
process.env["GITHUB_SHA"] = "a".repeat(40);
3735
process.env["ImageVersion"] = "2023.05.19.1";
38-
process.env["RUNNER_OS"] = "macOS";
39-
process.env["RUNNER_TEMP"] = tmpDir;
4036

4137
const getRequiredInput = sinon.stub(actionsUtil, "getRequiredInput");
4238
getRequiredInput.withArgs("matrix").resolves("input/matrix");

src/testing-utils.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,40 @@ export function setupTests(test: TestFn<any>) {
139139
});
140140
}
141141

142+
/**
143+
* Default values for environment variables typically set in an Actions
144+
* environment. Tests can override individual variables by passing them in the
145+
* `overrides` parameter.
146+
*/
147+
export const DEFAULT_ACTIONS_VARS = {
148+
GITHUB_ACTION_REPOSITORY: "github/codeql-action",
149+
GITHUB_API_URL: "https://api.github.com",
150+
GITHUB_EVENT_NAME: "push",
151+
GITHUB_JOB: "test-job",
152+
GITHUB_REF: "refs/heads/main",
153+
GITHUB_REPOSITORY: "github/codeql-action-testing",
154+
GITHUB_RUN_ATTEMPT: "1",
155+
GITHUB_RUN_ID: "1",
156+
GITHUB_SERVER_URL: "https://github.com",
157+
GITHUB_SHA: "0".repeat(40),
158+
GITHUB_WORKFLOW: "test-workflow",
159+
RUNNER_OS: "Linux",
160+
} as const satisfies Record<string, string>;
161+
142162
// Sets environment variables that make using some libraries designed for
143163
// use only on actions safe to use outside of actions.
144-
export function setupActionsVars(tempDir: string, toolsDir: string) {
164+
export function setupActionsVars(
165+
tempDir: string,
166+
toolsDir: string,
167+
overrides?: Partial<Record<keyof typeof DEFAULT_ACTIONS_VARS, string>>,
168+
) {
169+
const vars = { ...DEFAULT_ACTIONS_VARS, ...overrides };
170+
for (const [key, value] of Object.entries(vars)) {
171+
process.env[key] = value;
172+
}
145173
process.env["RUNNER_TEMP"] = tempDir;
146174
process.env["RUNNER_TOOL_CACHE"] = toolsDir;
147175
process.env["GITHUB_WORKSPACE"] = tempDir;
148-
process.env["GITHUB_EVENT_NAME"] = "push";
149176
}
150177

151178
type LogLevel = "debug" | "info" | "warning" | "error";

0 commit comments

Comments
 (0)