@@ -5,7 +5,8 @@ const sander = require('sander');
55const {
66 normaliseOutput,
77 runTestSuiteWithSamples,
8- assertDirectoriesAreEqual
8+ assertDirectoriesAreEqual,
9+ getFileNamesAndRemoveOutput
910} = require ( '../utils.js' ) ;
1011
1112const cwd = process . cwd ( ) ;
@@ -17,123 +18,131 @@ runTestSuiteWithSamples(
1718 'cli' ,
1819 path . resolve ( __dirname , 'samples' ) ,
1920 ( dir , config ) => {
20- ( config . skip ? it . skip : config . solo ? it . only : it ) (
21- path . basename ( dir ) + ': ' + config . description ,
22- done => {
23- process . chdir ( config . cwd || dir ) ;
24- if ( config . before ) config . before ( ) ;
21+ // allow to repeat flaky tests for debugging on CLI
22+ for ( let pass = 0 ; pass < ( config . repeat || 1 ) ; pass ++ ) {
23+ runTest ( dir , config , pass ) ;
24+ }
25+ } ,
26+ ( ) => process . chdir ( cwd )
27+ ) ;
2528
26- const command = config . command . replace (
27- / ( ^ | ) r o l l u p ( $ | ) / g,
28- `node ${ path . resolve ( __dirname , '../../dist/bin' ) } ${ path . sep } rollup `
29- ) ;
29+ function runTest ( dir , config , pass ) {
30+ const name = path . basename ( dir ) + ': ' + config . description ;
31+ ( config . skip ? it . skip : config . solo ? it . only : it ) (
32+ pass > 0 ? `${ name } (pass ${ pass + 1 } )` : name ,
33+ done => {
34+ process . chdir ( config . cwd || dir ) ;
35+ if ( pass > 0 ) {
36+ getFileNamesAndRemoveOutput ( dir ) ;
37+ }
38+ if ( config . before ) config . before ( ) ;
3039
31- const childProcess = exec (
32- command ,
33- {
34- timeout : 40000 ,
35- env : { ...process . env , FORCE_COLOR : '0' , ...config . env }
36- } ,
37- ( err , code , stderr ) => {
38- if ( config . after ) config . after ( err , code , stderr ) ;
39- if ( err && ! err . killed ) {
40- if ( config . error ) {
41- const shouldContinue = config . error ( err ) ;
42- if ( ! shouldContinue ) return done ( ) ;
43- } else {
44- throw err ;
45- }
46- }
40+ const command = config . command . replace (
41+ / ( ^ | ) r o l l u p ( $ | ) / g,
42+ `node ${ path . resolve ( __dirname , '../../dist/bin' ) } ${ path . sep } rollup `
43+ ) ;
4744
48- if ( 'stderr' in config ) {
49- const shouldContinue = config . stderr ( stderr ) ;
45+ const childProcess = exec (
46+ command ,
47+ {
48+ timeout : 40000 ,
49+ env : { ...process . env , FORCE_COLOR : '0' , ...config . env }
50+ } ,
51+ ( err , code , stderr ) => {
52+ if ( config . after ) config . after ( err , code , stderr ) ;
53+ if ( err && ! err . killed ) {
54+ if ( config . error ) {
55+ const shouldContinue = config . error ( err ) ;
5056 if ( ! shouldContinue ) return done ( ) ;
51- } else if ( stderr ) {
52- console . error ( stderr ) ;
57+ } else {
58+ throw err ;
5359 }
60+ }
5461
55- let unintendedError ;
56-
57- if ( config . execute ) {
58- try {
59- const fn = new Function ( 'require' , 'module' , 'exports' , 'assert' , code ) ;
60- const module = {
61- exports : { }
62- } ;
63- fn ( require , module , module . exports , assert ) ;
62+ if ( 'stderr' in config ) {
63+ const shouldContinue = config . stderr ( stderr ) ;
64+ if ( ! shouldContinue ) return done ( ) ;
65+ } else if ( stderr ) {
66+ console . error ( stderr ) ;
67+ }
6468
65- if ( config . error ) {
66- unintendedError = new Error ( 'Expected an error while executing output' ) ;
67- }
69+ let unintendedError ;
6870
69- if ( config . exports ) {
70- config . exports ( module . exports ) ;
71- }
72- } catch ( err ) {
73- if ( config . error ) {
74- config . error ( err ) ;
75- } else {
76- unintendedError = err ;
77- }
78- }
71+ if ( config . execute ) {
72+ try {
73+ const fn = new Function ( 'require' , 'module' , 'exports' , 'assert' , code ) ;
74+ const module = {
75+ exports : { }
76+ } ;
77+ fn ( require , module , module . exports , assert ) ;
7978
80- if ( config . show || unintendedError ) {
81- console . log ( code + '\n\n\n ') ;
79+ if ( config . error ) {
80+ unintendedError = new Error ( 'Expected an error while executing output ') ;
8281 }
8382
84- if ( config . solo ) console . groupEnd ( ) ;
85-
86- unintendedError ? done ( unintendedError ) : done ( ) ;
87- } else if ( config . result ) {
88- try {
89- config . result ( code ) ;
90- done ( ) ;
91- } catch ( err ) {
92- done ( err ) ;
93- }
94- } else if ( config . test ) {
95- try {
96- config . test ( ) ;
97- done ( ) ;
98- } catch ( err ) {
99- done ( err ) ;
100- }
101- } else if (
102- sander . existsSync ( '_expected' ) &&
103- sander . statSync ( '_expected' ) . isDirectory ( )
104- ) {
105- try {
106- assertDirectoriesAreEqual ( '_actual' , '_expected' ) ;
107- done ( ) ;
108- } catch ( err ) {
109- done ( err ) ;
83+ if ( config . exports ) {
84+ config . exports ( module . exports ) ;
11085 }
111- } else {
112- const expected = sander . readFileSync ( '_expected.js' ) . toString ( ) ;
113- try {
114- assert . equal ( normaliseOutput ( code ) , normaliseOutput ( expected ) ) ;
115- done ( ) ;
116- } catch ( err ) {
117- done ( err ) ;
86+ } catch ( err ) {
87+ if ( config . error ) {
88+ config . error ( err ) ;
89+ } else {
90+ unintendedError = err ;
11891 }
11992 }
120- }
121- ) ;
12293
123- childProcess . stderr . on ( 'data' , async data => {
124- if ( config . abortOnStderr ) {
94+ if ( config . show || unintendedError ) {
95+ console . log ( code + '\n\n\n' ) ;
96+ }
97+
98+ if ( config . solo ) console . groupEnd ( ) ;
99+
100+ unintendedError ? done ( unintendedError ) : done ( ) ;
101+ } else if ( config . result ) {
125102 try {
126- if ( await config . abortOnStderr ( data ) ) {
127- childProcess . kill ( 'SIGTERM' ) ;
128- }
103+ config . result ( code ) ;
104+ done ( ) ;
105+ } catch ( err ) {
106+ done ( err ) ;
107+ }
108+ } else if ( config . test ) {
109+ try {
110+ config . test ( ) ;
111+ done ( ) ;
112+ } catch ( err ) {
113+ done ( err ) ;
114+ }
115+ } else if ( sander . existsSync ( '_expected' ) && sander . statSync ( '_expected' ) . isDirectory ( ) ) {
116+ try {
117+ assertDirectoriesAreEqual ( '_actual' , '_expected' ) ;
118+ done ( ) ;
119+ } catch ( err ) {
120+ done ( err ) ;
121+ }
122+ } else {
123+ const expected = sander . readFileSync ( '_expected.js' ) . toString ( ) ;
124+ try {
125+ assert . equal ( normaliseOutput ( code ) , normaliseOutput ( expected ) ) ;
126+ done ( ) ;
129127 } catch ( err ) {
130- childProcess . kill ( 'SIGTERM' ) ;
131128 done ( err ) ;
132129 }
133130 }
134- } ) ;
135- }
136- ) . timeout ( 50000 ) ;
137- } ,
138- ( ) => process . chdir ( cwd )
139- ) ;
131+ }
132+ ) ;
133+
134+ childProcess . stderr . on ( 'data' , async data => {
135+ if ( config . abortOnStderr ) {
136+ try {
137+ if ( await config . abortOnStderr ( data ) ) {
138+ childProcess . kill ( 'SIGTERM' ) ;
139+ }
140+ } catch ( err ) {
141+ childProcess . kill ( 'SIGTERM' ) ;
142+ done ( err ) ;
143+ }
144+ }
145+ } ) ;
146+ }
147+ ) . timeout ( 50000 ) ;
148+ }
0 commit comments