testEditNeverRenamesAFunction

tcmts.cli.test.e2e.editing.testEditNeverRenamesAFunction · #kvpoaofj · term

A function's or class's name is part of its hash: tcm edit won't write two declared alike in one file (one would have to be renamed, and so be another definition), nor add one to a file declaring its name; and a scratch file declaring one as find_1 where the codebase has find isn't saved.

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 testEditNeverRenamesAFunction = () => {
  const dir = tempDir("edit never renames a function");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.rmSync(path.join(dir, "scratch.ts"), { force: true });
  const write = (f: string, text: string) => fs.writeFileSync(path.join(dir, f), text);
  write(
    "a.ts",
    `// tcm: namespace a\nexport function find(xs: number[]): number {\n  return xs[0]!;\n}\n`,
  );
  write(
    "b.ts",
    `// tcm: namespace b\nexport function find(xs: number[]): number {\n  return xs[1]!;\n}\n`,
  );
  ok("add", "a.ts", "b.ts");
  const together = tcmIn(dir, "edit", "a.find", "b.find");
  assert.notEqual(together.status, 0);
  assert.match(together.out, /part of its hash/, together.out);
  ok("edit", "a.find");
  const scratch = path.join(dir, "scratch.ts");
  fs.writeFileSync(scratch, fs.readFileSync(scratch, "utf8").replace("xs[0]!", "xs[0] ?? 0"));
  const into = tcmIn(dir, "edit", "b.find");
  assert.notEqual(into.status, 0);
  assert.match(into.out, /part of its hash/, into.out);
  // written so by hand (or by an older tcm)
  fs.writeFileSync(
    scratch,
    `// tcm: namespace b\n// tcm: path b.find\nexport function find_1(xs: number[]): number {\n  return xs[1] ?? 0;\n}\n`,
  );
  const saved = tcmIn(dir, "update");
  assert.notEqual(saved.status, 0);
  assert.match(saved.out, /declared find_1 .* and find in the codebase/, saved.out);
  assert.match(ok("view", "b.find"), /export function find\(/);
}

uses