Skip to content

Commit f75f1ed

Browse files
authored
fix: changing global proxy setting (#223)
1 parent dfd75b2 commit f75f1ed

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lib/dependencies/sub-process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function makeSpawnOptions(options?: ProcessOptions) {
1515
spawnOptions.cwd = options.cwd;
1616
}
1717
if (options && options.env) {
18-
spawnOptions.env = options.env;
18+
spawnOptions.env = { ...options.env };
1919
}
2020

2121
// Before spawning an external process, we look if we need to restore the system proxy configuration,

test/unit/sub-process.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { executeSync } from '../../lib/dependencies/sub-process';
2+
3+
describe('Test sub-process.ts', () => {
4+
it('test restoring proxy setting in executeSync()', async () => {
5+
const expectedProxy = 'proxy';
6+
const expectedProxyHTTPS = 'proxy2';
7+
const expectedNoProxy = 'no-proxy';
8+
9+
process.env.SNYK_SYSTEM_HTTP_PROXY = 'http://127.0.0.1:3128';
10+
process.env.SNYK_SYSTEM_HTTPS_PROXY = 'http://127.0.0.1:3129';
11+
process.env.SNYK_SYSTEM_NO_PROXY = 'something.com';
12+
13+
const options = {
14+
env: {
15+
HTTP_PROXY: expectedProxy,
16+
HTTPS_PROXY: expectedProxyHTTPS,
17+
NO_PROXY: expectedNoProxy,
18+
},
19+
};
20+
21+
let output = executeSync(
22+
'python3',
23+
['-c', "import os; print(os.environ['HTTP_PROXY'])"],
24+
options
25+
);
26+
expect(output.stdout.toString().trim()).toEqual(
27+
process.env.SNYK_SYSTEM_HTTP_PROXY
28+
);
29+
30+
output = executeSync(
31+
'python3',
32+
['-c', "import os; print(os.environ['HTTPS_PROXY'])"],
33+
options
34+
);
35+
expect(output.stdout.toString().trim()).toEqual(
36+
process.env.SNYK_SYSTEM_HTTPS_PROXY
37+
);
38+
39+
output = executeSync(
40+
'python3',
41+
['-c', "import os; print(os.environ['NO_PROXY'])"],
42+
options
43+
);
44+
expect(output.stdout.toString().trim()).toEqual(
45+
process.env.SNYK_SYSTEM_NO_PROXY
46+
);
47+
48+
// ensure that options remain unchanged
49+
expect(options.env.HTTP_PROXY).toEqual(expectedProxy);
50+
expect(options.env.HTTPS_PROXY).toEqual(expectedProxyHTTPS);
51+
expect(options.env.NO_PROXY).toEqual(expectedNoProxy);
52+
53+
delete process.env.SNYK_SYSTEM_HTTP_PROXY;
54+
delete process.env.SNYK_SYSTEM_HTTPS_PROXY;
55+
delete process.env.SNYK_SYSTEM_NO_PROXY;
56+
});
57+
58+
it('test executeSync()', async () => {
59+
const expectedProxy = 'proxy';
60+
const expectedProxyHTTPS = 'proxy2';
61+
const expectedNoProxy = 'no-proxy';
62+
63+
const options = {
64+
env: {
65+
HTTP_PROXY: expectedProxy,
66+
HTTPS_PROXY: expectedProxyHTTPS,
67+
NO_PROXY: expectedNoProxy,
68+
},
69+
};
70+
71+
let output = executeSync(
72+
'python3',
73+
['-c', "import os; print(os.environ['HTTP_PROXY'])"],
74+
options
75+
);
76+
expect(output.stdout.toString().trim()).toEqual(expectedProxy);
77+
78+
output = executeSync(
79+
'python3',
80+
['-c', "import os; print(os.environ['HTTPS_PROXY'])"],
81+
options
82+
);
83+
expect(output.stdout.toString().trim()).toEqual(expectedProxyHTTPS);
84+
85+
output = executeSync(
86+
'python3',
87+
['-c', "import os; print(os.environ['NO_PROXY'])"],
88+
options
89+
);
90+
expect(output.stdout.toString().trim()).toEqual(expectedNoProxy);
91+
});
92+
});

0 commit comments

Comments
 (0)