-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (105 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
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]))
}
}
next()
}), (err, files) => {
if (err) reject(err)
resolve(files)
})
})
}
function skipTest (testName, skipList = []) {
return skipList.map((skipName) => (new RegExp(`^${skipName}`)).test(testName)).some(isMatch => isMatch)
}
/**
* Loads a single test specified in a file
* @method getTestFromSource
* @param {String} file or path to load a single test from
* @param {Function} Callback function which is invoked, and passed the contents of the specified file (or an error message)
*/
const getTestFromSource = exports.getTestFromSource = function (file, onFile) {
let stream = fs.createReadStream(file)
let contents = ''
let test = null
stream.on('data', function (data) {
contents += data
}).on('error', function (err) {
onFile(err)
}).on('end', function () {
try {
test = JSON.parse(contents)
} catch (e) {
onFile(e)
}
let testName = Object.keys(test)[0]
let testData = test[testName]
testData.testName = testName
onFile(null, testData)
})
}
exports.getTestsFromArgs = function (testType, onFile, args = {}) {
let testsPath, testDir, fileFilter, excludeDir, skipFn
skipFn = (name) => {
return skipTest(name, args.skipTests)
}
if (testType === 'BlockchainTests') {
const forkFilter = new RegExp(`${args.forkConfig}$`)
skipFn = (name) => {
return ((forkFilter.test(name) === false) || skipTest(name, args.skipTests))
}
}
if (testType === 'VMTests') {
skipFn = (name) => {
return skipTest(name, args.skipVM)
}
}
if (args.singleSource) {
return getTestFromSource(args.singleSource, onFile)
}
if (args.dir) {
testDir = args.dir
}
if (args.file) {
fileFilter = new RegExp(args.file)
}
if (args.excludeDir) {
excludeDir = new RegExp(args.excludeDir)
}
if (args.test) {
skipFn = (testName) => {
return testName !== args.test
}
}
if (args.testsPath) {
testsPath = args.testsPath
}
return getTests(testType, onFile, fileFilter, skipFn, testDir, excludeDir, testsPath)
}
exports.getSingleFile = (file) => {
return require(path.join(__dirname, 'tests', file))
}