|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import test from "node:test"; |
| 5 | +import { readFile } from "node:fs/promises"; |
| 6 | +import { spawnSync } from "node:child_process"; |
| 7 | + |
| 8 | +const testDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const repoRoot = path.resolve(testDir, ".."); |
| 10 | + |
| 11 | +const scenarios = [ |
| 12 | + { |
| 13 | + name: "clean-project audit output stays stable", |
| 14 | + args: ["audit", "./test/fixtures/clean-project"], |
| 15 | + goldenFile: "clean-project.audit.txt", |
| 16 | + expectedStatus: 0, |
| 17 | + targetDir: path.join(repoRoot, "test", "fixtures", "clean-project"), |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "clean-project doctor output stays stable", |
| 21 | + args: ["doctor", "./test/fixtures/clean-project"], |
| 22 | + goldenFile: "clean-project.doctor.txt", |
| 23 | + expectedStatus: 0, |
| 24 | + targetDir: path.join(repoRoot, "test", "fixtures", "clean-project"), |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "reference env audit output stays stable", |
| 28 | + args: ["audit", "./test/fixtures/reference-env"], |
| 29 | + goldenFile: "env-missing-example.audit.txt", |
| 30 | + expectedStatus: 1, |
| 31 | + targetDir: path.join(repoRoot, "test", "fixtures", "reference-env"), |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "reference env fix dry-run output stays stable", |
| 35 | + args: ["fix", "./test/fixtures/reference-env", "--dry-run"], |
| 36 | + goldenFile: "env-missing-example.fix-dry-run.txt", |
| 37 | + expectedStatus: 1, |
| 38 | + targetDir: path.join(repoRoot, "test", "fixtures", "reference-env"), |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "reference tsconfig audit output stays stable", |
| 42 | + args: ["audit", "./test/fixtures/reference-tsconfig"], |
| 43 | + goldenFile: "tsconfig-missing-alias.audit.txt", |
| 44 | + expectedStatus: 1, |
| 45 | + targetDir: path.join(repoRoot, "test", "fixtures", "reference-tsconfig"), |
| 46 | + }, |
| 47 | +]; |
| 48 | + |
| 49 | +for (const scenario of scenarios) { |
| 50 | + test(scenario.name, async () => { |
| 51 | + const result = spawnSync(process.execPath, ["./bin/maximus.js", ...scenario.args], { |
| 52 | + cwd: repoRoot, |
| 53 | + encoding: "utf8", |
| 54 | + }); |
| 55 | + |
| 56 | + assert.equal(result.status, scenario.expectedStatus, result.stderr); |
| 57 | + |
| 58 | + const actual = normalizeOutput(result.stdout, scenario.targetDir); |
| 59 | + const golden = normalizeOutput( |
| 60 | + await readFile(path.join(repoRoot, "test", "golden-rust", scenario.goldenFile), "utf8"), |
| 61 | + scenario.targetDir, |
| 62 | + ); |
| 63 | + |
| 64 | + assert.equal(actual, golden); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function normalizeOutput(output, targetDir) { |
| 69 | + return output.replaceAll("\r\n", "\n").replaceAll(targetDir, "<TARGET>").trimEnd(); |
| 70 | +} |
0 commit comments