testStoppedTcmStillReportsWhatItRan

tcmts.cli.test.e2e.suites.testStoppedTcmStillReportsWhatItRan · #gjb65k4a · term

A tcm a test stops with a signal (the language server it started) still says what code it ran: a suite's cached result is only as good as that, and V8 writes coverage on exit, not when a signal ends the process.

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

export const testStoppedTcmStillReportsWhatItRan = async () => {
  const dir = tempDir("coverage");
  okIn(dir, "init");
  const coverage = path.join(dir, "coverage");
  const child = spawn(process.execPath, [tcmBin(), "lsp"], {
    cwd: dir,
    env: { ...plainEnv(), NODE_V8_COVERAGE: coverage },
    stdio: ["pipe", "pipe", "pipe"],
  });
  // started: it answers
  await new Promise<void>((resolve) => {
    child.stdout.once("data", () => resolve());
    const body = JSON.stringify({
      jsonrpc: "2.0",
      id: 1,
      method: "initialize",
      params: { processId: null, rootUri: null, capabilities: {} },
    });
    child.stdin.write(`Content-Length: ${Buffer.byteLength(body)}\r\n\r\n${body}`);
  });
  child.kill("SIGTERM");
  await new Promise((resolve) => child.on("exit", resolve));
  assert.ok(
    fs.existsSync(coverage) && fs.readdirSync(coverage).some((f) => f.endsWith(".json")),
    "coverage written",
  );
  // and it said it ran, so a suite waits for it to end even when its test doesn't (tcmts.cli.suites.tcmsEnded)
  assert.ok(
    fs.readdirSync(coverage).some((f) => /^tcm-pid-\d+$/.test(f)),
    `it says it ran: ${fs.readdirSync(coverage).join(", ")}`,
  );
}

uses