testPatchAFile

tcmts.cli.test.e2e.files.testPatchAFile · #t4sium57 · term

tcm patch changes a file kept as written (a page, a stylesheet) as its text, and saves it back as the file it is: not read as code.

import { okIn, 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 testPatchAFile = () => {
  const dir = tempDir("patch a file");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.writeFileSync(path.join(dir, "guide.md"), "# Guide\n\nRead this first.\n");
  ok("add", "guide.md", "--into", "docs");
  fs.writeFileSync(path.join(dir, "site.css"), "body { color: red; }\n");
  ok("add", "site.css", "--into", "styles");
  assert.match(
    ok("patch", "docs.guide", "--old", "Read this first.", "--new", "Read this *first*."),
    /update +docs\.guide : md/,
  );
  assert.match(ok("view", "docs.guide"), /Read this \*first\*\./);
  assert.match(
    ok("patch", "styles.site", "--old", "red", "--new", "blue"),
    /update +styles\.site : css/,
  );
  assert.match(ok("view", "styles.site"), /body \{ color: blue; \}/);
  // nothing left behind, and nothing new named
  assert.deepEqual(
    fs.readdirSync(dir).filter((f) => f.startsWith(".patch-") || f.startsWith("patch-")),
    [],
  );
  assert.doesNotMatch(ok("ls"), /patch/);
}

uses