Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected prepareSpawnOptions(options: IShellSpawnOptions) {
// Create a `process.env`-type object from all key/values of `process.env`,
// then `options.env`, then add several key/values. PATH is supplemented
// with the `node_modules\.bin` folder in the project directory so that we
// can run binaries inside a project.
options.env = createProcessEnv(process.env, options.env, {
PATH: this.alterPath(process.env.PATH || ''),
FORCE_COLOR: chalk.level > 0 ? '1' : '0',
});
}
}
it('should provide only PATH with empty env', async () => {
const PATH = process.env.PATH;
const cmd = new Subprocess('cmd', [], { env: {} });
expect(cmd.options).toEqual({ env: createProcessEnv({ PATH }) });
});
it('should call spawn with correct program/args with pathed name', async () => {
const result = {};
mockCrossSpawn.mockImplementation(() => result);
const name = path.resolve('/path', 'to', 'cmd');
const args = ['foo', 'bar', 'baz'];
const options = { env: { PATH: '' } };
const cmd = new Subprocess(name, args, options);
const expectedOptions = { env: createProcessEnv(options.env) };
expect(cmd.spawn()).toBe(result);
expect(mockCrossSpawn).toHaveBeenCalledTimes(1);
expect(mockCrossSpawn).toHaveBeenCalledWith(name, args, expectedOptions);
});
it('should provide default env option', async () => {
const cmd = new Subprocess('cmd', []);
expect(cmd.options).toEqual({ env: createProcessEnv(process.env) });
});
get options(): Readonly {
const opts = this._options;
if (!opts.env) {
opts.env = process.env;
}
const env = createProcessEnv(opts.env || {}, {
PATH: convertPATH(typeof opts.env.PATH === 'string' ? opts.env.PATH : process.env.PATH),
});
return { ...opts, env };
}