11import * as assert from 'assert' ;
22import { execSync } from 'child_process' ;
33import { valid as validSemVer } from 'semver' ;
4+ import { delimiter , join } from 'path' ;
5+ import * as semver from 'semver' ;
46import { rimraf } from '../../utils/fs' ;
57import { getActivePackageManager } from '../../utils/packages' ;
6- import { ng , npm } from '../../utils/process' ;
8+ import { exec , execAndWaitForOutputToMatch , ng , npm } from '../../utils/process' ;
79import { isPrereleaseCli } from '../../utils/project' ;
8- import { expectToFail } from '../../utils/utils' ;
910
10- const warningText = ' npm version 7.5 .6 or higher is recommended' ;
11+ 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 / ;
1112
1213export default async function ( ) {
1314 // Only relevant with npm as a package manager
@@ -44,61 +45,60 @@ export default async function () {
4445 extraArgs . push ( '--next' ) ;
4546 }
4647
48+ // Add the npmModuleDir to the PATH so the test-installed npm is used
49+ process . env . PATH = join ( currentDirectory , 'node_modules' , '.bin' ) + delimiter + process . env . PATH ;
50+
4751 try {
48- // Install version >=7.5.6
49- await npm ( 'install' , '--global' , 'npm@>=7.5.6' ) ;
52+ // Install and verify npm version >=7.5.6
53+ await npm ( 'install' , 'npm@>=7.5.6' ) ;
54+ const { stdout : npm75Version } = await exec ( 'npm' , '--version' ) ;
55+ if ( ! semver . gte ( npm75Version , '7.5.6' ) ) {
56+ throw new Error ( 'npm install >=7.5.6 failed' ) ;
57+ }
5058
5159 // Ensure `ng update` does not show npm warning
5260 const { stderr : stderrUpdate1 } = await ng ( 'update' , ...extraArgs ) ;
53- if ( stderrUpdate1 . includes ( warningText ) ) {
61+ if ( stderrUpdate1 . match ( warningTextRe ) ) {
5462 throw new Error ( 'ng update expected to not show npm version warning.' ) ;
5563 }
5664
57- // Install version <7.5.6
58- await npm ( 'install' , '--global' , 'npm@7.4.0' ) ;
65+ // Install and verify npm version <7.5.6
66+ await npm ( 'install' , 'npm@7.4.0' ) ;
67+ const { stdout : npm74Version } = await exec ( 'npm' , '--version' ) ;
68+ if ( ! semver . eq ( npm74Version , '7.4.0' ) ) {
69+ throw new Error ( 'npm install =7.4.0 failed' ) ;
70+ }
5971
6072 // Ensure `ng add` shows npm warning
61- const { stderr : stderrAdd } = await ng ( 'add' , '@angular/localize' ) ;
62- if ( ! stderrAdd . includes ( warningText ) ) {
63- throw new Error ( 'ng add expected to show npm version warning.' ) ;
64- }
73+ await execAndWaitForOutputToMatch ( 'ng' , [ 'add' , '@angular/localize' ] , warningTextRe ) ;
6574
6675 // Ensure `ng update` shows npm warning
67- const { stderr : stderrUpdate2 } = await ng ( 'update' , ...extraArgs ) ;
68- if ( ! stderrUpdate2 . includes ( warningText ) ) {
69- throw new Error ( 'ng update expected to show npm version warning.' ) ;
70- }
76+ await execAndWaitForOutputToMatch ( 'ng' , [ 'update' , ...extraArgs ] , warningTextRe ) ;
7177
7278 // Ensure `ng build` executes successfully
7379 const { stderr : stderrBuild } = await ng ( 'build' , '--configuration=development' ) ;
74- if ( stderrBuild . includes ( warningText ) ) {
80+ if ( stderrBuild . match ( warningTextRe ) ) {
7581 throw new Error ( 'ng build expected to not show npm version warning.' ) ;
7682 }
7783
7884 // Ensure `ng new` shows npm warning
7985 // Must be outside the project for `ng new`
8086 process . chdir ( '..' ) ;
81- const { message : stderrNew } = await expectToFail ( ( ) => ng ( 'new' ) ) ;
82- if ( ! stderrNew . includes ( warningText ) ) {
83- throw new Error ( 'ng new expected to show npm version warning.' ) ;
84- }
87+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' ] , warningTextRe ) ;
8588
8689 // Ensure `ng new --package-manager=npm` shows npm warning
87- const { message : stderrNewNpm } = await expectToFail ( ( ) => ng ( 'new' , '--package-manager=npm' ) ) ;
88- if ( ! stderrNewNpm . includes ( warningText ) ) {
89- throw new Error ( 'ng new expected to show npm version warning.' ) ;
90- }
90+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' , '--package-manager=npm' ] , warningTextRe ) ;
9191
9292 // Ensure `ng new --skip-install` executes successfully
9393 const { stderr : stderrNewSkipInstall } = await ng ( 'new' , 'npm-seven-skip' , '--skip-install' ) ;
94- if ( stderrNewSkipInstall . includes ( warningText ) ) {
94+ if ( stderrNewSkipInstall . match ( warningTextRe ) ) {
9595 throw new Error ( 'ng new --skip-install expected to not show npm version warning.' ) ;
9696 }
9797
9898 // Ensure `ng new --package-manager=yarn` executes successfully
9999 // Need an additional npmrc file since yarn does not use the NPM registry environment variable
100100 const { stderr : stderrNewYarn } = await ng ( 'new' , 'npm-seven-yarn' , '--package-manager=yarn' ) ;
101- if ( stderrNewYarn . includes ( warningText ) ) {
101+ if ( stderrNewYarn . match ( warningTextRe ) ) {
102102 throw new Error ( 'ng new --package-manager=yarn expected to not show npm version warning.' ) ;
103103 }
104104 } finally {
@@ -110,6 +110,6 @@ export default async function () {
110110 process . chdir ( currentDirectory ) ;
111111
112112 // Reset version back to initial version
113- await npm ( 'install' , '--global' , `npm@${ initialVersion } ` ) ;
113+ await npm ( 'install' , `npm@${ initialVersion } ` ) ;
114114 }
115115}
0 commit comments