Cotton code
82 lines · 5.4 KB · TypeScriptRaw
import { spawnSync } from "node:child_process";import { chmodSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, utimesSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, expect, it } from "vitest";const temps: string[] = [];afterEach(() => temps.splice(0).forEach((p) => rmSync(p, { recursive: true, force: true })));function fixture() {  const root = mkdtempSync(join(tmpdir(), "xc-cache-")); temps.push(root);  for (const dir of ["scripts/lib", "scripts/ci", "crates/cotton-ffi/include", "apps/cotton/Sources/CottonFFI", "bin", "cache"]) mkdirSync(join(root, dir), { recursive: true });  const scripts = join(__dirname, "..");  cpSync(join(scripts, "build-xcframework.sh"), join(root, "scripts/build-xcframework.sh"));  for (const lib of ["owned-root.sh", "cargo-path.sh", "rust-targets.sh", "rustup-lock.sh", "swiftpm-cache.sh"]) cpSync(join(scripts, "lib", lib), join(root, "scripts/lib", lib));  writeFileSync(join(root, "apps/cotton/project.yml"), "  iOS: 18.0\n");  writeFileSync(join(root, "apps/cotton/Package.swift"), 'platforms: [.macOS("15.0")]\n');  writeFileSync(join(root, "crates/cotton-ffi/include/cotton_ffi.h"), "header\n");  writeFileSync(join(root, "apps/cotton/Sources/CottonFFI/module.modulemap"), "module\n");  function shell(path: string, body: string) { writeFileSync(join(root, path), `#!/bin/sh\n${body}\n`); chmodSync(join(root, path), 0o755); }  shell("bin/git", 'shift 2; case "$1" in status) exit 0 ;; *) echo committed-hash ;; esac');  shell("bin/cargo", "exit 1");  shell("bin/rustc", "echo rustc-fixture");  shell("bin/rustup", "echo aarch64-apple-ios; echo aarch64-apple-ios-sim; echo aarch64-apple-darwin");  shell("bin/xcodebuild", 'if [ "$1" = -version ]; then echo Xcode-fixture; exit 0; fi\nwhile [ "$1" != -output ]; do shift; done; mkdir -p "$2"; echo framework > "$2/Info.plist"');  shell("scripts/ci/artifact-store.sh", "exit 1");  shell("scripts/disk-guard.sh", `touch '${root}/disk-guard-ran'`);  shell("scripts/with-build-lock.sh", `touch '${root}/lock-ran'; exit 99`);  shell("scripts/build-rust-ffi.sh", `touch '${root}/archive.a'; echo '${root}/archive.a'`);  const env = { ...process.env, PATH: `${root}/bin:${process.env.PATH}`, COTTON_XCFRAMEWORK_CACHE: join(root, "cache"), COTTON_XCFRAMEWORK_LOCKED: "", COTTON_XCFRAMEWORK_CACHE_KEEP: "", COTTON_RUSTUP_LOCK: join(root, "rustup.lock"), IPHONEOS_DEPLOYMENT_TARGET: "", MACOSX_DEPLOYMENT_TARGET: "" };  const run = (args: string[], extra = {}) => spawnSync("/bin/bash", [join(root, "scripts/build-xcframework.sh"), ...args], { encoding: "utf8", env: { ...env, ...extra } });  const keyRun = run(["--simulator-only", "--print-cache-key"]);  expect(keyRun.status, keyRun.stderr).toBe(0);  const key = keyRun.stdout.trim().split("\n").at(-1)!;  return { root, run, key };}// RULE: A cached XCFramework is copied before taking the build lock, so a hit never queues or invokes disk reclamation.it("serves a cache hit without entering the lock", () => {  const { root, run, key } = fixture();  const entry = join(root, "cache", key);  mkdirSync(join(entry, "CottonFFI.xcframework"), { recursive: true });  mkdirSync(join(entry, "include"));  writeFileSync(join(entry, "CottonFFI.xcframework/Info.plist"), "cached framework");  const result = run(["--simulator-only"]);  expect(result.status, result.stderr).toBe(0);  expect(readFileSync(join(root, "apps/cotton/.ffi/CottonFFI.xcframework/Info.plist"), "utf8")).toBe("cached framework");  expect(existsSync(join(root, "lock-ran"))).toBe(false);  expect(existsSync(join(root, "disk-guard-ran"))).toBe(false);});// RULE: A cache miss still acquires the build lock before any archive compilation, preserving the machine's capacity limit.it("a cache miss enters the lock before building", () => {  const { root, run } = fixture();  const result = run(["--simulator-only"]);  expect(result.status, result.stderr).toBe(99);  expect(existsSync(join(root, "lock-ran"))).toBe(true);  expect(existsSync(join(root, "archive.a"))).toBe(false);});// RULE: The XCFramework cache retains twelve complete entries by last use; a hit refreshes its age and in-flight staging directories are never eviction candidates.it("keeps twelve entries and evicts the least recently used", () => {  const { root, run, key } = fixture();  for (const [index, name] of [key, ...Array.from({ length: 11 }, (_, i) => `old-${i}`)].entries()) {    const entry = join(root, "cache", name);    mkdirSync(join(entry, "CottonFFI.xcframework"), { recursive: true });    mkdirSync(join(entry, "include"));    utimesSync(entry, 1_000_000_000 + index, 1_000_000_000 + index);  }  const partial = join(root, "cache", "other.partial.123");  mkdirSync(partial);  const hit = run(["--simulator-only"]);  expect(hit.status, hit.stderr).toBe(0);  // The dev loop builds in a `-sim-dev` target; naming one keeps the fixture  // off the Mac's real cargo targets (scripts/ci/cargo-target-dir.sh).  const miss = run(["--simulator-only", "--debug"], { COTTON_XCFRAMEWORK_LOCKED: "1", CARGO_TARGET_DIR: join(root, "target-sim-dev") });  expect(miss.status, miss.stdout + miss.stderr).toBe(0);  const complete = readdirSync(join(root, "cache")).filter((name) => existsSync(join(root, "cache", name, "CottonFFI.xcframework")));  expect(complete).toHaveLength(12);  expect(complete).toContain(key);  expect(complete).not.toContain("old-0");  expect(existsSync(partial)).toBe(true);});