diff --git a/package-lock.json b/package-lock.json index 7b329b9..4dde105 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "testfold", - "version": "0.4.2", + "version": "0.4.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "testfold", - "version": "0.4.2", + "version": "0.4.3", "license": "MIT", "dependencies": { "dotenv": "^17.4.2", "minimist": "^1.2.8", + "tsx": "^4.23.13", "zod": "^4.5.4" }, "bin": { @@ -24,7 +25,6 @@ "jest": "^30.5.1", "prettier": "^3.9.6", "ts-jest": "^29.4.12", - "tsx": "^4.23.13", "typescript": "^6.0.3", "typescript-eslint": "^8.69.0", "yaml": "2.9.0" @@ -594,7 +594,6 @@ "cpu": [ "ppc64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -611,7 +610,6 @@ "cpu": [ "arm" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -628,7 +626,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -645,7 +642,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -662,7 +658,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -679,7 +674,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -696,7 +690,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -713,7 +706,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -730,7 +722,6 @@ "cpu": [ "arm" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -747,7 +738,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -764,7 +754,6 @@ "cpu": [ "ia32" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -781,7 +770,6 @@ "cpu": [ "loong64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -798,7 +786,6 @@ "cpu": [ "mips64el" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -815,7 +802,6 @@ "cpu": [ "ppc64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -832,7 +818,6 @@ "cpu": [ "riscv64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -849,7 +834,6 @@ "cpu": [ "s390x" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -866,7 +850,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -883,7 +866,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -900,7 +882,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -917,7 +898,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -934,7 +914,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -951,7 +930,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -968,7 +946,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -985,7 +962,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -1002,7 +978,6 @@ "cpu": [ "ia32" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -1019,7 +994,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "MIT", "optional": true, "os": [ @@ -1887,9 +1861,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1911,9 +1882,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1935,9 +1903,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -1959,9 +1924,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1983,9 +1945,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2007,9 +1966,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2606,9 +2562,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2623,9 +2576,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2640,9 +2590,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2657,9 +2604,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2674,9 +2618,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2691,9 +2632,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2708,9 +2646,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -2725,9 +2660,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2742,9 +2674,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -2759,9 +2688,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3464,7 +3390,6 @@ "version": "0.28.2", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz", "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==", - "dev": true, "hasInstallScript": true, "license": "MIT", "bin": { @@ -3845,7 +3770,6 @@ "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, "hasInstallScript": true, "license": "MIT", "optional": true, @@ -5794,7 +5718,6 @@ "version": "4.23.13", "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.23.13.tgz", "integrity": "sha512-BL5MGkRln6aDYhb0xbQlEAGw743BaZYWdbWtdJOBriYJboKgUUYCadFp2/FpBBZquBC/ezNBn7wMMPx7FDZUDw==", - "dev": true, "license": "MIT", "dependencies": { "esbuild": "~0.28.0" diff --git a/package.json b/package.json index cf963e4..49252e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "testfold", - "version": "0.4.2", + "version": "0.4.3", "packageManager": "npm@11.19.1", "description": "Unified test runner with Jest and Playwright support, parallel execution, and detailed failure reporting", "type": "module", @@ -62,6 +62,7 @@ "dependencies": { "dotenv": "^17.4.2", "minimist": "^1.2.8", + "tsx": "^4.23.13", "zod": "^4.5.4" }, "devDependencies": { @@ -72,7 +73,6 @@ "jest": "^30.5.1", "prettier": "^3.9.6", "ts-jest": "^29.4.12", - "tsx": "^4.23.13", "typescript": "^6.0.3", "typescript-eslint": "^8.69.0", "yaml": "2.9.0" diff --git a/src/config/loader.ts b/src/config/loader.ts index 04b9ce8..7aff65b 100644 --- a/src/config/loader.ts +++ b/src/config/loader.ts @@ -36,8 +36,13 @@ export async function loadConfig(configPath?: string): Promise async function loadConfigFile(path: string): Promise { const fileUrl = pathToFileURL(path).href; - // Dynamic import for ESM/TS files - const module = (await import(fileUrl)) as { default?: Config }; + // Native Node ESM cannot import .ts files. Use tsx's scoped importer only for + // TypeScript configs; JavaScript configs stay on the native import path. + const module = ( + path.endsWith('.ts') + ? await (await import('tsx/esm/api')).tsImport(fileUrl, import.meta.url) + : await import(fileUrl) + ) as { default?: Config }; const config = module.default; if (!config) { diff --git a/src/core/runner.ts b/src/core/runner.ts index e01cfae..9551afd 100644 --- a/src/core/runner.ts +++ b/src/core/runner.ts @@ -17,7 +17,7 @@ import { TextReporter } from '../reporters/text.js'; import { SummaryLogReporter } from '../reporters/summary-log.js'; import type { Reporter } from '../reporters/types.js'; import { loadCustomReporter, isReporterPath } from '../reporters/custom.js'; -import { cleanSuiteArtifacts, type SuiteArtifacts } from '../utils/files.js'; +import { acquireArtifactsLock, cleanSuiteArtifacts, type SuiteArtifacts } from '../utils/files.js'; export interface RunOptions { /** Override config.parallel for this run. */ @@ -76,54 +76,59 @@ export class TestRunner { async run(suiteNames?: string[], options: RunOptions = {}): Promise { const cwd = options.cwd ?? this.cwd; const artifactsDir = resolve(cwd, this.config.artifactsDir); + const releaseArtifactsLock = await acquireArtifactsLock(artifactsDir); + + try { + // Load environment-specific .env file + let envFileVars: Record = {}; + if (options.env) { + const envResult = loadEnvFile(options.env, cwd); + envFileVars = envResult.env; + } - // Load environment-specific .env file - let envFileVars: Record = {}; - if (options.env) { - const envResult = loadEnvFile(options.env, cwd); - envFileVars = envResult.env; + // Determine which suites will run + const suitesToRun = + suiteNames && suiteNames.length > 0 + ? this.config.suites.filter( + (s) => suiteNames.includes(s.name) || suiteNames.includes(s.name.toLowerCase()), + ) + : this.config.suites; + + // Clean only artifacts for suites being run (preserves other suites' artifacts) + const suiteArtifacts: SuiteArtifacts[] = suitesToRun.map((s) => ({ + name: s.name, + resultFile: s.resultFile, + logFile: s.logFile ?? s.resultFile.replace('.json', '.log'), + })); + await cleanSuiteArtifacts(artifactsDir, suiteArtifacts); + + // Create reporters (use override if provided) + const reporterNames = + options.reporter && options.reporter.length > 0 ? options.reporter : this.config.reporters; + const reporters = await this.createReporters(artifactsDir, reporterNames, cwd); + + // Create and run orchestrator + const effectiveConfig: ValidatedConfig = { + ...this.config, + ...(options.parallel !== undefined ? { parallel: options.parallel } : {}), + ...(options.failFast !== undefined ? { failFast: options.failFast } : {}), + }; + const orchestrator = new Orchestrator({ + config: effectiveConfig, + reporters, + environment: options.env, + cwd, + passThrough: options.passThrough, + envFileVars, + grep: options.grep, + grepInvert: options.grepInvert, + file: options.file, + }); + + return await orchestrator.run(suiteNames); + } finally { + await releaseArtifactsLock(); } - - // Determine which suites will run - const suitesToRun = - suiteNames && suiteNames.length > 0 - ? this.config.suites.filter( - (s) => suiteNames.includes(s.name) || suiteNames.includes(s.name.toLowerCase()), - ) - : this.config.suites; - - // Clean only artifacts for suites being run (preserves other suites' artifacts) - const suiteArtifacts: SuiteArtifacts[] = suitesToRun.map((s) => ({ - name: s.name, - resultFile: s.resultFile, - logFile: s.logFile ?? s.resultFile.replace('.json', '.log'), - })); - await cleanSuiteArtifacts(artifactsDir, suiteArtifacts); - - // Create reporters (use override if provided) - const reporterNames = - options.reporter && options.reporter.length > 0 ? options.reporter : this.config.reporters; - const reporters = await this.createReporters(artifactsDir, reporterNames, cwd); - - // Create and run orchestrator - const effectiveConfig: ValidatedConfig = { - ...this.config, - ...(options.parallel !== undefined ? { parallel: options.parallel } : {}), - ...(options.failFast !== undefined ? { failFast: options.failFast } : {}), - }; - const orchestrator = new Orchestrator({ - config: effectiveConfig, - reporters, - environment: options.env, - cwd, - passThrough: options.passThrough, - envFileVars, - grep: options.grep, - grepInvert: options.grepInvert, - file: options.file, - }); - - return orchestrator.run(suiteNames); } private async createReporters( diff --git a/src/utils/files.ts b/src/utils/files.ts index 55af893..9799394 100644 --- a/src/utils/files.ts +++ b/src/utils/files.ts @@ -2,9 +2,10 @@ * File Utilities */ -import { mkdir, rm, writeFile, unlink } from 'node:fs/promises'; +import { randomUUID } from 'node:crypto'; +import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises'; import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; +import { basename, dirname, join } from 'node:path'; /** * Ensure directory exists @@ -26,10 +27,7 @@ export async function cleanDir(path: string): Promise { /** * Write file with parent directory creation */ -export async function writeFileWithDir( - path: string, - content: string, -): Promise { +export async function writeFileWithDir(path: string, content: string): Promise { await ensureDir(dirname(path)); await writeFile(path, content); } @@ -60,15 +58,11 @@ export async function cleanSuiteArtifacts( for (const suite of suites) { // Delete result file const resultPath = join(artifactsDir, suite.resultFile); - if (existsSync(resultPath)) { - await unlink(resultPath); - } + await rm(resultPath, { force: true }); // Delete log file const logPath = join(artifactsDir, suite.logFile); - if (existsSync(logPath)) { - await unlink(logPath); - } + await rm(logPath, { force: true }); // Delete failures directory for this suite // Use same sanitization as markdown.ts for consistency @@ -79,3 +73,98 @@ export async function cleanSuiteArtifacts( } } } + +export interface ArtifactsLockOptions { + pollIntervalMs?: number; +} + +/** + * Serialize independent Testfold processes that target the same artifacts directory. + * A whole-run lock is required: locking cleanup alone still lets two runners overwrite + * each other's JSON/log files before either parser reads them. + */ +export async function acquireArtifactsLock( + artifactsDir: string, + options: ArtifactsLockOptions = {}, +): Promise<() => Promise> { + await ensureDir(dirname(artifactsDir)); + const lockDir = join(dirname(artifactsDir), `.${basename(artifactsDir)}.testfold-lock`); + const ownerPath = join(lockDir, 'owner.json'); + const pollIntervalMs = options.pollIntervalMs ?? 100; + if (!Number.isSafeInteger(pollIntervalMs) || pollIntervalMs < 1) { + throw new Error('Artifact lock poll interval must be a positive integer'); + } + + while (true) { + const owner = { pid: process.pid, nonce: randomUUID() }; + try { + await mkdir(lockDir); + } catch (error) { + if (!isAlreadyExists(error)) throw error; + if (await lockOwnerIsGone(lockDir, ownerPath)) { + await rm(lockDir, { recursive: true, force: true }); + continue; + } + await new Promise((resolveDelay) => setTimeout(resolveDelay, pollIntervalMs)); + continue; + } + try { + await writeFile(ownerPath, JSON.stringify(owner)); + } catch (error) { + await rm(lockDir, { recursive: true, force: true }); + throw error; + } + let released = false; + return async () => { + if (released) return; + released = true; + if (await lockIsOwnedBy(ownerPath, owner)) { + await rm(lockDir, { recursive: true, force: true }); + } + }; + } +} + +async function lockIsOwnedBy( + ownerPath: string, + owner: { pid: number; nonce: string }, +): Promise { + try { + return (await readFile(ownerPath, 'utf8')) === JSON.stringify(owner); + } catch { + return false; + } +} + +async function lockOwnerIsGone(lockDir: string, ownerPath: string): Promise { + try { + const owner = JSON.parse(await readFile(ownerPath, 'utf8')) as { pid?: unknown }; + if (!Number.isSafeInteger(owner.pid) || Number(owner.pid) <= 0) return true; + try { + process.kill(Number(owner.pid), 0); + return false; + } catch (error) { + return ( + typeof error === 'object' && + error !== null && + 'code' in error && + (error as NodeJS.ErrnoException).code === 'ESRCH' + ); + } + } catch { + try { + return Date.now() - (await stat(lockDir)).mtimeMs > 5_000; + } catch { + return false; + } + } +} + +function isAlreadyExists(error: unknown): boolean { + return ( + typeof error === 'object' && + error !== null && + 'code' in error && + (error as NodeJS.ErrnoException).code === 'EEXIST' + ); +} diff --git a/tests/unit/config/loader.test.ts b/tests/unit/config/loader.test.ts new file mode 100644 index 0000000..052d593 --- /dev/null +++ b/tests/unit/config/loader.test.ts @@ -0,0 +1,50 @@ +import { mkdir, rm, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; + +const execFileAsync = promisify(execFile); + +describe('loadConfig', () => { + const tempRoot = join(process.cwd(), 'claude-temp-files', 'config-loader-test'); + + beforeEach(async () => { + await rm(tempRoot, { recursive: true, force: true }); + await mkdir(tempRoot, { recursive: true }); + }); + + afterAll(async () => { + await rm(tempRoot, { recursive: true, force: true }); + }); + + it('loads a TypeScript config through the published runtime', async () => { + const configPath = join(tempRoot, 'test-runner.config.ts'); + await writeFile( + configPath, + ` + type SuiteName = 'Unit'; + export default { + artifactsDir: './test-results', + suites: [{ name: 'Unit' as SuiteName, type: 'jest', command: 'true', resultFile: 'unit.json' }], + }; + `, + ); + + const script = ` + const { loadConfig } = await import(${JSON.stringify(new URL('../../../dist/config/loader.js', import.meta.url).href)}); + const config = await loadConfig(${JSON.stringify(configPath)}); + process.stdout.write(JSON.stringify(config)); + `; + const { stdout } = await execFileAsync(process.execPath, [ + '--input-type=module', + '--eval', + script, + ]); + expect(JSON.parse(stdout)).toEqual( + expect.objectContaining({ + artifactsDir: './test-results', + suites: [expect.objectContaining({ name: 'Unit', type: 'jest' })], + }), + ); + }); +}); diff --git a/tests/unit/utils/files-cleanup.test.ts b/tests/unit/utils/files-cleanup.test.ts index 1c01257..482a4e9 100644 --- a/tests/unit/utils/files-cleanup.test.ts +++ b/tests/unit/utils/files-cleanup.test.ts @@ -4,7 +4,11 @@ import { join } from 'node:path'; import { mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs'; -import { cleanSuiteArtifacts, type SuiteArtifacts } from '../../../src/utils/files.js'; +import { + acquireArtifactsLock, + cleanSuiteArtifacts, + type SuiteArtifacts, +} from '../../../src/utils/files.js'; describe('cleanSuiteArtifacts', () => { const testDir = join(process.cwd(), 'test-cleanup-artifacts'); @@ -150,4 +154,20 @@ describe('cleanSuiteArtifacts', () => { expect(existsSync(join(testDir, 'e2e.json'))).toBe(true); expect(existsSync(join(testDir, 'e2e.log'))).toBe(true); }); + + it('serializes independent runs that share one artifacts directory', async () => { + const releaseFirst = await acquireArtifactsLock(testDir, { pollIntervalMs: 5 }); + let secondAcquired = false; + const second = acquireArtifactsLock(testDir, { pollIntervalMs: 5 }).then(async (release) => { + secondAcquired = true; + await release(); + }); + + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(secondAcquired).toBe(false); + + await releaseFirst(); + await second; + expect(secondAcquired).toBe(true); + }); });