Repository navigation
Do not use core.exportVariable in unit tests (2nd attempt)
#4212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
63c5299
1728f0c
59ff83e
75ea4cb
ebd711f
d6196d8
bd97dda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import * as core from "@actions/core"; | ||
|
|
||
| /** | ||
| * Environment variables used by Default Setup to communicate the private registry proxy configuration. | ||
| */ | ||
|
|
@@ -211,8 +213,20 @@ export enum ActionsEnvVars { | |
| RUNNER_TOOL_CACHE = "RUNNER_TOOL_CACHE", | ||
| } | ||
|
|
||
| /** Environment variables which are not specific to CodeQL. */ | ||
| export enum SystemEnvVar { | ||
| /** | ||
| * Used by Node and related tools to indicate what kind of environment we are running in. | ||
| */ | ||
| NODE_ENV = "NODE_ENV", | ||
| } | ||
|
Comment on lines
+223
to
+227
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can now be cleaned up |
||
|
|
||
| /** A type representing all known environment variables. */ | ||
| export type KnownEnvVar = EnvVar | ActionsEnvVars | RegistryProxyVars; | ||
| export type KnownEnvVar = | ||
| | EnvVar | ||
| | ActionsEnvVars | ||
| | RegistryProxyVars | ||
| | SystemEnvVar; | ||
|
|
||
| /** | ||
| * Gets an environment variable, but throws an error if it is not set. | ||
|
|
@@ -289,6 +303,26 @@ export class ReadOnlyEnv<T extends string | undefined = string | undefined> { | |
| public entries(): Array<[string, T]> { | ||
| return Object.entries(this.vars); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a value indicating whether we should skip uploads of | ||
| * all kinds (SARIF results, status reports, DBs, ...). | ||
| * | ||
| * This is not guaranteed to be set in all test environments. | ||
| */ | ||
| public isSkippingUploadsInTests(): boolean { | ||
| return this.getOptional(EnvVar.TEST_MODE) === "true"; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a value indicative of whether we are in a testing environment | ||
| * by testing whether the value of the `NODE_ENV` variable is "test". | ||
| * This is expected to be the case if e.g. `ava` is running the tests | ||
| * or if this instance was constructed by `getTestEnv`. | ||
|
Comment on lines
+323
to
+326
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this JSDoc
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was worried I might have missed one |
||
| */ | ||
| public isTestingEnv(): boolean { | ||
| return this.getOptional(SystemEnvVar.NODE_ENV) === "test"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -307,6 +341,26 @@ export class Env< | |
| this.changed = true; | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper around `core.exportVariable` which does not call `core.exportVariable` | ||
| * when running unit tests. This is important, because otherwise `core.exportVariable` | ||
| * sets environment variables for other steps in a workflow when we run unit tests in CI. | ||
| * | ||
| * @param name The name of the environment variable to set and export. | ||
| * @param val The value to set and export for the environment variable. | ||
| */ | ||
| public export(name: string, val: T): void { | ||
| // Setting the environment variable for this instance is always OK, including | ||
| // in tests, since we use fresh `Env` instances whenever needed. This allows | ||
| // tests to pass that rely on that part of the `core.exportVariable` behaviour. | ||
| this.set(name, val); | ||
|
|
||
| // Call `core.exportVariable` whenever we are not in a test environment. | ||
| if (!this.isTestingEnv()) { | ||
| core.exportVariable(name, val); | ||
|
henrymercer marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| /** Gets a value indicating whether `set` was called at least once. */ | ||
| public hasChanged(): boolean { | ||
| return this.changed; | ||
|
|
@@ -317,3 +371,34 @@ export class Env< | |
| export function getEnv(env: NodeJS.ProcessEnv = process.env): Env { | ||
| return new Env(env); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether we are in test mode. This is used by CodeQL Action PR checks. | ||
| * | ||
| * In test mode, we skip several uploads (SARIF results, status reports, DBs, ...). | ||
| * | ||
| * @deprecated | ||
| * The purpose of this function is ambiguous. Use `isSkippingUploadsInTests` on | ||
| * a `ReadOnlyEnv` instance instead for equivalent behaviour. Use `isTestingEnv` | ||
| * to determine if we are running in a unit test. | ||
| */ | ||
| export function isInTestMode(): boolean { | ||
| return getEnv().isSkippingUploadsInTests(); | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper around `core.exportVariable` which does not call `core.exportVariable` | ||
| * when running unit tests. This is important, because otherwise `core.exportVariable` | ||
| * sets environment variables for other steps in a workflow when we run unit tests in CI. | ||
| * | ||
| * @deprecated Use `export` on an `Env` instance instead. | ||
| */ | ||
| export function exportVariable(name: string, val: any): void { | ||
| const env = getEnv(); | ||
|
|
||
| if (typeof val === "string") { | ||
| env.export(name, val); | ||
| } else { | ||
| env.export(name, JSON.stringify(val)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.