| 1 | import { afterAll, expect, test } from "bun:test"; |
| 2 | import { mkdtemp, rm |
| 3 | import os from "node:os |
| 4 | import path from "node:path |
| 5 | |
| 6 | import { PermanentImportError } |
| 7 | import { |
| 8 | credentialEnv, |
| 9 | fetchFailure, |
| 10 | listRefs, |
| 11 | presentCommits, |
| 12 | } from "./git.ts"; |
| 13 | |
| 14 | const never = new |
| 15 | const dir = await |
| 16 | afterAll(() => rm |
| 17 | |
| 18 | const env = { |
| 19 | GIT_AUTHOR_NAME: "t", |
| 20 | GIT_AUTHOR_EMAIL: "t@t", |
| 21 | GIT_COMMITTER_NAME: "t", |
| 22 | GIT_COMMITTER_EMAIL: "t@t", |
| 23 | }; |
| 24 | function git(...args |
| 25 | const result = |
| 26 | env: { ...process |
| 27 | }); |
| 28 | if (result.exitCode |
| 29 | return result.stdout |
| 30 | } |
| 31 | |
| 32 | git("init", " |
| 33 | git("commit", " |
| 34 | git("tag", " |
| 35 | const head = git |
| 36 | |
| 37 | test("credentialEnv scopes a basic header to one origin", () |
| 38 | expect(credentialEnv("https://github.com |
| 39 | GIT_TERMINAL_PROMPT: "0", |
| 40 | GIT_CONFIG_COUNT: "1", |
| 41 | GIT_CONFIG_KEY_0: "http.https://github.com/.extraHeader", |
| 42 | GIT_CONFIG_VALUE_0: `Authorization: Basic ${btoa |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | test("listRefs lists branches and tags", async |
| 47 | expect(await listRefs |
| 48 | new Set(["refs/heads/main |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | test("presentCommits keeps only commits the history has", async |
| 53 | expect(await presentCommits |
| 54 | new Set([head |
| 55 | ); |
| 56 | expect(await presentCommits |
| 57 | }); |
| 58 | |
| 59 | test("a failing git command reports its stderr", async |
| 60 | await expect(listRefs |
| 61 | /git -C .* for-each-ref .* exited with/ |
| 62 | ); |
| 63 | }); |
| 64 | |
| 65 | test("fetchFailure retries only a token that expired after working", () |
| 66 | const auth = |
| 67 | "fatal: Authentication failed for 'https://github.com/o/r.git/'", |
| 68 | ); |
| 69 | expect(fetchFailure(auth |
| 70 | const expired = |
| 71 | expect(expired).not |
| 72 | expect(expired.message |
| 73 | |
| 74 | const missing = |
| 75 | expect(fetchFailure(missing |
| 76 | const network = |
| 77 | expect(fetchFailure(network |
| 78 | }); |