1
1
#!/usr/bin/env node
2
2
3
- var child_process = require ( 'child_process' ) ,
4
- q = require ( 'q' ) ,
5
- fs = require ( 'fs' ) ;
6
-
7
- var CommandlineTest = function ( command ) {
8
- var self = this ;
9
- this . command_ = command ;
10
- this . expectedExitCode_ = 0 ;
11
- this . expectedErrors_ = [ ] ;
12
- this . assertExitCodeOnly_ = false ;
13
- this . testLogStream = undefined ;
3
+ const child_process = require ( 'child_process' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+
6
+ class CommandlineTest {
7
+ constructor ( command ) {
8
+ this . command = command ;
9
+ this . expectedExitCode = 0 ;
10
+ this . expectedErrors = [ ] ;
11
+ this . isExitCode = false ;
12
+ this . testLogStream = undefined ;
13
+ this . expectedMinTestDuration = undefined ;
14
+ this . expectedMaxTestDuration = undefined ;
15
+ }
16
+
14
17
15
18
// Only assert the exit code and not failures.
16
19
// This must be true if the command you're running does not support
17
20
// the flag '--resultJsonOutputFile'.
18
- this . assertExitCodeOnly = function ( ) {
19
- self . assertExitCodeOnly_ = true ;
20
- return self ;
21
- } ;
21
+ assertExitCodeOnly ( ) {
22
+ this . isExitCode = true ;
23
+ return this ;
24
+ }
22
25
23
- this . setTestLogFile = function ( filename ) {
24
- self . testLogStream = fs . createWriteStream ( filename , { flags : 'a' } ) ;
25
- } ;
26
+ setTestLogFile ( filename ) {
27
+ this . testLogStream = fs . createWriteStream ( filename , { flags : 'a' } ) ;
28
+ }
26
29
27
30
// Set the expected exit code for the test command.
28
- this . expectExitCode = function ( exitCode ) {
29
- self . expectedExitCode_ = exitCode ;
30
- return self ;
31
- } ;
31
+ expectExitCode ( exitCode ) {
32
+ this . expectedExitCode = exitCode ;
33
+ return this ;
34
+ }
32
35
33
36
// Set the expected total test duration in milliseconds.
34
- this . expectTestDuration = function ( min , max ) {
35
- self . expectedMinTestDuration_ = min ;
36
- self . expectedMaxTestDuration_ = max ;
37
- return self ;
38
- } ;
37
+ expectTestDuration ( min , max ) {
38
+ this . expectedMinTestDuration = min ;
39
+ this . expectedMaxTestDuration = max ;
40
+ return this ;
41
+ }
39
42
40
43
/**
41
44
* Add expected error(s) for the test command.
@@ -45,75 +48,75 @@ var CommandlineTest = function(command) {
45
48
* stackTrace: string, //optional regex
46
49
* }
47
50
*/
48
- this . expectErrors = function ( expectedErrors ) {
51
+ expectErrors ( expectedErrors ) {
49
52
if ( expectedErrors instanceof Array ) {
50
- self . expectedErrors_ = self . expectedErrors_ . concat ( expectedErrors ) ;
53
+ this . expectedErrors = this . expectedErrors . concat ( expectedErrors ) ;
51
54
} else {
52
- self . expectedErrors_ . push ( expectedErrors ) ;
55
+ this . expectedErrors . push ( expectedErrors ) ;
53
56
}
54
- return self ;
55
- } ;
57
+ return this ;
58
+ }
56
59
57
- this . run = function ( ) {
58
- var start = new Date ( ) . getTime ( ) ;
59
- var testOutputPath = ' test_output_' + start + ' .tmp' ;
60
- var output = '' ;
60
+ async run ( ) {
61
+ const start = new Date ( ) . getTime ( ) ;
62
+ const testOutputPath = ` test_output_${ start } .tmp` ;
63
+ let output = '' ;
61
64
62
- var flushAndFail = function ( errorMsg ) {
65
+ const flushAndFail = ( errorMsg ) => {
63
66
process . stdout . write ( output ) ;
64
67
throw new Error ( errorMsg ) ;
65
68
} ;
66
69
67
- return q . promise ( function ( resolve , reject ) {
68
- if ( ! self . assertExitCodeOnly_ ) {
69
- self . command_ = self . command_ + ' --resultJsonOutputFile ' + testOutputPath ;
70
- }
71
- var args = self . command_ . split ( / \s / ) ;
72
-
73
- var test_process ;
70
+ try {
74
71
75
- test_process = child_process . spawn ( args [ 0 ] , args . slice ( 1 ) ) ;
76
-
77
- var processData = function ( data ) {
78
- process . stdout . write ( '.' ) ;
79
- output += data ;
80
- if ( self . testLogStream ) {
81
- self . testLogStream . write ( data ) ;
72
+ let exitCode = await new Promise ( ( resolve , reject ) => {
73
+ if ( ! this . assertExitCodeOnly ) {
74
+ this . command = `${ this . command } --resultJsonOutputFile ${ testOutputPath } ` ;
82
75
}
83
- } ;
76
+ const args = this . command . split ( / \s / ) ;
77
+ const test_process = child_process . spawn ( args [ 0 ] , args . slice ( 1 ) ) ;
78
+
79
+ const processData = ( data ) => {
80
+ process . stdout . write ( '.' ) ;
81
+ output += data ;
82
+ if ( this . testLogStream ) {
83
+ this . testLogStream . write ( data ) ;
84
+ }
85
+ } ;
84
86
85
- test_process . stdout . on ( 'data' , processData ) ;
86
- test_process . stderr . on ( 'data' , processData ) ;
87
+ test_process . stdout . on ( 'data' , processData ) ;
88
+ test_process . stderr . on ( 'data' , processData ) ;
87
89
88
- test_process . on ( 'error' , function ( err ) {
89
- reject ( err ) ;
90
- } ) ;
90
+ test_process . on ( 'error' , ( err ) => {
91
+ reject ( err ) ;
92
+ } ) ;
91
93
92
- test_process . on ( 'exit' , function ( exitCode ) {
93
- resolve ( exitCode ) ;
94
+ test_process . on ( 'exit' , function ( exitCode ) {
95
+ resolve ( exitCode ) ;
96
+ } ) ;
94
97
} ) ;
95
- } ) . then ( function ( exitCode ) {
96
- if ( self . expectedExitCode_ !== exitCode ) {
97
- flushAndFail ( 'expecting exit code: ' + self . expectedExitCode_ +
98
+
99
+ if ( this . expectedExitCode !== exitCode ) {
100
+ flushAndFail ( 'expecting exit code: ' + this . expectedExitCode +
98
101
', actual: ' + exitCode ) ;
99
102
}
100
103
101
- if ( self . testLogStream ) {
102
- self . testLogStream . end ( ) ;
104
+ if ( this . testLogStream ) {
105
+ this . testLogStream . end ( ) ;
103
106
}
104
107
105
108
// Skip the rest if we are only verify exit code.
106
109
// Note: we're expecting a file populated by '--resultJsonOutputFile' after
107
110
// this point.
108
- if ( self . assertExitCodeOnly_ ) {
111
+ if ( this . assertExitCodeOnly ) {
109
112
return ;
110
113
}
111
114
112
- var raw_data = fs . readFileSync ( testOutputPath ) ;
113
- var testOutput = JSON . parse ( raw_data ) ;
115
+ const raw_data = fs . readFileSync ( testOutputPath ) ;
116
+ const testOutput = JSON . parse ( raw_data ) ;
114
117
115
- var actualErrors = [ ] ;
116
- var duration = 0 ;
118
+ let actualErrors = [ ] ;
119
+ let duration = 0 ;
117
120
testOutput . forEach ( function ( specResult ) {
118
121
duration += specResult . duration ;
119
122
specResult . assertions . forEach ( function ( assertion ) {
@@ -123,9 +126,9 @@ var CommandlineTest = function(command) {
123
126
} ) ;
124
127
} ) ;
125
128
126
- self . expectedErrors_ . forEach ( function ( expectedError ) {
127
- var found = false ;
128
- for ( var i = 0 ; i < actualErrors . length ; ++ i ) {
129
+ this . expectedErrors . forEach ( ( expectedError ) => {
130
+ let found = false ;
131
+ for ( let i = 0 ; i < actualErrors . length ; ++ i ) {
129
132
var actualError = actualErrors [ i ] ;
130
133
131
134
// if expected message is defined and messages don't match
@@ -167,25 +170,25 @@ var CommandlineTest = function(command) {
167
170
flushAndFail ( 'failed with ' + actualErrors . length + ' unexpected failures' ) ;
168
171
}
169
172
170
- if ( self . expectedMinTestDuration_
171
- && duration < self . expectedMinTestDuration_ ) {
173
+ if ( this . expectedMinTestDuration
174
+ && duration < this . expectedMinTestDuration ) {
172
175
flushAndFail ( 'expecting test min duration: ' +
173
- self . expectedMinTestDuration_ + ', actual: ' + duration ) ;
176
+ this . expectedMinTestDuration + ', actual: ' + duration ) ;
174
177
}
175
- if ( self . expectedMaxTestDuration_
176
- && duration > self . expectedMaxTestDuration_ ) {
178
+ if ( this . expectedMaxTestDuration
179
+ && duration > this . expectedMaxTestDuration ) {
177
180
flushAndFail ( 'expecting test max duration: ' +
178
- self . expectedMaxTestDuration_ + ', actual: ' + duration ) ;
181
+ this . expectedMaxTestDuration + ', actual: ' + duration ) ;
179
182
}
180
- } ) . fin ( function ( ) {
183
+ } finally {
181
184
try {
182
185
fs . unlinkSync ( testOutputPath ) ;
183
186
} catch ( err ) {
184
187
// don't do anything
185
188
}
186
- } ) ;
187
- } ;
188
- } ;
189
+ }
190
+ }
191
+ }
189
192
190
193
/**
191
194
* Util for running tests and testing functionalities including:
@@ -195,34 +198,36 @@ var CommandlineTest = function(command) {
195
198
* For now, this means protractor tests (jasmine/mocha).
196
199
*/
197
200
exports . Executor = function ( ) {
198
- var tests = [ ] ;
199
- this . addCommandlineTest = function ( command ) {
200
- var test = new CommandlineTest ( command ) ;
201
+ let tests = [ ] ;
202
+ this . addCommandlineTest = ( command ) => {
203
+ let test = new CommandlineTest ( command ) ;
201
204
tests . push ( test ) ;
202
205
return test ;
203
206
} ;
204
207
205
- this . execute = function ( logFile ) {
206
- var failed = false ;
207
-
208
- ( function runTests ( i ) {
209
- if ( i < tests . length ) {
210
- console . log ( 'running: ' + tests [ i ] . command_ ) ;
208
+ this . runTests = async function ( i , logFile , failed ) {
209
+ if ( i < tests . length ) {
210
+ try {
211
+ console . log ( 'running: ' + tests [ i ] . command ) ;
211
212
if ( logFile ) {
212
213
tests [ i ] . setTestLogFile ( logFile ) ;
213
214
}
214
- tests [ i ] . run ( ) . then ( function ( ) {
215
- console . log ( '\n>>> \033[1;32mpass\033[0m' ) ;
216
- } , function ( err ) {
217
- failed = true ;
218
- console . log ( '\n>>> \033[1;31mfail: ' + err . toString ( ) + '\033[0m' ) ;
219
- } ) . fin ( function ( ) {
220
- runTests ( i + 1 ) ;
221
- } ) . done ( ) ;
222
- } else {
223
- console . log ( 'Summary: ' + ( failed ? 'fail' : 'pass' ) ) ;
224
- process . exit ( failed ? 1 : 0 ) ;
215
+ await tests [ i ] . run ( ) ;
216
+ console . log ( '\n>>> \033[1;32mpass\033[0m' ) ;
217
+ } catch ( err ) {
218
+ failed = true ;
219
+ console . log ( '\n>>> \033[1;31mfail: ' + err . toString ( ) + '\033[0m' ) ;
220
+ } finally {
221
+ this . runTests ( i + 1 , logFile , failed ) ;
225
222
}
226
- } ( 0 ) ) ;
223
+ } else {
224
+ console . log ( 'Summary: ' + ( failed ? 'fail' : 'pass' ) ) ;
225
+ process . exit ( failed ? 1 : 0 ) ;
226
+ }
227
+ } ;
228
+
229
+ this . execute = ( logFile ) => {
230
+ let failed = false ;
231
+ this . runTests ( 0 , logFile , failed ) ;
227
232
} ;
228
233
} ;
0 commit comments