Skip to content

Commit 73b0850

Browse files
committed
Allow abstracting over process.env
1 parent 5e3e6b8 commit 73b0850

4 files changed

Lines changed: 60 additions & 11 deletions

File tree

lib/entry-points.js

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

src/environment.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ export enum EnvVar {
160160
/** Used by Code Scanning Risk Assessment to communicate the assessment ID to the CodeQL Action. */
161161
RISK_ASSESSMENT_ID = "CODEQL_ACTION_RISK_ASSESSMENT_ID",
162162
}
163+
164+
/** A wrapper around an environment, to allow abstracting away from `process.env` in tests. */
165+
export interface Env {
166+
/** Tries to get the value for `name` and throws if there isn't one. */
167+
getRequired(name: string): string;
168+
/** Gets the value for `name`, or `undefined` if it isn't set or empty. */
169+
getOptional(name: string): string | undefined;
170+
}

src/testing-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { CachingKind } from "./caching-utils";
1818
import * as codeql from "./codeql";
1919
import { Config } from "./config-utils";
2020
import * as defaults from "./defaults.json";
21+
import { Env } from "./environment";
2122
import {
2223
CodeQLDefaultVersionInfo,
2324
Feature,
@@ -29,6 +30,7 @@ import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
2930
import {
3031
DEFAULT_DEBUG_ARTIFACT_NAME,
3132
DEFAULT_DEBUG_DATABASE_NAME,
33+
getEnv,
3234
GitHubVariant,
3335
GitHubVersion,
3436
HTTPError,
@@ -172,6 +174,11 @@ export function makeMacro<Args extends unknown[]>(
172174
return wrapper;
173175
}
174176

177+
export function getTestEnv(): Env {
178+
const testEnv: NodeJS.ProcessEnv = {};
179+
return getEnv(testEnv);
180+
}
181+
175182
/**
176183
* Gets an `ActionsEnv` instance for use in tests.
177184
*/

src/util.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as apiCompatibility from "./api-compatibility.json";
1313
import type { CodeQL, VersionInfo } from "./codeql";
1414
import type { Pack } from "./config/db-config";
1515
import type { Config } from "./config-utils";
16-
import { EnvVar } from "./environment";
16+
import { Env, EnvVar } from "./environment";
1717
import * as json from "./json";
1818
import { Language } from "./languages";
1919
import { Logger } from "./logging";
@@ -566,28 +566,56 @@ export function initializeEnvironment(version: string) {
566566
core.exportVariable(EnvVar.VERSION, version);
567567
}
568568

569+
/** Gets an `Env` instance for `env`, which is `process.env` by default. */
570+
export function getEnv(env: NodeJS.ProcessEnv = process.env): Env {
571+
return {
572+
getRequired: (name) => getRequiredEnvVar(env, name),
573+
getOptional: (name) => getOptionalEnvVarFrom(env, name),
574+
};
575+
}
576+
569577
/**
570-
* Get an environment parameter, but throw an error if it is not set.
578+
* Gets an environment variable, but throws an error if it is not set.
571579
*/
572-
export function getRequiredEnvParam(paramName: string): string {
573-
const value = process.env[paramName];
580+
export function getRequiredEnvVar(
581+
env: NodeJS.ProcessEnv,
582+
paramName: string,
583+
): string {
584+
const value = env[paramName];
574585
if (value === undefined || value.length === 0) {
575586
throw new Error(`${paramName} environment variable must be set`);
576587
}
577588
return value;
578589
}
579590

580591
/**
581-
* Get an environment variable, but return `undefined` if it is not set or empty.
592+
* Get an environment parameter, but throw an error if it is not set.
582593
*/
583-
export function getOptionalEnvVar(paramName: string): string | undefined {
584-
const value = process.env[paramName];
594+
export function getRequiredEnvParam(paramName: string): string {
595+
return getRequiredEnvVar(process.env, paramName);
596+
}
597+
598+
/**
599+
* Gets an environment variable, but returns `undefined` if it is not set or empty.
600+
*/
601+
export function getOptionalEnvVarFrom(
602+
env: NodeJS.ProcessEnv,
603+
paramName: string,
604+
): string | undefined {
605+
const value = env[paramName];
585606
if (value?.trim().length === 0) {
586607
return undefined;
587608
}
588609
return value;
589610
}
590611

612+
/**
613+
* Get an environment variable, but return `undefined` if it is not set or empty.
614+
*/
615+
export function getOptionalEnvVar(paramName: string): string | undefined {
616+
return getOptionalEnvVarFrom(process.env, paramName);
617+
}
618+
591619
export class HTTPError extends Error {
592620
public status: number;
593621

0 commit comments

Comments
 (0)