-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork.js
200 lines (158 loc) · 5.05 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
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
/*
Based on https://github.com/avajs/ava/blob/033d4dcdcbdadbf665c740ff450c2a775a8373dc/lib/fork.js
The MIT License (MIT)
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
'use strict';
const childProcess = require('child_process');
const path = require('path');
const fs = require('fs');
const Promise = require('bluebird');
const AvaError = require('ava/lib/ava-error');
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`.');
}
let env = process.env;
// Ensure NODE_PATH paths are absolute
if (env.NODE_PATH) {
env = Object.assign({}, env);
env.NODE_PATH = env.NODE_PATH
.split(path.delimiter)
.map(x => path.resolve(x))
.join(path.delimiter);
}
// In case the test file imports a different AVA install,
// the presence of this variable allows it to require this one instead
env.AVA_PATH = path.resolve(require.resolve('ava'), '..');
module.exports = (file, opts, execArgv) => {
opts = Object.assign({
file,
baseDir: process.cwd(),
tty: process.stdout.isTTY ? {
columns: process.stdout.columns,
rows: process.stdout.rows
} : false
}, opts);
const ps = childProcess.spawn(require('electron'), [require.resolve('./main-process/test-starter'), JSON.stringify(opts)], {
stdio: [null, null, null, 'ipc'],
cwd: opts.pkgDir,
silent: true,
env,
execArgv: execArgv || process.execArgv
});
const relFile = path.relative('.', file);
let exiting = false;
const send = (name, data) => {
if (!exiting) {
// This seems to trigger a Node bug which kills the AVA master process, at
// least while running AVA's tests. See
// <https://github.com/novemberborn/_ava-tap-crash> for more details.
ps.send({
name: `ava-${name}`,
data,
ava: true
});
}
};
const testResults = [];
let results;
const promise = new Promise((resolve, reject) => {
ps.on('error', reject);
// Emit `test` and `stats` events
ps.on('message', event => {
if (!event.ava) {
return;
}
event.name = event.name.replace(/^ava-/, '');
event.data.file = relFile;
ps.emit(event.name, event.data);
});
ps.on('test', props => {
testResults.push(props);
});
ps.on('results', data => {
results = data;
data.tests = testResults;
send('teardown');
});
ps.on('exit', (code, signal) => {
if (code > 0) {
return reject(new AvaError(`${relFile} exited with a non-zero exit code: ${code}`));
}
if (code === null && signal) {
return reject(new AvaError(`${relFile} exited due to ${signal}`));
}
if (results) {
resolve(results);
} else {
reject(new AvaError(`Test results were not received from ${relFile}`));
}
});
ps.on('no-tests', data => {
send('teardown');
let message = `No tests found in ${relFile}`;
if (!data.avaRequired) {
message += ', make sure to import "ava" at the top of your test file';
}
reject(new AvaError(message));
});
});
// Teardown finished, now exit
ps.on('teardown', () => {
send('exit');
exiting = true;
});
// Uncaught exception in fork, need to exit
ps.on('uncaughtException', () => {
send('teardown');
});
ps.stdout.on('data', data => {
ps.emit('stdout', data);
});
ps.stderr.on('data', data => {
ps.emit('stderr', data);
});
promise.on = function () {
ps.on.apply(ps, arguments);
return promise;
};
promise.send = (name, data) => {
send(name, data);
return promise;
};
promise.exit = () => {
send('init-exit');
return promise;
};
// Send 'run' event only when fork is listening for it
let isReady = false;
ps.on('stats', () => {
isReady = true;
});
promise.run = options => {
if (isReady) {
send('run', options);
return promise;
}
ps.on('stats', () => {
send('run', options);
});
return promise;
};
return promise;
};