Skip to content

Commit b6828c4

Browse files
committed
update coverage
1 parent 7781caa commit b6828c4

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

internal/testhelper/git_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package testhelper
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
10+
)
11+
12+
func TestSetupTestRepo(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
remoteURL string
16+
branchName string
17+
}{
18+
{
19+
name: "basic repo without remote",
20+
remoteURL: "",
21+
branchName: "",
22+
},
23+
{
24+
name: "repo with remote URL",
25+
remoteURL: "https://github.com/test/repo.git",
26+
branchName: "",
27+
},
28+
{
29+
name: "repo with remote and branch",
30+
remoteURL: "https://github.com/test/repo.git",
31+
branchName: "feature/test",
32+
},
33+
{
34+
name: "repo with main branch",
35+
remoteURL: "https://github.com/test/repo.git",
36+
branchName: "main",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
// Call SetupTestRepo
43+
tmpDir, cleanup := SetupTestRepo(t, tt.remoteURL, tt.branchName)
44+
defer cleanup()
45+
46+
// Verify that the directory was created and exists
47+
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
48+
t.Errorf("SetupTestRepo() did not create directory at %s", tmpDir)
49+
}
50+
51+
// Verify that it's a git repository
52+
repo, err := git.PlainOpen(tmpDir)
53+
if err != nil {
54+
t.Errorf("SetupTestRepo() did not create a valid git repository: %v", err)
55+
}
56+
57+
// Verify that test.txt file was created
58+
testFile := filepath.Join(tmpDir, "test.txt")
59+
if _, err := os.Stat(testFile); os.IsNotExist(err) {
60+
t.Errorf("SetupTestRepo() did not create test.txt file")
61+
}
62+
63+
// Verify that the file is committed
64+
w, err := repo.Worktree()
65+
if err != nil {
66+
t.Errorf("SetupTestRepo() failed to get worktree: %v", err)
67+
}
68+
69+
status, err := w.Status()
70+
if err != nil {
71+
t.Errorf("SetupTestRepo() failed to get status: %v", err)
72+
}
73+
74+
// Should have no uncommitted changes
75+
if !status.IsClean() {
76+
t.Errorf("SetupTestRepo() left uncommitted changes in repository")
77+
}
78+
79+
// Verify remote is set up if remoteURL was provided
80+
if tt.remoteURL != "" {
81+
remote, err := repo.Remote("origin")
82+
if err != nil {
83+
t.Errorf("SetupTestRepo() did not create origin remote: %v", err)
84+
} else {
85+
config := remote.Config()
86+
if len(config.URLs) == 0 || config.URLs[0] != tt.remoteURL {
87+
t.Errorf("SetupTestRepo() did not set correct remote URL. Got %v, want %v", config.URLs, tt.remoteURL)
88+
}
89+
}
90+
}
91+
92+
// Verify branch is created and checked out if branchName was provided
93+
if tt.branchName != "" {
94+
head, err := repo.Head()
95+
if err != nil {
96+
t.Errorf("SetupTestRepo() did not set HEAD: %v", err)
97+
}
98+
99+
branchRefName := plumbing.ReferenceName("refs/heads/" + tt.branchName)
100+
branchRef, err := repo.Reference(branchRefName, true)
101+
if err != nil {
102+
t.Errorf("SetupTestRepo() did not create branch %s: %v", tt.branchName, err)
103+
}
104+
105+
if branchRef.Hash() != head.Hash() {
106+
t.Errorf("SetupTestRepo() did not checkout branch %s", tt.branchName)
107+
}
108+
}
109+
})
110+
}
111+
}
112+
113+
func TestSetupTestRepo_Cleanup(t *testing.T) {
114+
// Test that cleanup function works correctly
115+
tmpDir, cleanup := SetupTestRepo(t, "", "")
116+
defer cleanup()
117+
118+
// Verify directory exists before cleanup
119+
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
120+
t.Errorf("SetupTestRepo() did not create directory")
121+
}
122+
123+
// Call cleanup
124+
cleanup()
125+
126+
// Verify directory is removed after cleanup
127+
if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
128+
t.Errorf("SetupTestRepo() cleanup did not remove directory")
129+
}
130+
}

0 commit comments

Comments
 (0)