-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cli.js
157 lines (136 loc) 路 3.96 KB
/
cli.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
'use strict';
var path = require('path');
var childProcess = require('child_process');
var test = require('tap').test;
global.Promise = require('bluebird');
var getStream = require('get-stream');
var arrify = require('arrify');
var touch = require('touch');
var cliPath = path.join(__dirname, '../cli.js');
function execCli(args, dirname, cb) {
if (typeof dirname === 'function') {
cb = dirname;
dirname = __dirname;
} else {
dirname = path.join(__dirname, dirname);
}
var env = {};
if (process.env.AVA_APPVEYOR) {
env.AVA_APPVEYOR = 1;
}
var child;
var stdout;
var stderr;
var processPromise = new Promise(function (resolve) {
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(arrify(args)), {
cwd: dirname,
env: env,
stdio: [null, 'pipe', 'pipe']
});
child.on('close', function (code, signal) {
if (code) {
var err = new Error('test-worker exited with a non-zero exit code: ' + code);
err.code = code;
err.signal = signal;
resolve(err);
return;
}
resolve(code);
});
stdout = getStream(child.stdout);
stderr = getStream(child.stderr);
});
Promise.all([processPromise, stdout, stderr]).then(function (args) {
cb.apply(null, args);
});
return child;
}
test('throwing a named function will report the to the console', function (t) {
execCli('fixture/throw-named-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: fooFn]/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
test('babel require hook only applies to the test file', function (t) {
t.plan(3);
execCli('fixture/babel-hook.js', function (err, stdout, stderr) {
t.ok(err);
t.is(err.code, 1);
t.match(stderr, /Unexpected token/);
t.end();
});
});
test('throwing a anonymous function will report the function to the console', function (t) {
execCli('fixture/throw-anonymous-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: anonymous]/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
});
});
test('log failed tests', function (t) {
execCli('fixture/one-pass-one-fail.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /AssertionError: false == true/);
t.end();
});
});
test('pkg-conf: defaults', function (t) {
execCli([], 'fixture/pkg-conf/defaults', function (err) {
t.ifError(err);
t.end();
});
});
test('pkg-conf: pkg-overrides', function (t) {
execCli([], 'fixture/pkg-conf/pkg-overrides', function (err) {
t.ifError(err);
t.end();
});
});
test('pkg-conf: cli takes precedence', function (t) {
execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], 'fixture/pkg-conf/precedence', function (err) {
t.ifError(err);
t.end();
});
});
test('watcher works', function (t) {
var killed = false;
var hasChokidar = false;
try {
require('chokidar');
hasChokidar = true;
} catch (err) {}
var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) {
if (err && err.code === 1 && !hasChokidar) {
t.comment('chokidar dependency is missing, cannot test watcher');
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
t.end();
} else {
t.ok(killed);
t.ifError(err);
t.end();
}
});
var buffer = '';
var passedFirst = false;
// Pause the stream before attaching the 'data' listener. execCli() uses
// get-stream which read()s from the stream. The test just needs to piggyback
// on that without switching the stream to flowing mode.
child.stderr.pause().on('data', function (str) {
buffer += str;
if (/1 test passed/.test(str)) {
if (!passedFirst) {
touch.sync(path.join(__dirname, 'fixture/watcher/test.js'));
buffer = '';
passedFirst = true;
} else if (!killed) {
child.kill();
killed = true;
}
}
});
});