Skip to content

Commit aa86d83

Browse files
committed
Simplify integration test runner hierarchy
We were organizing into APIs and files, but we can just run a flat array of test files.
1 parent d65940f commit aa86d83

File tree

2 files changed

+76
-104
lines changed

2 files changed

+76
-104
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"chai": "^4.3.10",
6060
"cross-zip": "^4.0.0",
6161
"desm": "^1.3.0",
62+
"globby": "^13.2.2",
6263
"js-yaml": "^4.1.0",
6364
"license-checker": "^25.0.1",
6465
"node-fetch": "^2.7.0",

test/integration/index.js

Lines changed: 75 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const { join, sep } = require('path')
2929
const yaml = require('js-yaml')
3030
const minimist = require('minimist')
3131
const ms = require('ms')
32+
const globby = require('globby')
3233
const { Client } = require('../../index')
3334
const build = require('./test-runner')
3435
const createJunitReporter = require('./reporter')
@@ -45,18 +46,13 @@ const options = minimist(process.argv.slice(2), {
4546
string: ['suite', 'test'],
4647
})
4748

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()
6056
}
6157

6258
function runner (opts = {}) {
@@ -89,112 +85,87 @@ async function start ({ client }) {
8985
pass: 0,
9086
assertions: 0
9187
}
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)
10689

10790
const totalTime = now()
108-
for (const folder of folders) {
91+
for (const file of files) {
10992
// pretty name
110-
const apiName = folder[0].split(`${sep}tests${sep}`)[1].split(sep)[0]
93+
const apiName = file.split(`${sep}tests${sep}`)[1]
11194

11295
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+
}
139118

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)
178143
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)
181151
} else {
182-
log(' took: ' + ms(totalTestTime))
152+
continue
183153
}
184154
}
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))
189159
} else {
190-
log(` ${cleanPath} took: ` + ms(totalFileTime))
160+
log(' took: ' + ms(totalTestTime))
191161
}
192162
}
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))
196167
} else {
197-
log(`${apiName} took: ` + ms(totalApiTime))
168+
log(` ${cleanPath} took: ` + ms(totalFileTime))
198169
}
199170
}
200171
junitTestSuites.end()

0 commit comments

Comments
 (0)