@@ -70,7 +70,6 @@ function lastLogger() {
70
70
}
71
71
72
72
const testGlob = argv . glob || 'tests/**/*.ts' ;
73
- let currentFileName = null ;
74
73
75
74
const e2eRoot = path . join ( __dirname , 'e2e' ) ;
76
75
const allSetups = glob . sync ( 'setup/**/*.ts' , { nodir : true , cwd : e2eRoot } ) . sort ( ) ;
@@ -122,120 +121,114 @@ setGlobalVariable('argv', argv);
122
121
setGlobalVariable ( 'ci' , process . env [ 'CI' ] ?. toLowerCase ( ) === 'true' || process . env [ 'CI' ] === '1' ) ;
123
122
setGlobalVariable ( 'package-manager' , argv . yarn ? 'yarn' : 'npm' ) ;
124
123
125
- Promise . all ( [ findFreePort ( ) , findFreePort ( ) ] )
126
- . then ( async ( [ httpPort , httpsPort ] ) => {
127
- setGlobalVariable ( 'package-registry' , 'http://localhost:' + httpPort ) ;
128
- setGlobalVariable ( 'package-secure-registry' , 'http://localhost:' + httpsPort ) ;
124
+ Promise . all ( [ findFreePort ( ) , findFreePort ( ) ] ) . then ( async ( [ httpPort , httpsPort ] ) => {
125
+ setGlobalVariable ( 'package-registry' , 'http://localhost:' + httpPort ) ;
126
+ setGlobalVariable ( 'package-secure-registry' , 'http://localhost:' + httpsPort ) ;
129
127
128
+ let lastTestRun : string | null = null ;
129
+
130
+ try {
131
+ // NPM registries for the lifetime of the test execution
130
132
const registryProcess = await createNpmRegistry ( httpPort , httpPort ) ;
131
133
const secureRegistryProcess = await createNpmRegistry ( httpPort , httpsPort , true ) ;
134
+ try {
135
+ for ( const [ testIndex , test ] of testsToRun . entries ( ) ) {
136
+ await runTest ( ( lastTestRun = test ) , testIndex ) ;
137
+ }
138
+ } finally {
139
+ registryProcess . kill ( ) ;
140
+ secureRegistryProcess . kill ( ) ;
141
+ }
142
+
143
+ console . log ( colors . green ( 'Done.' ) ) ;
144
+ process . exit ( 0 ) ;
145
+ } catch ( err ) {
146
+ console . log ( '\n' ) ;
147
+ console . error ( colors . red ( `Test "${ lastTestRun } " failed...` ) ) ;
148
+ console . error ( colors . red ( err . message ) ) ;
149
+ console . error ( colors . red ( err . stack ) ) ;
132
150
133
- return testsToRun
134
- . reduce ( ( previous , relativeName , testIndex ) => {
135
- // Make sure this is a windows compatible path.
136
- let absoluteName = path . join ( e2eRoot , relativeName ) ;
137
- if ( / ^ w i n / . test ( process . platform ) ) {
138
- absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
139
- }
140
-
141
- return previous . then ( ( ) => {
142
- currentFileName = relativeName . replace ( / \. t s $ / , '' ) ;
143
- const start = + new Date ( ) ;
144
-
145
- const module = require ( absoluteName ) ;
146
- const originalEnvVariables = {
147
- ...process . env ,
148
- } ;
149
-
150
- const fn : ( skipClean ?: ( ) => void ) => Promise < void > | void =
151
- typeof module == 'function'
152
- ? module
153
- : typeof module . default == 'function'
154
- ? module . default
155
- : ( ) => {
156
- throw new Error ( 'Invalid test module.' ) ;
157
- } ;
158
-
159
- let clean = true ;
160
- let previousDir = null ;
161
-
162
- return Promise . resolve ( )
163
- . then ( ( ) => printHeader ( currentFileName , testIndex ) )
164
- . then ( ( ) => ( previousDir = process . cwd ( ) ) )
165
- . then ( ( ) => logStack . push ( lastLogger ( ) . createChild ( currentFileName ) ) )
166
- . then ( ( ) => fn ( ( ) => ( clean = false ) ) )
167
- . then (
168
- ( ) => logStack . pop ( ) ,
169
- ( err ) => {
170
- logStack . pop ( ) ;
171
- throw err ;
172
- } ,
173
- )
174
- . then ( ( ) => console . log ( '----' ) )
175
- . then ( ( ) => {
176
- // If we're not in a setup, change the directory back to where it was before the test.
177
- // This allows tests to chdir without worrying about keeping the original directory.
178
- if ( ! allSetups . includes ( relativeName ) && previousDir ) {
179
- process . chdir ( previousDir ) ;
180
-
181
- // Restore env variables before each test.
182
- console . log ( ' Restoring original environment variables...' ) ;
183
- process . env = originalEnvVariables ;
184
- }
185
- } )
186
- . then ( ( ) => {
187
- // Only clean after a real test, not a setup step. Also skip cleaning if the test
188
- // requested an exception.
189
- if ( ! allSetups . includes ( relativeName ) && clean ) {
190
- logStack . push ( new logging . NullLogger ( ) ) ;
191
- return gitClean ( ) . then (
192
- ( ) => logStack . pop ( ) ,
193
- ( err ) => {
194
- logStack . pop ( ) ;
195
- throw err ;
196
- } ,
197
- ) ;
198
- }
199
- } )
200
- . then (
201
- ( ) => printFooter ( currentFileName , start ) ,
202
- ( err ) => {
203
- printFooter ( currentFileName , start ) ;
204
- console . error ( err ) ;
205
- throw err ;
206
- } ,
207
- ) ;
208
- } ) ;
209
- } , Promise . resolve ( ) )
210
- . finally ( ( ) => {
211
- registryProcess . kill ( ) ;
212
- secureRegistryProcess . kill ( ) ;
213
- } ) ;
214
- } )
215
- . then (
216
- ( ) => {
217
- console . log ( colors . green ( 'Done.' ) ) ;
218
- process . exit ( 0 ) ;
219
- } ,
220
- ( err ) => {
221
- console . log ( '\n' ) ;
222
- console . error ( colors . red ( `Test "${ currentFileName } " failed...` ) ) ;
223
- console . error ( colors . red ( err . message ) ) ;
224
- console . error ( colors . red ( err . stack ) ) ;
225
-
226
- if ( argv . debug ) {
227
- console . log ( `Current Directory: ${ process . cwd ( ) } ` ) ;
228
- console . log ( 'Will loop forever while you debug... CTRL-C to quit.' ) ;
229
-
230
- /* eslint-disable no-constant-condition */
231
- while ( 1 ) {
232
- // That's right!
233
- }
151
+ if ( argv . debug ) {
152
+ console . log ( `Current Directory: ${ process . cwd ( ) } ` ) ;
153
+ console . log ( 'Will loop forever while you debug... CTRL-C to quit.' ) ;
154
+
155
+ /* eslint-disable no-constant-condition */
156
+ while ( 1 ) {
157
+ // That's right!
234
158
}
159
+ }
235
160
236
- process . exit ( 1 ) ;
237
- } ,
238
- ) ;
161
+ process . exit ( 1 ) ;
162
+ }
163
+ } ) ;
164
+
165
+ async function runTest ( relativeName : string , testIndex : number ) {
166
+ // Make sure this is a windows compatible path.
167
+ let absoluteName = path . join ( e2eRoot , relativeName ) ;
168
+ if ( / ^ w i n / . test ( process . platform ) ) {
169
+ absoluteName = absoluteName . replace ( / \\ / g, path . posix . sep ) ;
170
+ }
171
+
172
+ const currentFileName = relativeName . replace ( / \. t s $ / , '' ) ;
173
+ const start = + new Date ( ) ;
174
+
175
+ const module = require ( absoluteName ) ;
176
+ const originalEnvVariables = {
177
+ ...process . env ,
178
+ } ;
179
+
180
+ const fn : ( skipClean ?: ( ) => void ) => Promise < void > | void =
181
+ typeof module == 'function'
182
+ ? module
183
+ : typeof module . default == 'function'
184
+ ? module . default
185
+ : ( ) => {
186
+ throw new Error ( 'Invalid test module.' ) ;
187
+ } ;
188
+
189
+ printHeader ( currentFileName , testIndex ) ;
190
+
191
+ let clean = true ;
192
+ let previousDir = process . cwd ( ) ;
193
+ try {
194
+ // Run the test function with the current file on the logStack.
195
+ logStack . push ( lastLogger ( ) . createChild ( currentFileName ) ) ;
196
+ try {
197
+ await fn ( ( ) => ( clean = false ) ) ;
198
+ } finally {
199
+ logStack . pop ( ) ;
200
+ }
201
+
202
+ console . log ( '----' ) ;
203
+
204
+ // If we're not in a setup, change the directory back to where it was before the test.
205
+ // This allows tests to chdir without worrying about keeping the original directory.
206
+ if ( ! allSetups . includes ( relativeName ) && previousDir ) {
207
+ process . chdir ( previousDir ) ;
208
+
209
+ // Restore env variables before each test.
210
+ console . log ( ' Restoring original environment variables...' ) ;
211
+ process . env = originalEnvVariables ;
212
+ }
213
+
214
+ // Only clean after a real test, not a setup step. Also skip cleaning if the test
215
+ // requested an exception.
216
+ if ( ! allSetups . includes ( relativeName ) && clean ) {
217
+ logStack . push ( new logging . NullLogger ( ) ) ;
218
+ try {
219
+ await gitClean ( ) ;
220
+ } finally {
221
+ logStack . pop ( ) ;
222
+ }
223
+ }
224
+
225
+ printFooter ( currentFileName , start ) ;
226
+ } catch ( err ) {
227
+ printFooter ( currentFileName , start ) ;
228
+ console . error ( err ) ;
229
+ throw err ;
230
+ }
231
+ }
239
232
240
233
function printHeader ( testName : string , testIndex : number ) {
241
234
const text = `${ testIndex + 1 } of ${ testsToRun . length } ` ;
0 commit comments