Skip to content

Commit b70b9db

Browse files
authored
chore: fix environment variable parsing (#1505)
1 parent c6601d9 commit b70b9db

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"node-watch": "^0.7.3",
6262
"p-debounce": "^2.0.0",
6363
"package-json": "^7.0.0",
64+
"parse-env-string": "^1.0.1",
6465
"react": "^16.14.0",
6566
"react-dom": "^16.14.0",
6667
"react-mosaic-component": "^4.1.1",

src/renderer/runner.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
RunnableVersion,
1111
} from '../interfaces';
1212

13+
const parseEnvString = require('parse-env-string');
14+
1315
export enum ForgeCommands {
1416
PACKAGE = 'package',
1517
MAKE = 'make',
@@ -330,14 +332,26 @@ export class Runner {
330332
}
331333
}
332334

333-
private buildChildEnvVars(): { [x: string]: string | undefined } {
335+
public buildChildEnvVars(): { [x: string]: string | undefined } {
334336
const { environmentVariables } = this.appState;
335337

336338
const env: Record<string, string> = {};
337339

338-
for (const v of environmentVariables) {
339-
const [key, value] = v.split('=');
340-
env[key] = value;
340+
for (const envVar of environmentVariables) {
341+
const errMsg = `Could not parse environment variable: ${envVar}`;
342+
343+
try {
344+
const parsed: Record<string, string> | null = parseEnvString(envVar);
345+
if (!parsed || !Object.keys(parsed).length) {
346+
this.appState.showErrorDialog(errMsg);
347+
continue;
348+
}
349+
350+
const [key, value] = Object.entries(parsed)[0];
351+
env[key] = value;
352+
} catch (e) {
353+
this.appState.showErrorDialog(errMsg);
354+
}
341355
}
342356

343357
return env;

tests/renderer/runner-spec.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,48 @@ describe('Runner component', () => {
4141
instance = new Runner(store as unknown as AppState);
4242
});
4343

44+
describe('buildChildEnvVars', () => {
45+
it('fails when the environment variable is invalid', () => {
46+
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
47+
store.environmentVariables = ['bwap bwap'];
48+
const env = instance.buildChildEnvVars();
49+
expect(env).toEqual({});
50+
51+
expect(store.showErrorDialog).toHaveBeenCalledWith(
52+
expect.stringMatching(
53+
`Could not parse environment variable: bwap bwap`,
54+
),
55+
);
56+
});
57+
58+
it('returns an empty object when there are no environment variables', () => {
59+
store.environmentVariables = [];
60+
const env = instance.buildChildEnvVars();
61+
expect(env).toEqual({});
62+
});
63+
64+
it('returns an object with the environment variables', () => {
65+
store.environmentVariables = ['foo=bar', 'bar=baz'];
66+
const env = instance.buildChildEnvVars();
67+
expect(env).toEqual({ foo: 'bar', bar: 'baz' });
68+
});
69+
70+
it('returns an object with complex environment variables', () => {
71+
store.environmentVariables = [
72+
'NODE_OPTIONS="--no-warnings --max-old-space-size=2048"',
73+
'ELECTRON_TRASH=gvfs-trash',
74+
'ELECTRON_OVERRIDE_DIST_PATH=/Users/user=nam=!e/projects/electron/out/Testing',
75+
];
76+
const env = instance.buildChildEnvVars();
77+
expect(env).toEqual({
78+
NODE_OPTIONS: '--no-warnings --max-old-space-size=2048',
79+
ELECTRON_TRASH: 'gvfs-trash',
80+
ELECTRON_OVERRIDE_DIST_PATH:
81+
'/Users/user=nam=!e/projects/electron/out/Testing',
82+
});
83+
});
84+
});
85+
4486
describe('run()', () => {
4587
it('runs', async () => {
4688
// wait for run() to get running

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -9570,6 +9570,11 @@ parse-author@^2.0.0:
95709570
dependencies:
95719571
author-regex "^1.0.0"
95729572

9573+
parse-env-string@^1.0.1:
9574+
version "1.0.1"
9575+
resolved "https://registry.yarnpkg.com/parse-env-string/-/parse-env-string-1.0.1.tgz#9e1d06a5e561237d3ca7d06f6178f86b54d5c610"
9576+
integrity sha512-nozPS2x3SzOI6K2TUeeK/KwxMcg22SpAYVY75JeOXHu0M33L+Zo38NkNBlvUqajGwBGKYEbVTI4WemZJFo/OAA==
9577+
95739578
parse-json@^2.2.0:
95749579
version "2.2.0"
95759580
resolved "https://registry.npmjs.org/parse-json/-/parse-json-2.2.0.tgz"

0 commit comments

Comments
 (0)