git / tests / plain-diff.test.mjsmain at 517348b
import "@repos/stdlib/testing/mind-loader.mjs";
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
const { plainDiff } = await import("../plain-diff.ts");
const text = (lines) => lines.map((line) => (line.kind === "@" ? line.text : line.kind + line.text)).join("\n");
/** What `git diff` says of the same two texts: the parts alone, without its heading. */
function gitDiff(before, after) {
const folder = mkdtempSync(join(tmpdir(), "plain-diff-"));
try {
writeFileSync(join(folder, "a"), before);
writeFileSync(join(folder, "b"), after);
let out = "";
try {
execFileSync("git", ["diff", "--no-index", "--no-color", "--diff-algorithm=myers", "a", "b"], { cwd: folder });
} catch (error) {
out = String(error.stdout);
}
return out
.split("\n")
.filter((line) => /^[ +\-@]/.test(line) && !/^(---|\+\+\+) /.test(line))
.map((line) => line.replace(/^(@@ [^@]+ @@).*$/, "$1").replace(/(\d+),1 /g, "$1,1 "))
.join("\n");
} finally {
rmSync(folder, { recursive: true, force: true });
}
}
/** git writes `-4` for `-4,1`: the same place. */
const counted = (diff) => diff.replace(/^@@ -(\d+) /gm, "@@ -$1,1 ").replace(/^(@@ -\d+,\d+) \+(\d+) @@/gm, "$1 +$2,1 @@");
test("a changed line is shown as the line that went and the line that came, with three lines around", () => {
const before = "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\n";
const after = before.replace("five", "FIVE");
assert.equal(text(plainDiff(before, after)), "@@ -2,7 +2,7 @@\n two\n three\n four\n-five\n+FIVE\n six\n seven\n eight");
});
test("a file that came or went is every line of it, and two texts with the same lines are nothing", () => {
assert.equal(text(plainDiff("", "hello\nthere\n")), "@@ -0,0 +1,2 @@\n+hello\n+there");
assert.equal(text(plainDiff("bye\n", "")), "@@ -1,1 +0,0 @@\n-bye");
assert.deepEqual(plainDiff("same\n", "same\n"), []);
});
test("changes far apart are parts of their own, each with its place, as git shows them", () => {
const lines = Array.from({ length: 60 }, (_, index) => `line ${index + 1}`);
const next = [...lines];
next[4] = "changed 5";
next.splice(30, 2);
next.splice(50, 0, "came a", "came b");
const before = lines.join("\n") + "\n";
const after = next.join("\n") + "\n";
assert.equal(text(plainDiff(before, after)), counted(gitDiff(before, after)));
});
test("texts of many changed lines give the same parts as git", () => {
let seed = 7;
const random = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648;
for (let round = 0; round < 20; round++) {
const lines = Array.from({ length: 5 + Math.floor(random() * 200) }, (_, index) => `row ${index} ${Math.floor(random() * 4)}`);
const next = lines.flatMap((line) => (random() < 0.08 ? [] : random() < 0.08 ? [line, `new ${Math.floor(random() * 9)}`] : [line]));
const before = lines.join("\n") + "\n";
const after = next.join("\n") + "\n";
const ours = plainDiff(before, after);
// the lines that went and came turn the one text into the other
assert.equal(ours.filter((line) => line.kind === "-").length - ours.filter((line) => line.kind === "+").length, lines.length - next.length);
assert.equal(text(ours), counted(gitDiff(before, after)), `round ${round}`);
}
});
test("a change of more steps than the page works out is the old lines, then the new ones", () => {
const before = Array.from({ length: 2000 }, (_, index) => `old ${index}`).join("\n") + "\n";
const after = Array.from({ length: 2000 }, (_, index) => `new ${index}`).join("\n") + "\n";
const lines = plainDiff(before, after);
assert.equal(lines.length, 4001);
assert.equal(lines[0].text, "@@ -1,2000 +1,2000 @@");
assert.deepEqual(lines[1], { kind: "-", text: "old 0" });
assert.deepEqual(lines.at(-1), { kind: "+", text: "new 1999" });
});