@@ -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 . includes ( `${ sep } tests${ sep } ` ) && ( ! 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,87 @@ 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 . lastIndexOf ( `${ sep } tests` ) , file . lastIndexOf ( sep ) )
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 ] . split ( sep ) [ 0 ]
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 fileTime = now ( )
97
+ const data = readFileSync ( file , 'utf8' )
98
+
99
+ // get the test yaml (as object), some file has multiple yaml documents inside,
100
+ // every document is separated by '---', so we split on the separator
101
+ // and then we remove the empty strings, finally we parse them
102
+ const tests = data
103
+ . split ( '\n---\n' )
104
+ . map ( s => s . trim ( ) )
105
+ // empty strings
106
+ . filter ( Boolean )
107
+ . map ( parse )
108
+ // null values
109
+ . filter ( Boolean )
110
+
111
+ // get setup and teardown if present
112
+ let setupTest = null
113
+ let teardownTest = null
114
+ for ( const test of tests ) {
115
+ if ( test . setup ) setupTest = test . setup
116
+ if ( test . teardown ) teardownTest = test . teardown
117
+ }
139
118
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
119
+ const cleanPath = file . slice ( file . lastIndexOf ( apiName ) )
120
+
121
+ // skip if --suite CLI arg doesn't match
122
+ if ( options . suite && ! cleanPath . endsWith ( options . suite ) ) continue
123
+
124
+ const junitTestSuite = junitTestSuites . testsuite ( apiName . slice ( 1 ) + ' - ' + cleanPath )
125
+
126
+ for ( const test of tests ) {
127
+ const testTime = now ( )
128
+ const name = Object . keys ( test ) [ 0 ]
129
+
130
+ // skip setups, teardowns and anything that doesn't match --test flag when present
131
+ if ( name === 'setup' || name === 'teardown' ) continue
132
+ if ( options . test && ! name . endsWith ( options . test ) ) continue
133
+
134
+ const junitTestCase = junitTestSuite . testcase ( name , `node_${ process . version } /${ cleanPath } ` )
135
+
136
+ stats . total += 1
137
+ log ( ' - ' + name )
138
+ try {
139
+ await testRunner . run ( setupTest , test [ name ] , teardownTest , stats , junitTestCase )
140
+ stats . pass += 1
141
+ } catch ( err ) {
142
+ junitTestCase . failure ( err )
178
143
junitTestCase . end ( )
179
- if ( totalTestTime > MAX_TEST_TIME ) {
180
- log ( ' took too long: ' + ms ( totalTestTime ) )
144
+ junitTestSuite . end ( )
145
+ junitTestSuites . end ( )
146
+ generateJunitXmlReport ( junit , 'serverless' )
147
+ console . error ( err )
148
+
149
+ if ( options . bail ) {
150
+ process . exit ( 1 )
181
151
} else {
182
- log ( ' took: ' + ms ( totalTestTime ) )
152
+ continue
183
153
}
184
154
}
185
- junitTestSuite . end ( )
186
- const totalFileTime = now ( ) - fileTime
187
- if ( totalFileTime > MAX_FILE_TIME ) {
188
- log ( ` ${ cleanPath } took too long: ` + ms ( totalFileTime ) )
155
+ const totalTestTime = now ( ) - testTime
156
+ junitTestCase . end ( )
157
+ if ( totalTestTime > MAX_TEST_TIME ) {
158
+ log ( ' took too long: ' + ms ( totalTestTime ) )
189
159
} else {
190
- log ( ` ${ cleanPath } took: ` + ms ( totalFileTime ) )
160
+ log ( ' took: ' + ms ( totalTestTime ) )
191
161
}
192
162
}
193
- const totalApiTime = now ( ) - apiTime
194
- if ( totalApiTime > MAX_API_TIME ) {
195
- log ( `${ apiName } took too long: ` + ms ( totalApiTime ) )
163
+ junitTestSuite . end ( )
164
+ const totalFileTime = now ( ) - fileTime
165
+ if ( totalFileTime > MAX_FILE_TIME ) {
166
+ log ( ` ${ cleanPath } took too long: ` + ms ( totalFileTime ) )
196
167
} else {
197
- log ( `${ apiName } took: ` + ms ( totalApiTime ) )
168
+ log ( ` ${ cleanPath } took: ` + ms ( totalFileTime ) )
198
169
}
199
170
}
200
171
junitTestSuites . end ( )
0 commit comments