Skip to content

Commit b843cbe

Browse files
authored
Merge pull request #3233 from github/mbg/getOptionalEnvVar
Add `getOptionalEnvVar` helper
2 parents e576807 + 1ecd563 commit b843cbe

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

‎lib/analyze-action.js‎

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

‎lib/upload-lib.js‎

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

‎lib/upload-sarif-action.js‎

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

‎src/upload-lib.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,10 @@ export async function writePostProcessedFiles(
747747
postProcessingResults: PostProcessingResults,
748748
) {
749749
// If there's an explicit input, use that. Otherwise, use the value from the environment variable.
750-
const outputPath = pathInput || process.env[EnvVar.SARIF_DUMP_DIR];
750+
const outputPath = pathInput || util.getOptionalEnvVar(EnvVar.SARIF_DUMP_DIR);
751751

752752
// If we have a non-empty output path, write the SARIF file to it.
753-
if (outputPath !== undefined && outputPath.trim() !== "") {
753+
if (outputPath !== undefined) {
754754
dumpSarifFile(
755755
JSON.stringify(postProcessingResults.sarif),
756756
outputPath,

‎src/util.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ test("allowed API versions", async (t) => {
252252
);
253253
});
254254

255+
test("getRequiredEnvParam - gets environment variables", (t) => {
256+
process.env.SOME_UNIT_TEST_VAR = "foo";
257+
const result = util.getRequiredEnvParam("SOME_UNIT_TEST_VAR");
258+
t.is(result, "foo");
259+
});
260+
261+
test("getRequiredEnvParam - throws if an environment variable isn't set", (t) => {
262+
t.throws(() => util.getRequiredEnvParam("SOME_UNIT_TEST_VAR"));
263+
});
264+
265+
test("getOptionalEnvVar - gets environment variables", (t) => {
266+
process.env.SOME_UNIT_TEST_VAR = "foo";
267+
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
268+
t.is(result, "foo");
269+
});
270+
271+
test("getOptionalEnvVar - gets undefined for empty environment variables", (t) => {
272+
process.env.SOME_UNIT_TEST_VAR = "";
273+
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
274+
t.is(result, undefined);
275+
});
276+
277+
test("getOptionalEnvVar - doesn't throw for undefined environment variables", (t) => {
278+
t.notThrows(() => {
279+
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
280+
t.is(result, undefined);
281+
});
282+
});
283+
255284
test("doesDirectoryExist", async (t) => {
256285
// Returns false if no file/dir of this name exists
257286
t.false(util.doesDirectoryExist("non-existent-file.txt"));

‎src/util.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,17 @@ export function getRequiredEnvParam(paramName: string): string {
673673
return value;
674674
}
675675

676+
/**
677+
* Get an environment variable, but return `undefined` if it is not set or empty.
678+
*/
679+
export function getOptionalEnvVar(paramName: string): string | undefined {
680+
const value = process.env[paramName];
681+
if (value?.trim().length === 0) {
682+
return undefined;
683+
}
684+
return value;
685+
}
686+
676687
export class HTTPError extends Error {
677688
public status: number;
678689

0 commit comments

Comments
 (0)