@@ -3,9 +3,10 @@ import { SpawnOptions } from 'child_process';
33import * as child_process from 'child_process' ;
44import { concat , defer , EMPTY , from } from 'rxjs' ;
55import { repeat , takeLast } from 'rxjs/operators' ;
6- import { getGlobalVariable } from './env' ;
6+ import { getGlobalVariable , getGlobalVariablesEnv } from './env' ;
77import { catchError } from 'rxjs/operators' ;
88import treeKill from 'tree-kill' ;
9+ import { delimiter , join , resolve } from 'path' ;
910
1011interface ExecOptions {
1112 silent ?: boolean ;
@@ -299,22 +300,21 @@ export function silentNpm(
299300 {
300301 silent : true ,
301302 cwd : ( options as { cwd ?: string } | undefined ) ?. cwd ,
302- env : extractNpmEnv ( ) ,
303303 } ,
304304 'npm' ,
305305 params ,
306306 ) ;
307307 } else {
308- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'npm' , args as string [ ] ) ;
308+ return _exec ( { silent : true } , 'npm' , args as string [ ] ) ;
309309 }
310310}
311311
312312export function silentYarn ( ...args : string [ ] ) {
313- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'yarn' , args ) ;
313+ return _exec ( { silent : true } , 'yarn' , args ) ;
314314}
315315
316316export function npm ( ...args : string [ ] ) {
317- return _exec ( { env : extractNpmEnv ( ) } , 'npm' , args ) ;
317+ return _exec ( { } , 'npm' , args ) ;
318318}
319319
320320export function node ( ...args : string [ ] ) {
@@ -328,3 +328,39 @@ export function git(...args: string[]) {
328328export function silentGit ( ...args : string [ ] ) {
329329 return _exec ( { silent : true } , 'git' , args ) ;
330330}
331+
332+ /**
333+ * Launch the given entry in an child process isolated to the test environment.
334+ *
335+ * The test environment includes the local NPM registry, isolated NPM globals,
336+ * the PATH variable only referencing the local node_modules and local NPM
337+ * registry (not the test runner or standard global node_modules).
338+ */
339+ export async function launchTestProcess ( entry : string , ...args : any [ ] ) {
340+ const tempRoot : string = getGlobalVariable ( 'tmp-root' ) ;
341+
342+ // Extract explicit environment variables for the test process.
343+ const env : NodeJS . ProcessEnv = {
344+ ...extractNpmEnv ( ) ,
345+ ...getGlobalVariablesEnv ( ) ,
346+ } ;
347+
348+ // Modify the PATH environment variable...
349+ let paths = process . env . PATH . split ( delimiter ) ;
350+
351+ // Only include paths within the sandboxed test environment or external
352+ // non angular-cli paths such as /usr/bin for generic commands.
353+ paths = paths . filter ( ( p ) => p . startsWith ( tempRoot ) || ! p . includes ( 'angular-cli' ) ) ;
354+
355+ // Ensure the custom npm global bin is on the PATH
356+ // https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
357+ if ( process . platform . startsWith ( 'win' ) ) {
358+ paths . unshift ( env . NPM_CONFIG_PREFIX ) ;
359+ } else {
360+ paths . unshift ( join ( env . NPM_CONFIG_PREFIX , 'bin' ) ) ;
361+ }
362+
363+ env . PATH = paths . join ( delimiter ) ;
364+
365+ return _exec ( { env } , process . execPath , [ resolve ( __dirname , 'run_test_process' ) , entry , ...args ] ) ;
366+ }
0 commit comments