|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - These fixtures drive the install script against real Git config. |
| 2 | + |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeFS from "node:fs"; |
| 5 | +import * as NodeOS from "node:os"; |
| 6 | +import * as NodePath from "node:path"; |
| 7 | + |
| 8 | +import { afterEach, assert, describe, it } from "@effect/vitest"; |
| 9 | + |
| 10 | +import { pinGhDefault } from "./fork-gh-default.ts"; |
| 11 | + |
| 12 | +const containers: string[] = []; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + for (const container of containers.splice(0)) { |
| 16 | + NodeFS.rmSync(container, { recursive: true, force: true }); |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +function repository(remotes: Readonly<Record<string, string>>): { |
| 21 | + readonly cwd: string; |
| 22 | + readonly config: (key: string) => string | undefined; |
| 23 | +} { |
| 24 | + const cwd = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-gh-default-")); |
| 25 | + containers.push(cwd); |
| 26 | + const git = (args: readonly string[]) => |
| 27 | + NodeChildProcess.spawnSync("git", [...args], { cwd, encoding: "utf8" }); |
| 28 | + git(["init", "-q"]); |
| 29 | + for (const [name, url] of Object.entries(remotes)) git(["remote", "add", name, url]); |
| 30 | + return { |
| 31 | + cwd, |
| 32 | + config: (key) => { |
| 33 | + const result = git(["config", "--get", key]); |
| 34 | + return result.status === 0 ? result.stdout.trim() : undefined; |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +describe("pinGhDefault", () => { |
| 40 | + it("moves gh's default from upstream to the fork remote, once", () => { |
| 41 | + const repo = repository({ |
| 42 | + origin: "[email protected]:RSI-Software/t3code-hyprws.git", |
| 43 | + upstream: "[email protected]:pingdotgg/t3code.git", |
| 44 | + }); |
| 45 | + NodeChildProcess.execFileSync("git", ["config", "remote.upstream.gh-resolved", "base"], { |
| 46 | + cwd: repo.cwd, |
| 47 | + }); |
| 48 | + |
| 49 | + assert.strictEqual(pinGhDefault(repo.cwd), "origin"); |
| 50 | + assert.strictEqual(repo.config("remote.origin.gh-resolved"), "base"); |
| 51 | + assert.strictEqual(repo.config("remote.upstream.gh-resolved"), undefined); |
| 52 | + assert.strictEqual(pinGhDefault(repo.cwd), undefined); |
| 53 | + }); |
| 54 | + |
| 55 | + it("leaves a clone without a fork remote, or no repository, untouched", () => { |
| 56 | + const repo = repository({ origin: "https://github.com/pingdotgg/t3code.git" }); |
| 57 | + assert.strictEqual(pinGhDefault(repo.cwd), undefined); |
| 58 | + assert.strictEqual(repo.config("remote.origin.gh-resolved"), undefined); |
| 59 | + |
| 60 | + const plain = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-gh-default-")); |
| 61 | + containers.push(plain); |
| 62 | + assert.strictEqual(pinGhDefault(plain), undefined); |
| 63 | + }); |
| 64 | +}); |
0 commit comments