|
| 1 | +import fs from "fs"; |
| 2 | +import { access, readFile, writeFile } from "fs/promises"; |
| 3 | +import assert from "node:assert"; |
| 4 | +import { after, describe, test } from "node:test"; |
| 5 | +import { checkTgz, getProblems, summarizeProblems } from "@arethetypeswrong/core"; |
| 6 | + |
| 7 | +const updateSnapshots = process.env.UPDATE_SNAPSHOTS; |
| 8 | + |
| 9 | +describe("snapshots", async () => { |
| 10 | + const snapshotsWritten: URL[] = []; |
| 11 | + |
| 12 | + after(() => { |
| 13 | + if (updateSnapshots && snapshotsWritten.length > 0) { |
| 14 | + console.log(`Updated ${snapshotsWritten.length} snapshots:`); |
| 15 | + } else if (snapshotsWritten.length > 0) { |
| 16 | + console.log(`Wrote ${snapshotsWritten.length} snapshots:`); |
| 17 | + } |
| 18 | + |
| 19 | + if (snapshotsWritten.length > 0) { |
| 20 | + console.log(snapshotsWritten.map((url) => ` ${url.pathname}`).join("\n")); |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + for (const fixture of fs.readdirSync(new URL("../fixtures", import.meta.url))) { |
| 25 | + test(fixture, async () => { |
| 26 | + const tarball = await readFile(new URL(`../fixtures/${fixture}`, import.meta.url)); |
| 27 | + const analysis = await checkTgz(tarball); |
| 28 | + assert(analysis.containsTypes); |
| 29 | + const problems = getProblems(analysis); |
| 30 | + const summary = summarizeProblems(problems, analysis); |
| 31 | + const snapshotURL = new URL(`../snapshots/${fixture}.md`, import.meta.url); |
| 32 | + const expectedSnapshot = [ |
| 33 | + `# ${fixture}`, |
| 34 | + "", |
| 35 | + "## Summary", |
| 36 | + "", |
| 37 | + "```json", |
| 38 | + JSON.stringify(summary, null, 2), |
| 39 | + "```", |
| 40 | + "", |
| 41 | + "## Problems", |
| 42 | + "", |
| 43 | + "```json", |
| 44 | + JSON.stringify(problems, null, 2), |
| 45 | + "```", |
| 46 | + ].join("\n"); |
| 47 | + |
| 48 | + if ( |
| 49 | + await access(snapshotURL) |
| 50 | + .then(() => true) |
| 51 | + .catch(() => false) |
| 52 | + ) { |
| 53 | + const snapshot = await readFile(snapshotURL, "utf8"); |
| 54 | + if (updateSnapshots) { |
| 55 | + await writeFile(snapshotURL, expectedSnapshot); |
| 56 | + snapshotsWritten.push(snapshotURL); |
| 57 | + } else { |
| 58 | + assert.strictEqual(snapshot, expectedSnapshot); |
| 59 | + } |
| 60 | + } else { |
| 61 | + await writeFile(snapshotURL, expectedSnapshot); |
| 62 | + snapshotsWritten.push(snapshotURL); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | +}); |
0 commit comments