testRunAnOlderBuild

tcmts.cli.test.e2e.builds.testRunAnOlderBuild · #chfjn23n · term

tcm --build <name> <command> runs the command with a kept build (known-good, previous, an id, a path), as that build runs it: here, on the codebase and branch TCM_CODEBASE and TCM_BRANCH name, or with -C <dir> (after it or before it) on the codebase there; its exit status is the command's. tcm builds lists them, each with the history entry it was made from and what changed since the build before it.

import { fakeBuild } from "#codebase/tcmts/cli/builds/test";
import { okIn, plainEnv, tcmBin, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import { Store } from "#codebase/tcmts/store";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export const testRunAnOlderBuild = () => {
  const root = tempDir("builds");
  const dir = tempDir("builds codebase");
  const repro = tempDir("builds repro");
  okIn(dir, "init");
  okIn(repro, "init");
  for (const v of [1, 2, 3]) {
    fs.writeFileSync(path.join(dir, "scratch.ts"), `export const v${v} = ${v};\n`);
    okIn(dir, "add");
  }
  const store = new Store(path.join(dir, ".tcm", "codebase.sqlite"));
  const log = store.causalLog(5).map((c) => c.hash);
  store.close();
  fakeBuild(root, "known-good", log[0]!, 1);
  fakeBuild(root, "known-good-previous", log[2]!, 5);
  const tcm = (cwd: string, env: Record<string, string>, ...args: string[]) => {
    const r = spawnSync(process.execPath, [tcmBin(), ...args], {
      cwd,
      encoding: "utf8",
      env: { ...plainEnv(), TCM_BUILDS: root, ...env },
    });
    return { status: r.status, out: r.stdout + r.stderr };
  };
  const said = (out: string) =>
    JSON.parse(out.trim().split("\n").at(-1)!) as Record<string, unknown>;
  const caller = { TCM_CODEBASE: dir, TCM_BRANCH: "session/x" };

  const here = tcm(dir, caller, "--build", "previous", "view", "x");
  assert.equal(here.status, 0, here.out);
  assert.deepEqual(said(here.out), {
    says: "known-good-previous",
    args: ["view", "x"],
    cwd: dir,
    codebase: dir,
    branch: "session/x",
  });
  // -C after it: the codebase there, not the caller's
  const there = tcm(dir, caller, "--build", "known-good", "-C", repro, "status");
  assert.deepEqual(said(there.out), {
    says: "known-good",
    args: ["status"],
    cwd: repro,
    codebase: null,
    branch: null,
  });
  const before = tcm(dir, caller, "-C", repro, `--build=#${log[2]!.slice(0, 6)}`, "status");
  assert.deepEqual(said(before.out), {
    says: "known-good-previous",
    args: ["status"],
    cwd: repro,
    codebase: null,
    branch: null,
  });
  // its exit status
  assert.equal(tcm(dir, {}, "--build", "previous", "fail").status, 1);
  const missing = tcm(dir, {}, "--build", "older", "status");
  assert.notEqual(missing.status, 0);
  assert.match(missing.out, /no build older: kept are known-good, previous/);

  const listed = tcm(dir, {}, "builds").out;
  assert.match(
    listed,
    new RegExp(
      `known-good  #${log[0]!.slice(0, 8)}  verified [\\d-]+ [\\d:]+  0 back: add v3\\n +since #${log[2]!.slice(0, 8)}: add v3; add v2\\n +${path.join(root, "known-good")}`,
    ),
  );
  assert.match(
    listed,
    new RegExp(`previous    #${log[2]!.slice(0, 8)}  verified [\\d-]+ [\\d:]+  2 back: add v1`),
  );
  assert.match(listed, /`tcm --build <name> <command>` runs one/);
  // elsewhere, what they were made from isn't in the history
  assert.match(
    tcm(repro, {}, "builds").out,
    /known-good  #\w{8}  verified [\d-]+ [\d:]+  \(not in this codebase's history\)/,
  );
  const none = tempDir("builds none");
  assert.match(tcm(dir, { TCM_BUILDS: none }, "builds").out, /no builds in /);
}

uses