testPackagesCarryWhatSiblingsKeep

tcmts.cli.test.e2e.releases.testPackagesCarryWhatSiblingsKeep · #xouruerk · term

A package carries what it uses of a sibling package that the sibling keeps to itself (doesn't export): two packages of one codebase share code without either publishing it. What the sibling does export still comes from its release.

import { codebase } from "#codebase/tcmts/cli/test/e2e/releases";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testPackagesCarryWhatSiblingsKeep = () => {
  const a = codebase("carry");
  a.save(
    `// tcm: namespace geo\nexport const manifest = { name: "geo", exports: ["distance"] };\nexport const squared = (x: number): number => x * x;\nexport const distance = (dx: number, dy: number): number => Math.sqrt(squared(dx) + squared(dy));\n`,
    "add",
  );
  a.save(
    `// tcm: namespace atlas\nimport { squared } from "#codebase/geo";\nexport const manifest = { name: "atlas", bin: { atlas: "main" } };\nexport const main = (): number => (console.log(squared(3)), 0);\n`,
    "add",
  );
  // geo has no release, and atlas needs none: what it uses, geo doesn't export
  assert.match(
    a.tcm("pack", "atlas"),
    /packed atlas@0\.0\.0 into \.tcm\/pack\/atlas: 2 module\(s\)/,
  );
  const dir = path.join(a.dir, ".tcm", "pack", "atlas");
  assert.equal(
    JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")).dependencies?.geo,
    undefined,
  );
  const modules = fs
    .readdirSync(path.join(dir, "modules"))
    .map((f) => fs.readFileSync(path.join(dir, "modules", f), "utf8"))
    .join("\n");
  assert.match(modules, /x \* x/, "geo's squared is carried in");
  assert.doesNotMatch(modules, /from "geo"/);

  // what geo exports still comes from its release
  a.save(
    `// tcm: namespace atlas\nimport { distance } from "#codebase/geo";\nexport const main = (): number => (console.log(distance(3, 4)), 0);\n`,
  );
  assert.match(
    a.run("pack", "atlas").out,
    /this uses geo\.distance from geo, which geo hasn't released yet: release geo first/,
  );
}

uses