env.js
This file
Path
test/arguments/env.js
64 lines · 3.2 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60
line 2why, one step back2import path from 'node:path';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 3why, one step back3import process from 'node:process';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60
lines 5–6why, one step back5import {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 back78setFixtureDirectory();9process.env.FOO = 'foo';1011const isWindows = process.platform === 'win32';12TweaksSindre Sorhusbefore adoption698a1b8
line 13why, one step back13test('use environment variables by default', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 14why, one step back14 const {stdout} = await execa('environment.js');TweaksSindre Sorhusbefore adoption698a1b8
line 15why, one step back15 assert.deepEqual(stdout.split('\n'), ['foo', 'undefined']);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 16–17why, one step back16});17TweaksSindre Sorhusbefore adoption698a1b8
line 18why, one step back18test('extend environment variables by default', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 19why, one step back19 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}});TweaksSindre Sorhusbefore adoption698a1b8
line 20why, one step back20 assert.deepEqual(stdout.split('\n'), ['foo', 'bar']);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–22why, one step back21});22TweaksSindre Sorhusbefore adoption698a1b8
line 23why, one step back23test('do not extend environment with `extendEnv: false`', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 24why, one step back24 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', [PATH_KEY]: process.env[PATH_KEY]}, extendEnv: false});TweaksSindre Sorhusbefore adoption698a1b8
line 25why, one step back25 assert.deepEqual(stdout.split('\n'), ['undefined', 'bar']);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 26why, one step back26});Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60
lines 27–31why, one step back2728// `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');31TweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32const testNoEnvironment = async (execaMethod, options) => {Fix `extendEnv: false` inheriting `process.env` when `env` is not setSindre Sorhusbefore adoption67acf60
line 33why, one step back33 const {stdout} = await execaMethod(process.execPath, [environmentFile], {extendEnv: false, ...options});TweaksSindre Sorhusbefore adoption698a1b8
line 34why, one step back34 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 back35};36TweaksSindre Sorhusbefore adoption698a1b8
lines 37–40why, one step back37test('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 back41TweaksSindre Sorhusbefore adoption698a1b8
lines 42–47why, one step back42// `preferLocal` adds directories to `process.env.PATH` when `env` has no `PATH`, so system commands still resolve43test('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});47Harden some thingsSindre Sorhusbefore adoption4a0a121
line 48why, one step back48// The `env` option is kept on a null-prototype object, so the properties it inherits are not passed to the subprocessTweaksSindre Sorhusbefore adoption698a1b8
line 49why, one step back49const testInheritedEnv = async execaMethod => {Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 50–51why, one step back50 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 back52 assert.deepEqual(stdout.split('\n'), ['undefined', 'bar']);Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 53–54why, one step back53};54TweaksSindre Sorhusbefore adoption698a1b8
lines 55–56why, one step back55test('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 back57TweaksSindre Sorhusbefore adoption698a1b8
line 58why, one step back58test('use extend environment with `extendEnv: true` and `shell: true`', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 59–61why, one step back59 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 back62 assert.equal(stdout, 'test');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 63–64why, one step back63 delete process.env.TEST;64});