From 8a884bdb366e48fe97d4249777075f9079de8cd1 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Tue, 3 Mar 2026 19:09:57 +0100 Subject: [PATCH] Extend `setupActionsVars` --- src/testing-utils.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/testing-utils.ts b/src/testing-utils.ts index 3abc1f4f4..317f9fa9b 100644 --- a/src/testing-utils.ts +++ b/src/testing-utils.ts @@ -139,13 +139,40 @@ export function setupTests(test: TestFn) { }); } +/** + * Default values for environment variables typically set in an Actions + * environment. Tests can override individual variables by passing them in the + * `overrides` parameter. + */ +const DEFAULT_ACTIONS_VARS: Record = { + 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", +}; + // 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>, +) { + 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";