1
1
import * as assert from 'assert' ;
2
2
import { execSync } from 'child_process' ;
3
3
import { valid as validSemVer } from 'semver' ;
4
+ import { delimiter , join } from 'path' ;
5
+ import * as semver from 'semver' ;
4
6
import { rimraf } from '../../utils/fs' ;
5
7
import { getActivePackageManager } from '../../utils/packages' ;
6
- import { ng , npm } from '../../utils/process' ;
8
+ import { exec , execAndWaitForOutputToMatch , ng , npm } from '../../utils/process' ;
7
9
import { isPrereleaseCli } from '../../utils/project' ;
8
- import { expectToFail } from '../../utils/utils' ;
9
10
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 / ;
11
12
12
13
export default async function ( ) {
13
14
// Only relevant with npm as a package manager
@@ -44,61 +45,60 @@ export default async function () {
44
45
extraArgs . push ( '--next' ) ;
45
46
}
46
47
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
+
47
51
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
+ }
50
58
51
59
// Ensure `ng update` does not show npm warning
52
60
const { stderr : stderrUpdate1 } = await ng ( 'update' , ...extraArgs ) ;
53
- if ( stderrUpdate1 . includes ( warningText ) ) {
61
+ if ( stderrUpdate1 . match ( warningTextRe ) ) {
54
62
throw new Error ( 'ng update expected to not show npm version warning.' ) ;
55
63
}
56
64
57
- // Install version <7.5.6
58
- await npm ( 'install' , '--global' , '[email protected] ' ) ;
65
+ // Install and verify npm version <7.5.6
66
+ await npm ( 'install' , '[email protected] ' ) ;
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
+ }
59
71
60
72
// 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 ) ;
65
74
66
75
// 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 ) ;
71
77
72
78
// Ensure `ng build` executes successfully
73
79
const { stderr : stderrBuild } = await ng ( 'build' , '--configuration=development' ) ;
74
- if ( stderrBuild . includes ( warningText ) ) {
80
+ if ( stderrBuild . match ( warningTextRe ) ) {
75
81
throw new Error ( 'ng build expected to not show npm version warning.' ) ;
76
82
}
77
83
78
84
// Ensure `ng new` shows npm warning
79
85
// Must be outside the project for `ng new`
80
86
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 ) ;
85
88
86
89
// 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 ) ;
91
91
92
92
// Ensure `ng new --skip-install` executes successfully
93
93
const { stderr : stderrNewSkipInstall } = await ng ( 'new' , 'npm-seven-skip' , '--skip-install' ) ;
94
- if ( stderrNewSkipInstall . includes ( warningText ) ) {
94
+ if ( stderrNewSkipInstall . match ( warningTextRe ) ) {
95
95
throw new Error ( 'ng new --skip-install expected to not show npm version warning.' ) ;
96
96
}
97
97
98
98
// Ensure `ng new --package-manager=yarn` executes successfully
99
99
// Need an additional npmrc file since yarn does not use the NPM registry environment variable
100
100
const { stderr : stderrNewYarn } = await ng ( 'new' , 'npm-seven-yarn' , '--package-manager=yarn' ) ;
101
- if ( stderrNewYarn . includes ( warningText ) ) {
101
+ if ( stderrNewYarn . match ( warningTextRe ) ) {
102
102
throw new Error ( 'ng new --package-manager=yarn expected to not show npm version warning.' ) ;
103
103
}
104
104
} finally {
@@ -110,6 +110,6 @@ export default async function () {
110
110
process . chdir ( currentDirectory ) ;
111
111
112
112
// Reset version back to initial version
113
- await npm ( 'install' , '--global' , `npm@${ initialVersion } ` ) ;
113
+ await npm ( 'install' , `npm@${ initialVersion } ` ) ;
114
114
}
115
115
}
0 commit comments