|
| 1 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { afterEach, describe, expect, test } from "vitest"; |
| 6 | +import { getBranches, getTags } from "./git.js"; |
| 7 | + |
| 8 | +const runGit = ( |
| 9 | + repoPath: string, |
| 10 | + args: string[], |
| 11 | + env: Record<string, string> = {}, |
| 12 | +) => { |
| 13 | + execFileSync("git", args, { |
| 14 | + cwd: repoPath, |
| 15 | + env: { |
| 16 | + ...process.env, |
| 17 | + ...env, |
| 18 | + }, |
| 19 | + stdio: "pipe", |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +const createTempRepo = async () => { |
| 24 | + const repoPath = await mkdtemp(join(tmpdir(), "sourcebot-git-test-")); |
| 25 | + |
| 26 | + runGit(repoPath, ["init", "--initial-branch=main"]); |
| 27 | + runGit(repoPath, ["config", "user.name", "Sourcebot Test"]); |
| 28 | + runGit(repoPath, ["config", "user.email", "sourcebot@example.com"]); |
| 29 | + runGit(repoPath, ["config", "tag.sort", "refname"]); |
| 30 | + runGit(repoPath, ["config", "branch.sort", "refname"]); |
| 31 | + |
| 32 | + return repoPath; |
| 33 | +}; |
| 34 | + |
| 35 | +const commitFile = async ({ |
| 36 | + repoPath, |
| 37 | + fileName, |
| 38 | + content, |
| 39 | + message, |
| 40 | + timestamp, |
| 41 | +}: { |
| 42 | + repoPath: string; |
| 43 | + fileName: string; |
| 44 | + content: string; |
| 45 | + message: string; |
| 46 | + timestamp: string; |
| 47 | +}) => { |
| 48 | + await writeFile(join(repoPath, fileName), content); |
| 49 | + runGit(repoPath, ["add", fileName]); |
| 50 | + runGit(repoPath, ["commit", "-m", message], { |
| 51 | + GIT_AUTHOR_DATE: timestamp, |
| 52 | + GIT_COMMITTER_DATE: timestamp, |
| 53 | + }); |
| 54 | +}; |
| 55 | + |
| 56 | +describe("git ref ordering", () => { |
| 57 | + const repoPaths: string[] = []; |
| 58 | + |
| 59 | + afterEach(async () => { |
| 60 | + await Promise.all( |
| 61 | + repoPaths |
| 62 | + .splice(0) |
| 63 | + .map((repoPath) => |
| 64 | + rm(repoPath, { recursive: true, force: true }), |
| 65 | + ), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test("getTags returns newest tags first by creator date", async () => { |
| 70 | + const repoPath = await createTempRepo(); |
| 71 | + repoPaths.push(repoPath); |
| 72 | + |
| 73 | + await commitFile({ |
| 74 | + repoPath, |
| 75 | + fileName: "README.md", |
| 76 | + content: "base\n", |
| 77 | + message: "initial commit", |
| 78 | + timestamp: "2024-01-01T00:00:00Z", |
| 79 | + }); |
| 80 | + |
| 81 | + runGit(repoPath, ["tag", "-a", "a-oldest", "-m", "oldest tag"], { |
| 82 | + GIT_COMMITTER_DATE: "2024-01-02T00:00:00Z", |
| 83 | + }); |
| 84 | + runGit(repoPath, ["tag", "-a", "z-newest", "-m", "newest tag"], { |
| 85 | + GIT_COMMITTER_DATE: "2024-01-03T00:00:00Z", |
| 86 | + }); |
| 87 | + |
| 88 | + const tags = await getTags(repoPath); |
| 89 | + |
| 90 | + expect(tags).toContain("z-newest"); |
| 91 | + expect(tags).toContain("a-oldest"); |
| 92 | + expect(tags.indexOf("z-newest")).toBeLessThan(tags.indexOf("a-oldest")); |
| 93 | + }); |
| 94 | + |
| 95 | + test("getBranches returns newest branches first by last commit date", async () => { |
| 96 | + const repoPath = await createTempRepo(); |
| 97 | + repoPaths.push(repoPath); |
| 98 | + |
| 99 | + await commitFile({ |
| 100 | + repoPath, |
| 101 | + fileName: "README.md", |
| 102 | + content: "base\n", |
| 103 | + message: "initial commit", |
| 104 | + timestamp: "2024-01-01T00:00:00Z", |
| 105 | + }); |
| 106 | + |
| 107 | + runGit(repoPath, ["checkout", "-b", "aaa-oldest"]); |
| 108 | + await commitFile({ |
| 109 | + repoPath, |
| 110 | + fileName: "oldest.txt", |
| 111 | + content: "oldest\n", |
| 112 | + message: "oldest branch commit", |
| 113 | + timestamp: "2024-01-02T00:00:00Z", |
| 114 | + }); |
| 115 | + |
| 116 | + runGit(repoPath, ["checkout", "main"]); |
| 117 | + runGit(repoPath, ["checkout", "-b", "zzz-newest"]); |
| 118 | + await commitFile({ |
| 119 | + repoPath, |
| 120 | + fileName: "newest.txt", |
| 121 | + content: "newest\n", |
| 122 | + message: "newest branch commit", |
| 123 | + timestamp: "2024-01-03T00:00:00Z", |
| 124 | + }); |
| 125 | + |
| 126 | + runGit(repoPath, ["checkout", "main"]); |
| 127 | + |
| 128 | + const branches = await getBranches(repoPath); |
| 129 | + |
| 130 | + expect(branches).toContain("zzz-newest"); |
| 131 | + expect(branches).toContain("aaa-oldest"); |
| 132 | + expect(branches.indexOf("zzz-newest")).toBeLessThan( |
| 133 | + branches.indexOf("aaa-oldest"), |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments