-
Notifications
You must be signed in to change notification settings - Fork 10
/
kni-test.js
156 lines (141 loc) · 4.65 KB
/
kni-test.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
'use strict';
var fs = require('fs');
var rimraf = require('rimraf'); // TODO fs.rm in node v14
var runKni = require('./kni');
var stripAnsi = require('strip-ansi');
// TODO better file diffing
function diffFiles(a, b, done) {
fs.readFile(a, 'utf8', function aRead(err, aData) {
if (err) {
done(err, null);
return;
}
fs.readFile(b, 'utf8', function bRead(err, bData) {
if (err) {
done(err, null);
return;
}
aData = stripAnsi(aData);
bData = stripAnsi(bData);
done(null, {
same: aData == bData,
aData: aData,
bData: bData,
});
});
});
}
function runArgs(args, outfile, done) {
var out = fs.createWriteStream(outfile);
runKni(args, out, function runDone(err) {
done(err ? new Error(JSON.stringify(args) + ' failed: ' + err) : null);
});
}
function withTempDir(name, fn, done) {
var cleaned = name.replace(/[^\w.]+/, '_');
fs.mkdtemp(cleaned + '-', function maybeTempDir(err, dir) {
if (err) {
done(err);
return;
}
fn(dir, function funDone(err) {
rimraf(dir, fs, function rmDone(rmErr) {
done(err || rmErr);
});
});
});
}
function testBasic(kniscript, transcript, done) {
withTempDir(transcript, function under(dir, fin) {
var outfile = dir + '/out';
runArgs([kniscript, '-v', transcript], outfile, fin);
}, done);
}
function testDescribe(kniscript, descript, done) {
withTempDir(descript, function under(dir, fin) {
var outfile = dir + '/out';
runArgs([kniscript, '-d'], outfile, function runDone(err) {
if (err) {
fin(err);
return;
}
diffFiles(descript, outfile, function diffed(err, res) {
if (err) {
fin(err);
return;
}
if (!res.same) {
console.log('aBytes', res.aData.length);
console.log('bBytes', res.bData.length);
console.log('aLines', res.aData.split(/\n/));
console.log('bLines', res.bData.split(/\n/));
fin(new Error('output does not match'));
return;
}
fin(null);
});
});
}, done);
}
function main() {
fs.readdir('tests', function(err, files) {
if (err) {
process.exitCode |= 1;
console.error('unable to read tests dir');
return;
}
function kniFor(somescript) {
var match = /(.+)\./.exec(somescript);
var nom = match && match[1];
if (!nom) {
return '';
}
if (nom == 'hello') {
return 'hello.kni';
}
return 'examples/' + nom + '.kni';
}
// description tests
files
.map(function(file) {
var kniscript = kniFor(file);
if (!kniscript || !/\.desc$/.test(file)) {
return null;
}
return [kniscript, 'tests/' + file];
})
.filter(function(testCase) { return testCase != null })
.forEach(function eachTestCase(testCase) {
var kniscript = testCase[0];
var descript = testCase[1];
testDescribe(kniscript, descript, function testRunDone(err) {
if (err) {
process.exitCode |= 1;
console.log('FAIL', 'describe', kniscript, descript, err);
}
});
});
// verification tests
[
// TODO also derive from files and/or reconcile with engine-test
['basic', testBasic],
].forEach(function eachTestMode(testMode) {
var testModeName = testMode[0];
var runTest = testMode[1];
[
// TODO reconcile table with engine-test.js
['hello.kni', 'tests/hello.1'],
].forEach(function eachTestCase(testCase) {
var kniscript = testCase[0];
var transcript = testCase[1];
runTest(kniscript, transcript, function testRunDone(err) {
if (err) {
process.exitCode |= 1;
console.log('FAIL', testModeName, kniscript, transcript, err);
}
});
});
});
});
}
main();