-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
testRunner.js
168 lines (151 loc) · 4.39 KB
/
testRunner.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
166
167
168
#!/usr/bin/env node
// @ts-check
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var readfiles = require('node-readfiles');
var mdv = require('./index.js');
var argv = require('yargs')
.usage('testRunner [options] [{path-to-specs}...]')
.demand(1)
.boolean('debug')
.alias('d','debug')
.describe('debug','pass debug flag to mdv')
.string('encoding')
.alias('e','encoding')
.default('encoding','utf8')
.describe('encoding','encoding for input/output files')
.string('fail')
.describe('fail','path to specs expected to fail')
.alias('f','fail')
.boolean('quiet')
.alias('q','quiet')
.describe('quiet','do not show test passes on console, for CI')
.boolean('stop')
.alias('s','stop')
.describe('stop','stop on first error')
.count('verbose')
.alias('v','verbose')
.describe('verbose','increase verbosity')
.help('h')
.alias('h', 'help')
.strict()
.version()
.argv;
var red = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[31m';
var green = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[32m';
var yellow = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[33;1m';
var normal = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[0m';
var pass = 0;
var fail = 0;
var failures = [];
var warnings = [];
var options = argv;
function checkOutput(output) {
var result = true;
for (var k in output) {
if (typeof output[k] === 'number') {
if (output[k] > 0) result = false;
}
if (Array.isArray(output[k])) {
if (output[k].length > 0) result = false;
}
}
return result;
}
function check(file,force,expectFailure) {
let result = false;
options.expectFailure = expectFailure;
options.source = file;
let components = file.split(path.sep);
let name = components[components.length-1];
if ((name.endsWith('.md')) || force) {
options.warnings = name.startsWith('warn-');
var srcStr = fs.readFileSync(path.resolve(file),options.encoding);
var output = {failed:true};
result = false;
try {
output = mdv.validate(srcStr,options);
result = checkOutput(output);
}
catch (ex) {
console.log(red+'mdv threw an error: '+ex.message);
warnings.push('mdv threw '+options.source);
result = false;
}
console.log((result ? green : red)+file);
if (!result) {
console.log(util.inspect(output));
}
if (expectFailure) result = !result;
if (result) {
pass++;
}
else {
fail++;
}
}
else {
result = true;
}
return result;
}
function processPathSpec(pathspec,expectFailure) {
pathspec = path.resolve(pathspec);
var stats = fs.statSync(pathspec);
if (stats.isFile()) {
check(pathspec,true,expectFailure);
}
else {
readfiles(pathspec, {readContents: false, filenameFormat: readfiles.FULL_PATH}, function (err) {
if (err) console.log(util.inspect(err));
})
.then(files => {
files = files.sort(function(a,b){
if (a<b) return +1;
if (a>b) return -1;
return 0;
});
for (var file of files) {
check(file,false,expectFailure);
}
})
.catch(err => {
console.log(util.inspect(err));
});
}
}
process.exitCode = 1;
console.log('Gathering...');
if ((!argv._.length) && (!argv.fail)) {
argv._.push('test/pass/');
}
for (let pathspec of argv._) {
processPathSpec(pathspec,false);
}
if (argv.fail) {
if (!Array.isArray(argv.fail)) argv.fail = [argv.fail];
for (let pathspec of argv.fail) {
processPathSpec(pathspec,true);
}
}
process.on('exit', function() {
if (warnings.length) {
warnings.sort();
console.log(normal+'\nWarnings:'+yellow);
for (var w in warnings) {
console.log(warnings[w]);
}
}
if (failures.length) {
failures.sort();
console.log(normal+'\nFailures:'+red);
for (var f in failures) {
console.log(failures[f]);
}
}
console.log(normal);
console.log('Tests: %s passing, %s failing, %s warnings', pass, fail, warnings.length);
process.exitCode = ((fail === 0) && (pass > 0)) ? 0 : 1;
});