Skip to content

Commit

Permalink
Refactor out the directory crawler from the test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Sep 8, 2019
1 parent ad56f38 commit d6ce2fa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import run from './runner.mjs'
import concise from './report-concise.mjs'
import verbose from './report-verbose.mjs'
import crawl from './crawler.mjs'

run('./test/', process.env.CI ? verbose : concise)
crawl('./test/')
.then(files => run(files, process.env.CI ? verbose : concise))
.then(({ passed, failed }) => failed ? process.exit(1) : process.exit(0))
.catch(err => {
process.stderr.write(err.toString())
Expand Down
10 changes: 10 additions & 0 deletions crawler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from 'fs'
import util from 'util'
import path from 'path'

const readdir = util.promisify(fs.readdir);

export default function crawl(dir){
return readdir(dir)
.then(files => files.map(file => path.resolve(dir, file)))
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"files": [
"batch-promise.mjs",
"cli.mjs",
"crawler.mjs",
"oletus.mjs",
"report-concise.mjs",
"report-verbose.mjs",
Expand Down
39 changes: 15 additions & 24 deletions runner.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import fs from 'fs'
import childProcess from 'child_process'
import perfHooks from 'perf_hooks'
import os from 'os'
import batchPromise from './batch-promise.mjs'

export default function run (testDirectory, report = () => {}) {
export default function run (files, report = () => {}) {
const timestamp = perfHooks.performance.now()
let passed = 0
let failed = 0

return new Promise((resolve, reject) => {
fs.readdir(testDirectory, (err, files) => {
if (err) reject(err)
return batchPromise(files, os.cpus().length, file => {
return new Promise(resolve => {
const forked = childProcess.fork(file)

let passed = 0
let failed = 0

batchPromise(files, os.cpus().length, file => {
return new Promise(resolve => {
const forked = childProcess.fork(`${testDirectory}${file}`)

forked.on('message', ({ didPass, title, location, message }) => {
didPass ? passed++ : failed++
report({ didPass, title, location, message, timestamp, file, passed, failed, isComplete: false })
})

forked.on('close', () => resolve())
})
forked.on('message', ({ didPass, title, location, message }) => {
didPass ? passed++ : failed++
report({ didPass, title, location, message, timestamp, file, passed, failed, isComplete: false })
})
.then(() => {
report({ timestamp, passed, failed, isComplete: true })
resolve({ passed, failed })
})
.catch(err => reject(err))

forked.on('close', () => resolve())
})
})
.then(() => {
report({ timestamp, passed, failed, isComplete: true })
return { passed, failed }
})
}
4 changes: 3 additions & 1 deletion test/run.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import run from '../runner.mjs'
import crawl from '../crawler.mjs'
import assert from 'assert'

function fail (e) {
console.error(e.toString())
process.exit(1)
}

run('./test/fixtures/')
crawl('./test/fixtures/')
.then(run)
.then(({ passed, failed }) => {
assert.strict.equal(passed, 6)
assert.strict.equal(failed, 4)
Expand Down

0 comments on commit d6ce2fa

Please sign in to comment.