testConflictFileImportsWhatItsCodeUses

tcmts.cli.test.e2e.merge.testConflictFileImportsWhatItsCodeUses · #wbahkpuq · term

A conflicted definition's file imports what its code uses: not y, which only ours used, on a line theirs changed (merged, nothing uses it), but x, which ours' side of the conflict marked uses. Settled for theirs, nothing uses x either, and saving the file takes its import out.

import { okIn, tcmIn, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testConflictFileImportsWhatItsCodeUses = () => {
  const dir = tempDir("merge imports");
  const tcm = (...args: string[]) => okIn(dir, ...args);
  const scratch = path.join(dir, "scratch.ts");
  const f = (a: string, b: string) =>
    `export const f = (): number => {\n  const a = ${a};\n  const pad = 0;\n  const pad2 = 0;\n  const b = ${b};\n  return a + b + pad + pad2;\n};`;
  tcm("init");
  fs.writeFileSync(
    scratch,
    `// tcm: namespace lib\nexport const x = (): number => 5;\nexport const y = (): number => 7;\n${f("1", "y()")}\n`,
  );
  tcm("update");
  tcm("branch", "s");
  tcm("switch", "s");
  tcm(
    "patch",
    "lib.f",
    "--edits",
    JSON.stringify([{ old: "const a = 1;", new: "const a = x() * 10;" }]),
  );
  tcm("switch", "main");
  tcm(
    "patch",
    "lib.f",
    "--edits",
    JSON.stringify([
      { old: "const a = 1;", new: "const a = 100;" },
      { old: "const b = y();", new: "const b = 2;" },
    ]),
  );
  tcm("switch", "s");
  assert.equal(tcmIn(dir, "merge", "main").status, 1);
  assert.match(tcm("edit", "lib.f"), /1 conflict marked/);
  const file = path.join(dir, "lib.f.ts");
  const text = fs.readFileSync(file, "utf8");
  assert.match(text, /import \{ x \} from "#codebase\/lib";/, text);
  assert.doesNotMatch(text, /\by\b/, text);
  assert.match(text, /const b = 2;/, text);
  fs.writeFileSync(
    file,
    text.replace(/<<<<<<< [\s\S]*>>>>>>> theirs \(main\)\n/, "  const a = 100;\n"),
  );
  const out = tcm("update", "lib.f.ts");
  assert.match(out, /took out imports nothing uses: x/, out);
  assert.doesNotMatch(fs.readFileSync(file, "utf8"), /import/);
  assert.equal(tcm("run", "lib.f").trim(), "102");
}

uses