This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate-junit-xml.js
183 lines (163 loc) · 5.45 KB
/
aggregate-junit-xml.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
#!/usr/bin/env node
/*
* Asserts JUnit test results were generated for the specified test categories.
* If any vscode-integration test runs appear to have crashed, then
* rerun them once. Lastly, aggregate all results into a directory.
*
*
* e.g. with the following structure:
*
* packages/salesforcecx-vscode-lwc/test/unit
* packages/salesforcecx-vscode-lwc/test/vscode-integration
*
* these files will be asserted to exist:
*
* packages/salesforcedx-vscode-lwc/junit-custom-unitTests.xml
* packages/salesforcedx-vscode-lwc/junit-custom-vscodeIntegrationTests.xml
*
* and then are copied into a top-level directory called junit-aggregate.'
*
*
* Valid categories: unit, integration, vscode-integration
*
* By default, all categories will be tested. To only process specific
* categories, call the script with desired categories separated by a space.
*
* e.g.
* node aggregate-junit-xml.js -t integration vscode-integration
* npm run aggregateJUnit -- -t integration vscode-integration
*/
const fs = require('fs-extra');
const path = require('path');
const shell = require('shelljs');
const TEST_TYPE_ARG = '-t';
const categoryToFile = {
'vscode-integration': 'junit-custom-vscodeIntegrationTests.xml',
integration: 'junit-custom-integrationTests.xml',
unit: 'junit-custom-unitTests.xml',
system: 'junit-custom.xml'
};
const missingResults = {
'vscode-integration': [],
integration: [],
unit: [],
system: []
};
function getTestCategories() {
let flags;
if (process.argv.indexOf(TEST_TYPE_ARG) > -1) {
flags = new Set(process.argv.slice(process.argv.indexOf(TEST_TYPE_ARG) + 1));
}
if (!flags || flags.size === 0) {
console.log('Checking all test categories.');
flags = new Set(Object.keys(categoryToFile).map(c => `${c}`));
}
return flags;
}
function createAggregateDirectory() {
const aggregateDir = path.join(process.cwd(), 'junit-aggregate');
if (!fs.existsSync(aggregateDir)) {
shell.mkdir(aggregateDir);
}
}
function getTestDirectory(packagePath) {
if (fs.statSync(packagePath).isDirectory()) {
// scan test directory for each package that has one
const testDir = path.join(packagePath, 'test');
if (fs.existsSync(testDir) && fs.statSync(testDir).isDirectory()) {
return testDir;
}
}
return '';
}
function getMissingResults(flags) {
const packagesDir = path.join(process.cwd(), 'packages');
for (const packageName of fs.readdirSync(packagesDir)) {
const packagePath = path.join(packagesDir, packageName);
const testDir = getTestDirectory(packagePath);
if (testDir) {
for (const testEntry of fs.readdirSync(testDir)) {
if (flags.has(`${testEntry}`)) {
if (!copyJunitTestResult(packagePath, packageName, testEntry)) {
if (testEntry === 'vscode-integration') {
rerunCrashedVSCodeIntegrationTests(packagePath, packageName, testEntry);
} else {
missingResults[testEntry].push(packageName);
}
}
}
}
}
}
}
/*
* If the current test directory is one we want to aggregate, copy the
* junit results to the aggregate folder.
*
* Return true or false if the test result was found.
*/
function copyJunitTestResult(packagePath, packageName, testEntry) {
const junitFilePath = path.join(
packagePath,
categoryToFile[testEntry]
);
if (fs.existsSync(junitFilePath)) {
shell.cp(
junitFilePath,
path.join(
process.cwd(),
'junit-aggregate',
`${packageName}-${categoryToFile[testEntry]}`
)
);
return true;
}
return false;
}
/*
* Rerun vscode test suites that have crashed. The reason for this crash appears
* to be due to either Windows or Electron. See W-9138899 for more information.
*/
function rerunCrashedVSCodeIntegrationTests(packagePath, packageName, testEntry) {
console.assert(testEntry === 'vscode-integration');
console.log(`\nRerunning vscode integration test suite ${packageName} due to crash.`);
shell.exec(`npm run --prefix ${packagePath} test:vscode-integration`);
if (!copyJunitTestResult(packagePath, packageName, testEntry)) {
missingResults[testEntry].push(packageName);
}
}
function generateMissingMessage(missingResults) {
let missingMessage;
for (const [testType, pkgs] of Object.entries(missingResults)) {
if (pkgs.length > 0) {
if (!missingMessage) {
missingMessage = '\nMissing junit results for the following packages:\n';
}
missingMessage += `\n* ${testType}:`;
missingMessage = pkgs.reduce(
(previous, current) => `${previous}\n\t- ${current}`,
missingMessage
);
}
}
return missingMessage;
}
function checkMissingMessage(missingMessage) {
if (missingMessage) {
missingMessage += '\n\nPossible Issues:\n\n';
missingMessage +=
"1) Tests in the expected suite categories haven't run yet (unit, integration, etc.).\n";
missingMessage +=
'2) An unexpected test runner or reporter failure while running tests. Sometimes extension activation issues or issues in the tests can silently fail.\n';
missingMessage += '3) Test run configuration is improperly set up.\n';
missingMessage += '4) Circular imports.\n';
console.error(missingMessage);
process.exit(1);
}
}
const flags = getTestCategories();
createAggregateDirectory();
getMissingResults(flags);
const missingMessage = generateMissingMessage(missingResults);
checkMissingMessage(missingMessage);
console.log('Finished test check and aggregation.');