-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathgetTestMatrix.ts
More file actions
203 lines (168 loc) · 6.25 KB
/
getTestMatrix.ts
File metadata and controls
203 lines (168 loc) · 6.25 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
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
import { execSync } from 'child_process';
import * as fs from 'fs';
import { sync as globSync } from 'glob';
import * as path from 'path';
import { dirname } from 'path';
import { parseArgs } from 'util';
interface MatrixInclude {
/** The test application (directory) name. */
'test-application': string;
/** Optional override for the build command to run. */
'build-command'?: string;
/** Optional override for the assert command to run. */
'assert-command'?: string;
/** Optional label for the test run. If not set, defaults to value of `test-application`. */
label?: string;
}
interface PackageJsonSentryTestConfig {
/** If this is true, the test app is optional. */
optional?: boolean;
/** Variant configs that should be run in non-optional test runs. */
variants?: Partial<MatrixInclude>[];
/** Variant configs that should be run in optional test runs. */
optionalVariants?: Partial<MatrixInclude>[];
/** Skip this test app for matrix generation. */
skip?: boolean;
}
/**
* This methods generates a matrix for the GitHub Actions workflow to run the E2E tests.
* It checks which test applications are affected by the current changes in the PR and then generates a matrix
* including all test apps that have at least one dependency that was changed in the PR.
* If no `--base=xxx` is provided, it will output all test applications.
*
* If `--optional=true` is set, it will generate a matrix of optional test applications only.
* Otherwise, these will be skipped.
*/
function run(): void {
const { values } = parseArgs({
args: process.argv.slice(2),
options: {
base: { type: 'string' },
head: { type: 'string' },
optional: { type: 'string', default: 'false' },
},
});
const { base, head = 'HEAD', optional } = values;
const testApplications = globSync('*/package.json', {
cwd: `${__dirname}/../test-applications`,
}).map(filePath => dirname(filePath));
// If `--base=xxx` is defined, we only want to get test applications changed since that base
// Else, we take all test applications (e.g. on push)
const includedTestApplications = base
? getAffectedTestApplications(testApplications, { base, head })
: testApplications;
const optionalMode = optional === 'true';
const includes: MatrixInclude[] = [];
includedTestApplications.forEach(testApp => {
addIncludesForTestApp(testApp, includes, { optionalMode });
});
// We print this to the output, so the GHA can use it for the matrix
// eslint-disable-next-line no-console
console.log(`matrix=${JSON.stringify({ include: includes })}`);
}
function addIncludesForTestApp(
testApp: string,
includes: MatrixInclude[],
{ optionalMode }: { optionalMode: boolean },
): void {
const packageJson = getPackageJson(testApp);
const shouldSkip = packageJson.sentryTest?.skip || false;
const isOptional = packageJson.sentryTest?.optional || false;
const variants = (optionalMode ? packageJson.sentryTest?.optionalVariants : packageJson.sentryTest?.variants) || [];
if (shouldSkip) {
return;
}
// Add the basic test-application itself, if it is in the current mode
if (optionalMode === isOptional) {
includes.push({
'test-application': testApp,
});
}
variants.forEach(variant => {
includes.push({
'test-application': testApp,
...variant,
});
});
}
function getSentryDependencies(appName: string): string[] {
const packageJson = getPackageJson(appName);
const dependencies = {
...packageJson.devDependencies,
...packageJson.dependencies,
};
return Object.keys(dependencies).filter(key => key.startsWith('@sentry'));
}
function getPackageJson(appName: string): {
dependencies?: { [key: string]: string };
devDependencies?: { [key: string]: string };
sentryTest?: PackageJsonSentryTestConfig;
} {
const fullPath = path.resolve(__dirname, '..', 'test-applications', appName, 'package.json');
if (!fs.existsSync(fullPath)) {
throw new Error(`Could not find package.json for ${appName}`);
}
return JSON.parse(fs.readFileSync(fullPath, 'utf8'));
}
run();
function getAffectedTestApplications(
testApplications: string[],
{ base = 'develop', head }: { base?: string; head?: string },
): string[] {
const additionalArgs = [`--base=${base}`];
if (head) {
additionalArgs.push(`--head=${head}`);
}
const affectedProjects = execSync(`yarn --silent nx show projects --affected ${additionalArgs.join(' ')}`)
.toString()
.split('\n')
.map(line => line.trim())
.filter(Boolean);
// If something in e2e tests themselves are changed, check if only test applications were changed
if (affectedProjects.includes('@sentry-internal/e2e-tests')) {
try {
const changedTestApps = getChangedTestApps(base, head);
// Shared code was changed, run all tests
if (changedTestApps === false) {
return testApplications;
}
// Only test applications that were changed, run selectively
if (changedTestApps.size > 0) {
return testApplications.filter(testApp => changedTestApps.has(testApp));
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Failed to get changed files, running all tests:', error);
return testApplications;
}
// Fall back to running all tests
return testApplications;
}
return testApplications.filter(testApp => {
const sentryDependencies = getSentryDependencies(testApp);
return sentryDependencies.some(dep => affectedProjects.includes(dep));
});
}
function getChangedTestApps(base: string, head?: string): false | Set<string> {
const changedFiles = execSync(`git diff --name-only ${base}${head ? `..${head}` : ''} -- .`, {
encoding: 'utf-8',
})
.toString()
.split('\n')
.map(line => line.trim())
.filter(Boolean);
const changedTestApps: Set<string> = new Set();
const testAppsPrefix = 'dev-packages/e2e-tests/test-applications/';
for (const file of changedFiles) {
if (!file.startsWith(testAppsPrefix)) {
// Shared code change - need to run all tests
return false;
}
const pathAfterPrefix = file.slice(testAppsPrefix.length);
const slashIndex = pathAfterPrefix.indexOf('/');
if (slashIndex > 0) {
changedTestApps.add(pathAfterPrefix.slice(0, slashIndex));
}
}
return changedTestApps;
}