fix: add validations to --shard cli parameter (#34463) (#34479)

This commit is contained in:
ReaZzy 2025-01-27 23:31:14 +01:00 коммит произвёл GitHub
Родитель d63907fc5b
Коммит eaaef29dbd
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 35 добавлений и 3 удалений

Просмотреть файл

@ -280,8 +280,6 @@ async function mergeReports(reportDir: string | undefined, opts: { [key: string]
}
function overridesFromOptions(options: { [key: string]: any }): ConfigCLIOverrides {
const shardPair = options.shard ? options.shard.split('/').map((t: string) => parseInt(t, 10)) : undefined;
let updateSnapshots: 'all' | 'changed' | 'missing' | 'none' | undefined;
if (['all', 'changed', 'missing', 'none'].includes(options.updateSnapshots))
updateSnapshots = options.updateSnapshots;
@ -298,7 +296,7 @@ function overridesFromOptions(options: { [key: string]: any }): ConfigCLIOverrid
repeatEach: options.repeatEach ? parseInt(options.repeatEach, 10) : undefined,
retries: options.retries ? parseInt(options.retries, 10) : undefined,
reporter: resolveReporterOption(options.reporter),
shard: shardPair ? { current: shardPair[0], total: shardPair[1] } : undefined,
shard: resolveShardOption(options.shard),
timeout: options.timeout ? parseInt(options.timeout, 10) : undefined,
tsconfig: options.tsconfig ? path.resolve(process.cwd(), options.tsconfig) : undefined,
ignoreSnapshots: options.ignoreSnapshots ? !!options.ignoreSnapshots : undefined,
@ -341,6 +339,34 @@ function resolveReporterOption(reporter?: string): ReporterDescription[] | undef
return reporter.split(',').map((r: string) => [resolveReporter(r)]);
}
function resolveShardOption(shard?: string): ConfigCLIOverrides['shard'] {
if (!shard)
return undefined;
const shardPair = shard.split('/');
if (shardPair.length !== 2) {
throw new Error(
`--shard "${shard}", expected format is "current/all", 1-based, for example "3/5".`,
);
}
const current = parseInt(shardPair[0], 10);
const total = parseInt(shardPair[1], 10);
if (isNaN(total) || total < 1)
throw new Error(`--shard "${shard}" total must be a positive number`);
if (isNaN(current) || current < 1 || current > total) {
throw new Error(
`--shard "${shard}" current must be a positive number, not greater than shard total`,
);
}
return { current, total };
}
function resolveReporter(id: string) {
if (builtInReporters.includes(id as any))
return id;

Просмотреть файл

@ -139,6 +139,12 @@ test('should respect shard=3/4', async ({ runInlineTest }) => {
]);
});
test('should exit with shard=/3', async ({ runInlineTest }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/34463' });
const result = await runInlineTest(tests, { shard: '/3' });
expect(result.exitCode).toBe(1);
});
test('should not produce skipped tests for zero-sized shards', async ({ runInlineTest }) => {
const result = await runInlineTest(tests, { shard: '10/10', workers: 1 });
expect(result.exitCode).toBe(0);