|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +export interface TestFailure { |
| 6 | + testName: string; |
| 7 | + type: 'empty-scip-index' | 'missing-output' | 'content-mismatch' | 'orphaned-output' | 'caught-exception'; |
| 8 | + message: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface ValidationResults { |
| 12 | + passed: string[]; |
| 13 | + failed: TestFailure[]; |
| 14 | + skipped: string[]; |
| 15 | +} |
| 16 | + |
| 17 | +export interface TestRunnerOptions { |
| 18 | + snapshotRoot: string; |
| 19 | + filterTests?: string; |
| 20 | + failFast: boolean; |
| 21 | + quiet: boolean; |
| 22 | + mode: 'check' | 'update'; |
| 23 | +} |
| 24 | + |
| 25 | +export interface SingleTestOptions { |
| 26 | + check: boolean; |
| 27 | + quiet: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +function validateFilterTestNames(inputDirectory: string, filterTestNames: string[]): void { |
| 31 | + const availableTests = fs.readdirSync(inputDirectory); |
| 32 | + const missingTests = filterTestNames.filter(name => !availableTests.includes(name)); |
| 33 | + |
| 34 | + if (missingTests.length > 0) { |
| 35 | + console.error(`ERROR: The following test names were not found: ${missingTests.join(', ')}. Available tests: ${availableTests.join(', ')}`); |
| 36 | + process.exit(1); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function handleOrphanedOutputs(inputTests: Set<string>, outputDirectory: string, mode: 'check' | 'update'): TestFailure[] { |
| 41 | + if (!fs.existsSync(outputDirectory)) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + const outputTests = fs.readdirSync(outputDirectory); |
| 46 | + const orphanedOutputs: TestFailure[] = []; |
| 47 | + |
| 48 | + for (const outputTest of outputTests) { |
| 49 | + if (inputTests.has(outputTest)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (mode === 'update') { |
| 53 | + const orphanedPath = path.join(outputDirectory, outputTest); |
| 54 | + fs.rmSync(orphanedPath, { recursive: true, force: true }); |
| 55 | + console.log(`Delete output folder with no corresponding input folder: ${outputTest}`); |
| 56 | + continue; |
| 57 | + } |
| 58 | + orphanedOutputs.push({ |
| 59 | + testName: outputTest, |
| 60 | + type: 'orphaned-output', |
| 61 | + message: `Output folder exists but no corresponding input folder found` |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + return orphanedOutputs; |
| 66 | +} |
| 67 | + |
| 68 | +function reportResults(results: ValidationResults): void { |
| 69 | + const totalTests = results.passed.length + results.failed.length + results.skipped.length; |
| 70 | + console.assert(totalTests > 0, 'No tests found'); |
| 71 | + |
| 72 | + for (const failure of results.failed) { |
| 73 | + console.error(`FAIL [${failure.testName}]: ${failure.message}`); |
| 74 | + } |
| 75 | + |
| 76 | + let summaryStr = `\n${results.passed.length}/${totalTests} tests passed, ${results.failed.length} failed`; |
| 77 | + if (results.skipped.length > 0) { |
| 78 | + summaryStr += `, ${results.skipped.length} skipped`; |
| 79 | + } |
| 80 | + console.log(summaryStr); |
| 81 | + |
| 82 | + if (results.failed.length > 0) { |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export class TestRunner { |
| 88 | + constructor(private options: TestRunnerOptions) {} |
| 89 | + |
| 90 | + runTests( |
| 91 | + runSingleTest: (testName: string, inputDir: string, outputDir: string) => ValidationResults |
| 92 | + ): void { |
| 93 | + const inputDirectory = path.resolve(join(this.options.snapshotRoot, 'input')); |
| 94 | + const outputDirectory = path.resolve(join(this.options.snapshotRoot, 'output')); |
| 95 | + |
| 96 | + const results: ValidationResults = { |
| 97 | + passed: [], |
| 98 | + failed: [], |
| 99 | + skipped: [] |
| 100 | + }; |
| 101 | + |
| 102 | + let snapshotDirectories = fs.readdirSync(inputDirectory); |
| 103 | + |
| 104 | + const orphanedOutputs = handleOrphanedOutputs(new Set(snapshotDirectories), outputDirectory, this.options.mode); |
| 105 | + if (orphanedOutputs.length > 0) { |
| 106 | + results.failed.push(...orphanedOutputs); |
| 107 | + if (this.options.failFast) { |
| 108 | + reportResults(results); |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (this.options.filterTests) { |
| 114 | + const filterTestNames = this.options.filterTests.split(',').map(name => name.trim()); |
| 115 | + validateFilterTestNames(inputDirectory, filterTestNames); |
| 116 | + snapshotDirectories = snapshotDirectories.filter(dir => filterTestNames.includes(dir)); |
| 117 | + if (snapshotDirectories.length === 0) { |
| 118 | + console.error(`No tests found matching filter: ${this.options.filterTests}`); |
| 119 | + process.exit(1); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for (let i = 0; i < snapshotDirectories.length; i++) { |
| 124 | + const testName = snapshotDirectories[i]; |
| 125 | + if (!this.options.quiet) { |
| 126 | + console.log(`--- Running snapshot test: ${testName} ---`); |
| 127 | + } |
| 128 | + |
| 129 | + let testResults: ValidationResults; |
| 130 | + try { |
| 131 | + testResults = runSingleTest( |
| 132 | + testName, |
| 133 | + inputDirectory, |
| 134 | + outputDirectory, |
| 135 | + ); |
| 136 | + } catch (error) { |
| 137 | + testResults = { |
| 138 | + passed: [], |
| 139 | + failed: [{ |
| 140 | + testName, |
| 141 | + type: 'caught-exception', |
| 142 | + message: `Test runner failed: ${error}` |
| 143 | + }], |
| 144 | + skipped: [] |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + results.passed.push(...testResults.passed); |
| 149 | + results.failed.push(...testResults.failed); |
| 150 | + |
| 151 | + if (this.options.failFast && testResults.failed.length > 0) { |
| 152 | + for (let j = i + 1; j < snapshotDirectories.length; j++) { |
| 153 | + results.skipped.push(snapshotDirectories[j]); |
| 154 | + } |
| 155 | + reportResults(results); |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + reportResults(results); |
| 161 | + } |
| 162 | +} |
0 commit comments