-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathqueries.test.ts
More file actions
95 lines (78 loc) · 3.15 KB
/
queries.test.ts
File metadata and controls
95 lines (78 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createGitClient } from "./client";
import { detectDefaultBranch } from "./queries";
async function setupRepo(defaultBranch = "main"): Promise<string> {
const dir = await mkdtemp(path.join(tmpdir(), "posthog-code-queries-"));
const git = createGitClient(dir);
await git.init(["--initial-branch", defaultBranch]);
await git.addConfig("user.name", "Test");
await git.addConfig("user.email", "test@example.com");
await writeFile(path.join(dir, "file.txt"), "content\n");
await git.add(["file.txt"]);
await git.commit("initial");
return dir;
}
describe("detectDefaultBranch", () => {
let repoDir: string;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
}
});
it("detects 'main' as default branch", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("main");
});
it("detects 'master' as default branch", async () => {
repoDir = await setupRepo("master");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("master");
});
it("detects non-standard default branch via init.defaultBranch config", async () => {
repoDir = await setupRepo("develop");
const git = createGitClient(repoDir);
// Set init.defaultBranch in the repo's local config
await git.addConfig("init.defaultBranch", "develop");
const result = await detectDefaultBranch(git);
expect(result).toBe("develop");
});
it("falls back to current branch when no standard branch exists", async () => {
repoDir = await setupRepo("trunk");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("trunk");
});
it("prefers 'main' over other detection methods", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
// Create additional branches
await git.checkoutLocalBranch("develop");
await git.checkout("main");
const result = await detectDefaultBranch(git);
expect(result).toBe("main");
});
it("prefers remote HEAD over local detection", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
// Set up a bare remote with a non-standard default branch
const remoteDir = await mkdtemp(
path.join(tmpdir(), "posthog-code-remote-"),
);
const remoteGit = createGitClient(remoteDir);
await remoteGit.init(["--bare", "--initial-branch", "production"]);
await git.addRemote("origin", remoteDir);
// Push main as production on remote and set HEAD
await git.push(["origin", "main:production"]);
await remoteGit.raw(["symbolic-ref", "HEAD", "refs/heads/production"]);
await git.fetch(["origin"]);
const result = await detectDefaultBranch(git);
expect(result).toBe("production");
await rm(remoteDir, { recursive: true, force: true });
});
});