From c2b7b7f97734fa04a28b0b0581a25f7effce9255 Mon Sep 17 00:00:00 2001 From: Esben Sparre Andreasen Date: Thu, 6 Aug 2020 09:30:16 +0200 Subject: [PATCH] add getExtraOptions utility --- src/util.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/util.ts | 20 ++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/util.test.ts b/src/util.test.ts index 844c749a4..e31c96557 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -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(), 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; +}); diff --git a/src/util.ts b/src/util.ts index fa92c7f4f..741dc8d63 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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'