Cotton code
127 lines · 5.7 KB · TypeScriptRaw
// `scripts/ios-test.sh` under a build lock that YIELDS and runs it again.//// Todo 0e2060ba. The script minted `RESULTS=$(mktemp -d)/ios-test.xcresult`// once and ran xcodebuild under `scripts/with-build-lock.sh`. A local holder of// that lock yields to a check job's claim — its whole process group is TERMed —// and then starts the SAME command again from the top. The first attempt had// already created the bundle, so the second `xcodebuild test` refused it// (`Existing file at -resultBundlePath`, exit 64), tested nothing, and the// summary read the killed attempt's bundle and said "NO TESTS RAN".//// Everything heavy is a stub on PATH and the lock is this test's own directory,// so what is proved is the retry, not Xcode: the stub `xcodebuild` behaves like// the real one about an existing bundle, files a check job's claim on its first// attempt so the lock really yields, and passes on its second.import { spawnSync } from "node:child_process";import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";import { afterEach, describe, expect, it } from "vitest";import { publishBuildPeaks, publishDiskReading } from "./helpers/disk-reading";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");const IOS_TEST = join(REPO_ROOT, "scripts", "ios-test.sh");let scratch = "";afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = "";});function stub(dir: string, name: string, body: string): void {  const path = join(dir, name);  writeFileSync(path, `#!/bin/sh\n${body}\n`);  chmodSync(path, 0o755);}describe("ios-test.sh when the build lock yields and re-runs its command", () => {  // RULE: Every start of ios-test.sh's xcodebuild under the build lock writes a fresh result bundle, and the summary reads the bundle of the run that finished.  it("the second attempt tests into a fresh bundle, and its counts are the ones reported", () => {    scratch = mkdtempSync(join(tmpdir(), "ios-test-bundle-"));    const lock = join(scratch, "lock");    const count = join(scratch, "attempts");    const reads = join(scratch, "reads");    // Like the real xcodebuild, it refuses a bundle path that already exists.    // First attempt: create the bundle (no tests in it yet), file a live claim    // as a waiting check job, and build "forever" — the lock's watcher TERMs    // the group, which also ends the claim's process, so the lock comes back.    // Second attempt: a finished run of three tests.    stub(      scratch,      "xcodebuild",      [        'R=""; prev=""',        'for a in "$@"; do [ "$prev" = -resultBundlePath ] && R="$a"; prev="$a"; done',        `n=$(( $(cat '${count}' 2>/dev/null || echo 0) + 1 )); echo "$n" > '${count}'`,        'if [ -e "$R" ]; then echo "xcodebuild: error: Existing file at -resultBundlePath \\"$R\\"" >&2; exit 64; fi',        'mkdir -p "$R"',        'if [ "$n" = 1 ]; then',        '  echo 0 > "$R/tests"',        `  mkdir -p '${lock}.ci-waiting'; echo ci-swift > '${lock}.ci-waiting/'$$`,        "  sleep 60; exit 0",        "fi",        'echo 3 > "$R/tests"',      ].join("\n"),    );    // `xcrun xcresulttool get test-results summary --path <bundle> --format json`    stub(      scratch,      "xcrun",      [        'P=""; prev=""',        'for a in "$@"; do [ "$prev" = --path ] && P="$a"; prev="$a"; done',        `echo "$P" >> '${reads}'`,        't=$(cat "$P/tests")',        `printf '{"result":"Passed","passedTests":%s,"failedTests":0,"skippedTests":0,"totalTestCount":%s}\\n' "$t" "$t"`,      ].join("\n"),    );    // The project generation ios-test.sh runs first (as in build-lock-reentrant).    stub(scratch, "xcodegen", "exit 0");    stub(scratch, "npx", "exit 3");    // A scratch HOME with a fresh device reading: on a Mac the build lock admits    // through the device policy, which reads it there — never the person's HOME.    const home = join(scratch, "home");    publishDiskReading(home);    // A byte of RAM per build: admission must not turn on the machine's free RAM.    publishBuildPeaks(home);    const run = spawnSync("/bin/bash", [IOS_TEST], {      cwd: REPO_ROOT,      encoding: "utf8",      env: {        ...process.env,        HOME: home,        PATH: `${scratch}:${process.env.PATH ?? ""}`,        COTTON_SIM: "00000000-0000-0000-0000-000000000000",        COTTON_SKIP_SMOKE: "1",        COTTON_ONLY_TESTING: "",        COTTON_DERIVED_DATA: join(scratch, "derived"),        BUILD_LOCK_DIR: lock,        BUILD_LOCK_LOG: join(scratch, "wait.log"),        BUILD_LOCK_LABEL: "local-ios-test",        // A local run is the one that yields — even when this suite runs in CI.        BUILD_LOCK_CLASS: "local",        BUILD_LOCK_YIELD: "",        BUILD_LOCK_POLL_SECONDS: "1",        BUILD_LOCK_YIELD_POLL_SECONDS: "1",        BUILD_LOCK_YIELD_GRACE_SECONDS: "2",        BUILD_LOCK_LOCAL_NICE: "0",        TINMEN_BUILD_LOCK_HELD: "",        TINMEN_BUILD_LOCK_DIR: "",      },    });    const out = `${run.stdout}${run.stderr}`;    expect(out, "the lock never yielded, so nothing was re-run").toContain("YIELDED");    expect(readFileSync(count, "utf8").trim(), out).toBe("2");    expect(out).not.toContain("Existing file at -resultBundlePath");    expect(out).not.toContain("NO TESTS RAN");    expect(out).toContain("3 passed, 0 failed, 0 skipped of 3 tests");    expect(run.status, out).toBe(0);    // One summary, of the bundle the second attempt wrote.    expect(existsSync(reads) ? readFileSync(reads, "utf8").trim().split("\n") : []).toHaveLength(1);  }, 120_000);});