forked from tnicola/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·148 lines (130 loc) · 4.31 KB
/
Copy pathcli.js
File metadata and controls
executable file
·148 lines (130 loc) · 4.31 KB
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
#!/usr/bin/env node
const Table = require('cli-table3');
const colors = require('@colors/colors/safe');
const path = require('path');
const fs = require('fs-extra');
const { settings } = require('./settings');
const {
getTestSuitePaths,
distributeTestsByWeight,
getMaxPathLenghtFrom
} = require('./test-suites');
const {
formatTime,
generateWeightsFile,
collectResults
} = require('./utility');
const { executeThread } = require('./thread');
const { resultsPath } = require('./shared-config');
function cleanResultsPath() {
if (!fs.existsSync(resultsPath)) {
fs.mkdirSync(resultsPath, { recursive: true });
} else {
fs.readdir(resultsPath, (err, files) => {
if (err) console.log(err);
for (const file of files) {
fs.unlink(path.join(resultsPath, file), (err) => {
if (err) console.log(err);
});
}
});
}
}
async function start() {
cleanResultsPath();
const testSuitePaths = await getTestSuitePaths();
const threads = distributeTestsByWeight(testSuitePaths);
const start = new Date();
const results = await Promise.allSettled(threads.map(executeThread));
const failedResultsLength = results.filter(r => r.status === 'rejected').length;
const end = new Date();
const timeTaken = end.getTime() - start.getTime();
const resultsPath = path.join(process.cwd(), settings.runnerResults);
const resultMaps = [collectResults(resultsPath)];
let timeMap = new Map();
// should split below into calculateStatistics and presentResult methods
resultMaps.forEach((m, t) => {
let totTimeThread = 0;
for (let [name, test] of m) {
totTimeThread += test.duration;
}
timeMap = new Map([...timeMap, ...m]);
});
let table = new Table({
head: ['Spec', 'Time', 'Tests', 'Passing', 'Failing', 'Pending'],
style: { head: ['blue'] },
colWidths: [getMaxPathLenghtFrom(testSuitePaths), 10, 10, 10, 10, 10]
});
let totalTests = 0;
let totalPasses = 0;
let totalFailures = 0;
let totalDuration = 0;
let totalPending = 0;
let totalWeight = timeMap.size * 10;
let specWeights = {};
for (let [name, suite] of timeMap) {
//The value of suite.tests is not what we expect.
const nbTests = suite.passes + suite.pending + suite.failures;
totalDuration += suite.duration;
totalTests += nbTests;
totalPasses += suite.passes;
totalPending += suite.pending;
totalFailures += suite.failures;
specWeights[name] = { time: suite.duration, weight: 0 };
table.push([
name,
`${formatTime(suite.duration)}`,
nbTests,
suite.passes,
suite.failures > 0 ? colors.red(suite.failures) : suite.failures,
suite.pending
]);
}
table.push([
'Results',
`${formatTime(totalDuration)}`,
totalTests,
totalPasses,
totalFailures > 0 ? colors.red(totalFailures) : totalFailures,
totalPending
]);
console.log(table.toString());
// fail on missing results (had some issues with missing test results, prevent that from slipping past again)
if (settings.strictMode && timeMap.size !== testSuitePaths.length) {
console.error(`Found test suites does not match results.`);
console.error(`Test suites found: ${testSuitePaths.length}`);
console.error(`Test suite results: ${timeMap.size}`);
console.error(
'Some test suites likely terminated without passing on results, exit with error'
);
const missingTestResults = testSuitePaths.filter(
(path) => !timeMap.get(path)
);
console.log(
`The following test suites are missing results: ${missingTestResults}`
);
process.exit(1);
}
if (totalFailures > 0) {
process.stderr.write(`\x1b[31m${totalFailures} test failure(s)\n`);
process.exit(1);
}
if (failedResultsLength > 0) {
console.error(
`Some threads failed to execute, check the logs for more details.`
);
process.stderr.write(`\x1b[31m${failedResultsLength} child threads exited with non-zero status code\n`);
process.exit(failedResultsLength);
}
const timeSaved = totalDuration - timeTaken;
console.log(
`Total run time: ${totalDuration / 1000}s, executed in: ${
timeTaken / 1000
}, saved ${timeSaved / 1000} (~${Math.round(
(timeSaved / totalDuration) * 100
)}%)`
);
generateWeightsFile(specWeights, totalDuration, totalWeight);
process.exit(totalFailures);
}
start();