| 1 | import path from "path"; |
| 2 | import fs from "fs"; |
| 3 | import child from "child_process"; |
| 4 | import { report } from "./utils"; |
| 5 | |
| 6 | const fixturesPath = path.resolve(__dirname, "fixtures"); |
| 7 | |
| 8 | function runFixtures() { |
| 9 | let hadErrors = false; |
| 10 | const fixtures = fs.readdirSync(fixturesPath, { withFileTypes: true }); |
| 11 | for (const fixture of fixtures) { |
| 12 | if (!fixture.isDirectory()) continue; |
| 13 | const fixturePath = path.resolve(fixturesPath, fixture.name); |
| 14 | |
| 15 | const command = path.resolve(fixturePath, "command.ts"); |
| 16 | const expected = path.resolve(fixturePath, "expected.ts"); |
| 17 | const output = path.resolve(fixturePath, "output.ts"); |
| 18 | const options = path.resolve(fixturePath, "options.json"); |
| 19 | |
| 20 | const args: string[] = []; |
| 21 | if (fs.existsSync(options)) { |
| 22 | const parsedOptions = JSON.parse(fs.readFileSync(options, { encoding: "utf8" })); |
| 23 | if (parsedOptions.flags) args.push(parsedOptions.flags); |
| 24 | args.push(parsedOptions.subcommandName || "generate-fig-spec"); |
| 25 | } else { |
| 26 | args.push("generate-fig-spec"); // default generate subcommand |
| 27 | } |
| 28 | |
| 29 | if (!fs.existsSync(command)) { |
| 30 | hadErrors = true; |
| 31 | continue; |
| 32 | } |
| 33 | try { |
| 34 | const cmd = `node -r tsx/cjs ${command} ${args.join(" ")} > ${output}`; |
| 35 | child.execSync(cmd); |
| 36 | } catch { |
| 37 | report(fixture.name, "errored"); |
| 38 | hadErrors = true; |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | if (!fs.existsSync(expected) || process.env.OVERWRITE) { |
| 43 | fs.copyFileSync(output, expected); |
| 44 | report(fixture.name, process.env.OVERWRITE ? "regenerated" : "generated"); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | const outputFile = fs.readFileSync(output); |
| 49 | const expectedFile = fs.readFileSync(expected); |
| 50 | |
| 51 | const successful = outputFile.equals(expectedFile); |
| 52 | if (!successful) { |
| 53 | hadErrors = true; |
| 54 | } |
| 55 | report(fixture.name, successful ? "successful" : "failed"); |
| 56 | } |
| 57 | |
| 58 | if (hadErrors) { |
| 59 | process.exit(1); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | runFixtures(); |