Skip to content

Commit

Permalink
add getExtraOptions utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Esben Sparre Andreasen committed Aug 18, 2020
1 parent 008b006 commit c2b7b7f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,37 @@ test('prepareEnvironment() when a local run', t => {

process.env.CODEQL_LOCAL_RUN = origLocalRun;
});

test('getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)', t => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;

const options = {foo: 42};

process.env.CODEQL_ACTION_EXTRA_OPTIONS = JSON.stringify(options);

t.deepEqual(util.getExtraOptionsEnvParam(), <any>options);

process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});


test('getExtraOptionsEnvParam() succeeds on valid options', t => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;

const options = { database: { init: ["--debug"] } };
process.env.CODEQL_ACTION_EXTRA_OPTIONS =
JSON.stringify(options);

t.deepEqual(util.getExtraOptionsEnvParam(), options);

process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});

test('getExtraOptionsEnvParam() fails on invalid JSON', t => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;

process.env.CODEQL_ACTION_EXTRA_OPTIONS = "{{invalid-json}}";
t.throws(util.getExtraOptionsEnvParam);

process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});
20 changes: 20 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ export function getRequiredEnvParam(paramName: string): string {
return value;
}

/**
* Get the extra options for the codeql commands.
*/
export function getExtraOptionsEnvParam(): object {
const varName = 'CODEQL_ACTION_EXTRA_OPTIONS';
const raw = process.env[varName];
if (raw === undefined || raw.length === 0) {
return {};
}
try {
return JSON.parse(raw);
} catch (e) {
throw new Error(
varName +
' environment variable is set, but does not contain valid JSON: ' +
e.message
);
}
}

export function isLocalRun(): boolean {
return !!process.env.CODEQL_LOCAL_RUN
&& process.env.CODEQL_LOCAL_RUN !== 'false'
Expand Down

0 comments on commit c2b7b7f

Please sign in to comment.