Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/analyze-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions lib/upload-sarif-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,10 @@ export async function writePostProcessedFiles(
postProcessingResults: PostProcessingResults,
) {
// If there's an explicit input, use that. Otherwise, use the value from the environment variable.
const outputPath = pathInput || process.env[EnvVar.SARIF_DUMP_DIR];
const outputPath = pathInput || util.getOptionalEnvVar(EnvVar.SARIF_DUMP_DIR);

// If we have a non-empty output path, write the SARIF file to it.
if (outputPath !== undefined && outputPath.trim() !== "") {
if (outputPath !== undefined) {
dumpSarifFile(
JSON.stringify(postProcessingResults.sarif),
outputPath,
Expand Down
29 changes: 29 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ test("allowed API versions", async (t) => {
);
});

test("getRequiredEnvParam - gets environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "foo";
const result = util.getRequiredEnvParam("SOME_UNIT_TEST_VAR");
t.is(result, "foo");
});

test("getRequiredEnvParam - throws if an environment variable isn't set", (t) => {
t.throws(() => util.getRequiredEnvParam("SOME_UNIT_TEST_VAR"));
});

test("getOptionalEnvVar - gets environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "foo";
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, "foo");
});

test("getOptionalEnvVar - gets undefined for empty environment variables", (t) => {
process.env.SOME_UNIT_TEST_VAR = "";
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, undefined);
});

test("getOptionalEnvVar - doesn't throw for undefined environment variables", (t) => {
t.notThrows(() => {
const result = util.getOptionalEnvVar("SOME_UNIT_TEST_VAR");
t.is(result, undefined);
});
});

test("doesDirectoryExist", async (t) => {
// Returns false if no file/dir of this name exists
t.false(util.doesDirectoryExist("non-existent-file.txt"));
Expand Down
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,17 @@ export function getRequiredEnvParam(paramName: string): string {
return value;
}

/**
* Get an environment variable, but return `undefined` if it is not set or empty.
*/
export function getOptionalEnvVar(paramName: string): string | undefined {
const value = process.env[paramName];
if (value?.trim().length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: this trims but getRequiredEnvParam doesn't — should we be consistent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be inconsistent for now. Mainly because this would be a stricter requirement for getOptionalEnvVar than what we currently have and getOptionalEnvVar is only used in one (new) place for now where we don't risk breaking anything.

return undefined;
}
return value;
}

export class HTTPError extends Error {
public status: number;

Expand Down
Loading