initTestRepo creates a temporary git repository with an initial commit and returns its path.
(t *testing.T)
| 10 | |
| 11 | // initTestRepo creates a temporary git repository with an initial commit and returns its path. |
| 12 | func initTestRepo(t *testing.T) string { |
| 13 | t.Helper() |
| 14 | dir := t.TempDir() |
| 15 | |
| 16 | cmds := [][]string{ |
| 17 | {"git", "init"}, |
| 18 | {"git", "config", "user.email", "test@test.com"}, |
| 19 | {"git", "config", "user.name", "Test"}, |
| 20 | {"git", "checkout", "-b", "main"}, |
| 21 | } |
| 22 | for _, args := range cmds { |
| 23 | cmd := exec.Command(args[0], args[1:]...) |
| 24 | cmd.Dir = dir |
| 25 | if out, err := cmd.CombinedOutput(); err != nil { |
| 26 | t.Fatalf("setup command %v failed: %s", args, string(out)) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Create an initial commit so branches can be created |
| 31 | readme := filepath.Join(dir, "README.md") |
| 32 | if err := os.WriteFile(readme, []byte("# Test\n"), 0644); err != nil { |
| 33 | t.Fatalf("failed to create README: %v", err) |
| 34 | } |
| 35 | cmd := exec.Command("git", "add", ".") |
| 36 | cmd.Dir = dir |
| 37 | if out, err := cmd.CombinedOutput(); err != nil { |
| 38 | t.Fatalf("git add failed: %s", string(out)) |
| 39 | } |
| 40 | cmd = exec.Command("git", "commit", "-m", "initial commit") |
| 41 | cmd.Dir = dir |
| 42 | if out, err := cmd.CombinedOutput(); err != nil { |
| 43 | t.Fatalf("git commit failed: %s", string(out)) |
| 44 | } |
| 45 | |
| 46 | return dir |
| 47 | } |
| 48 | |
| 49 | func TestGetDefaultBranch(t *testing.T) { |
| 50 | t.Run("detects main branch", func(t *testing.T) { |
no outgoing calls
no test coverage detected