forked from avajs/ava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
318 lines (273 loc) · 9.88 KB
/
api.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
const commonPathPrefix = require('common-path-prefix');
const escapeStringRegexp = require('escape-string-regexp');
const uniqueTempDir = require('unique-temp-dir');
const isCi = require('is-ci');
const resolveCwd = require('resolve-cwd');
const debounce = require('lodash.debounce');
const Bluebird = require('bluebird');
const getPort = require('get-port');
const arrify = require('arrify');
const makeDir = require('make-dir');
const ms = require('ms');
const babelPipeline = require('./lib/babel-pipeline');
const Emittery = require('./lib/emittery');
const RunStatus = require('./lib/run-status');
const AvaFiles = require('./lib/ava-files');
const fork = require('./lib/fork');
const serializeError = require('./lib/serialize-error');
function resolveModules(modules) {
return arrify(modules).map(name => {
const modulePath = resolveCwd.silent(name);
if (modulePath === null) {
throw new Error(`Could not resolve required module '${name}'`);
}
return modulePath;
});
}
class Api extends Emittery {
constructor(options) {
super();
this.options = Object.assign({match: []}, options);
this.options.require = resolveModules(this.options.require);
this._allExtensions = this.options.extensions.all;
this._regexpFullExtensions = new RegExp(`\\.(${this.options.extensions.full.map(ext => escapeStringRegexp(ext)).join('|')})$`);
this._precompiler = null;
}
run(files, runtimeOptions) {
const apiOptions = this.options;
runtimeOptions = runtimeOptions || {};
// Each run will have its own status. It can only be created when test files
// have been found.
let runStatus;
// Irrespectively, perform some setup now, before finding test files.
// Track active forks and manage timeouts.
const failFast = apiOptions.failFast === true;
let bailed = false;
const pendingWorkers = new Set();
const timedOutWorkerFiles = new Set();
let restartTimer;
if (apiOptions.timeout) {
const timeout = ms(apiOptions.timeout);
restartTimer = debounce(() => {
// If failFast is active, prevent new test files from running after
// the current ones are exited.
if (failFast) {
bailed = true;
}
for (const worker of pendingWorkers) {
timedOutWorkerFiles.add(worker.file);
worker.exit();
}
runStatus.emitStateChange({type: 'timeout', period: timeout});
}, timeout);
} else {
restartTimer = Object.assign(() => {}, {cancel() {}});
}
// Find all test files.
return new AvaFiles({cwd: apiOptions.resolveTestsFrom, files, extensions: this._allExtensions}).findTestFiles()
.then(files => {
if (this.options.parallelRuns) {
// The files must be in the same order across all runs, so sort them.
files = files.sort((a, b) => a.localeCompare(b, [], {numeric: true}));
const {currentIndex, totalRuns} = this.options.parallelRuns;
const fileCount = files.length;
const each = Math.floor(fileCount / totalRuns);
const remainder = fileCount % totalRuns;
const offset = Math.min(currentIndex, remainder) + (currentIndex * each);
const currentFileCount = each + (currentIndex < remainder ? 1 : 0);
files = files.slice(offset, offset + currentFileCount);
runStatus = new RunStatus(fileCount, {currentFileCount, currentIndex, totalRuns});
} else {
runStatus = new RunStatus(files.length, null);
}
const emittedRun = this.emit('run', {
clearLogOnNextRun: runtimeOptions.clearLogOnNextRun === true,
failFastEnabled: failFast,
filePathPrefix: commonPathPrefix(files),
files,
matching: apiOptions.match.length > 0,
previousFailures: runtimeOptions.previousFailures || 0,
runOnlyExclusive: runtimeOptions.runOnlyExclusive === true,
runVector: runtimeOptions.runVector || 0,
status: runStatus
});
// Bail out early if no files were found.
if (files.length === 0) {
return emittedRun.then(() => {
return runStatus;
});
}
runStatus.on('stateChange', record => {
if (record.testFile && !timedOutWorkerFiles.has(record.testFile)) {
// Restart the timer whenever there is activity from workers that
// haven't already timed out.
restartTimer();
}
if (failFast && (record.type === 'hook-failed' || record.type === 'test-failed' || record.type === 'worker-failed')) {
// Prevent new test files from running once a test has failed.
bailed = true;
// Try to stop currently scheduled tests.
for (const worker of pendingWorkers) {
worker.notifyOfPeerFailure();
}
}
});
return emittedRun
.then(() => this._setupPrecompiler())
.then(precompilation => {
if (!precompilation.enabled) {
return null;
}
// Compile all test and helper files. Assumes the tests only load
// helpers from within the `resolveTestsFrom` directory. Without
// arguments this is the `projectDir`, else it's `process.cwd()`
// which may be nested too deeply.
return new AvaFiles({cwd: this.options.resolveTestsFrom, extensions: this._allExtensions})
.findTestHelpers().then(helpers => {
return {
cacheDir: precompilation.cacheDir,
map: [...files, ...helpers].reduce((acc, file) => {
try {
const realpath = fs.realpathSync(file);
const filename = path.basename(realpath);
const cachePath = this._regexpFullExtensions.test(filename) ?
precompilation.precompileFull(realpath) :
precompilation.precompileEnhancementsOnly(realpath);
if (cachePath) {
acc[realpath] = cachePath;
}
} catch (err) {
throw Object.assign(err, {file});
}
return acc;
}, {})
};
});
})
.then(precompilation => {
// Resolve the correct concurrency value.
let concurrency = Math.min(os.cpus().length, isCi ? 2 : Infinity);
if (apiOptions.concurrency > 0) {
concurrency = apiOptions.concurrency;
}
if (apiOptions.serial) {
concurrency = 1;
}
// Try and run each file, limited by `concurrency`.
return Bluebird.map(files, file => {
// No new files should be run once a test has timed out or failed,
// and failFast is enabled.
if (bailed) {
return;
}
return this._computeForkExecArgv().then(execArgv => {
const options = Object.assign({}, apiOptions, {
// If we're looking for matches, run every single test process in exclusive-only mode
runOnlyExclusive: apiOptions.match.length > 0 || runtimeOptions.runOnlyExclusive === true
});
if (precompilation) {
options.cacheDir = precompilation.cacheDir;
options.precompiled = precompilation.map;
} else {
options.precompiled = {};
}
if (runtimeOptions.updateSnapshots) {
// Don't use in Object.assign() since it'll override options.updateSnapshots even when false.
options.updateSnapshots = true;
}
const worker = fork(file, options, execArgv);
runStatus.observeWorker(worker, file);
pendingWorkers.add(worker);
worker.promise.then(() => { // eslint-disable-line max-nested-callbacks
pendingWorkers.delete(worker);
});
restartTimer();
return worker.promise;
});
}, {concurrency});
})
.catch(err => {
runStatus.emitStateChange({type: 'internal-error', err: serializeError('Internal error', false, err)});
})
.then(() => {
restartTimer.cancel();
return runStatus;
});
});
}
_setupPrecompiler() {
if (this._precompiler) {
return this._precompiler;
}
const cacheDir = this.options.cacheEnabled === false ?
uniqueTempDir() :
path.join(this.options.projectDir, 'node_modules', '.cache', 'ava');
// Ensure cacheDir exists
makeDir.sync(cacheDir);
const {projectDir, babelConfig} = this.options;
const compileEnhancements = this.options.compileEnhancements !== false;
const precompileFull = babelConfig ?
babelPipeline.build(projectDir, cacheDir, babelConfig, compileEnhancements) :
filename => {
throw new Error(`Cannot apply full precompilation, possible bad usage: ${filename}`);
};
let precompileEnhancementsOnly = () => null;
if (compileEnhancements) {
precompileEnhancementsOnly = this.options.extensions.enhancementsOnly.length > 0 ?
babelPipeline.build(projectDir, cacheDir, null, compileEnhancements) :
filename => {
throw new Error(`Cannot apply enhancement-only precompilation, possible bad usage: ${filename}`);
};
}
this._precompiler = {
cacheDir,
enabled: babelConfig || compileEnhancements,
precompileEnhancementsOnly,
precompileFull
};
return this._precompiler;
}
_computeForkExecArgv() {
const execArgv = this.options.testOnlyExecArgv || process.execArgv;
if (execArgv.length === 0) {
return Promise.resolve(execArgv);
}
let debugArgIndex = -1;
// --inspect-brk is used in addition to --inspect to break on first line and wait
execArgv.some((arg, index) => {
const isDebugArg = /^--inspect(-brk)?($|=)/.test(arg);
if (isDebugArg) {
debugArgIndex = index;
}
return isDebugArg;
});
const isInspect = debugArgIndex >= 0;
if (!isInspect) {
execArgv.some((arg, index) => {
const isDebugArg = /^--debug(-brk)?($|=)/.test(arg);
if (isDebugArg) {
debugArgIndex = index;
}
return isDebugArg;
});
}
if (debugArgIndex === -1) {
return Promise.resolve(execArgv);
}
return getPort().then(port => {
const forkExecArgv = execArgv.slice();
let flagName = isInspect ? '--inspect' : '--debug';
const oldValue = forkExecArgv[debugArgIndex];
if (oldValue.indexOf('brk') > 0) {
flagName += '-brk';
}
forkExecArgv[debugArgIndex] = `${flagName}=${port}`;
return forkExecArgv;
});
}
}
module.exports = Api;