@@ -3,9 +3,10 @@ import { SpawnOptions } from 'child_process';
3
3
import * as child_process from 'child_process' ;
4
4
import { concat , defer , EMPTY , from } from 'rxjs' ;
5
5
import { repeat , takeLast } from 'rxjs/operators' ;
6
- import { getGlobalVariable } from './env' ;
6
+ import { getGlobalVariable , getGlobalVariablesEnv } from './env' ;
7
7
import { catchError } from 'rxjs/operators' ;
8
8
import treeKill from 'tree-kill' ;
9
+ import { delimiter , join , resolve } from 'path' ;
9
10
10
11
interface ExecOptions {
11
12
silent ?: boolean ;
@@ -300,22 +301,21 @@ export function silentNpm(
300
301
{
301
302
silent : true ,
302
303
cwd : ( options as { cwd ?: string } | undefined ) ?. cwd ,
303
- env : extractNpmEnv ( ) ,
304
304
} ,
305
305
'npm' ,
306
306
params ,
307
307
) ;
308
308
} else {
309
- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'npm' , args as string [ ] ) ;
309
+ return _exec ( { silent : true } , 'npm' , args as string [ ] ) ;
310
310
}
311
311
}
312
312
313
313
export function silentYarn ( ...args : string [ ] ) {
314
- return _exec ( { silent : true , env : extractNpmEnv ( ) } , 'yarn' , args ) ;
314
+ return _exec ( { silent : true } , 'yarn' , args ) ;
315
315
}
316
316
317
317
export function npm ( ...args : string [ ] ) {
318
- return _exec ( { env : extractNpmEnv ( ) } , 'npm' , args ) ;
318
+ return _exec ( { } , 'npm' , args ) ;
319
319
}
320
320
321
321
export function node ( ...args : string [ ] ) {
@@ -329,3 +329,39 @@ export function git(...args: string[]) {
329
329
export function silentGit ( ...args : string [ ] ) {
330
330
return _exec ( { silent : true } , 'git' , args ) ;
331
331
}
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