Skip to content

Commit c2b7b7f

Browse files
committed
add getExtraOptions utility
1 parent 008b006 commit c2b7b7f

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/util.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,37 @@ test('prepareEnvironment() when a local run', t => {
116116

117117
process.env.CODEQL_LOCAL_RUN = origLocalRun;
118118
});
119+
120+
test('getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)', t => {
121+
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
122+
123+
const options = {foo: 42};
124+
125+
process.env.CODEQL_ACTION_EXTRA_OPTIONS = JSON.stringify(options);
126+
127+
t.deepEqual(util.getExtraOptionsEnvParam(), <any>options);
128+
129+
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
130+
});
131+
132+
133+
test('getExtraOptionsEnvParam() succeeds on valid options', t => {
134+
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
135+
136+
const options = { database: { init: ["--debug"] } };
137+
process.env.CODEQL_ACTION_EXTRA_OPTIONS =
138+
JSON.stringify(options);
139+
140+
t.deepEqual(util.getExtraOptionsEnvParam(), options);
141+
142+
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
143+
});
144+
145+
test('getExtraOptionsEnvParam() fails on invalid JSON', t => {
146+
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
147+
148+
process.env.CODEQL_ACTION_EXTRA_OPTIONS = "{{invalid-json}}";
149+
t.throws(util.getExtraOptionsEnvParam);
150+
151+
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
152+
});

src/util.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ export function getRequiredEnvParam(paramName: string): string {
3939
return value;
4040
}
4141

42+
/**
43+
* Get the extra options for the codeql commands.
44+
*/
45+
export function getExtraOptionsEnvParam(): object {
46+
const varName = 'CODEQL_ACTION_EXTRA_OPTIONS';
47+
const raw = process.env[varName];
48+
if (raw === undefined || raw.length === 0) {
49+
return {};
50+
}
51+
try {
52+
return JSON.parse(raw);
53+
} catch (e) {
54+
throw new Error(
55+
varName +
56+
' environment variable is set, but does not contain valid JSON: ' +
57+
e.message
58+
);
59+
}
60+
}
61+
4262
export function isLocalRun(): boolean {
4363
return !!process.env.CODEQL_LOCAL_RUN
4464
&& process.env.CODEQL_LOCAL_RUN !== 'false'

0 commit comments

Comments
 (0)