|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { readdirSync } from 'node:fs'; |
| 4 | +import { resolve, join } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const DEFAULT_TIMEOUT_MS = 90_000; |
| 8 | + |
| 9 | +export function parseArgs(argv) { |
| 10 | + const positional = []; |
| 11 | + let timeoutMs = Number(process.env.TEST_FILE_TIMEOUT_MS || DEFAULT_TIMEOUT_MS); |
| 12 | + for (let i = 0; i < argv.length; i++) { |
| 13 | + const arg = argv[i]; |
| 14 | + if (arg === '--timeout-ms') { |
| 15 | + timeoutMs = Number(argv[++i]); |
| 16 | + continue; |
| 17 | + } |
| 18 | + if (arg?.startsWith('--timeout-ms=')) { |
| 19 | + timeoutMs = Number(arg.slice('--timeout-ms='.length)); |
| 20 | + continue; |
| 21 | + } |
| 22 | + positional.push(arg); |
| 23 | + } |
| 24 | + |
| 25 | + const shardIndex = Number(positional[0] ?? process.env.TEST_SHARD_INDEX ?? 0); |
| 26 | + const shardTotal = Number(positional[1] ?? process.env.TEST_SHARD_TOTAL ?? 1); |
| 27 | + if (!Number.isInteger(shardIndex) || shardIndex < 0) { |
| 28 | + throw new Error(`Invalid shard index: ${positional[0] ?? process.env.TEST_SHARD_INDEX ?? ''}`); |
| 29 | + } |
| 30 | + if (!Number.isInteger(shardTotal) || shardTotal < 1) { |
| 31 | + throw new Error(`Invalid shard total: ${positional[1] ?? process.env.TEST_SHARD_TOTAL ?? ''}`); |
| 32 | + } |
| 33 | + if (shardIndex >= shardTotal) { |
| 34 | + throw new Error(`Shard index ${shardIndex} must be smaller than shard total ${shardTotal}`); |
| 35 | + } |
| 36 | + if (!Number.isFinite(timeoutMs) || timeoutMs < 1_000) { |
| 37 | + throw new Error(`Invalid per-file timeout: ${timeoutMs}`); |
| 38 | + } |
| 39 | + return { shardIndex, shardTotal, timeoutMs }; |
| 40 | +} |
| 41 | + |
| 42 | +export function listTopLevelTestFiles(root = process.cwd()) { |
| 43 | + const testDir = join(root, 'test'); |
| 44 | + return readdirSync(testDir) |
| 45 | + .filter(name => name.endsWith('.test.js')) |
| 46 | + .sort((a, b) => a.localeCompare(b)) |
| 47 | + .map(name => join('test', name).replace(/\\/g, '/')); |
| 48 | +} |
| 49 | + |
| 50 | +export function selectShard(files, shardIndex, shardTotal) { |
| 51 | + return files.filter((_, i) => i % shardTotal === shardIndex); |
| 52 | +} |
| 53 | + |
| 54 | +function runOne(file, timeoutMs) { |
| 55 | + return new Promise(resolveRun => { |
| 56 | + const child = spawn(process.execPath, ['--test', '--test-force-exit', file], { |
| 57 | + cwd: process.cwd(), |
| 58 | + env: process.env, |
| 59 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 60 | + }); |
| 61 | + const prefix = `[${file}] `; |
| 62 | + child.stdout.on('data', chunk => process.stdout.write(prefix + String(chunk).replace(/\n/g, `\n${prefix}`))); |
| 63 | + child.stderr.on('data', chunk => process.stderr.write(prefix + String(chunk).replace(/\n/g, `\n${prefix}`))); |
| 64 | + |
| 65 | + let timedOut = false; |
| 66 | + const timer = setTimeout(() => { |
| 67 | + timedOut = true; |
| 68 | + child.kill('SIGTERM'); |
| 69 | + setTimeout(() => child.kill('SIGKILL'), 5_000).unref?.(); |
| 70 | + }, timeoutMs); |
| 71 | + |
| 72 | + child.on('close', code => { |
| 73 | + clearTimeout(timer); |
| 74 | + resolveRun({ |
| 75 | + file, |
| 76 | + ok: !timedOut && code === 0, |
| 77 | + code, |
| 78 | + timedOut, |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +export async function runShard({ shardIndex, shardTotal, timeoutMs, root = process.cwd() }) { |
| 85 | + const files = listTopLevelTestFiles(root); |
| 86 | + const selected = selectShard(files, shardIndex, shardTotal); |
| 87 | + console.log(`Running test shard ${shardIndex + 1}/${shardTotal}: ${selected.length}/${files.length} files`); |
| 88 | + for (const file of selected) console.log(`- ${file}`); |
| 89 | + |
| 90 | + const failures = []; |
| 91 | + for (const file of selected) { |
| 92 | + const result = await runOne(file, timeoutMs); |
| 93 | + if (!result.ok) failures.push(result); |
| 94 | + } |
| 95 | + |
| 96 | + if (failures.length) { |
| 97 | + console.error(`Test shard ${shardIndex + 1}/${shardTotal} failed:`); |
| 98 | + for (const failure of failures) { |
| 99 | + const suffix = failure.timedOut ? `timed out after ${timeoutMs}ms` : `exit ${failure.code}`; |
| 100 | + console.error(`- ${failure.file}: ${suffix}`); |
| 101 | + } |
| 102 | + return 1; |
| 103 | + } |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +if (resolve(process.argv[1] || '') === resolve(fileURLToPath(import.meta.url))) { |
| 108 | + try { |
| 109 | + const opts = parseArgs(process.argv.slice(2)); |
| 110 | + process.exitCode = await runShard(opts); |
| 111 | + } catch (err) { |
| 112 | + console.error(err?.message || err); |
| 113 | + process.exitCode = 2; |
| 114 | + } |
| 115 | +} |
0 commit comments