-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_hobbits.js
executable file
·165 lines (138 loc) · 5.33 KB
/
test_hobbits.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
const { readdirSync, unlinkSync, existsSync } = require('fs')
const { join } = require('path')
const { execFileSync } = require("child_process");
const glob = require('glob');
const filecompare = require('filecompare');
const argv = require('yargs').command('* <hobbits_runner> [python_home] [-p plugin_path]', 'Tests hobbits processing with known input/output files for various batches', (yargs) => {
yargs.positional('hobbits_runner', {
describe: 'the path of the hobbits-runner binary you want to test',
type: 'string'
}).positional('python_home', {
describe: 'optional PYTHONHOME path to pass to hobbits_runner'
}).option(
"plugin_path", {
"alias": "p",
describe: 'optional extra plugin path to pass to hobbits_runner'
}
)
}).help().alias('help', 'h').argv;
async function runTests() {
let tests = 0;
let failures = 0;
let successes = 0;
let baseDir = join(__dirname, 'extracted_tests')
testDirs = readdirSync(baseDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
for (let testDir of testDirs) {
tests++
console.log(`Testing ${testDir}...`)
testDir = join(baseDir, testDir)
try {
let inputGlob = join(testDir, "input*.bits")
let inputMatches = glob.sync(inputGlob, {nonull: false})
if (inputMatches.length < 1) {
throw Error(`Failed to find input files matching pattern ${inputGlob}!`)
}
let outputGlob = join(testDir, "output.bits*")
let outputMatches = glob.sync(outputGlob, {nonull: false})
if (outputMatches.length < 1) {
throw Error(`Failed to find output files matching pattern ${outputGlob}!`)
}
let testOutputPrefix = join(testDir, "testrunoutput.")
let testOutputGlob = testOutputPrefix+"*"
let batchGlob = join(testDir, "*.hobbits_batch")
let batchMatches = glob.sync(batchGlob, {nonull: false})
if (batchMatches.length < 1) {
throw Error(`Failed to find a test batch file matching ${batchGlob}`)
}
let batch = batchMatches[0]
let testOutputMatches = glob.sync(testOutputGlob, {nonull: false})
for (let oldOutput of testOutputMatches) {
unlinkSync(oldOutput)
}
let args = [
'run',
'-b', batch,
'-o', testOutputPrefix,
'-platform', 'offscreen'
]
if (argv.python_home) {
args.push('--python-home')
args.push(argv.python_home)
}
if (argv.plugin_path) {
args.push('-p')
args.push(argv.plugin_path)
}
inputMatches.sort()
inputMatches.forEach(input => {
args.push('-i')
args.push(input)
})
execFileSync(argv.hobbits_runner, args, {stdio: 'inherit'});
testOutputMatches = glob.sync(testOutputGlob, {nonull: false})
if (testOutputMatches.length < 1) {
throw Error(`Failed to find an output file matching ${testOutputGlob}`)
}
let testOutputs = {}
let max = -1;
let lastOutput = testOutputMatches[0]
for (let outputFile of testOutputMatches) {
let thisVal = parseInt(outputFile.split(".")[1]);
if (thisVal > max) {
lastOutput = outputFile;
}
testOutputs[thisVal] = outputFile
}
let match = false
let outputIndexedGlob = join(testDir, "output.bits.*")
let outputIndexedMatches = glob.sync(outputIndexedGlob, {nonull: false})
if (outputIndexedMatches.length < 1) {
match = await new Promise((resolve, reject) => {
filecompare(outputMatches[0], lastOutput, isEqual => resolve(isEqual))
})
}
else {
for (let outputFile of outputIndexedMatches) {
let key = parseInt(outputFile.split(".")[2]);
match = await new Promise((resolve, reject) => {
filecompare(outputFile, testOutputs[key], isEqual => resolve(isEqual))
})
if (!match) {
break;
}
}
}
if (match) {
successes++;
console.log("=> PASS");
}
else {
failures++;
console.log("=> FAIL");
}
} catch (err) {
console.error(`=> ERROR: ${err}`)
failures++;
}
}
console.log(`Finished running ${tests} tests`)
console.log(`Successes: ${successes}`)
console.log(`Failures: ${failures}`)
if (failures > 0) {
return -1
}
if (tests < 1) {
console.log("Treating lack of tests as failure...")
return -1
}
return 0
}
runTests()
.then(val => process.exitCode = val)
.catch((err) => {
console.error(err)
process.exitCode = -1
})