/** * Runs the real `dotnet` CLI for tests: an isolated HOME (so the user-wide NuGet.Config and * the package cache are the test's own), one SDK picked per run through `global.json`, and a * local folder feed packed on the fly so nothing reaches nuget.org. * * With `CI` set a missing SDK fails the run instead of skipping it, so a runner without .NET * cannot pass these tests silently. */ import { spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; /** Installed SDK versions whose major version is one of `majors` (10 and 11 are the ones tested). */ export function cliSdks(majors: number[] = [10, 11]): string[] { const out = spawnSync("dotnet", ["--list-sdks"], { encoding: "utf8" }); if (out.status !== 0) { if (process.env.CI) throw new Error("dotnet is required in CI but was not found"); return []; } const versions = out.stdout .split(/\r?\n/) .map((l) => l.split(" ")[0]!) .filter((v) => majors.includes(Number(v.split(".")[0]))); // One SDK per major: the newest installed one. return majors.flatMap((m) => versions.filter((v) => v.startsWith(`${m}.`)).slice(-1)); } export interface Sandbox { /** Workspace root; its global.json pins the SDK. */ root: string; /** The test's HOME: holds the user-wide NuGet.Config. */ home: string; major: number; run(args: string[], cwd?: string): { code: number; out: string }; /** Like `run` but throws on a non-zero exit, as the IDE's `dotnet()` does. */ ok(args: string[], cwd?: string): string; /** Packs `id` at each version into `feed` (a local folder feed). */ pack(feed: string, id: string, versions: string[], extra?: string): void; dispose(): void; } export function sandbox(sdk: string): Sandbox { // realpath: the CLI prints resolved paths (macOS /var -> /private/var). const base = realpathSync(mkdtempSync(join(tmpdir(), "oi-nuget-"))); const root = join(base, "ws"); const home = join(base, "home"); mkdirSync(root); mkdirSync(home); writeFileSync(join(root, "global.json"), JSON.stringify({ sdk: { version: sdk, rollForward: "disable" } })); const major = Number(sdk.split(".")[0]); const env = { ...process.env, HOME: home, USERPROFILE: home, APPDATA: join(home, "AppData"), DOTNET_CLI_HOME: home, NUGET_PACKAGES: join(base, "packages"), DOTNET_CLI_UI_LANGUAGE: "en", DOTNET_NOLOGO: "1", DOTNET_CLI_TELEMETRY_OPTOUT: "1", DOTNET_SKIP_FIRST_TIME_EXPERIENCE: "1", MSBUILDDISABLENODEREUSE: "1", }; const run = (args: string[], cwd = root) => { const r = spawnSync("dotnet", args, { cwd, env, encoding: "utf8", timeout: 180_000 }); return { code: r.status ?? -1, out: `${r.stdout}${r.stderr}` }; }; const ok = (args: string[], cwd = root) => { const r = run(args, cwd); if (r.code !== 0) throw new Error(`dotnet ${args.join(" ")} exited with ${r.code}\n${r.out}`); return r.out; }; return { root, home, major, run, ok, pack(feed, id, versions, extra = "") { const dir = join(base, `pack-${id}`); mkdirSync(dir, { recursive: true }); mkdirSync(feed, { recursive: true }); writeFileSync( join(dir, `${id}.csproj`), `net${major}.0` + `falsetruefalsefalsefalseNU5128;NU5127` + `${extra}`, ); // Nothing to restore: an empty source list keeps the user's and the test's sources out of packing. writeFileSync( join(dir, "NuGet.Config"), "", ); writeFileSync(join(dir, "content.txt"), id); for (const v of versions) ok(["pack", dir, "-p:PackageVersion=" + v, "-o", feed, "--nologo"], dir); }, dispose: () => rmSync(base, { recursive: true, force: true }), }; }