| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | import prettier from "prettier"; |
| 4 | import { getVersionFromVersionedSpec } from "../src/versions"; |
| 5 | |
| 6 | const fixturesPath = path.join(__dirname, "fixtures"); |
| 7 | const dirs = fs |
| 8 | .readdirSync(fixturesPath, { withFileTypes: true }) |
| 9 | .filter((file) => file.isDirectory() && file.name !== ".DS_Store"); |
| 10 | |
| 11 | async function runFixtures() { |
| 12 | let hadErrors = false; |
| 13 | for (const dir of dirs) { |
| 14 | const fixtureDirPath = path.join(fixturesPath, dir.name); |
| 15 | const specVersionPath = path.join(fixtureDirPath, "spec", "1.0.0.js"); |
| 16 | const outputSpecPath = path.join(fixtureDirPath, "output.json"); // gitignored |
| 17 | const expectedSpecPath = path.join(fixtureDirPath, "expected.json"); |
| 18 | const configPath = path.join(fixtureDirPath, "config.json"); |
| 19 | |
| 20 | const { targetVersion } = JSON.parse(fs.readFileSync(configPath, "utf8")); |
| 21 | |
| 22 | // eslint-disable-next-line no-await-in-loop |
| 23 | const importedModule = await import(specVersionPath); // note this is CJS |
| 24 | // eslint-disable-next-line no-await-in-loop |
| 25 | const outputSpec = await prettier.format( |
| 26 | JSON.stringify( |
| 27 | getVersionFromVersionedSpec( |
| 28 | importedModule.completion, |
| 29 | importedModule.versions, |
| 30 | targetVersion |
| 31 | ) |
| 32 | ), |
| 33 | { parser: "json" } |
| 34 | ); |
| 35 | fs.writeFileSync(outputSpecPath, outputSpec); |
| 36 | |
| 37 | if (process.env.OVERWRITE || !fs.existsSync(expectedSpecPath)) { |
| 38 | fs.copyFileSync(outputSpecPath, expectedSpecPath); |
| 39 | } else { |
| 40 | const expectedSpec = fs.readFileSync(expectedSpecPath, { encoding: "utf8" }); |
| 41 | if (expectedSpec !== outputSpec) { |
| 42 | hadErrors = true; |
| 43 | console.error(`- Fixture ${fixtureDirPath} is failing.`); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | if (hadErrors) { |
| 48 | process.exit(1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | runFixtures(); |