Skip to content

Commit ec0c6ce

Browse files
authored
Merge pull request #3540 from github/henrymercer/stub-actions-vars
Testing: Provide default value for more environment variables in `setupActionsVars`
2 parents a5b959e + 71d7981 commit ec0c6ce

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("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("init-post action with debug mode off", async (t) => {
6263

6364
test("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();
@@ -315,11 +315,7 @@ test("not uploading failed SARIF when `code-scanning` is not an enabled analysis
315315

316316
test("saves overlay status when overlay-base analysis did not complete successfully", async (t) => {
317317
return await util.withTmpDir(async (tmpDir) => {
318-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
319-
process.env["GITHUB_RUN_ID"] = "12345";
320-
process.env["GITHUB_RUN_ATTEMPT"] = "1";
321-
process.env["GITHUB_JOB"] = "analyze";
322-
process.env["RUNNER_TEMP"] = tmpDir;
318+
setupActionsVars(tmpDir, tmpDir);
323319
// Ensure analyze did not complete successfully.
324320
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
325321

@@ -375,9 +371,9 @@ test("saves overlay status when overlay-base analysis did not complete successfu
375371
builtOverlayBaseDatabase: false,
376372
job: {
377373
checkRunId: undefined,
378-
workflowRunId: 12345,
379-
workflowRunAttempt: 1,
380-
name: "analyze",
374+
workflowRunId: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ID),
375+
workflowRunAttempt: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ATTEMPT),
376+
name: DEFAULT_ACTIONS_VARS.GITHUB_JOB,
381377
},
382378
},
383379
"fourth arg should be the overlay status recording an unsuccessful build attempt with job details",
@@ -387,8 +383,7 @@ test("saves overlay status when overlay-base analysis did not complete successfu
387383

388384
test("does not save overlay status when OverlayAnalysisStatusSave feature flag is disabled", async (t) => {
389385
return await util.withTmpDir(async (tmpDir) => {
390-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
391-
process.env["RUNNER_TEMP"] = tmpDir;
386+
setupActionsVars(tmpDir, tmpDir);
392387
// Ensure analyze did not complete successfully.
393388
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
394389

@@ -424,8 +419,7 @@ test("does not save overlay status when OverlayAnalysisStatusSave feature flag i
424419

425420
test("does not save overlay status when build successful", async (t) => {
426421
return await util.withTmpDir(async (tmpDir) => {
427-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
428-
process.env["RUNNER_TEMP"] = tmpDir;
422+
setupActionsVars(tmpDir, tmpDir);
429423
// Mark analyze as having completed successfully.
430424
process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY] = "true";
431425

@@ -461,8 +455,7 @@ test("does not save overlay status when build successful", async (t) => {
461455

462456
test("does not save overlay status when overlay not enabled", async (t) => {
463457
return await util.withTmpDir(async (tmpDir) => {
464-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
465-
process.env["RUNNER_TEMP"] = tmpDir;
458+
setupActionsVars(tmpDir, tmpDir);
466459
delete process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY];
467460

468461
sinon.stub(util, "checkDiskUsage").resolves({
@@ -547,9 +540,8 @@ async function testFailedSarifUpload(
547540
config.dbLocation = "path/to/database";
548541
}
549542
process.env["GITHUB_JOB"] = "analyze";
550-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
551-
process.env["GITHUB_WORKSPACE"] =
552-
"/home/runner/work/codeql-action/codeql-action";
543+
process.env["GITHUB_REPOSITORY"] = DEFAULT_ACTIONS_VARS.GITHUB_REPOSITORY;
544+
process.env["GITHUB_WORKSPACE"] = "/tmp";
553545
sinon
554546
.stub(actionsUtil, "getRequiredInput")
555547
.withArgs("matrix")

src/setup-codeql.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ test("getCodeQLSource correctly returns nightly CLI version when forced by FF",
353353
sinon.stub(api, "getApiClient").value(() => client);
354354

355355
await withTmpDir(async (tmpDir) => {
356-
setupActionsVars(tmpDir, tmpDir);
357-
process.env["GITHUB_EVENT_NAME"] = "dynamic";
356+
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
358357

359358
const source = await setupCodeql.getCodeQLSource(
360359
undefined,
@@ -405,8 +404,7 @@ test("getCodeQLSource correctly returns latest version from toolcache when tools
405404
.returns(latestVersionPath);
406405

407406
await withTmpDir(async (tmpDir) => {
408-
setupActionsVars(tmpDir, tmpDir);
409-
process.env["GITHUB_EVENT_NAME"] = "dynamic";
407+
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
410408

411409
const source = await setupCodeql.getCodeQLSource(
412410
"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)