Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extend environment by default - fixes #87 (#88)
  • Loading branch information
SamVerschueren authored and sindresorhus committed Jun 9, 2017
1 parent b82905c commit e370117
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fixtures/environment
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict';
console.log(process.env.FOO);
console.log(process.env.BAR);
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -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 = {
Expand Down
9 changes: 8 additions & 1 deletion readme.md
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions test.js
Expand Up @@ -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']);
Expand Down Expand Up @@ -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.