Cotton code
66 lines · 2.8 KB · TypeScriptRaw
/** * EVERY ACCOUNT'S SCCACHE HAS A SIZE CAP. The Mac runner's 15 GB cap is an * environment variable its steps set; a builder's account had none (29 Sep * 2026). `scripts/ensure-sccache-cap.sh` writes the cap into the account's * sccache config file, which every server sccache starts obeys. */import { spawnSync } from "node:child_process";import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { afterEach, describe, expect, it } from "vitest";const SCRIPT = resolve(__dirname, "../ensure-sccache-cap.sh");const GB = 1024 ** 3;let scratch = "";afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = "";});function capWith(initial: string | null, args: string[] = []) {  scratch = mkdtempSync(join(tmpdir(), "sccache-cap-"));  const conf = join(scratch, "config");  if (initial !== null) writeFileSync(conf, initial);  const r = spawnSync("bash", [SCRIPT, ...args], {    env: { ...process.env, SCCACHE_CONF: conf, COTTON_SCCACHE_CAP_GB: "" },    encoding: "utf8",  });  expect(r.status, r.stderr).toBe(0);  return { text: readFileSync(conf, "utf8"), stdout: r.stdout };}describe("ensure-sccache-cap.sh", () => {  // RULE: Every account's sccache config caps the disk cache (15 GB by default: a Mac's floor arithmetic is the same everywhere, and at 40 GB the serving Mac's cache sat at its cap while every build was refused at the disk floor, 14 Oct 2026); a larger or missing cap is lowered to it and a smaller one is kept.  it("adds a 15 GB [cache.disk] size to an account with no config", () => {    const { text } = capWith(null);    expect(text).toContain("[cache.disk]");    expect(text).toContain(`size = ${15 * GB}`);  });  it("lowers a larger cap and keeps every other line", () => {    const { text } = capWith(`[cache.disk]\ndir = "/x/sccache"\nsize = ${200 * GB}\n\n[dist]\nscheduler_url = "y"\n`);    expect(text).toContain('dir = "/x/sccache"');    expect(text).toContain(`size = ${15 * GB}`);    expect(text).not.toContain(`${200 * GB}`);    expect(text).toContain('scheduler_url = "y"');    expect(text.match(/\[cache\.disk\]/g)).toHaveLength(1);  });  it("adds the size inside an existing [cache.disk] that has none", () => {    const { text } = capWith(`[cache.disk]\ndir = "/x"\n[dist]\nscheduler_url = "y"\n`);    expect(text).toMatch(/\[cache\.disk\]\ndir = "\/x"\nsize = \d+\n\[dist\]/);  });  it("leaves a smaller cap somebody chose alone", () => {    const initial = `[cache.disk]\nsize = ${10 * GB}\n`;    const { text, stdout } = capWith(initial);    expect(text).toBe(initial);    expect(stdout).toContain("already caps the cache at 10 GB");  });  it("takes the cap as an argument", () => {    expect(capWith(null, ["20"]).text).toContain(`size = ${20 * GB}`);  });});