-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·236 lines (191 loc) · 4.57 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var figures = require('figures');
var flatten = require('arr-flatten');
var globby = require('globby');
var meow = require('meow');
var updateNotifier = require('update-notifier');
var chalk = require('chalk');
var Promise = require('bluebird');
var fork = require('./lib/fork');
var log = require('./lib/logger');
// Bluebird specific
Promise.longStackTraces();
var cli = meow([
'Usage',
' ava [<file|folder|glob> ...]',
'',
'Options',
' --init Add AVA to your project',
' --fail-fast Stop after first test failure',
' --serial Run tests serially',
'',
'Examples',
' ava',
' ava test.js test2.js',
' ava test-*.js',
' ava --init',
' ava --init foo.js',
'',
'Default patterns when no arguments:',
'test.js test-*.js test/*.js'
], {
string: ['_'],
boolean: [
'fail-fast',
'serial'
]
});
var rejectionCount = 0;
var exceptionCount = 0;
var testCount = 0;
var fileCount = 0;
var errors = [];
function prefixTitle(file) {
var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';
var base = path.dirname(cli.input[0]);
if (base === '.') {
base = cli.input[0] || 'test';
}
base += path.sep;
var prefix = path.relative('.', file)
.replace(base, '')
.replace(/\.spec/, '')
.replace(/test\-/g, '')
.replace(/\.js$/, '')
.split(path.sep)
.join(separator);
if (prefix.length > 0) {
prefix += separator;
}
return prefix;
}
function stats(stats) {
testCount += stats.testCount;
}
function test(data) {
var isError = data.error.message;
if (fileCount > 1) {
data.title = prefixTitle(data.file) + data.title;
}
if (isError) {
log.error(data.title, chalk.red(data.error.message));
errors.push(data);
} else {
// don't log it if there's only one file and one anonymous test
if (fileCount === 1 && testCount === 1 && data.title === '[anonymous]') {
return;
}
log.test(data);
}
}
function run(file) {
var args = [file];
if (cli.flags.failFast) {
args.push('--fail-fast');
}
if (cli.flags.serial) {
args.push('--serial');
}
return fork(args)
.on('stats', stats)
.on('test', test)
.on('unhandledRejections', handleRejections)
.on('uncaughtException', handleExceptions)
.on('data', function (data) {
process.stdout.write(data);
});
}
function handleRejections(data) {
log.unhandledRejections(data.file, data.rejections);
rejectionCount += data.rejections.length;
}
function handleExceptions(data) {
log.uncaughtException(data.file, data.exception);
exceptionCount++;
}
function sum(arr, key) {
var result = 0;
arr.forEach(function (item) {
result += item[key];
});
return result;
}
function exit(results) {
// assemble stats from all tests
var stats = results.map(function (result) {
return result.stats;
});
var tests = results.map(function (result) {
return result.tests;
});
var passed = sum(stats, 'passCount');
var failed = sum(stats, 'failCount');
log.write();
log.report(passed, failed, rejectionCount, exceptionCount);
log.write();
if (failed > 0) {
log.errors(flatten(tests));
}
process.stdout.write('');
flushIoAndExit(failed > 0 || rejectionCount > 0 || exceptionCount > 0 ? 1 : 0);
}
function flushIoAndExit(code) {
// TODO: figure out why this needs to be here to
// correctly flush the output when multiple test files
process.stdout.write('');
process.stderr.write('');
// timeout required to correctly flush IO on Node.js 0.10 on Windows
setTimeout(function () {
process.exit(code);
}, process.env.AVA_APPVEYOR ? 500 : 0);
}
function init(files) {
log.write();
return handlePaths(files)
.map(function (file) {
return path.resolve('.', file);
})
.then(function (files) {
if (files.length === 0) {
log.error('Couldn\'t find any files to test\n');
process.exit(1);
}
fileCount = files.length;
var tests = files.map(run);
return Promise.all(tests);
});
}
function handlePaths(files) {
if (files.length === 0) {
files = [
'test.js',
'test-*.js',
'test/*.js'
];
}
// convert pinkie-promise to Bluebird promise
files = Promise.resolve(globby(files));
return files
.map(function (file) {
if (fs.statSync(file).isDirectory()) {
return handlePaths([path.join(file, '*.js')]);
}
return file;
})
.then(flatten)
.filter(function (file) {
return path.extname(file) === '.js' && path.basename(file)[0] !== '_';
});
}
updateNotifier({pkg: cli.pkg}).notify();
if (cli.flags.init) {
require('ava-init')();
} else {
init(cli.input).then(exit).catch(function (err) {
console.error(err.stack);
flushIoAndExit(1);
});
}