testOneCopyOfEachVersion

tcmts.loader.packages.testOneCopyOfEachVersion · #zzsb7ki4 · term

import type { Layout } from "#codebase/tcmts/loader";
import { installDir, oneCopyEach } from "#codebase/tcmts/loader/packages";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testOneCopyOfEachVersion = () => {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "one-copy-"));
  try {
    const cacheDir = path.join(root, "cache");
    const lay = { root, cacheDir } as unknown as Layout;
    const pkg = (dir: string, version: string) => {
      fs.mkdirSync(dir, { recursive: true });
      fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ name: "react", version }));
    };
    const alias = installDir(lay, "react", "19");
    pkg(alias, "19.3.1");
    pkg(path.join(cacheDir, "node_modules", "react"), "19.3.1");
    pkg(path.join(cacheDir, "node_modules", "other"), "1.0.0");
    oneCopyEach(lay, [
      { name: "react", range: "19", version: "19.3.1" },
      { name: "other", range: "2", version: "2.0.0" },
    ]);
    const named = path.join(cacheDir, "node_modules", "react");
    if (!fs.lstatSync(named).isSymbolicLink() || fs.realpathSync(named) !== fs.realpathSync(alias))
      throw new Error("react at its name isn't the alias's copy");
    if (fs.lstatSync(path.join(cacheDir, "node_modules", "other")).isSymbolicLink())
      throw new Error("another version at the name is left as it is");
    oneCopyEach(lay, [{ name: "react", range: "19", version: "19.3.1" }]);
    if (fs.realpathSync(named) !== fs.realpathSync(alias))
      throw new Error("again: nothing changes");
  } finally {
    fs.rmSync(root, { recursive: true, force: true });
  }
}

uses