@@ -29,6 +29,7 @@ const { join, sep } = require('path')
29
29
const yaml = require ( 'js-yaml' )
30
30
const minimist = require ( 'minimist' )
31
31
const ms = require ( 'ms' )
32
+ const globby = require ( 'globby' )
32
33
const { Client } = require ( '../../index' )
33
34
const build = require ( './test-runner' )
34
35
const createJunitReporter = require ( './reporter' )
@@ -45,18 +46,13 @@ const options = minimist(process.argv.slice(2), {
45
46
string : [ 'suite' , 'test' ] ,
46
47
} )
47
48
48
- const getAllFiles = dir => {
49
- return readdirSync ( dir )
50
- . reduce ( ( files , file ) => {
51
- const name = join ( dir , file )
52
- if ( statSync ( name ) . isDirectory ( ) ) {
53
- return [ ...files , ...getAllFiles ( name ) ]
54
- } else if ( ! name . endsWith ( '.yaml' ) && ! name . endsWith ( '.yml' ) ) {
55
- return files
56
- } else {
57
- return [ ...files , name ]
58
- }
59
- } , [ ] )
49
+ const getAllFiles = async dir => {
50
+ const files = await globby ( dir , {
51
+ expandDirectories : {
52
+ extensions : [ 'yml' , 'yaml' ]
53
+ }
54
+ } )
55
+ return files . sort ( )
60
56
}
61
57
62
58
function runner ( opts = { } ) {
@@ -89,112 +85,88 @@ async function start ({ client }) {
89
85
pass : 0 ,
90
86
assertions : 0
91
87
}
92
- const folders = getAllFiles ( yamlFolder )
93
- . reduce ( ( arr , file ) => {
94
- const path = file . slice ( file . indexOf ( '/tests' ) , file . lastIndexOf ( '/' ) )
95
- let inserted = false
96
- for ( let i = 0 ; i < arr . length ; i ++ ) {
97
- if ( arr [ i ] [ 0 ] . includes ( path ) ) {
98
- inserted = true
99
- arr [ i ] . push ( file )
100
- break
101
- }
102
- }
103
- if ( ! inserted ) arr . push ( [ file ] )
104
- return arr
105
- } , [ ] )
88
+ const files = await getAllFiles ( yamlFolder )
106
89
107
90
const totalTime = now ( )
108
- for ( const folder of folders ) {
91
+ for ( const file of files ) {
109
92
// pretty name
110
- const apiName = folder [ 0 ] . split ( `${ sep } tests${ sep } ` ) [ 1 ]
93
+ const apiName = file . split ( `${ sep } tests${ sep } ` ) [ 1 ]
111
94
112
95
log ( 'Testing ' + apiName )
113
- const apiTime = now ( )
114
-
115
- for ( const file of folder ) {
116
- const testRunner = build ( { client } )
117
- const fileTime = now ( )
118
- const data = readFileSync ( file , 'utf8' )
119
-
120
- // get the test yaml (as object), some file has multiple yaml documents inside,
121
- // every document is separated by '---', so we split on the separator
122
- // and then we remove the empty strings, finally we parse them
123
- const tests = data
124
- . split ( '\n---\n' )
125
- . map ( s => s . trim ( ) )
126
- // empty strings
127
- . filter ( Boolean )
128
- . map ( parse )
129
- // null values
130
- . filter ( Boolean )
131
-
132
- // get setup and teardown if present
133
- let setupTest = null
134
- let teardownTest = null
135
- for ( const test of tests ) {
136
- if ( test . setup ) setupTest = test . setup
137
- if ( test . teardown ) teardownTest = test . teardown
138
- }
96
+ const testRunner = build ( { client } )
97
+ const fileTime = now ( )
98
+ const data = readFileSync ( file , 'utf8' )
99
+
100
+ // get the test yaml (as object), some file has multiple yaml documents inside,
101
+ // every document is separated by '---', so we split on the separator
102
+ // and then we remove the empty strings, finally we parse them
103
+ const tests = data
104
+ . split ( '\n---\n' )
105
+ . map ( s => s . trim ( ) )
106
+ // empty strings
107
+ . filter ( Boolean )
108
+ . map ( parse )
109
+ // null values
110
+ . filter ( Boolean )
111
+
112
+ // get setup and teardown if present
113
+ let setupTest = null
114
+ let teardownTest = null
115
+ for ( const test of tests ) {
116
+ if ( test . setup ) setupTest = test . setup
117
+ if ( test . teardown ) teardownTest = test . teardown
118
+ }
139
119
140
- const cleanPath = file . slice ( file . lastIndexOf ( apiName ) )
141
-
142
- // skip if --suite CLI arg doesn't match
143
- if ( options . suite && ! cleanPath . endsWith ( options . suite ) ) continue
144
-
145
- log ( ' ' + cleanPath )
146
- const junitTestSuite = junitTestSuites . testsuite ( apiName . slice ( 1 ) + ' - ' + cleanPath )
147
-
148
- for ( const test of tests ) {
149
- const testTime = now ( )
150
- const name = Object . keys ( test ) [ 0 ]
151
-
152
- // skip setups, teardowns and anything that doesn't match --test flag when present
153
- if ( name === 'setup' || name === 'teardown' ) continue
154
- if ( options . test && ! name . endsWith ( options . test ) ) continue
155
-
156
- const junitTestCase = junitTestSuite . testcase ( name , `node_${ process . version } /${ cleanPath } ` )
157
-
158
- stats . total += 1
159
- log ( ' - ' + name )
160
- try {
161
- await testRunner . run ( setupTest , test [ name ] , teardownTest , stats , junitTestCase )
162
- stats . pass += 1
163
- } catch ( err ) {
164
- junitTestCase . failure ( err )
165
- junitTestCase . end ( )
166
- junitTestSuite . end ( )
167
- junitTestSuites . end ( )
168
- generateJunitXmlReport ( junit , 'serverless' )
169
- console . error ( err )
170
-
171
- if ( options . bail ) {
172
- process . exit ( 1 )
173
- } else {
174
- continue
175
- }
176
- }
177
- const totalTestTime = now ( ) - testTime
120
+ const cleanPath = file . slice ( file . lastIndexOf ( apiName ) )
121
+
122
+ // skip if --suite CLI arg doesn't match
123
+ if ( options . suite && ! cleanPath . endsWith ( options . suite ) ) continue
124
+
125
+ const junitTestSuite = junitTestSuites . testsuite ( apiName . slice ( 1 ) + ' - ' + cleanPath )
126
+
127
+ for ( const test of tests ) {
128
+ const testTime = now ( )
129
+ const name = Object . keys ( test ) [ 0 ]
130
+
131
+ // skip setups, teardowns and anything that doesn't match --test flag when present
132
+ if ( name === 'setup' || name === 'teardown' ) continue
133
+ if ( options . test && ! name . endsWith ( options . test ) ) continue
134
+
135
+ const junitTestCase = junitTestSuite . testcase ( name , `node_${ process . version } /${ cleanPath } ` )
136
+
137
+ stats . total += 1
138
+ log ( ' - ' + name )
139
+ try {
140
+ await testRunner . run ( setupTest , test [ name ] , teardownTest , stats , junitTestCase )
141
+ stats . pass += 1
142
+ } catch ( err ) {
143
+ junitTestCase . failure ( err )
178
144
junitTestCase . end ( )
179
- if ( totalTestTime > MAX_TEST_TIME ) {
180
- log ( ' took too long: ' + ms ( totalTestTime ) )
145
+ junitTestSuite . end ( )
146
+ junitTestSuites . end ( )
147
+ generateJunitXmlReport ( junit , 'serverless' )
148
+ console . error ( err )
149
+
150
+ if ( options . bail ) {
151
+ process . exit ( 1 )
181
152
} else {
182
- log ( ' took: ' + ms ( totalTestTime ) )
153
+ continue
183
154
}
184
155
}
185
- junitTestSuite . end ( )
186
- const totalFileTime = now ( ) - fileTime
187
- if ( totalFileTime > MAX_FILE_TIME ) {
188
- log ( ` ${ cleanPath } took too long: ` + ms ( totalFileTime ) )
156
+ const totalTestTime = now ( ) - testTime
157
+ junitTestCase . end ( )
158
+ if ( totalTestTime > MAX_TEST_TIME ) {
159
+ log ( ' took too long: ' + ms ( totalTestTime ) )
189
160
} else {
190
- log ( ` ${ cleanPath } took: ` + ms ( totalFileTime ) )
161
+ log ( ' took: ' + ms ( totalTestTime ) )
191
162
}
192
163
}
193
- const totalApiTime = now ( ) - apiTime
194
- if ( totalApiTime > MAX_API_TIME ) {
195
- log ( `${ apiName } took too long: ` + ms ( totalApiTime ) )
164
+ junitTestSuite . end ( )
165
+ const totalFileTime = now ( ) - fileTime
166
+ if ( totalFileTime > MAX_FILE_TIME ) {
167
+ log ( ` ${ cleanPath } took too long: ` + ms ( totalFileTime ) )
196
168
} else {
197
- log ( `${ apiName } took: ` + ms ( totalApiTime ) )
169
+ log ( ` ${ cleanPath } took: ` + ms ( totalFileTime ) )
198
170
}
199
171
}
200
172
junitTestSuites . end ( )
@@ -204,6 +176,7 @@ async function start ({ client }) {
204
176
- Total: ${ stats . total }
205
177
- Skip: ${ stats . skip }
206
178
- Pass: ${ stats . pass }
179
+ - Fail: ${ stats . total - stats . pass }
207
180
- Assertions: ${ stats . assertions }
208
181
` )
209
182
}
0 commit comments