Skip to content
Permalink
Browse files
Extend environment by default - fixes #87 (#88)
  • Loading branch information
SamVerschueren authored and sindresorhus committed Jun 9, 2017
1 parent b82905c commit e370117ff3fe963c9ec1cc538414961b306c00bd
Showing 4 changed files with 35 additions and 1 deletion.
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict';
console.log(process.env.FOO);
console.log(process.env.BAR);
@@ -16,6 +16,10 @@ const TEN_MEGABYTES = 1000 * 1000 * 10;
function handleArgs(cmd, args, opts) {
let parsed;

if (opts && opts.env && opts.extendEnv !== false) {
opts.env = Object.assign({}, process.env, opts.env);
}

if (opts && opts.__winShell === true) {
delete opts.__winShell;
parsed = {
@@ -114,7 +114,14 @@ Current working directory of the child process.
Type: `Object`<br>
Default: `process.env`

Environment key-value pairs.
Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.

#### extendEnv

Type: `boolean`<br>
Default: `true`

Set to `false` if you don't want to extend the environment variables when providing the `env` property.

#### argv0

19 test.js
@@ -10,6 +10,7 @@ import tempfile from 'tempfile';
import m from './';

process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
process.env.FOO = 'foo';

test('execa()', async t => {
const {stdout} = await m('noop', ['foo']);
@@ -418,3 +419,21 @@ if (process.platform !== 'win32') {
}
});
}

test('extend environment variables by default', async t => {
const result = await m.stdout('environment', [], {env: {BAR: 'bar'}});

t.deepEqual(result.split('\n'), [
'foo',
'bar'
]);
});

test('do not extend environment with `envExtend` option', async t => {
const result = await m.stdout('environment', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});

t.deepEqual(result.split('\n'), [
'undefined',
'bar'
]);
});

0 comments on commit e370117

Please sign in to comment.