Skip to content

Commit

Permalink
Improve env option (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Aug 23, 2024
1 parent d95242e commit a9d7f6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {spawn} from 'node:child_process';
import {once} from 'node:events';
import process from 'node:process';
import {finished} from 'node:stream/promises';
import {lineIterator, combineAsyncIterators} from './utilities.js';

Expand Down Expand Up @@ -27,10 +28,12 @@ const getOptions = ({
stdout,
stderr,
stdio = [stdin, stdout, stderr],
env,
...options
}) => ({
...options,
stdio,
env: env === undefined ? env : {...process.env, ...env},
});

const getResult = async subprocess => {
Expand Down
13 changes: 11 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ test('options.stdio string has priority over options.stdout', async t => {
await promise;
});

test.serial('options.env augments process.env', async t => {
process.env.ONE = 'one';
process.env.TWO = 'two';
const {stdout} = await nanoSpawn('node', ['-p', 'process.env.ONE + process.env.TWO'], {env: {TWO: testString}});
t.is(stdout, `${process.env.ONE}${testString}`);
delete process.env.ONE;
delete process.env.TWO;
});

test('can pass options object without any arguments', async t => {
const {exitCode, signalName} = await t.throwsAsync(nanoSpawn('node', {timeout: 1}));
t.is(exitCode, undefined);
Expand Down Expand Up @@ -300,7 +309,7 @@ if (isWindows) {
test('Ignores PATHEXT without options.shell', async t => {
t.is(path.extname(process.execPath), '.exe');
const {stdout} = await nanoSpawn(process.execPath.slice(0, -4), ['-e', 'console.log(".")'], {
env: {...process.env, PATHEXT: '.COM'},
env: {PATHEXT: '.COM'},
shell: false,
});
t.is(stdout, '.');
Expand All @@ -309,7 +318,7 @@ if (isWindows) {
test('Uses PATHEXT with options.shell', async t => {
t.is(path.extname(process.execPath), '.exe');
const {exitCode, stderr} = await t.throwsAsync(nanoSpawn(process.execPath.slice(0, -4), ['-e', 'console.log(".")'], {
env: {...process.env, PATHEXT: '.COM'},
env: {PATHEXT: '.COM'},
shell: true,
}));
t.is(exitCode, 1);
Expand Down

0 comments on commit a9d7f6f

Please sign in to comment.