forked from workshopper/workshopper
-
Notifications
You must be signed in to change notification settings - Fork 5
/
verify.js
114 lines (94 loc) · 2.56 KB
/
verify.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
const spawn = require('child_process').spawn
, tuple = require('tuple-stream')
, through = require('through')
, split = require('split')
, path = require('path')
const wrap = require('./term-util').wrap
, red = require('./term-util').red
, green = require('./term-util').green
, yellow = require('./term-util').yellow
function verify (acmd, bcmd, opts) {
if (!opts) opts = {}
var a = spawn(process.execPath, acmd)
, b
, c
, tr
, kill = function () {
if (a && a.kill)
a.kill()
if (b && b.kill)
b.kill()
}
if (opts.run) {
;(opts.a || a.stdout).pipe(process.stdout)
;(opts.a || a.stdout).on('end', kill)
if (a.stderr) a.stderr.pipe(process.stderr)
return opts.a || a.stdin
}
b = spawn(process.execPath, bcmd)
c = compare(opts.a || a.stdout, opts.b || b.stdout, opts)
c.on('pass', function () {
kill()
tr.emit('pass')
})
c.on('fail', function () {
kill()
tr.emit('fail')
})
process.once('exit', function () {
kill()
});
tr = through()
tr.pipe(opts.a || a.stdin)
tr.pipe(opts.b || b.stdin)
return tr
}
function colourfn (type) {
return type == 'PASS' ? green : red
}
function compare (actual, expected, opts) {
var equal = true
, write = function (pair) {
var eq = pair[0] === pair[1]
equal = equal && eq
if (opts.long) {
this.queue('ACTUAL: '
+ colourfn(eq ? 'PASS' : 'FAIL')(JSON.stringify(pair[0]))
+ '\n'
+ 'EXPECTED: '
+ JSON.stringify(pair[1])
+ '\n\n'
)
} else {
this.queue(
colourfn(eq ? 'PASS' : 'FAIL')(
wrap(JSON.stringify(pair[0]), 30)
+ ' ' + (eq ? ' ' : '!=') + ' '
+ wrap(JSON.stringify(pair[1]), 30)
)
+ '\n'
)
}
}
, end = function () {
this.queue(null)
if (!equal)
return this.emit('fail')
if (typeof opts.custom != 'function')
return this.emit('pass')
opts.custom(function (err) {
this.emit(!err ? 'pass' : 'fail')
}.bind(this))
}
, output = through(write, end).pause()
if (!opts.long) {
output.queue(wrap('ACTUAL', 30) + ' EXPECTED\n')
output.queue(wrap('------', 30) + ' --------\n')
}
tuple(actual.pipe(split()), expected.pipe(split()))
.pipe(output)
.pipe(process.stdout)
output.resume()
return output
}
module.exports = verify