Skip to content

Commit 488656a

Browse files
committed
Protect existing files when saving CLI repairs
1 parent ff543a6 commit 488656a

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎bin/articulate-xliff-doctor.js‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env node
22

3-
import { readFile, writeFile } from "node:fs/promises";
3+
import { link, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
44
import path from "node:path";
55
import { analyze, repair } from "../src/index.js";
66

77
const USAGE = `Usage:
88
articulate-xliff-doctor analyze <original.xlf> <translated.xlf> [--json]
9-
articulate-xliff-doctor repair <original.xlf> <translated.xlf> --out <repaired.xlf>
9+
articulate-xliff-doctor repair <original.xlf> <translated.xlf> --out <new-repaired.xlf>
1010
1111
Examples:
1212
articulate-xliff-doctor analyze original.xlf translated.xlf
@@ -36,7 +36,7 @@ async function main(argv) {
3636
}
3737

3838
if (command === "repair" && !outPath) {
39-
console.error("Repair requires --out <repaired.xlf>.");
39+
console.error("Repair requires --out <new-repaired.xlf>.");
4040
console.error(USAGE);
4141
return 1;
4242
}
@@ -64,7 +64,7 @@ async function main(argv) {
6464
return 2;
6565
}
6666

67-
await writeFile(outPath, result.text, "utf8");
67+
await writeNewOutput(outPath, result.text);
6868
if (json) {
6969
console.log(JSON.stringify({ ...result, output: outPath, text: undefined }, null, 2));
7070
} else {
@@ -74,6 +74,23 @@ async function main(argv) {
7474
return 0;
7575
}
7676

77+
async function writeNewOutput(outPath, text) {
78+
const stagingDirectory = await mkdtemp(path.join(path.dirname(outPath), ".untwine-output-"));
79+
try {
80+
const stagingFile = path.join(stagingDirectory, "repaired.xlf");
81+
await writeFile(stagingFile, text, "utf8");
82+
// Create the final name only after the full file is written, without replacing any entry.
83+
await link(stagingFile, outPath);
84+
} catch (error) {
85+
if (error.code === "EEXIST") {
86+
throw new Error("Output already exists. Choose a new file for --out.");
87+
}
88+
throw error;
89+
} finally {
90+
await rm(stagingDirectory, { recursive: true, force: true });
91+
}
92+
}
93+
7794
function parseArgs(args) {
7895
const positional = [];
7996
let json = false;

‎test/articulate-xliff-doctor.test.mjs‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert/strict";
22
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";
44
import os from "node:os";
55
import path from "node:path";
66
import { test } from "node:test";
@@ -69,6 +69,50 @@ test("rejects undecodable input on either side and preserves valid mislabeled UT
6969
}
7070
});
7171

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+
72116
test("runs the analyzer and repair flow across the public proof corpus", async () => {
73117
const manifest = JSON.parse(await readCorpus("proof-corpus/manifest.json"));
74118

0 commit comments

Comments
 (0)