@@ -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 ;
@@ -300,22 +301,21 @@ export function silentNpm(
300301 {
301302 silent : true ,
302303 cwd : ( options as { cwd ?: string } | undefined ) ?. cwd ,
303- env : extractNpmEnv ( ) ,
304304 } ,
305305 'npm' ,
306306 params ,
307307 ) ;
308308 } else {
309- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'npm' , args as string [ ] ) ;
309+ return _exec ( { silent : true } , 'npm' , args as string [ ] ) ;
310310 }
311311}
312312
313313export function silentYarn ( ...args : string [ ] ) {
314- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'yarn' , args ) ;
314+ return _exec ( { silent : true } , 'yarn' , args ) ;
315315}
316316
317317export function npm ( ...args : string [ ] ) {
318- return _exec ( { env : extractNpmEnv ( ) } , 'npm' , args ) ;
318+ return _exec ( { } , 'npm' , args ) ;
319319}
320320
321321export function node ( ...args : string [ ] ) {
@@ -329,3 +329,39 @@ export function git(...args: string[]) {
329329export function silentGit ( ...args : string [ ] ) {
330330 return _exec ( { silent : true } , 'git' , args ) ;
331331}
332+
333+ /**
334+ * Launch the given entry in an child process isolated to the test environment.
335+ *
336+ * The test environment includes the local NPM registry, isolated NPM globals,
337+ * the PATH variable only referencing the local node_modules and local NPM
338+ * registry (not the test runner or standard global node_modules).
339+ */
340+ export async function launchTestProcess ( entry : string , ...args : any [ ] ) {
341+ const tempRoot : string = getGlobalVariable ( 'tmp-root' ) ;
342+
343+ // Extract explicit environment variables for the test process.
344+ const env : NodeJS . ProcessEnv = {
345+ ...extractNpmEnv ( ) ,
346+ ...getGlobalVariablesEnv ( ) ,
347+ } ;
348+
349+ // Modify the PATH environment variable...
350+ let paths = process . env . PATH ! . split ( delimiter ) ;
351+
352+ // Only include paths within the sandboxed test environment or external
353+ // non angular-cli paths such as /usr/bin for generic commands.
354+ paths = paths . filter ( ( p ) => p . startsWith ( tempRoot ) || ! p . includes ( 'angular-cli' ) ) ;
355+
356+ // Ensure the custom npm global bin is on the PATH
357+ // https://docs.npmjs.com/cli/v8/configuring-npm/folders#executables
358+ if ( process . platform . startsWith ( 'win' ) ) {
359+ paths . unshift ( env . NPM_CONFIG_PREFIX ! ) ;
360+ } else {
361+ paths . unshift ( join ( env . NPM_CONFIG_PREFIX ! , 'bin' ) ) ;
362+ }
363+
364+ env . PATH = paths . join ( delimiter ) ;
365+
366+ return _exec ( { env } , process . execPath , [ resolve ( __dirname , 'run_test_process' ) , entry , ...args ] ) ;
367+ }
0 commit comments