Skip to content
Myr

env.js

This file

Path

test/arguments/env.js

64 lines · 3.2 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60

line 2why, one step back
2import path from 'node:path';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 3why, one step back
3import process from 'node:process';

TweaksSindre Sorhusbefore adoption698a1b8

line 4why, one step back
4import test from 'node:test';

Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60

lines 5–6why, one step back
5import {execa, execaSync} from '../../index.js';
6import {setFixtureDirectory, PATH_KEY, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–12why, one step back
7
8setFixtureDirectory();9process.env.FOO = 'foo';1011const isWindows = process.platform === 'win32';12

TweaksSindre Sorhusbefore adoption698a1b8

line 13why, one step back
13test('use environment variables by default', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 14why, one step back
14 const {stdout} = await execa('environment.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 15why, one step back
15 assert.deepEqual(stdout.split('\n'), ['foo', 'undefined']);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 16–17why, one step back
16});
17

TweaksSindre Sorhusbefore adoption698a1b8

line 18why, one step back
18test('extend environment variables by default', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 19why, one step back
19 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}});

TweaksSindre Sorhusbefore adoption698a1b8

line 20why, one step back
20 assert.deepEqual(stdout.split('\n'), ['foo', 'bar']);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 21–22why, one step back
21});
22

TweaksSindre Sorhusbefore adoption698a1b8

line 23why, one step back
23test('do not extend environment with `extendEnv: false`', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 24why, one step back
24 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}, extendEnv: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 25why, one step back
25 assert.deepEqual(stdout.split('\n'), ['undefined', 'bar']);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 26why, one step back
26});

Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60

lines 27–31why, one step back
27
28// `process.env` must not leak when the `env` option is not set, since `extendEnv: false` means only `env` is used.29// The file is spawned with an absolute path since the environment has no `PATH`.30const environmentFile = path.join(FIXTURES_DIRECTORY, 'environment.js');31

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32const testNoEnvironment = async (execaMethod, options) => {

Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60

line 33why, one step back
33 const {stdout} = await execaMethod(process.execPath, [environmentFile], {extendEnv: false, ...options});

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34 assert.deepEqual(stdout.split('\n'), ['undefined', 'undefined']);

Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60

lines 35–36why, one step back
35};
36

TweaksSindre Sorhusbefore adoption698a1b8

lines 37–40why, one step back
37test('do not use process.env with `extendEnv: false` and no `env`', () => testNoEnvironment(execa, {}));
38test('do not use process.env with `extendEnv: false` and no `env`, sync', () => testNoEnvironment(execaSync, {}));39test('do not use process.env with `extendEnv: false` and `env: {}`', () => testNoEnvironment(execa, {env: {}}));40test('do not use process.env with `extendEnv: false`, no `env` and `preferLocal`', () => testNoEnvironment(execa, {preferLocal: true}));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 41why, one step back
41

TweaksSindre Sorhusbefore adoption698a1b8

lines 42–47why, one step back
42// `preferLocal` adds directories to `process.env.PATH` when `env` has no `PATH`, so system commands still resolve
43test('use process.env.PATH with `extendEnv: false`, no `env` and `preferLocal`', async () => {44 const {stdout} = await execa('node', ['-p', '"ok"'], {extendEnv: false, preferLocal: true});45 assert.equal(stdout, 'ok');46});47

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 48why, one step back
48// The `env` option is kept on a null-prototype object, so the properties it inherits are not passed to the subprocess

TweaksSindre Sorhusbefore adoption698a1b8

line 49why, one step back
49const testInheritedEnv = async execaMethod => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 50–51why, one step back
50 const env = Object.assign(Object.create({FOO: 'foo'}), {BAR: 'bar'});
51 const {stdout} = await execaMethod(process.execPath, [environmentFile], {env, extendEnv: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 52why, one step back
52 assert.deepEqual(stdout.split('\n'), ['undefined', 'bar']);

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 53–54why, one step back
53};
54

TweaksSindre Sorhusbefore adoption698a1b8

lines 55–56why, one step back
55test('The "env" option does not pass inherited properties', () => testInheritedEnv(execa));
56test('The "env" option does not pass inherited properties, sync', () => testInheritedEnv(execaSync));

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 57why, one step back
57

TweaksSindre Sorhusbefore adoption698a1b8

line 58why, one step back
58test('use extend environment with `extendEnv: true` and `shell: true`', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 59–61why, one step back
59 process.env.TEST = 'test';
60 const command = isWindows ? 'echo %TEST%' : 'echo $TEST';61 const {stdout} = await execa(command, {shell: true, env: {}, extendEnv: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62 assert.equal(stdout, 'test');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 63–64why, one step back
63 delete process.env.TEST;
64});