forked from tnicola/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·242 lines (214 loc) · 6.12 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
237
238
239
240
241
242
#!/usr/bin/env node
const Table = require('cli-table');
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const yargs = require('yargs');
const { isYarn } = require('is-npm');
const WEIGHTS_JSON = 'cypress/parallel-weights.json';
const argv = yargs
.option('script', {
alias: 's',
type: 'string',
description: 'Your npm Cypress command'
})
.option('threads', {
alias: 't',
type: 'number',
description: 'Number of threads'
})
.option('specsDir', {
alias: 'd',
type: 'string',
description: 'Cypress specs directory'
})
.option('args', {
alias: 'a',
type: 'string',
description: 'Your npm Cypress command arguments'
}).option('write_weights_file', {
alias: 'wwf',
type: 'boolean',
description: `Write ${WEIGHTS_JSON} file ? `
}).argv;
const CY_SCRIPT = argv.script;
if (!CY_SCRIPT) {
throw new Error('Expected command, e.g.: cypress-parallel <cypress-script>');
}
let N_THREADS = argv.threads ? argv.threads : 2;
const DAFAULT_WEIGHT = 1;
const SPEC_FILES_PATH = argv.specsDir ? argv.specsDir : 'cypress/integration';
const WRITE_WEIGHTS_FILE = argv.write_weights_file ? argv.write_weights_file : false;
const CY_SCRIPT_ARGS = argv.args ? argv.args.split(' ') : [];
const COLORS = [
'\x1b[32m',
'\x1b[36m',
'\x1b[29m',
'\x1b[33m',
'\x1b[37m',
'\x1b[38m',
'\x1b[39m',
'\x1b[40m'
];
const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
if (isDirectory) return [...files, ...getAllFiles(name)];
return [...files, name];
}, []);
const logger = function(c) {
const color = c;
return function(message) {
console.log(`${color}${message}`);
};
};
const getRandomInt = function (max) {
return Math.floor(Math.random() * Math.floor(max));
};
const formatTime = function(timeMs) {
const seconds = Math.ceil(timeMs / 1000);
const sec = seconds % 60;
const min = Math.floor(seconds / 60);
let res = '';
if (min) res += `${min}m `;
res += `${sec}s`;
return res;
};
const start = () => {
const fileList = getAllFiles(SPEC_FILES_PATH);
let specWeights = {};
try {
specWeights = JSON.parse(fs.readFileSync(WEIGHTS_JSON, 'utf8'));
} catch (err) {
console.log(`Weight file not found in path: ${WEIGHTS_JSON}`);
}
let map = new Map();
for (let f of fileList) {
let specWeight = getRandomInt(3);
Object.keys(specWeights).forEach(spec => {
if (f.endsWith(spec)) {
specWeight = specWeights[spec].weight;
}
});
map.set(f, specWeight);
}
map = new Map([...map.entries()].sort((a, b) => b[1] - a[1]));
// Reduce useless number of threads
if (N_THREADS > fileList.length) {
N_THREADS /= 2;
}
const weigths = [];
for (let i = 0; i < N_THREADS; i++) {
weigths.push({
weight: 0,
list: []
});
}
for (const [key, value] of map.entries()) {
weigths.sort((w1, w2) => w1.weight - w2.weight);
weigths[0].list.push(key);
weigths[0].weight += +value;
}
const commands = weigths.map((w, i) => ({
color: COLORS[i],
tests: `'${w.list.join(',')}'`
}));
const children = [];
commands.forEach(command => {
const promise = new Promise((resolve, reject) => {
const timeMap = new Map();
let suiteDuration = 0;
const pckManager = isYarn
? 'yarn'
: process.platform === 'win32'
? 'npm.cmd'
: 'npm';
const child = spawn(pckManager, [
'run',
`${CY_SCRIPT}`,
'--',
'--reporter',
'cypress-parallel/json-stream.reporter.js',
'--spec',
command.tests,
...CY_SCRIPT_ARGS
]);
child.stdout.on('data', data => {
try {
const test = JSON.parse(data);
if (test[0] === 'pass') {
const testDuration = test[1].duration;
suiteDuration += testDuration;
console.log(
`\x1b[32m✔ \x1b[0m${test[1].title} (${testDuration}ms)`
);
}
if (test[0] === 'fail') {
console.log(`\x1b[31m✖ \x1b[0m${test[1].title} ( - ms)`);
console.log(`\x1b[31m${test[1].err}`);
console.log(`\x1b[31m${test[1].stack}`);
}
if (test[0] === 'suiteEnd' && test[1].title != null) {
timeMap.set(test[1].title, { ...test[1], duration: suiteDuration });
}
} catch (error) {
// No error logs
}
});
child.stderr.on('data', data => {
// only for debug purpose
console.log('\x1b[31m', `${data}`);
});
child.on('exit', () => {
resolve(timeMap);
});
});
children.push(promise);
});
let timeMap = new Map();
Promise.all(children).then(resultMaps => {
resultMaps.forEach((m, t) => {
let totTimeThread = 0;
for (let [name, test] of m) {
totTimeThread += test.duration;
}
console.log(`Thread ${t} time: ${formatTime(totTimeThread)}`);
timeMap = new Map([...timeMap, ...m]);
});
let table = new Table({
head: ['Spec', 'Time', 'Tests', 'Passing', 'Failing', 'Pending'],
style: { head: ['green'] },
colWidths: [25, 8, 7, 9, 9, 9]
});
let totalDuration = 0;
let totalWeight = timeMap.size * 10;
let specWeights = {};
for (let [name, test] of timeMap) {
totalDuration += test.duration;
specWeights[name] = { time: test.duration, weight: 0 };
table.push([
name,
`${formatTime(test.duration)}`,
test.tests,
test.passes,
test.failures,
test.pending
]);
}
console.log(table.toString());
Object.keys(specWeights).forEach(spec => {
specWeights[spec].weight = Math.floor(
(specWeights[spec].time / totalDuration) * totalWeight
);
});
const weightsJson = JSON.stringify(specWeights);
if (WRITE_WEIGHTS_FILE) {
fs.writeFile(`${WEIGHTS_JSON}`, weightsJson, 'utf8', err => {
if (err) throw err;
console.log('Generated file parallel-weights.json.');
});
}
});
};
start();