|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import { spawnSync } from "node:child_process"; |
3 | | -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { link, mkdtemp, readFile, readdir, rm, symlink, writeFile } from "node:fs/promises"; |
4 | 4 | import os from "node:os"; |
5 | 5 | import path from "node:path"; |
6 | 6 | import { test } from "node:test"; |
@@ -69,6 +69,50 @@ test("rejects undecodable input on either side and preserves valid mislabeled UT |
69 | 69 | } |
70 | 70 | }); |
71 | 71 |
|
| 72 | +test("CLI repair creates a new output without overwriting existing files or aliases", async () => { |
| 73 | + const directory = await mkdtemp(path.join(os.tmpdir(), "untwine-cli-fresh-output-")); |
| 74 | + try { |
| 75 | + const original = path.join(directory, "original.xlf"); |
| 76 | + const translation = path.join(directory, "translated.xlf"); |
| 77 | + const existing = path.join(directory, "existing.xlf"); |
| 78 | + const aliased = path.join(directory, "hardlink.xlf"); |
| 79 | + const symbolic = path.join(directory, "symlink.xlf"); |
| 80 | + const output = path.join(directory, "repaired.xlf"); |
| 81 | + const originalBytes = await readFile(path.join(root, "demo-files/demo-articulate-original.xlf")); |
| 82 | + const translatedBytes = await readFile(path.join(root, "demo-files/demo-articulate-translated-safe-repair.xlf")); |
| 83 | + |
| 84 | + await writeFile(original, originalBytes); |
| 85 | + await writeFile(translation, translatedBytes); |
| 86 | + await writeFile(existing, "Earlier repair"); |
| 87 | + await link(original, aliased); |
| 88 | + await symlink(translation, symbolic); |
| 89 | + |
| 90 | + const run = (destination) => spawnSync(process.execPath, [ |
| 91 | + cliPath, "repair", original, translation, "--out", destination |
| 92 | + ], { encoding: "utf8" }); |
| 93 | + for (const destination of [original, translation, existing, aliased, symbolic]) { |
| 94 | + const result = run(destination); |
| 95 | + assert.equal(result.status, 1); |
| 96 | + assert.match(result.stderr, /Output already exists.*Choose a new file/); |
| 97 | + } |
| 98 | + assert.deepEqual(await readFile(original), originalBytes); |
| 99 | + assert.deepEqual(await readFile(translation), translatedBytes); |
| 100 | + assert.equal(await readFile(existing, "utf8"), "Earlier repair"); |
| 101 | + assert.deepEqual(await readFile(aliased), originalBytes); |
| 102 | + assert.deepEqual(await readFile(symbolic), translatedBytes); |
| 103 | + |
| 104 | + const result = run(output); |
| 105 | + assert.equal(result.status, 0, result.stderr); |
| 106 | + const analyzed = spawnSync(process.execPath, [cliPath, "analyze", original, output], { encoding: "utf8" }); |
| 107 | + assert.equal(analyzed.status, 0, analyzed.stderr); |
| 108 | + assert.match(analyzed.stdout, /Verdict: clean/); |
| 109 | + assert.deepEqual((await readdir(directory)).sort(), |
| 110 | + ["existing.xlf", "hardlink.xlf", "original.xlf", "repaired.xlf", "symlink.xlf", "translated.xlf"]); |
| 111 | + } finally { |
| 112 | + await rm(directory, { recursive: true, force: true }); |
| 113 | + } |
| 114 | +}); |
| 115 | + |
72 | 116 | test("runs the analyzer and repair flow across the public proof corpus", async () => { |
73 | 117 | const manifest = JSON.parse(await readCorpus("proof-corpus/manifest.json")); |
74 | 118 |
|
|
0 commit comments