Skip to content

Commit 691b303

Browse files
jbedardalan-agius4
authored andcommitted
test: ensure private npm is always on PATH
1 parent 14f8f5c commit 691b303

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

tests/legacy-cli/e2e/setup/002-npm-sandbox.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mkdir, writeFile } from 'fs/promises';
22
import { join } from 'path';
3-
import { getGlobalVariable } from '../utils/env';
3+
import { getGlobalVariable, setGlobalVariable } from '../utils/env';
44

55
/**
66
* Configure npm to use a unique sandboxed environment.
@@ -29,5 +29,7 @@ export default async function () {
2929
await writeFile(npmrc, `registry=${npmRegistry}\nprefix=${npmModulesPrefix}`);
3030
await mkdir(npmModulesPrefix);
3131

32+
setGlobalVariable('npm-global', npmModulesPrefix);
33+
3234
console.log(` Using "${npmModulesPrefix}" as e2e test global npm cache.`);
3335
}

tests/legacy-cli/e2e/utils/process.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
3232
let stdout = '';
3333
let stderr = '';
3434
const cwd = options.cwd ?? process.cwd();
35-
const env = options.env;
35+
const env = options.env ?? process.env;
3636
console.log(
3737
`==========================================================================================`,
3838
);
3939

40+
// Ensure the custom npm and yarn global bin is on the PATH
41+
// https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
42+
const paths = [
43+
join(getGlobalVariable('npm-global'), process.platform.startsWith('win') ? '' : 'bin'),
44+
env.PATH || process.env['PATH'],
45+
].join(delimiter);
46+
4047
args = args.filter((x) => x !== undefined);
4148
const flags = [
4249
options.silent && 'silent',
@@ -51,7 +58,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
5158

5259
const spawnOptions: SpawnOptions = {
5360
cwd,
54-
...(env ? { env } : {}),
61+
env: { ...env, PATH: paths },
5562
};
5663

5764
if (process.platform.startsWith('win')) {
@@ -146,15 +153,10 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
146153
export function extractNpmEnv() {
147154
return Object.keys(process.env)
148155
.filter((v) => NPM_CONFIG_RE.test(v))
149-
.reduce<NodeJS.ProcessEnv>(
150-
(vars, n) => {
151-
vars[n] = process.env[n];
152-
return vars;
153-
},
154-
{
155-
PATH: process.env.PATH,
156-
},
157-
);
156+
.reduce<NodeJS.ProcessEnv>((vars, n) => {
157+
vars[n] = process.env[n];
158+
return vars;
159+
}, {});
158160
}
159161

160162
export function waitForAnyProcessOutputToMatch(
@@ -364,20 +366,12 @@ export async function launchTestProcess(entry: string, ...args: any[]) {
364366
};
365367

366368
// Modify the PATH environment variable...
367-
let paths = process.env.PATH!.split(delimiter);
369+
let paths = (env.PATH || process.env.PATH)!.split(delimiter);
368370

369371
// Only include paths within the sandboxed test environment or external
370372
// non angular-cli paths such as /usr/bin for generic commands.
371373
paths = paths.filter((p) => p.startsWith(tempRoot) || !p.includes('angular-cli'));
372374

373-
// Ensure the custom npm global bin is on the PATH
374-
// https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
375-
if (process.platform.startsWith('win')) {
376-
paths.unshift(env.NPM_CONFIG_PREFIX!);
377-
} else {
378-
paths.unshift(join(env.NPM_CONFIG_PREFIX!, 'bin'));
379-
}
380-
381375
env.PATH = paths.join(delimiter);
382376

383377
return _exec({ env }, process.execPath, [resolve(__dirname, 'run_test_process'), entry, ...args]);

0 commit comments

Comments
 (0)