48 lines · 1.6 KB · TypeScriptRaw
import { spawnSync } from "node:child_process";import { beforeEach, type TestContext } from "vitest";declare module "@vitest/runner" { interface TaskMeta { environmentSkipReason?: string; }}export interface Requirement { probe: () => boolean; reason: string;}/** Probe before fixtures or the test body; exceptions are defects, not skips. */export function requires(ctx: TestContext, ...requirements: Requirement[]): void { for (const { probe, reason } of requirements) { if (probe()) continue; ctx.task.meta.environmentSkipReason = reason; ctx.skip(); }}/** Apply the same prerequisite to each case in the current suite. */export function requireEnvironment(...requirements: Requirement[]): void { beforeEach(ctx => requires(ctx, ...requirements));}export function macOS(): Requirement { return { probe: () => process.platform === "darwin", reason: "macOS is unavailable on this runner" };}/** The CI host's python3; macOS ships 3.9, without tomllib or hashlib.file_digest. */export function python311(): Requirement { return { probe: () => spawnSync("python3", ["-c", "import sys; sys.exit(sys.version_info < (3, 11))"], { stdio: "ignore", timeout: 5_000 }).status === 0, reason: "python3 3.11 or newer is unavailable on this runner", };}export function linux(): Requirement { return { probe: () => process.platform === "linux", reason: "Linux is unavailable on this runner" };}export function command(name: string, args = ["--version"]): Requirement { return { probe: () => spawnSync(name, args, { stdio: "ignore", timeout: 5_000 }).status === 0, reason: `${name} is unavailable on this runner`, };}