1+ import { mkdtempSync , realpathSync } from 'fs' ;
2+ import { tmpdir } from 'os' ;
3+ import { delimiter , join } from 'path' ;
4+ import * as semver from 'semver' ;
15import { rimraf } from '../../utils/fs' ;
26import { getActivePackageManager } from '../../utils/packages' ;
3- import { ng , npm } from '../../utils/process' ;
7+ import { exec , execAndWaitForOutputToMatch , ng , npm } from '../../utils/process' ;
48import { isPrereleaseCli } from '../../utils/project' ;
5- import { expectToFail } from '../../utils/utils' ;
69
7- const warningText = ' npm version 7.5 .6 or higher is recommended' ;
10+ const warningTextRe = / n p m v e r s i o n 7 \. 5 \ .6 o r h i g h e r i s r e c o m m e n d e d / ;
811
912export default async function ( ) {
1013 // Only relevant with npm as a package manager
@@ -24,61 +27,60 @@ export default async function () {
2427 extraArgs . push ( '--next' ) ;
2528 }
2629
30+ // Add the npmModuleDir to the PATH so the test-installed npm is used
31+ process . env . PATH = join ( currentDirectory , 'node_modules' , '.bin' ) + delimiter + process . env . PATH ;
32+
2733 try {
28- // Install version >=7.5.6
29- await npm ( 'install' , '--global' , 'npm@>=7.5.6' ) ;
34+ // Install and verify npm version >=7.5.6
35+ await npm ( 'install' , 'npm@>=7.5.6' ) ;
36+ const { stdout : npm75Version } = await exec ( 'npm' , '--version' ) ;
37+ if ( ! semver . gte ( npm75Version , '7.5.6' ) ) {
38+ throw new Error ( 'npm install >=7.5.6 failed' ) ;
39+ }
3040
3141 // Ensure `ng update` does not show npm warning
3242 const { stderr : stderrUpdate1 } = await ng ( 'update' , ...extraArgs ) ;
33- if ( stderrUpdate1 . includes ( warningText ) ) {
43+ if ( stderrUpdate1 . match ( warningTextRe ) ) {
3444 throw new Error ( 'ng update expected to not show npm version warning.' ) ;
3545 }
3646
37- // Install version <7.5.6
38- await npm ( 'install' , '--global' , 'npm@7.4.0' ) ;
47+ // Install and verify npm version <7.5.6
48+ await npm ( 'install' , 'npm@7.4.0' ) ;
49+ const { stdout : npm74Version } = await exec ( 'npm' , '--version' ) ;
50+ if ( ! semver . eq ( npm74Version , '7.4.0' ) ) {
51+ throw new Error ( 'npm install =7.4.0 failed' ) ;
52+ }
3953
4054 // Ensure `ng add` shows npm warning
41- const { stderr : stderrAdd } = await ng ( 'add' , '@angular/localize' ) ;
42- if ( ! stderrAdd . includes ( warningText ) ) {
43- throw new Error ( 'ng add expected to show npm version warning.' ) ;
44- }
55+ await execAndWaitForOutputToMatch ( 'ng' , [ 'add' , '@angular/localize' ] , warningTextRe ) ;
4556
4657 // Ensure `ng update` shows npm warning
47- const { stderr : stderrUpdate2 } = await ng ( 'update' , ...extraArgs ) ;
48- if ( ! stderrUpdate2 . includes ( warningText ) ) {
49- throw new Error ( 'ng update expected to show npm version warning.' ) ;
50- }
58+ await execAndWaitForOutputToMatch ( 'ng' , [ 'update' , ...extraArgs ] , warningTextRe ) ;
5159
5260 // Ensure `ng build` executes successfully
5361 const { stderr : stderrBuild } = await ng ( 'build' , '--configuration=development' ) ;
54- if ( stderrBuild . includes ( warningText ) ) {
62+ if ( stderrBuild . match ( warningTextRe ) ) {
5563 throw new Error ( 'ng build expected to not show npm version warning.' ) ;
5664 }
5765
5866 // Ensure `ng new` shows npm warning
5967 // Must be outside the project for `ng new`
6068 process . chdir ( '..' ) ;
61- const { message : stderrNew } = await expectToFail ( ( ) => ng ( 'new' ) ) ;
62- if ( ! stderrNew . includes ( warningText ) ) {
63- throw new Error ( 'ng new expected to show npm version warning.' ) ;
64- }
69+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' ] , warningTextRe ) ;
6570
6671 // Ensure `ng new --package-manager=npm` shows npm warning
67- const { message : stderrNewNpm } = await expectToFail ( ( ) => ng ( 'new' , '--package-manager=npm' ) ) ;
68- if ( ! stderrNewNpm . includes ( warningText ) ) {
69- throw new Error ( 'ng new expected to show npm version warning.' ) ;
70- }
72+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' , '--package-manager=npm' ] , warningTextRe ) ;
7173
7274 // Ensure `ng new --skip-install` executes successfully
7375 const { stderr : stderrNewSkipInstall } = await ng ( 'new' , 'npm-seven-skip' , '--skip-install' ) ;
74- if ( stderrNewSkipInstall . includes ( warningText ) ) {
76+ if ( stderrNewSkipInstall . match ( warningTextRe ) ) {
7577 throw new Error ( 'ng new --skip-install expected to not show npm version warning.' ) ;
7678 }
7779
7880 // Ensure `ng new --package-manager=yarn` executes successfully
7981 // Need an additional npmrc file since yarn does not use the NPM registry environment variable
8082 const { stderr : stderrNewYarn } = await ng ( 'new' , 'npm-seven-yarn' , '--package-manager=yarn' ) ;
81- if ( stderrNewYarn . includes ( warningText ) ) {
83+ if ( stderrNewYarn . match ( warningTextRe ) ) {
8284 throw new Error ( 'ng new --package-manager=yarn expected to not show npm version warning.' ) ;
8385 }
8486 } finally {
@@ -89,7 +91,7 @@ export default async function () {
8991 // Change directory back
9092 process . chdir ( currentDirectory ) ;
9193
92- // Reset version back to 6.x
93- await npm ( 'install ' , '--global' , ' npm@6 ') ;
94+ // Remove the locally installed npm
95+ await npm ( 'uninstall ' , 'npm' ) ;
9496 }
9597}
0 commit comments