Skip to content

Commit 68f5b57

Browse files
authored
Merge pull request #4212 from github/mbg/tests/fix-export-variable-2
Do not use `core.exportVariable` in unit tests (2nd attempt)
2 parents 5aa5056 + bd97dda commit 68f5b57

26 files changed

Lines changed: 720 additions & 551 deletions

‎ava.setup.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import pkg from "./package.json" with { type: "json" };
22

33
globalThis.__CODEQL_ACTION_VERSION__ = pkg.version;
4+
globalThis.__CODEQL_ACTION_TEST_ENV__ = "unit-test";

‎build.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const UPLOAD_LIB_SRC = "./src/upload-lib";
7878
*
7979
* The virtual module additionally re-exports `upload-lib` under the `uploadLib` namespace so that
8080
* external consumers can access it via the small `lib/upload-lib.js` stub emitted below.
81-
*
81+
*
8282
* A tiny stub file is emitted for each Action entrypoint, and one for `upload-lib`. Each stub
8383
* imports the shared bundle and calls/re-exports from the respective entry point.
8484
*
@@ -212,6 +212,7 @@ const context = await esbuild.context({
212212
target: ["node20"],
213213
define: {
214214
__CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version),
215+
__CODEQL_ACTION_TEST_ENV__: JSON.stringify(""),
215216
},
216217
metafile: true,
217218
});

‎eslint.config.mjs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ export default [
140140
"no-async-foreach/no-async-foreach": "error",
141141
"no-sequences": "error",
142142
"no-shadow": "off",
143+
144+
// A basic check that we don't use `exportVariable` from `@actions/core`.
145+
"no-restricted-syntax": [
146+
"error",
147+
{
148+
selector: "MemberExpression[property.name='exportVariable']",
149+
message:
150+
"Use the `export` method of an `Env` instance or `exportEnvVar` from `environment.ts` instead.",
151+
},
152+
],
153+
143154
// This is overly restrictive with unsetting `EnvVar`s
144155
"@typescript-eslint/no-dynamic-delete": "off",
145156
"@typescript-eslint/no-shadow": "error",
@@ -157,6 +168,15 @@ export default [
157168
],
158169
},
159170
},
171+
{
172+
files: ["src/environment.ts"],
173+
174+
// We allow `exportVariable` from `@actions/core` to be used in this file
175+
// since it defines the wrapper around it that other modules use.
176+
rules: {
177+
"no-restricted-syntax": "off",
178+
},
179+
},
160180
{
161181
files: ["**/*.ts", "**/*.js"],
162182

‎lib/entry-points.js‎

Lines changed: 488 additions & 446 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎queries/default-setup-environment-variables.ql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ predicate isSafeForDefaultSetup(string envVar) {
2323
"GITHUB_BASE_REF", "GITHUB_EVENT_NAME", "GITHUB_JOB", "GITHUB_RUN_ATTEMPT", "GITHUB_RUN_ID",
2424
"GITHUB_SHA", "GITHUB_REPOSITORY", "GITHUB_SERVER_URL", "GITHUB_TOKEN", "GITHUB_WORKFLOW",
2525
"GITHUB_WORKSPACE", "GOFLAGS", "ImageVersion", "JAVA_TOOL_OPTIONS", "RUNNER_ARCH",
26-
"RUNNER_ENVIRONMENT", "RUNNER_NAME", "RUNNER_OS", "RUNNER_TEMP", "RUNNER_TOOL_CACHE"
26+
"RUNNER_ENVIRONMENT", "RUNNER_NAME", "RUNNER_OS", "RUNNER_TEMP", "RUNNER_TOOL_CACHE",
27+
"NODE_ENV"
2728
]
2829
}
2930

‎src/actions-util.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ declare const __CODEQL_ACTION_VERSION__: string;
3030
export interface ActionsEnv {
3131
getRequiredInput: (name: string) => string;
3232
getOptionalInput: (name: string) => string | undefined;
33-
exportVariable: (name: string, value: string) => void;
3433
}
3534

