diff --git a/ava.setup.mjs b/ava.setup.mjs index e6c370e01..b40599112 100644 --- a/ava.setup.mjs +++ b/ava.setup.mjs @@ -1,3 +1,4 @@ import pkg from "./package.json" with { type: "json" }; globalThis.__CODEQL_ACTION_VERSION__ = pkg.version; +globalThis.__CODEQL_ACTION_TEST_ENV__ = "unit-test"; diff --git a/build.mjs b/build.mjs index a0c3c0618..ecbe5be84 100644 --- a/build.mjs +++ b/build.mjs @@ -78,7 +78,7 @@ const UPLOAD_LIB_SRC = "./src/upload-lib"; * * The virtual module additionally re-exports `upload-lib` under the `uploadLib` namespace so that * external consumers can access it via the small `lib/upload-lib.js` stub emitted below. - * + * * A tiny stub file is emitted for each Action entrypoint, and one for `upload-lib`. Each stub * imports the shared bundle and calls/re-exports from the respective entry point. * @@ -212,6 +212,7 @@ const context = await esbuild.context({ target: ["node20"], define: { __CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version), + __CODEQL_ACTION_TEST_ENV__: JSON.stringify(""), }, metafile: true, }); diff --git a/lib/entry-points.js b/lib/entry-points.js index 6bf927591..d537ba343 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -166161,7 +166161,7 @@ var ReadOnlyEnv = class { * or if this instance was constructed by `getTestEnv`. */ isTestingEnv() { - return this.getOptional("NODE_ENV" /* NODE_ENV */) === "test"; + return false; } }; var Env = class extends ReadOnlyEnv { diff --git a/src/environment.test.ts b/src/environment.test.ts new file mode 100644 index 000000000..2c8a8a8f1 --- /dev/null +++ b/src/environment.test.ts @@ -0,0 +1,12 @@ +import test from "ava"; + +import { Env, ReadOnlyEnv } from "./environment"; +import { getTestEnv, setupTests } from "./testing-utils"; + +setupTests(test); + +test("isTestingEnv() is true", (t) => { + t.true(new ReadOnlyEnv(process.env).isTestingEnv()); + t.true(new Env(process.env).isTestingEnv()); + t.true(getTestEnv().isTestingEnv()); +}); diff --git a/src/environment.ts b/src/environment.ts index c5ef6c579..c3aad9086 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -1,5 +1,10 @@ import * as core from "@actions/core"; +/** + * This constant is set in `ava.setup.mjs` for tests. + */ +declare const __CODEQL_ACTION_TEST_ENV__: string | undefined; + /** * Environment variables used by Default Setup to communicate the private registry proxy configuration. */ @@ -321,7 +326,7 @@ export class ReadOnlyEnv { * or if this instance was constructed by `getTestEnv`. */ public isTestingEnv(): boolean { - return this.getOptional(SystemEnvVar.NODE_ENV) === "test"; + return __CODEQL_ACTION_TEST_ENV__ === "unit-test"; } } diff --git a/src/testing-utils.ts b/src/testing-utils.ts index 2011b56e0..cd6031894 100644 --- a/src/testing-utils.ts +++ b/src/testing-utils.ts @@ -184,12 +184,6 @@ export function makeMacro( } export function getTestEnv(testEnv: NodeJS.ProcessEnv = {}): Env { - // In unit tests run by `ava`, this environment variable is set for the process, - // but we default to an empty environment here for `testEnv`, so we must set it - // explicitly to ensure that we get test-specific behaviour in functions when - // we need it. - testEnv["NODE_ENV"] = "test"; - return getEnv(testEnv); }