@@ -70,7 +70,6 @@ function lastLogger() {
7070}
7171
7272const testGlob = argv . glob || 'tests/**/*.ts' ;
73- let currentFileName = null ;
7473
7574const e2eRoot = path . join ( __dirname , 'e2e' ) ;
7675const allSetups = glob . sync ( 'setup/**/*.ts' , { nodir : true , cwd : e2eRoot } ) . sort ( ) ;
@@ -122,120 +121,120 @@ setGlobalVariable('argv', argv);
122121setGlobalVariable ( 'ci' , process . env [ 'CI' ] ?. toLowerCase ( ) === 'true' || process . env [ 'CI' ] === '1' ) ;
123122setGlobalVariable ( 'package-manager' , argv . yarn ? 'yarn' : 'npm' ) ;
124123
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 ) ;
129127
128+ let lastTestRun : string | null = null ;
129+
130+ try {
131+ // NPM registries for the lifetime of the test execution
130132 const registryProcess = await createNpmRegistry ( httpPort , httpPort ) ;
131133 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 ) ) ;
150+
151+ if ( argv . debug ) {
152+ console . log ( `Current Directory: ${ process . cwd ( ) } ` ) ;
153+ console . log ( 'Will loop forever while you debug... CTRL-C to quit.' ) ;
132154
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- }
155+ /* eslint-disable no-constant-condition */
156+ while ( 1 ) {
157+ // That's right!
234158 }
159+ }
235160
236- process . exit ( 1 ) ;
237- } ,
238- ) ;
161+ process . exit ( 1 ) ;
162+ }
163+ } ) ;
164+
165+ function normalizeTestStep ( relativeName : string ) {
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+
174+ return { absoluteName, currentFileName } ;
175+ }
176+
177+ async function runTest ( relativeName : string , testIndex : number ) {
178+ const { absoluteName, currentFileName } = normalizeTestStep ( relativeName ) ;
179+ const start = + new Date ( ) ;
180+
181+ const module = require ( absoluteName ) ;
182+ const originalEnvVariables = {
183+ ...process . env ,
184+ } ;
185+
186+ const fn : ( skipClean ?: ( ) => void ) => Promise < void > | void =
187+ typeof module == 'function'
188+ ? module
189+ : typeof module . default == 'function'
190+ ? module . default
191+ : ( ) => {
192+ throw new Error ( 'Invalid test module.' ) ;
193+ } ;
194+
195+ printHeader ( currentFileName , testIndex ) ;
196+
197+ let clean = true ;
198+ let previousDir = process . cwd ( ) ;
199+ try {
200+ // Run the test function with the current file on the logStack.
201+ logStack . push ( lastLogger ( ) . createChild ( currentFileName ) ) ;
202+ try {
203+ await fn ( ( ) => ( clean = false ) ) ;
204+ } finally {
205+ logStack . pop ( ) ;
206+ }
207+
208+ console . log ( '----' ) ;
209+
210+ // If we're not in a setup, change the directory back to where it was before the test.
211+ // This allows tests to chdir without worrying about keeping the original directory.
212+ if ( ! allSetups . includes ( relativeName ) && previousDir ) {
213+ process . chdir ( previousDir ) ;
214+
215+ // Restore env variables before each test.
216+ console . log ( ' Restoring original environment variables...' ) ;
217+ process . env = originalEnvVariables ;
218+ }
219+
220+ // Only clean after a real test, not a setup step. Also skip cleaning if the test
221+ // requested an exception.
222+ if ( ! allSetups . includes ( relativeName ) && clean ) {
223+ logStack . push ( new logging . NullLogger ( ) ) ;
224+ try {
225+ await gitClean ( ) ;
226+ } finally {
227+ logStack . pop ( ) ;
228+ }
229+ }
230+
231+ printFooter ( currentFileName , start ) ;
232+ } catch ( err ) {
233+ printFooter ( currentFileName , start ) ;
234+ console . error ( err ) ;
235+ throw err ;
236+ }
237+ }
239238
240239function printHeader ( testName : string , testIndex : number ) {
241240 const text = `${ testIndex + 1 } of ${ testsToRun . length } ` ;
0 commit comments