3635
/**
@@ -40,7 +39,6 @@ export function getActionsEnv(): ActionsEnv {
4039
return {
4140
getRequiredInput,
4241
getOptionalInput,
43-
exportVariable: core.exportVariable,
4442
};
4543
}
4644

‎src/analyze-action.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ async function runAutobuildIfLegacyGoWorkflow(config: Config, logger: Logger) {
214214

215215
async function run({
216216
startedAt,
217+
env,
217218
logger,
218219
actions,
219-
}: ActionState<["Base", "Logger", "Actions"]>) {
220+
}: ActionState<["Base", "Env", "Logger", "Actions"]>) {
220221
// To capture errors appropriately, keep as much code within the try-catch as
221222
// possible, and only use safe functions outside.
222223

@@ -287,7 +288,7 @@ async function run({
287288

288289
const apiDetails = getApiDetails();
289290
const outputDir = actionsUtil.getRequiredInput("output");
290-
core.exportVariable(EnvVar.SARIF_RESULTS_OUTPUT_DIR, outputDir);
291+
env.export(EnvVar.SARIF_RESULTS_OUTPUT_DIR, outputDir);
291292
const threads = util.getThreadsFlag(
292293
actionsUtil.getOptionalInput("threads") || process.env["CODEQL_THREADS"],
293294
logger,
@@ -456,7 +457,7 @@ async function run({
456457
`expect-error input was set to true but no error was thrown.`,
457458
);
458459
}
459-
core.exportVariable(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
460+
env.export(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
460461
} catch (unwrappedError) {
461462
const error = util.wrapError(unwrappedError);
462463
if (

‎src/api-client.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ReadOnlyEnv,
1818
RegistryProxyVars,
1919
getEnv,
20+
exportEnvVar,
2021
} from "./environment";
2122
import { Logger } from "./logging";
2223
import { getRepositoryNwo, RepositoryNwo } from "./repository";
@@ -316,7 +317,7 @@ export async function getAnalysisKey(): Promise<string> {
316317
const jobName = getRequiredEnvParam("GITHUB_JOB");
317318

318319
analysisKey = `${workflowPath}:${jobName}`;
319-
core.exportVariable(EnvVar.ANALYSIS_KEY, analysisKey);
320+
exportEnvVar(EnvVar.ANALYSIS_KEY, analysisKey);
320321
return analysisKey;
321322
}
322323

‎src/autobuild-action.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ async function sendCompletedStatusReport(
6868
}
6969
}
7070

71-
async function run({ startedAt, logger }: ActionState<["Base", "Logger"]>) {
71+
async function run({
72+
startedAt,
73+
env,
74+
logger,
75+
}: ActionState<["Base", "Env", "Logger"]>) {
7276
// To capture errors appropriately, keep as much code within the try-catch as
7377
// possible, and only use safe functions outside.
7478

@@ -135,7 +139,7 @@ async function run({ startedAt, logger }: ActionState<["Base", "Logger"]>) {
135139
return;
136140
}
137141

138-
core.exportVariable(EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY, "true");
142+
env.export(EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY, "true");
139143

140144
await sendCompletedStatusReport(config, logger, startedAt, languages ?? []);
141145
}

‎src/autobuild.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import * as core from "@actions/core";
2-
31
import { getTemporaryDirectory, getWorkflowEventName } from "./actions-util";
42
import { getGitHubVersion } from "./api-client";
53
import { CodeQL, getCodeQL } from "./codeql";
64
import * as configUtils from "./config-utils";
75
import { DocUrl } from "./doc-url";
8-
import { ActionsEnvVars, EnvVar } from "./environment";
6+
import { ActionsEnvVars, EnvVar, exportEnvVar } from "./environment";
97
import { Feature, featureConfig, initFeatures } from "./feature-flags";
108
import { BuiltInLanguage, Language } from "./languages";
119
import { Logger } from "./logging";
@@ -136,16 +134,16 @@ export async function setupCppAutobuild(codeql: CodeQL, logger: Logger) {
136134
: ""
137135
}`,
138136
);
139-
core.exportVariable(envVar, "false");
137+
exportEnvVar(envVar, "false");
140138
} else {
141139
logger.info(
142140
`Enabling ${featureName}. This can be disabled by setting the ${envVar} environment variable to 'false'. See ${DocUrl.DEFINE_ENV_VARIABLES} for more information.`,
143141
);
144-
core.exportVariable(envVar, "true");
142+
exportEnvVar(envVar, "true");
145143
}
146144
} else {
147145
logger.info(`Disabling ${featureName}.`);
148-
core.exportVariable(envVar, "false");
146+
exportEnvVar(envVar, "false");
149147
}
150148
}
151149

@@ -165,7 +163,7 @@ export async function runAutobuild(
165163
await codeQL.runAutobuild(config, language);
166164
}
167165
if (language === BuiltInLanguage.go) {
168-
core.exportVariable(EnvVar.DID_AUTOBUILD_GOLANG, "true");
166+
exportEnvVar(EnvVar.DID_AUTOBUILD_GOLANG, "true");
169167
}
170168
logger.endGroup();
171169
}

0 commit comments

Comments
 (0)