Skip to content

Commit d4f1b14

Browse files
committed
Use new setupActionsVars pattern
1 parent 8a884bd commit d4f1b14

File tree

6 files changed

+23
-43
lines changed

6 files changed

+23
-43
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

@@ -374,9 +370,9 @@ test("saves overlay status when overlay-base analysis did not complete successfu
374370
attemptedToBuildOverlayBaseDatabase: true,
375371
builtOverlayBaseDatabase: false,
376372
job: {
377-
workflowRunId: 12345,
378-
workflowRunAttempt: 1,
379-
name: "analyze",
373+
workflowRunId: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ID),
374+
workflowRunAttempt: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ATTEMPT),
375+
name: DEFAULT_ACTIONS_VARS.GITHUB_JOB,
380376
},
381377
},
382378
"fourth arg should be the overlay status recording an unsuccessful build attempt with job details",
@@ -386,8 +382,7 @@ test("saves overlay status when overlay-base analysis did not complete successfu
386382

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

@@ -423,8 +418,7 @@ test("does not save overlay status when OverlayAnalysisStatusSave feature flag i
423418

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

@@ -460,8 +454,7 @@ test("does not save overlay status when build successful", async (t) => {
460454

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

467460
sinon.stub(util, "checkDiskUsage").resolves({
@@ -546,9 +539,8 @@ async function testFailedSarifUpload(
546539
config.dbLocation = "path/to/database";
547540
}
548541
process.env["GITHUB_JOB"] = "analyze";
549-
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
550-
process.env["GITHUB_WORKSPACE"] =
551-
"/home/runner/work/codeql-action/codeql-action";
542+
process.env["GITHUB_REPOSITORY"] = DEFAULT_ACTIONS_VARS.GITHUB_REPOSITORY;
543+
process.env["GITHUB_WORKSPACE"] = "/tmp";
552544
sinon
553545
.stub(actionsUtil, "getRequiredInput")
554546
.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function setupTests(test: TestFn<any>) {
144144
* environment. Tests can override individual variables by passing them in the
145145
* `overrides` parameter.
146146
*/
147-
const DEFAULT_ACTIONS_VARS: Record<string, string> = {
147+
export const DEFAULT_ACTIONS_VARS: Record<string, string> = {
148148
GITHUB_ACTION_REPOSITORY: "github/codeql-action",
149149
GITHUB_API_URL: "https://api.github.com",
150150
GITHUB_EVENT_NAME: "push",

0 commit comments

Comments
 (0)