|
1 | | -import { describe, expect, test } from "bun:test"; |
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdir, mkdtemp, writeFile, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { execFile } from "node:child_process"; |
| 6 | +import { promisify } from "node:util"; |
2 | 7 |
|
3 | | -import { parseArgs } from "./eval-capability.ts"; |
| 8 | +import { initEvalGitRepo, parseArgs } from "./eval-capability.ts"; |
| 9 | + |
| 10 | +const execFileAsync = promisify(execFile); |
4 | 11 |
|
5 | 12 | describe("parseArgs", () => { |
6 | 13 | test("--help does not require provider or model", () => { |
@@ -55,3 +62,67 @@ describe("parseArgs", () => { |
55 | 62 | expect(pair.model).toBe("bar"); |
56 | 63 | }); |
57 | 64 | }); |
| 65 | + |
| 66 | +describe("initEvalGitRepo", () => { |
| 67 | + const savedGitConfigGlobal = process.env.GIT_CONFIG_GLOBAL; |
| 68 | + |
| 69 | + const restoreGitConfigGlobal = (): void => { |
| 70 | + if (savedGitConfigGlobal === undefined) { |
| 71 | + delete process.env.GIT_CONFIG_GLOBAL; |
| 72 | + } else { |
| 73 | + process.env.GIT_CONFIG_GLOBAL = savedGitConfigGlobal; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + restoreGitConfigGlobal(); |
| 79 | + }); |
| 80 | + |
| 81 | + test("makes a fixture copy a git work tree with a commit", async () => { |
| 82 | + const dir = await mkdtemp(join(tmpdir(), "corbits-eval-git-")); |
| 83 | + try { |
| 84 | + await writeFile(join(dir, "README"), "fixture\n", "utf8"); |
| 85 | + await initEvalGitRepo(dir); |
| 86 | + const { stdout } = await execFileAsync("git", ["rev-parse", "--is-inside-work-tree"], { |
| 87 | + cwd: dir, |
| 88 | + }); |
| 89 | + expect(stdout.trim()).toBe("true"); |
| 90 | + const { stdout: head } = await execFileAsync("git", ["rev-parse", "HEAD"], { cwd: dir }); |
| 91 | + expect(head.trim().length).toBeGreaterThan(0); |
| 92 | + const { stdout: count } = await execFileAsync("git", ["rev-list", "--count", "HEAD"], { |
| 93 | + cwd: dir, |
| 94 | + }); |
| 95 | + expect(Number(count.trim())).toBeGreaterThanOrEqual(1); |
| 96 | + const { stdout: log } = await execFileAsync("git", ["log", "-1", "--pretty=%s"], { |
| 97 | + cwd: dir, |
| 98 | + }); |
| 99 | + expect(log.trim()).toBe("eval fixture"); |
| 100 | + } finally { |
| 101 | + await rm(dir, { recursive: true, force: true }); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + test("succeeds when the process would otherwise sign", async () => { |
| 106 | + const root = await mkdtemp(join(tmpdir(), "corbits-eval-git-sign-")); |
| 107 | + const work = join(root, "work"); |
| 108 | + const configPath = join(root, "gitconfig"); |
| 109 | + try { |
| 110 | + await mkdir(work); |
| 111 | + await writeFile( |
| 112 | + configPath, |
| 113 | + "[commit]\ngpgsign = true\n[user]\nsigningkey = DEADKEY\n", |
| 114 | + "utf8", |
| 115 | + ); |
| 116 | + process.env.GIT_CONFIG_GLOBAL = configPath; |
| 117 | + await writeFile(join(work, "README"), "fixture\n", "utf8"); |
| 118 | + await initEvalGitRepo(work); |
| 119 | + const { stdout: head } = await execFileAsync("git", ["rev-parse", "HEAD"], { cwd: work }); |
| 120 | + expect(head.trim().length).toBeGreaterThan(0); |
| 121 | + const { stdout: cat } = await execFileAsync("git", ["cat-file", "-p", "HEAD"], { cwd: work }); |
| 122 | + expect(cat).not.toContain("gpgsig"); |
| 123 | + } finally { |
| 124 | + restoreGitConfigGlobal(); |
| 125 | + await rm(root, { recursive: true, force: true }); |
| 126 | + } |
| 127 | + }); |
| 128 | +}); |
0 commit comments