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

Clean up getTests function, remove async/await library #38

Merged
merged 6 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
language: node_js
node_js:
- "6"
- "8"
env:
- CXX=g++-4.8
addons:
apt:
sources:
Expand All @@ -14,15 +9,22 @@ addons:
env:
global:
- DISPLAY=:99.0
matrix:
- CXX=g++-4.8 TEST_SUITE=test
- CXX=g++-4.8
matrix:
fast_finish: true
allowed_failures:
- node_js: "node"
include:
- os: linux
node_js: "6"
env: CXX=g++-4.8 TEST_SUITE=lint
node_js: "8"
env: TEST_SUITE=lint
- os: linux
node_js: "8"
env: TEST_SUITE=test
- os: linux
node_js: "10"
env: TEST_SUITE=test
- os: linux
node_js: "6"
env: CXX=g++-4.8 TEST_SUITE=test
node_js: "node"
env: TEST_SUITE=test
script: npm run $TEST_SUITE
78 changes: 52 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
const fs = require('fs')
const dir = require('node-dir')
const path = require('path')
var asyncFromLib = require('asyncawait/async')
var awaitFromLib = require('asyncawait/await')

const falsePredicate = () => false
const defaultTestsPath = path.join(__dirname, 'tests')

/**
* 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)
* Returns the list of test files matching the given parameters
* @param {string} testType the test type (path segment)
* @param {Function} onFile a callback for each file
* @param {RegExp|Array<string>} fileFilter a {@code RegExp} or array to specify filenames to operate on
* @param {Function<boolean>} skipPredicate a filtering function for test names
* @param {string} testDir the directory inside the {@code tests/} directory to use
* @param {RegExp|Array<string>} excludeDir a {@code RegExp} or array to specify directories to ignore
* @param {string} testsPath the path to the {@code tests/} directory
* @return {Promise<Array<string>>} the list of test files
*/
const getTests = exports.getTests = (testType, onFile, fileFilter = /.json$/, skipFn = () => {
return false
}, testDir = '', excludeDir = '', testsPath = __dirname + '/tests') => { // eslint-disable-line
const getTests = exports.getTests = (
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
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ tape('Test access tests', function (t) {
st.end()
})

t.test('should read tests', async function (st) {
const allTestFiles = await testing.getTests('BlockchainTests', () => {})
st.ok(allTestFiles.length !== 0, 'empty test list')
st.end()
})

t.test('should read tests from args', function (st) {
let args = {}
args.dir = 'GeneralStateTests/stCallCodes'
Expand Down