-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecosystem-ci.ts
More file actions
320 lines (306 loc) · 10.4 KB
/
ecosystem-ci.ts
File metadata and controls
320 lines (306 loc) · 10.4 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { cac } from 'cac';
import type { CommandOptions, RunOptions, Stack } from './types';
import {
bisectStack,
buildStack,
getDefaultRepository,
ignorePrecoded,
parseMajorVersion,
parseStackMajor,
setupEnvironment,
setupStackRepo,
} from './utils';
const STACK_CHOICES: Stack[] = [
'rsbuild',
'rspack',
'rstest',
'rslib',
'rsdoctor',
'rspress',
];
const cli = cac();
cli
.command('[...suites]', 'build selected stack and run suites')
.option('--stack <stack>', `target stack (${STACK_CHOICES.join('|')})`)
.option('--verify', 'verify checkouts by running tests', { default: false })
.option('--repo <repo>', 'repository to use')
.option('--branch <branch>', 'branch to use', { default: 'main' })
.option('--tag <tag>', 'tag to use')
.option('--commit <commit>', 'commit sha to use')
.option('--release <version>', 'release to use from npm registry')
.option('--suite-precoded', 'use precoded suite options under tests dir')
.option('--suite-branch <branch>', 'suite branch to use')
.option('--suite-tag <tag>', 'suite tag to use')
.option('--suite-commit <commit>', 'suite commit sha to use')
.action(async (suites, options: CommandOptions) => {
const stack = resolveStack(options.stack);
const envData = await setupEnvironment(stack);
const suitesToRun = getSuitesToRun(stack, suites, envData.root);
let stackMajor: number;
if (!options.release) {
await setupStackRepo({
repo: options.repo ?? getDefaultRepository(stack),
branch: options.branch,
tag: options.tag,
commit: options.commit,
});
await buildStack({ verify: options.verify });
stackMajor = parseStackMajor(envData.stackPath);
} else {
stackMajor = parseMajorVersion(options.release);
}
const baseRunOptions = createRunOptions(
stack,
envData,
stackMajor,
options,
);
for (const suite of suitesToRun) {
await runSuite(stack, suite, baseRunOptions);
}
});
cli
.command('build', 'build selected stack only')
.option('--stack <stack>', `target stack (${STACK_CHOICES.join('|')})`)
.option('--verify', 'verify checkout by running tests', { default: false })
.option('--repo <repo>', 'repository to use')
.option('--branch <branch>', 'branch to use', { default: 'main' })
.option('--tag <tag>', 'tag to use')
.option('--commit <commit>', 'commit sha to use')
.action(async (options: CommandOptions) => {
const stack = resolveStack(options.stack);
await handleBuild(stack, options);
});
registerBuildAlias('build-rsbuild', 'rsbuild');
registerBuildAlias('build-rspack', 'rspack');
registerBuildAlias('build-rstest', 'rstest');
registerBuildAlias('build-rslib', 'rslib');
registerBuildAlias('build-rsdoctor', 'rsdoctor');
registerBuildAlias('build-rspress', 'rspress');
cli
.command(
'run-suites [...suites]',
'run suites using a pre-built version of the selected stack',
)
.option('--stack <stack>', `target stack (${STACK_CHOICES.join('|')})`)
.option(
'--verify',
'verify checkout by running tests before using local stack',
{ default: false },
)
.option('--repo <repo>', 'repository to use')
.option('--release <version>', 'release to use from npm registry')
.option('--suite-precoded', 'use precoded suite options under tests dir')
.option('--suite-branch <branch>', 'suite branch to use')
.option('--suite-tag <tag>', 'suite tag to use')
.option('--suite-commit <commit>', 'suite commit sha to use')
.action(async (suites, options: CommandOptions) => {
const stack = resolveStack(options.stack);
const envData = await setupEnvironment(stack);
const suitesToRun = getSuitesToRun(stack, suites, envData.root);
const stackMajor = options.release
? parseMajorVersion(options.release)
: parseStackMajor(envData.stackPath);
const baseRunOptions = createRunOptions(
stack,
envData,
stackMajor,
options,
);
for (const suite of suitesToRun) {
await runSuite(stack, suite, baseRunOptions);
}
});
cli
.command(
'bisect [...suites]',
'use git bisect to find a commit in the selected stack that broke suites',
)
.option('--stack <stack>', `target stack (${STACK_CHOICES.join('|')})`)
.option('--good <ref>', 'last known good ref, e.g. a previous tag. REQUIRED!')
.option('--verify', 'verify checkouts by running tests', { default: false })
.option('--repo <repo>', 'repository to use')
.option('--branch <branch>', 'branch to use', { default: 'main' })
.option('--tag <tag>', 'tag to use')
.option('--commit <commit>', 'commit sha to use')
.option('--suite-precoded', 'use precoded suite options under tests dir')
.option('--suite-branch <branch>', 'suite branch to use')
.option('--suite-tag <tag>', 'suite tag to use')
.option('--suite-commit <commit>', 'suite commit sha to use')
.action(async (suites, options: CommandOptions & { good: string }) => {
if (!options.good) {
console.log(
'you have to specify a known good version with `--good <commit|tag>`',
);
process.exit(1);
}
const stack = resolveStack(options.stack);
const envData = await setupEnvironment(stack);
const suitesToRun = getSuitesToRun(stack, suites, envData.root);
let isFirstRun = true;
const { verify } = options;
const runSuiteForBisect = async (): Promise<Error | void> => {
try {
await buildStack({ verify: isFirstRun && verify });
const stackMajor = parseStackMajor(envData.stackPath);
for (const suite of suitesToRun) {
await runSuite(
stack,
suite,
createRunOptions(stack, envData, stackMajor, options, {
verify: !!(isFirstRun && verify),
skipGit: !isFirstRun,
}),
);
}
isFirstRun = false;
return undefined;
} catch (error) {
return error as Error;
}
};
await setupStackRepo({
repo: options.repo ?? getDefaultRepository(stack),
branch: options.branch,
tag: options.tag,
commit: options.commit,
shallow: false,
});
const initialError = await runSuiteForBisect();
if (initialError) {
await bisectStack(options.good, runSuiteForBisect);
} else {
console.log('no errors for starting commit, cannot bisect');
}
});
cli.help();
cli.parse();
async function handleBuild(stack: Stack, options: CommandOptions) {
await setupEnvironment(stack);
await setupStackRepo({
repo: options.repo ?? getDefaultRepository(stack),
branch: options.branch,
tag: options.tag,
commit: options.commit,
});
await buildStack({ verify: options.verify });
}
function registerBuildAlias(commandName: string, stack: Stack) {
cli
.command(commandName, `build ${stack} only`)
.option('--verify', `verify ${stack} checkout by running tests`, {
default: false,
})
.option('--repo <repo>', `${stack} repository to use`)
.option('--branch <branch>', `${stack} branch to use`, { default: 'main' })
.option('--tag <tag>', `${stack} tag to use`)
.option('--commit <commit>', `${stack} commit sha to use`)
.action(async (options: CommandOptions) => {
await handleBuild(stack, options);
});
}
function resolveStack(value: string | undefined): Stack {
if (!value) {
console.log(
`missing required --stack option. Available stacks: ${STACK_CHOICES.join(', ')}`,
);
process.exit(1);
}
const normalized = value.toLowerCase() as Stack;
if (!STACK_CHOICES.includes(normalized)) {
console.log(
`invalid stack: ${value}. Available stacks: ${STACK_CHOICES.join(', ')}`,
);
process.exit(1);
}
return normalized;
}
function createRunOptions(
stack: Stack,
envData: Awaited<ReturnType<typeof setupEnvironment>>,
stackMajor: number,
options: CommandOptions,
overrides: Partial<RunOptions> = {},
): RunOptions {
const runOptions: RunOptions = {
stack,
root: envData.root,
workspace: envData.workspace,
stackPath: envData.stackPath,
projectPath: envData.projectPath,
stackMajor,
projectMajor: stackMajor,
release: options.release,
verify: options.verify,
skipGit: false,
suiteBranch: ignorePrecoded(options.suiteBranch),
suiteTag: ignorePrecoded(options.suiteTag),
suiteCommit: ignorePrecoded(options.suiteCommit),
};
assignStackAliases(runOptions, stack, envData.stackPath, stackMajor);
return { ...runOptions, ...overrides };
}
async function runSuite(stack: Stack, suite: string, options: RunOptions) {
const { test } = await import(`./tests/${stack}/${suite}.ts`);
await test({
...options,
workspace: path.resolve(options.workspace, suite),
});
}
function getSuitesToRun(stack: Stack, suites: string[], root: string) {
const stackDir = path.join(root, 'tests', stack);
if (!fs.existsSync(stackDir)) {
console.log(`invalid stack "${stack}", expected suites under ${stackDir}`);
process.exit(1);
}
let suitesToRun: string[] = suites;
const availableSuites: string[] = fs
.readdirSync(stackDir)
.filter((f: string) => !f.startsWith('_') && f.endsWith('.ts'))
.map((f: string) => f.slice(0, -3));
availableSuites.sort();
if (suitesToRun.length === 0) {
suitesToRun = availableSuites;
} else {
const invalidSuites = suitesToRun.filter(
(name) => !name.startsWith('_') && !availableSuites.includes(name),
);
if (invalidSuites.length) {
console.log(
`invalid suite(s): ${invalidSuites.join(', ')} for stack ${stack}`,
);
console.log(`available suites: ${availableSuites.join(', ')}`);
process.exit(1);
}
}
return suitesToRun;
}
function assignStackAliases(
runOptions: RunOptions,
stack: Stack,
stackPath: string,
stackMajor: number,
) {
if (stack === 'rsbuild') {
runOptions.rsbuildPath = stackPath;
runOptions.rsbuildMajor = stackMajor;
} else if (stack === 'rspack') {
runOptions.rspackPath = stackPath;
runOptions.rspackMajor = stackMajor;
} else if (stack === 'rstest') {
runOptions.rstestPath = stackPath;
runOptions.rstestMajor = stackMajor;
} else if (stack === 'rslib') {
runOptions.rslibPath = stackPath;
runOptions.rslibMajor = stackMajor;
} else if (stack === 'rsdoctor') {
runOptions.rsdoctorPath = stackPath;
runOptions.rsdoctorMajor = stackMajor;
} else if (stack === 'rspress') {
runOptions.rspressPath = stackPath;
runOptions.rspressMajor = stackMajor;
}
}