Skip to content

Commit dc5f2b9

Browse files
committed
Add wrapper around core.exportVariable
1 parent ffebdc8 commit dc5f2b9

18 files changed

Lines changed: 517 additions & 504 deletions

lib/entry-points.js

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

src/analyze-action.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
DependencyCacheUploadStatusReport,
2929
uploadDependencyCaches,
3030
} from "./dependency-caching";
31-
import { EnvVar } from "./environment";
31+
import { EnvVar, exportVariable } from "./environment";
3232
import { initFeatures } from "./feature-flags";
3333
import { BuiltInLanguage } from "./languages";
3434
import { getActionsLogger, Logger } from "./logging";
@@ -284,7 +284,7 @@ async function run(startedAt: Date) {
284284

285285
const apiDetails = getApiDetails();
286286
const outputDir = actionsUtil.getRequiredInput("output");
287-
core.exportVariable(EnvVar.SARIF_RESULTS_OUTPUT_DIR, outputDir);
287+
exportVariable(EnvVar.SARIF_RESULTS_OUTPUT_DIR, outputDir);
288288
const threads = util.getThreadsFlag(
289289
actionsUtil.getOptionalInput("threads") || process.env["CODEQL_THREADS"],
290290
logger,
@@ -444,7 +444,7 @@ async function run(startedAt: Date) {
444444
`expect-error input was set to true but no error was thrown.`,
445445
);
446446
}
447-
core.exportVariable(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
447+
exportVariable(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
448448
} catch (unwrappedError) {
449449
const error = util.wrapError(unwrappedError);
450450
if (

src/api-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as githubUtils from "@actions/github/lib/utils";
33
import * as retry from "@octokit/plugin-retry";
44

55
import { getActionVersion, getRequiredInput } from "./actions-util";
6-
import { EnvVar } from "./environment";
6+
import { EnvVar, exportVariable } from "./environment";
77
import { Logger } from "./logging";
88
import { getRepositoryNwo, RepositoryNwo } from "./repository";
99
import {
@@ -216,7 +216,7 @@ export async function getAnalysisKey(): Promise<string> {
216216
const jobName = getRequiredEnvParam("GITHUB_JOB");
217217

218218
analysisKey = `${workflowPath}:${jobName}`;
219-
core.exportVariable(EnvVar.ANALYSIS_KEY, analysisKey);
219+
exportVariable(EnvVar.ANALYSIS_KEY, analysisKey);
220220
return analysisKey;
221221
}
222222

src/autobuild-action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getGitHubVersion } from "./api-client";
99
import { determineAutobuildLanguages, runAutobuild } from "./autobuild";
1010
import { getCodeQL } from "./codeql";
1111
import { Config, getConfig } from "./config-utils";
12-
import { EnvVar } from "./environment";
12+
import { EnvVar, exportVariable } from "./environment";
1313
import { Language } from "./languages";
1414
import { Logger, getActionsLogger } from "./logging";
1515
import {
@@ -137,7 +137,7 @@ async function run(startedAt: Date) {
137137
return;
138138
}
139139

140-
core.exportVariable(EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY, "true");
140+
exportVariable(EnvVar.AUTOBUILD_DID_COMPLETE_SUCCESSFULLY, "true");
141141

142142
await sendCompletedStatusReport(config, logger, startedAt, languages ?? []);
143143
}

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 { EnvVar } from "./environment";
6+
import { EnvVar, exportVariable } 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+
exportVariable(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+
exportVariable(envVar, "true");
145143
}
146144
} else {
147145
logger.info(`Disabling ${featureName}.`);
148-
core.exportVariable(envVar, "false");
146+
exportVariable(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+
exportVariable(EnvVar.DID_AUTOBUILD_GOLANG, "true");
169167
}
170168
logger.endGroup();
171169
}

src/codeql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as api from "./api-client";
1515
import { CliError, wrapCliConfigurationError } from "./cli-errors";
1616
import { appendExtraQueryExclusions, type Config } from "./config-utils";
1717
import { DocUrl } from "./doc-url";
18-
import { EnvVar } from "./environment";
18+
import { EnvVar, exportVariable } from "./environment";
1919
import {
2020
CodeQLDefaultVersionInfo,
2121
Feature,
@@ -1096,7 +1096,7 @@ async function getCodeQLForCmd(
10961096
}' by 'github/codeql-action/*@v${getActionVersion()}' in your code scanning workflow to ` +
10971097
"continue using this version of the CodeQL Action.",
10981098
);
1099-
core.exportVariable(EnvVar.SUPPRESS_DEPRECATED_SOON_WARNING, "true");
1099+
exportVariable(EnvVar.SUPPRESS_DEPRECATED_SOON_WARNING, "true");
11001100
}
11011101
return codeql;
11021102
}

src/config-utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from "fs";
22
import * as path from "path";
33
import { performance } from "perf_hooks";
44

5-
import * as core from "@actions/core";
65
import * as yaml from "js-yaml";
76

87
import {
@@ -32,7 +31,7 @@ import {
3231
makeTelemetryDiagnostic,
3332
} from "./diagnostics";
3433
import { prepareDiffInformedAnalysis } from "./diff-informed-analysis-utils";
35-
import { EnvVar } from "./environment";
34+
import { EnvVar, exportVariable } from "./environment";
3635
import * as errorMessages from "./error-messages";
3736
import { Feature, FeatureEnablement } from "./feature-flags";
3837
import {
@@ -1045,10 +1044,10 @@ async function setCppTrapCachingEnvironmentVariables(
10451044
);
10461045
} else if (config.trapCaches[BuiltInLanguage.cpp]) {
10471046
logger.info("Enabling TRAP caching for C/C++.");
1048-
core.exportVariable(envVar, "true");
1047+
exportVariable(envVar, "true");
10491048
} else {
10501049
logger.debug(`Disabling TRAP caching for C/C++.`);
1051-
core.exportVariable(envVar, "false");
1050+
exportVariable(envVar, "false");
10521051
}
10531052
}
10541053
}

src/debug-artifacts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { dbIsFinalized } from "./analyze";
1111
import { scanArtifactsForTokens } from "./artifact-scanner";
1212
import { type CodeQL } from "./codeql";
1313
import { Config } from "./config-utils";
14-
import { EnvVar } from "./environment";
14+
import { EnvVar, exportVariable } from "./environment";
1515
import * as json from "./json";
1616
import { Language } from "./languages";
1717
import { Logger, withGroup } from "./logging";
@@ -330,7 +330,7 @@ export async function uploadArtifacts(
330330
// some issues early.
331331
if (isInTestMode()) {
332332
await scanArtifactsForTokens(toUpload, logger);
333-
core.exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true");
333+
exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true");
334334
}
335335

336336
const suffix = getArtifactSuffix(getOptionalInput("matrix"));

src/environment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as core from "@actions/core";
2+
13
/**
24
* Environment variables used by the CodeQL Action.
35
*
@@ -163,3 +165,20 @@ export enum EnvVar {
163165
export function isInTestMode(): boolean {
164166
return process.env[EnvVar.TEST_MODE] === "true";
165167
}
168+
169+
/**
170+
* Wrapper around `core.exportVariable` which does not call `core.exportVariable`
171+
* when running unit tests. This is important, because otherwise `core.exportVariable`
172+
* sets environment variables for other steps in a workflow when we run unit tests in CI.
173+
*/
174+
export function exportVariable(name: string, val: any): void {
175+
if (process.env["NODE_ENV"] === "test") {
176+
// Setting the environment variable for the current process is OK since we reset
177+
// those at the end of each test. This allows tests to pass that rely on that
178+
// part of the `core.exportVariable` behaviour.
179+
process.env[name] = val;
180+
} else {
181+
// Call `core.exportVariable` whenever we are not in a test environment.
182+
core.exportVariable(name, val);
183+
}
184+
}

src/init-action-post.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
DependencyCachingUsageReport,
2121
getDependencyCacheUsage,
2222
} from "./dependency-caching";
23-
import { EnvVar } from "./environment";
23+
import { EnvVar, exportVariable } from "./environment";
2424
import { initFeatures } from "./feature-flags";
2525
import * as gitUtils from "./git-utils";
2626
import * as initActionPostHelper from "./init-action-post-helper";
@@ -157,7 +157,7 @@ function getFinalJobStatus(config: Config | undefined): JobStatus {
157157
let jobStatus: JobStatus;
158158

159159
if (process.env[EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY] === "true") {
160-
core.exportVariable(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
160+
exportVariable(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
161161
jobStatus = JobStatus.SuccessStatus;
162162
} else if (config !== undefined) {
163163
// - We have computed a CodeQL config
@@ -182,7 +182,7 @@ function getFinalJobStatus(config: Config | undefined): JobStatus {
182182

183183
// This shouldn't be necessary, but in the odd case that we run more than one
184184
// `init` post step, ensure the job status is consistent between them.
185-
core.exportVariable(EnvVar.JOB_STATUS, jobStatus);
185+
exportVariable(EnvVar.JOB_STATUS, jobStatus);
186186
return jobStatus;
187187
}
188188

0 commit comments

Comments
 (0)