Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
Clean up getTests function, remove async/await library
Browse files Browse the repository at this point in the history
  • Loading branch information
whymarrh committed Mar 15, 2019
1 parent c34bb57 commit e7b9106
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
71 changes: 43 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
const fs = require('fs')
const dir = require('node-dir')
const path = require('path')
var asyncFromLib = require('asyncawait/async')
var awaitFromLib = require('asyncawait/await')

/**
* Runs a battery of tests
* @method runTests
* @param {Function} runner the test runner
* @param {Object} tests the tests usally fetched using `getTests`
* @param {Function} filter to enable test skipping, called with skipFn(index, testName, testData)
*/
const getTests = exports.getTests = (testType, onFile, fileFilter = /.json$/, skipFn = () => {
return false
}, testDir = '', excludeDir = '', testsPath = __dirname + '/tests') => { // eslint-disable-line
const falsePredicate = () => false
const defaultTestsPath = path.join(__dirname, 'tests')

const getTests = exports.getTests = async (
testType,
onFile,
fileFilter = /.json$/,
skipPredicate = falsePredicate,
testDir = '',
excludeDir = '',
testsPath = defaultTestsPath
) => {
const directory = path.join(testsPath, testType, testDir)
const options = {
match: fileFilter,
excludeDir: excludeDir
}

return new Promise((resolve, reject) => {
dir.readFiles(path.join(testsPath, testType, testDir), {
match: fileFilter,
excludeDir: excludeDir
}, asyncFromLib((err, content, fileName, next) => {
if (err) reject(err)

fileName = path.parse(fileName).name
const tests = JSON.parse(content)

for (let testName in tests) {
if (!skipFn(testName)) {
awaitFromLib(onFile(fileName, testName, tests[testName]))
const finishedCallback = (err, files) => {
if (err) {
reject(err)
return
}

resolve(files)
}

const fileCallback = async (err, content, fileName, next) => {
if (err) {
reject(err)
return
}

const parsedFileName = path.parse(fileName).name
const testsByName = JSON.parse(content)
const testNames = Object.keys(testsByName)
for (const testName of testNames) {
if (!skipPredicate(testName)) {
await onFile(parsedFileName, testName, testsByName[testName])
}
}

next()
}), (err, files) => {
if (err) reject(err)
resolve(files)
})
}

dir.readFiles(directory, options, fileCallback, finishedCallback)
})
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"author": "mjbecze <mjbecze@gmail.com>",
"license": "MPL-2.0",
"dependencies": {
"asyncawait": "^1.0.6",
"node-dir": "^0.1.16"
},
"devDependencies": {
Expand Down

0 comments on commit e7b9106

Please sign in to comment.