Skip to content

Commit

Permalink
fix: changing global proxy setting
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Sep 22, 2023
1 parent 65eacd7 commit d00c855
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dependencies/sub-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function makeSpawnOptions(options?: ProcessOptions) {
spawnOptions.cwd = options.cwd;
}
if (options && options.env) {
spawnOptions.env = options.env;
spawnOptions.env = { ...options.env };
}

// Before spawning an external process, we look if we need to restore the system proxy configuration,
Expand Down
92 changes: 92 additions & 0 deletions test/unit/sub-process.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { executeSync } from '../../lib/dependencies/sub-process';

describe('Test sub-process.ts', () => {
it('test restoring proxy setting in executeSync()', async () => {
const expectedProxy = 'proxy';
const expectedProxyHTTPS = 'proxy2';
const expectedNoProxy = 'no-proxy';

process.env.SNYK_SYSTEM_HTTP_PROXY = 'http://127.0.0.1:3128';
process.env.SNYK_SYSTEM_HTTPS_PROXY = 'http://127.0.0.1:3129';
process.env.SNYK_SYSTEM_NO_PROXY = 'something.com';

const options = {
env: {
HTTP_PROXY: expectedProxy,
HTTPS_PROXY: expectedProxyHTTPS,
NO_PROXY: expectedNoProxy,
},
};

let output = executeSync(
'python3',
['-c', "import os; print(os.environ['HTTP_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(
process.env.SNYK_SYSTEM_HTTP_PROXY
);

output = executeSync(
'python3',
['-c', "import os; print(os.environ['HTTPS_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(
process.env.SNYK_SYSTEM_HTTPS_PROXY
);

output = executeSync(
'python3',
['-c', "import os; print(os.environ['NO_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(
process.env.SNYK_SYSTEM_NO_PROXY
);

// ensure that options remain unchanged
expect(options.env.HTTP_PROXY).toEqual(expectedProxy);
expect(options.env.HTTPS_PROXY).toEqual(expectedProxyHTTPS);
expect(options.env.NO_PROXY).toEqual(expectedNoProxy);

delete process.env.SNYK_SYSTEM_HTTP_PROXY;
delete process.env.SNYK_SYSTEM_HTTPS_PROXY;
delete process.env.SNYK_SYSTEM_NO_PROXY;
});

it('test executeSync()', async () => {
const expectedProxy = 'proxy';
const expectedProxyHTTPS = 'proxy2';
const expectedNoProxy = 'no-proxy';

const options = {
env: {
HTTP_PROXY: expectedProxy,
HTTPS_PROXY: expectedProxyHTTPS,
NO_PROXY: expectedNoProxy,
},
};

let output = executeSync(
'python3',
['-c', "import os; print(os.environ['HTTP_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(expectedProxy);

output = executeSync(
'python3',
['-c', "import os; print(os.environ['HTTPS_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(expectedProxyHTTPS);

output = executeSync(
'python3',
['-c', "import os; print(os.environ['NO_PROXY'])"],
options
);
expect(output.stdout.toString().trim()).toEqual(expectedNoProxy);
});
});

0 comments on commit d00c855

Please sign in to comment.