Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/analyze-action-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ test("analyze action with RAM & threads from environment variables", async (t) =
// it a bit to 20s.
t.timeout(1000 * 20);
await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_API_URL"] = "https://api.github.com";
setupActionsVars(tmpDir, tmpDir);
sinon
.stub(statusReport, "createStatusReportBase")
.resolves({} as statusReport.StatusReportBase);
Expand All @@ -54,7 +52,6 @@ test("analyze action with RAM & threads from environment variables", async (t) =
const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput");
optionalInputStub.withArgs("expect-error").returns("false");
sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion);
setupActionsVars(tmpDir, tmpDir);
mockFeatureFlagApiEndpoint(200, {});

// When there are no action inputs for RAM and threads, the action uses
Expand Down
5 changes: 1 addition & 4 deletions src/analyze-action-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ setupTests(test);
test("analyze action with RAM & threads from action inputs", async (t) => {
t.timeout(1000 * 20);
await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_SERVER_URL"] = util.GITHUB_DOTCOM_URL;
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_API_URL"] = "https://api.github.com";
setupActionsVars(tmpDir, tmpDir);
sinon
.stub(statusReport, "createStatusReportBase")
.resolves({} as statusReport.StatusReportBase);
Expand All @@ -51,7 +49,6 @@ test("analyze action with RAM & threads from action inputs", async (t) => {
optionalInputStub.withArgs("expect-error").returns("false");
sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion);
sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true);
setupActionsVars(tmpDir, tmpDir);
mockFeatureFlagApiEndpoint(200, {});

process.env["CODEQL_THREADS"] = "1";
Expand Down
34 changes: 13 additions & 21 deletions src/init-action-post-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { parseRepositoryNwo } from "./repository";
import {
createFeatures,
createTestConfig,
DEFAULT_ACTIONS_VARS,
makeVersionInfo,
setupActionsVars,
setupTests,
} from "./testing-utils";
import * as uploadLib from "./upload-lib";
Expand All @@ -28,8 +30,7 @@ setupTests(test);

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

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

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

const uploadAllAvailableDebugArtifactsSpy = sinon.spy();
const printDebugLogsSpy = sinon.spy();
Expand Down Expand Up @@ -315,11 +315,7 @@ test("not uploading failed SARIF when `code-scanning` is not an enabled analysis

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

Expand Down Expand Up @@ -375,9 +371,9 @@ test("saves overlay status when overlay-base analysis did not complete successfu
builtOverlayBaseDatabase: false,
job: {
checkRunId: undefined,
workflowRunId: 12345,
workflowRunAttempt: 1,
name: "analyze",
workflowRunId: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ID),
workflowRunAttempt: Number(DEFAULT_ACTIONS_VARS.GITHUB_RUN_ATTEMPT),
name: DEFAULT_ACTIONS_VARS.GITHUB_JOB,
},
},
"fourth arg should be the overlay status recording an unsuccessful build attempt with job details",
Expand All @@ -387,8 +383,7 @@ test("saves overlay status when overlay-base analysis did not complete successfu

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

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

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

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

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

sinon.stub(util, "checkDiskUsage").resolves({
Expand Down Expand Up @@ -547,9 +540,8 @@ async function testFailedSarifUpload(
config.dbLocation = "path/to/database";
}
process.env["GITHUB_JOB"] = "analyze";
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_WORKSPACE"] =
"/home/runner/work/codeql-action/codeql-action";
process.env["GITHUB_REPOSITORY"] = DEFAULT_ACTIONS_VARS.GITHUB_REPOSITORY;
process.env["GITHUB_WORKSPACE"] = "/tmp";
sinon
.stub(actionsUtil, "getRequiredInput")
.withArgs("matrix")
Expand Down
6 changes: 2 additions & 4 deletions src/setup-codeql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ test("getCodeQLSource correctly returns nightly CLI version when forced by FF",
sinon.stub(api, "getApiClient").value(() => client);

await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
process.env["GITHUB_EVENT_NAME"] = "dynamic";
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });

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

await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
process.env["GITHUB_EVENT_NAME"] = "dynamic";
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });

const source = await setupCodeql.getCodeQLSource(
"toolcache",
Expand Down
14 changes: 5 additions & 9 deletions src/status-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ import { BuildMode, ConfigurationError, withTmpDir, wrapError } from "./util";
setupTests(test);

function setupEnvironmentAndStub(tmpDir: string) {
setupActionsVars(tmpDir, tmpDir);
setupActionsVars(tmpDir, tmpDir, {
GITHUB_EVENT_NAME: "dynamic",
GITHUB_RUN_ATTEMPT: "2",
GITHUB_RUN_ID: "100",
});

process.env[EnvVar.ANALYSIS_KEY] = "analysis-key";
process.env["GITHUB_EVENT_NAME"] = "dynamic";
process.env["GITHUB_REF"] = "refs/heads/main";
process.env["GITHUB_REPOSITORY"] = "octocat/HelloWorld";
process.env["GITHUB_RUN_ATTEMPT"] = "2";
process.env["GITHUB_RUN_ID"] = "100";
process.env["GITHUB_SHA"] = "a".repeat(40);
process.env["ImageVersion"] = "2023.05.19.1";
process.env["RUNNER_OS"] = "macOS";
process.env["RUNNER_TEMP"] = tmpDir;

const getRequiredInput = sinon.stub(actionsUtil, "getRequiredInput");
getRequiredInput.withArgs("matrix").resolves("input/matrix");
Expand Down
31 changes: 29 additions & 2 deletions src/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,40 @@ export function setupTests(test: TestFn<any>) {
});
}

/**
* Default values for environment variables typically set in an Actions
* environment. Tests can override individual variables by passing them in the
* `overrides` parameter.
*/
export const DEFAULT_ACTIONS_VARS = {
GITHUB_ACTION_REPOSITORY: "github/codeql-action",
GITHUB_API_URL: "https://api.github.com",
GITHUB_EVENT_NAME: "push",
GITHUB_JOB: "test-job",
GITHUB_REF: "refs/heads/main",
GITHUB_REPOSITORY: "github/codeql-action-testing",
GITHUB_RUN_ATTEMPT: "1",
GITHUB_RUN_ID: "1",
GITHUB_SERVER_URL: "https://github.com",
GITHUB_SHA: "0".repeat(40),
GITHUB_WORKFLOW: "test-workflow",
RUNNER_OS: "Linux",
} as const satisfies Record<string, string>;

// Sets environment variables that make using some libraries designed for
// use only on actions safe to use outside of actions.
export function setupActionsVars(tempDir: string, toolsDir: string) {
export function setupActionsVars(
tempDir: string,
toolsDir: string,
overrides?: Partial<Record<keyof typeof DEFAULT_ACTIONS_VARS, string>>,
) {
const vars = { ...DEFAULT_ACTIONS_VARS, ...overrides };
for (const [key, value] of Object.entries(vars)) {
process.env[key] = value;
}
process.env["RUNNER_TEMP"] = tempDir;
process.env["RUNNER_TOOL_CACHE"] = toolsDir;
process.env["GITHUB_WORKSPACE"] = tempDir;
process.env["GITHUB_EVENT_NAME"] = "push";
}

type LogLevel = "debug" | "info" | "warning" | "error";
Expand Down
Loading