/
fork.js
139 lines (114 loc) 路 3.45 KB
/
fork.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
'use strict';
const childProcess = require('child_process');
const path = require('path');
const fs = require('fs');
const Promise = require('bluebird');
const Emittery = require('emittery');
if (fs.realpathSync(__filename) !== __filename) {
console.warn('WARNING: `npm link ava` and the `--preserve-symlink` flag are incompatible. We have detected that AVA is linked via `npm link`, and that you are using either an early version of Node 6, or the `--preserve-symlink` flag. This breaks AVA. You should upgrade to Node 6.2.0+, avoid the `--preserve-symlink` flag, or avoid using `npm link ava`.');
}
// In case the test file imports a different AVA install,
// the presence of this variable allows it to require this one instead
const AVA_PATH = path.resolve(__dirname, '..');
// Ensure NODE_PATH paths are absolute
let NODE_PATH;
if (process.env.NODE_PATH) {
NODE_PATH = process.env.NODE_PATH
.split(path.delimiter)
.map(x => path.resolve(x))
.join(path.delimiter);
}
const describeTTY = tty => ({
colorDepth: tty.getColorDepth ? tty.getColorDepth() : undefined,
hasColors: typeof tty.hasColors === 'function',
columns: tty.columns || 80,
rows: tty.rows
});
const workerPath = require.resolve('./worker/subprocess');
module.exports = (file, opts, execArgv) => {
let finished = false;
const emitter = new Emittery();
const emitStateChange = evt => {
if (!finished) {
emitter.emit('stateChange', Object.assign(evt, {testFile: file}));
}
};
opts = {
file,
baseDir: process.cwd(),
tty: {
stderr: process.stderr.isTTY ? describeTTY(process.stderr) : false,
stdout: process.stdout.isTTY ? describeTTY(process.stdout) : false
},
...opts
};
const args = [opts.color ? '--color' : '--no-color'].concat(opts.workerArgv);
const subprocess = childProcess.fork(workerPath, args, {
cwd: opts.projectDir,
silent: true,
env: {NODE_ENV: 'test', ...process.env, ...opts.environmentVariables, AVA_PATH, NODE_PATH},
execArgv: execArgv || process.execArgv
});
subprocess.stdout.on('data', chunk => {
emitStateChange({type: 'worker-stdout', chunk});
});
subprocess.stderr.on('data', chunk => {
emitStateChange({type: 'worker-stderr', chunk});
});
let forcedExit = false;
const send = evt => {
if (subprocess.connected && !finished && !forcedExit) {
subprocess.send({ava: evt});
}
};
const promise = new Promise(resolve => {
const finish = () => {
finished = true;
resolve();
};
subprocess.on('message', message => {
if (!message.ava) {
return;
}
if (message.ava.type === 'ready-for-options') {
send({type: 'options', options: opts});
return;
}
if (message.ava.type === 'ping') {
send({type: 'pong'});
} else {
emitStateChange(message.ava);
}
});
subprocess.on('error', err => {
emitStateChange({type: 'worker-failed', err});
finish();
});
subprocess.on('exit', (code, signal) => {
if (forcedExit) {
emitStateChange({type: 'worker-finished', forcedExit});
} else if (code > 0) {
emitStateChange({type: 'worker-failed', nonZeroExitCode: code});
} else if (code === null && signal) {
emitStateChange({type: 'worker-failed', signal});
} else {
emitStateChange({type: 'worker-finished', forcedExit});
}
finish();
});
});
return {
exit() {
forcedExit = true;
subprocess.kill();
},
notifyOfPeerFailure() {
send({type: 'peer-failed'});
},
onStateChange(listener) {
return emitter.on('stateChange', listener);
},
file,
promise
};
};