forked from ethereumjs/ethereumjs-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (126 loc) · 3.91 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs')
const dir = require('node-dir')
const path = require('path')
const falsePredicate = () => false
const defaultTestsPath = path.join(__dirname, 'tests')
/**
* 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$/,
skipPredicate = falsePredicate,
testDir = '',
excludeDir = '',
testsPath = defaultTestsPath
) => {
const directory = path.join(testsPath, testType, testDir)
const options = {
match: fileFilter,
excludeDir: excludeDir
}
return new Promise((resolve, reject) => {
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()
}
dir.readFiles(directory, options, fileCallback, finishedCallback)
})
}
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) {
const 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)
}
const testName = Object.keys(test)[0]
const 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))
}