|
| 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' | 'missing-output' | 'content-mismatch' | 'orphaned-output'; |
| 8 | + message: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface TestError { |
| 12 | + testName: string; |
| 13 | + type: 'invalid-filter' | 'empty-scip'; |
| 14 | + message: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface ValidationResults { |
| 18 | + passed: string[]; |
| 19 | + failed: TestFailure[]; |
| 20 | + errors: TestError[]; |
| 21 | +} |
| 22 | + |
| 23 | +export interface TestRunnerOptions { |
| 24 | + snapshotRoot: string; |
| 25 | + filterTests?: string; |
| 26 | + failFast?: boolean; |
| 27 | + quiet?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +export interface SingleTestOptions { |
| 31 | + check: boolean; |
| 32 | + quiet: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +function validateFilterTestNames(inputDirectory: string, filterTestNames: string[]): TestError[] { |
| 36 | + const availableTests = fs.readdirSync(inputDirectory); |
| 37 | + const missingTests = filterTestNames.filter(name => !availableTests.includes(name)); |
| 38 | + |
| 39 | + if (missingTests.length > 0) { |
| 40 | + return [{ |
| 41 | + testName: missingTests.join(', '), |
| 42 | + type: 'invalid-filter', |
| 43 | + message: `The following test names were not found: ${missingTests.join(', ')}. Available tests: ${availableTests.join(', ')}` |
| 44 | + }]; |
| 45 | + } |
| 46 | + |
| 47 | + return []; |
| 48 | +} |
| 49 | + |
| 50 | +function detectOrphanedOutputs(inputDirectory: string, outputDirectory: string): TestFailure[] { |
| 51 | + if (!fs.existsSync(outputDirectory)) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + const inputTests = new Set(fs.readdirSync(inputDirectory)); |
| 56 | + const outputTests = fs.readdirSync(outputDirectory); |
| 57 | + const orphanedOutputs: TestFailure[] = []; |
| 58 | + |
| 59 | + for (const outputTest of outputTests) { |
| 60 | + if (!inputTests.has(outputTest)) { |
| 61 | + orphanedOutputs.push({ |
| 62 | + testName: outputTest, |
| 63 | + type: 'orphaned-output', |
| 64 | + message: `Output folder exists but no corresponding input folder found` |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return orphanedOutputs; |
| 70 | +} |
| 71 | + |
| 72 | +function reportResults(results: ValidationResults, failFast: boolean): void { |
| 73 | + const totalTests = results.passed.length + results.failed.length; |
| 74 | + const errorCount = results.errors.length; |
| 75 | + |
| 76 | + // Report errors first |
| 77 | + for (const error of results.errors) { |
| 78 | + console.error(`ERROR [${error.testName}]: ${error.message}`); |
| 79 | + } |
| 80 | + |
| 81 | + // Report failures |
| 82 | + for (const failure of results.failed) { |
| 83 | + console.error(`FAIL [${failure.testName}]: ${failure.message}`); |
| 84 | + } |
| 85 | + |
| 86 | + // Report summary |
| 87 | + if (totalTests > 0 || errorCount > 0) { |
| 88 | + console.log(`\n${results.passed.length}/${totalTests} tests passed, ${results.failed.length} failed, ${errorCount} errored`); |
| 89 | + } |
| 90 | + |
| 91 | + // Exit with non-zero status if there were failures or errors |
| 92 | + if (results.failed.length > 0 || results.errors.length > 0) { |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export class TestRunner { |
| 98 | + constructor(private options: TestRunnerOptions) {} |
| 99 | + |
| 100 | + async runTests( |
| 101 | + singleTestRunner: (testName: string, inputDir: string, outputDir: string, options: SingleTestOptions) => Promise<ValidationResults> |
| 102 | + ): Promise<void> { |
| 103 | + const inputDirectory = path.resolve(join(this.options.snapshotRoot, 'input')); |
| 104 | + const outputDirectory = path.resolve(join(this.options.snapshotRoot, 'output')); |
| 105 | + const failFast = this.options.failFast ?? false; |
| 106 | + |
| 107 | + const results: ValidationResults = { |
| 108 | + passed: [], |
| 109 | + failed: [], |
| 110 | + errors: [] |
| 111 | + }; |
| 112 | + |
| 113 | + // Pre-execution validation: determine test directories to process |
| 114 | + let snapshotDirectories = fs.readdirSync(inputDirectory); |
| 115 | + let isFilterMode = false; |
| 116 | + |
| 117 | + if (this.options.filterTests) { |
| 118 | + // Filter to specific tests |
| 119 | + const filterTestNames = this.options.filterTests.split(',').map(name => name.trim()); |
| 120 | + isFilterMode = true; |
| 121 | + |
| 122 | + // Validate filter test names exist |
| 123 | + const filterErrors = validateFilterTestNames(inputDirectory, filterTestNames); |
| 124 | + if (filterErrors.length > 0) { |
| 125 | + results.errors.push(...filterErrors); |
| 126 | + reportResults(results, failFast); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + snapshotDirectories = snapshotDirectories.filter(dir => filterTestNames.includes(dir)); |
| 131 | + } |
| 132 | + |
| 133 | + // In non-filtering mode, detect orphaned outputs that should be cleaned up |
| 134 | + if (!isFilterMode) { |
| 135 | + const orphanedOutputs = detectOrphanedOutputs(inputDirectory, outputDirectory); |
| 136 | + |
| 137 | + // For orphaned outputs in check mode, report as failures |
| 138 | + if (orphanedOutputs.length > 0) { |
| 139 | + results.failed.push(...orphanedOutputs); |
| 140 | + |
| 141 | + if (failFast) { |
| 142 | + reportResults(results, failFast); |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Process each test directory |
| 149 | + for (const testName of snapshotDirectories) { |
| 150 | + if (!this.options.quiet) { |
| 151 | + console.log(`Processing test: ${testName}`); |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + const testResults = await singleTestRunner( |
| 156 | + testName, |
| 157 | + inputDirectory, |
| 158 | + outputDirectory, |
| 159 | + { |
| 160 | + check: true, // We'll determine this based on the context |
| 161 | + quiet: this.options.quiet ?? false |
| 162 | + } |
| 163 | + ); |
| 164 | + |
| 165 | + // Merge results |
| 166 | + results.passed.push(...testResults.passed); |
| 167 | + results.failed.push(...testResults.failed); |
| 168 | + results.errors.push(...testResults.errors); |
| 169 | + |
| 170 | + // Check for fail-fast condition |
| 171 | + if (failFast && (testResults.failed.length > 0 || testResults.errors.length > 0)) { |
| 172 | + reportResults(results, failFast); |
| 173 | + return; |
| 174 | + } |
| 175 | + } catch (error) { |
| 176 | + results.errors.push({ |
| 177 | + testName, |
| 178 | + type: 'empty-scip', |
| 179 | + message: `Test runner failed: ${error}` |
| 180 | + }); |
| 181 | + |
| 182 | + if (failFast) { |
| 183 | + reportResults(results, failFast); |
| 184 | + return; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Report final results |
| 190 | + reportResults(results, failFast); |
| 191 | + } |
| 192 | +} |
0 commit comments