Skip to content

Commit 8e15feb

Browse files
committed
Add some tests
1 parent a847778 commit 8e15feb

1 file changed

Lines changed: 160 additions & 37 deletions

File tree

go/cmd/gitter/repository_test.go

Lines changed: 160 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,84 @@ import (
1313
"github.com/google/go-cmp/cmp/cmpopts"
1414
)
1515

16-
// A very simple test repository with 3 commits and 2 tags.
17-
func setupTestRepo(t *testing.T) string {
16+
func runGit(t *testing.T, repoPath string, args ...string) {
1817
t.Helper()
19-
repoPath := t.TempDir()
18+
cmd := exec.Command("git", args...)
19+
cmd.Dir = repoPath
20+
if out, err := cmd.CombinedOutput(); err != nil {
21+
t.Fatalf("git %v failed: %v\nOutput: %s", args, err, out)
22+
}
23+
}
2024

21-
runGit := func(args ...string) {
22-
cmd := exec.Command("git", args...)
23-
cmd.Dir = repoPath
24-
if out, err := cmd.CombinedOutput(); err != nil {
25-
t.Fatalf("git %v failed: %v\nOutput: %s", args, err, out)
26-
}
25+
// A very simple test repository with 3 commits and 2 tags.
26+
func setupTestRepo(t *testing.T, url string) string {
27+
t.Helper()
28+
gitStorePath = t.TempDir()
29+
repoPath := filepath.Join(gitStorePath, getRepoDirName(url))
30+
if err := os.MkdirAll(repoPath, 0755); err != nil {
31+
t.Fatalf("failed to create repo path %s: %v", repoPath, err)
2732
}
2833

29-
runGit("init")
30-
runGit("config", "user.email", "test@test.com")
31-
runGit("config", "user.name", "Test Name")
34+
runGit(t, repoPath, "init")
35+
runGit(t, repoPath, "config", "user.email", "test@test.com")
36+
runGit(t, repoPath, "config", "user.name", "Test Name")
3237

3338
// Commit 1
3439
err := os.WriteFile(filepath.Join(repoPath, "file1"), []byte("1"), 0600)
3540
if err != nil {
3641
t.Fatalf("failed to write file1 for git repo setup: %v", err)
3742
}
38-
runGit("add", "file1")
39-
runGit("commit", "-m", "commit 1")
43+
runGit(t, repoPath, "add", "file1")
44+
runGit(t, repoPath, "commit", "-m", "commit 1")
4045

4146
// Commit 2 + Tag
4247
err = os.WriteFile(filepath.Join(repoPath, "file2"), []byte("2"), 0600)
4348
if err != nil {
4449
t.Fatalf("failed to write file2 for git repo setup: %v", err)
4550
}
46-
runGit("add", "file2")
47-
runGit("commit", "-m", "commit 2")
48-
runGit("tag", "v1.0.0")
51+
runGit(t, repoPath, "add", "file2")
52+
runGit(t, repoPath, "commit", "-m", "commit 2")
53+
runGit(t, repoPath, "tag", "v1.0.0")
4954

5055
// Commit 3 + Tag
5156
err = os.WriteFile(filepath.Join(repoPath, "file3"), []byte("3"), 0600)
5257
if err != nil {
5358
t.Fatalf("failed to write file3 for git repo setup: %v", err)
5459
}
55-
runGit("add", "file3")
56-
runGit("commit", "-m", "commit 3")
57-
runGit("tag", "v1.1.0")
60+
runGit(t, repoPath, "add", "file3")
61+
runGit(t, repoPath, "commit", "-m", "commit 3")
62+
runGit(t, repoPath, "tag", "v1.1.0")
63+
64+
return repoPath
65+
}
66+
67+
// An extremely simple test repository with 1 commit and no tags.
68+
func setupEmptyTestRepo(t *testing.T, url string) string {
69+
t.Helper()
70+
gitStorePath = t.TempDir()
71+
repoPath := filepath.Join(gitStorePath, getRepoDirName(url))
72+
if err := os.MkdirAll(repoPath, 0755); err != nil {
73+
t.Fatalf("failed to create repo path %s: %v", repoPath, err)
74+
}
75+
76+
runGit(t, repoPath, "init")
77+
runGit(t, repoPath, "config", "user.email", "test@test.com")
78+
runGit(t, repoPath, "config", "user.name", "Test Name")
79+
80+
err := os.WriteFile(filepath.Join(repoPath, "file1"), []byte("1"), 0600)
81+
if err != nil {
82+
t.Fatalf("failed to write file1 for git repo setup: %v", err)
83+
}
84+
runGit(t, repoPath, "add", "file1")
85+
runGit(t, repoPath, "commit", "-m", "commit 1")
5886

5987
return repoPath
6088
}
6189

6290
func TestBuildCommitGraph(t *testing.T) {
63-
repoPath := setupTestRepo(t)
64-
r := NewRepository("test-url")
65-
r.repoPath = repoPath
66-
ctx := context.WithValue(t.Context(), urlKey, "test-url")
91+
setupTestRepo(t, "git://test-repo.git")
92+
r := NewRepository("git://test-repo.git")
93+
ctx := context.WithValue(t.Context(), urlKey, "git://test-repo.git")
6794

6895
newCommits, err := r.buildCommitGraph(ctx, nil)
6996

@@ -86,10 +113,9 @@ func TestBuildCommitGraph(t *testing.T) {
86113
}
87114

88115
func TestCalculatePatchIDs(t *testing.T) {
89-
repoPath := setupTestRepo(t)
90-
r := NewRepository("test-url")
91-
r.repoPath = repoPath
92-
ctx := context.WithValue(t.Context(), urlKey, "test-url")
116+
setupTestRepo(t, "git://test-repo.git")
117+
r := NewRepository("git://test-repo.git")
118+
ctx := context.WithValue(t.Context(), urlKey, "git://test-repo.git")
93119

94120
newCommits, err := r.buildCommitGraph(ctx, nil)
95121
if err != nil {
@@ -111,8 +137,8 @@ func TestCalculatePatchIDs(t *testing.T) {
111137
}
112138

113139
func TestLoadRepository(t *testing.T) {
114-
repoPath := setupTestRepo(t)
115-
ctx := context.WithValue(t.Context(), urlKey, "test-url")
140+
repoPath := setupTestRepo(t, "git://test-repo.git")
141+
ctx := context.WithValue(t.Context(), urlKey, "git://test-repo.git")
116142

117143
// First loadRepository with a brand new repo
118144
r1, err := LoadRepository(ctx, repoPath)
@@ -193,7 +219,7 @@ var cmpSHA1Opts = []cmp.Option{
193219
}
194220

195221
func TestExpandByCherrypick(t *testing.T) {
196-
repo := NewRepository("test-url")
222+
repo := NewRepository("git://test-repo.git")
197223
repo.repoPath = "/repo"
198224

199225
// Commit hashes
@@ -249,7 +275,7 @@ func TestExpandByCherrypick(t *testing.T) {
249275

250276
// Testing cases with introduced and fixed only.
251277
func TestAffected_Introduced_Fixed(t *testing.T) {
252-
repo := NewRepository("test-url")
278+
repo := NewRepository("git://test-repo.git")
253279
repo.repoPath = "/repo"
254280

255281
// Graph: (Parent -> Child)
@@ -366,7 +392,7 @@ func TestAffected_Introduced_Fixed(t *testing.T) {
366392
}
367393

368394
func TestAffected_Introduced_LastAffected(t *testing.T) {
369-
repo := NewRepository("test-url")
395+
repo := NewRepository("git://test-repo.git")
370396
repo.repoPath = "/repo"
371397

372398
// Graph: (Parent -> Child)
@@ -484,7 +510,7 @@ func TestAffected_Introduced_LastAffected(t *testing.T) {
484510

485511
// Testing with both fixed and lastAffected
486512
func TestAffected_Combined(t *testing.T) {
487-
repo := NewRepository("test-url")
513+
repo := NewRepository("git://test-repo.git")
488514
repo.repoPath = "/repo"
489515

490516
// Graph: (Parent -> Child)
@@ -589,7 +615,7 @@ func TestAffected_Combined(t *testing.T) {
589615
}
590616

591617
func TestAffected_Cherrypick(t *testing.T) {
592-
repo := NewRepository("test-url")
618+
repo := NewRepository("git://test-repo.git")
593619
repo.repoPath = "/repo"
594620

595621
// Graph: (Parent -> Child)
@@ -718,7 +744,7 @@ func TestAffected_Cherrypick(t *testing.T) {
718744
}
719745

720746
func TestLimit(t *testing.T) {
721-
repo := NewRepository("test-url")
747+
repo := NewRepository("git://test-repo.git")
722748
repo.repoPath = "/repo"
723749

724750
// Graph: (Parent -> Child)
@@ -801,7 +827,7 @@ func TestLimit(t *testing.T) {
801827
}
802828

803829
func TestLimit_Cherrypick(t *testing.T) {
804-
repo := NewRepository("test-url")
830+
repo := NewRepository("git://test-repo.git")
805831
repo.repoPath = "/repo"
806832

807833
// Graph: (Parent -> Child)
@@ -1035,3 +1061,100 @@ func TestResolveEvents_MultipleRoots(t *testing.T) {
10351061
})
10361062
}
10371063
}
1064+
1065+
// Test runAndParseTags() with mock stdout
1066+
func TestRunAndParseTags(t *testing.T) {
1067+
tests := []struct {
1068+
name string
1069+
cmd *exec.Cmd
1070+
want map[string]SHA1
1071+
wantErr bool
1072+
}{
1073+
{
1074+
name: "Parse mock data",
1075+
cmd: exec.Command("echo", "000000000000000000000000000000000000aaaa refs/tags/v1.0.0\n000000000000000000000000000000000000bbbb refs/tags/v1.1.0\n"),
1076+
want: map[string]SHA1{
1077+
"v1.0.0": decodeSHA1("aaaa"),
1078+
"v1.1.0": decodeSHA1("bbbb"),
1079+
},
1080+
},
1081+
{
1082+
name: "Stdout contains ref/heads and tags, should filter out non-tags",
1083+
cmd: exec.Command("printf", "000000000000000000000000000000000000aaaa refs/tags/v1.0.0\n000000000000000000000000000000000000cccc refs/heads/main\n"),
1084+
want: map[string]SHA1{
1085+
"v1.0.0": decodeSHA1("aaaa"),
1086+
},
1087+
},
1088+
{
1089+
// git show-ref returns exit code 1 when there are no tags, so make sure we don't throw error in this case
1090+
name: "Exit code 1 (no tags)",
1091+
cmd: exec.Command("bash", "-c", "exit 1"),
1092+
want: map[string]SHA1{},
1093+
},
1094+
{
1095+
name: "Exit code 128 (actual error)",
1096+
cmd: exec.Command("bash", "-c", "echo 'some error' >&2; exit 128"),
1097+
wantErr: true,
1098+
},
1099+
}
1100+
1101+
for _, tt := range tests {
1102+
t.Run(tt.name, func(t *testing.T) {
1103+
r := &Repository{}
1104+
got, err := r.runAndParseTags(t.Context(), tt.cmd)
1105+
if (err != nil) != tt.wantErr {
1106+
t.Fatalf("runAndParseTags() error = %v, wantErr %v", err, tt.wantErr)
1107+
}
1108+
if !tt.wantErr {
1109+
if diff := cmp.Diff(tt.want, got); diff != "" {
1110+
t.Errorf("runAndParseTags() mismatch (-want +got):\n%s", diff)
1111+
}
1112+
}
1113+
})
1114+
}
1115+
}
1116+
1117+
func TestGetLocalTags(t *testing.T) {
1118+
tests := []struct {
1119+
name string
1120+
setupFunc func(t *testing.T, url string) string
1121+
wantTags []string
1122+
wantCount int
1123+
}{
1124+
{
1125+
name: "Repo with tags",
1126+
setupFunc: setupTestRepo,
1127+
wantTags: []string{"v1.0.0", "v1.1.0"},
1128+
wantCount: 2,
1129+
},
1130+
{
1131+
name: "Empty repo (no tags)",
1132+
setupFunc: setupEmptyTestRepo,
1133+
wantTags: []string{},
1134+
wantCount: 0,
1135+
},
1136+
}
1137+
1138+
for _, tt := range tests {
1139+
t.Run(tt.name, func(t *testing.T) {
1140+
tt.setupFunc(t, "git://test-repo.git")
1141+
r := NewRepository("git://test-repo.git")
1142+
ctx := context.WithValue(t.Context(), urlKey, "git://test-repo.git")
1143+
1144+
tags, err := r.GetLocalTags(ctx)
1145+
if err != nil {
1146+
t.Fatalf("GetLocalTags failed: %v", err)
1147+
}
1148+
1149+
if len(tags) != tt.wantCount {
1150+
t.Errorf("expected %d tags, got %d", tt.wantCount, len(tags))
1151+
}
1152+
1153+
for _, wantTag := range tt.wantTags {
1154+
if _, ok := tags[wantTag]; !ok {
1155+
t.Errorf("expected tag %s to exist", wantTag)
1156+
}
1157+
}
1158+
})
1159+
}
1160+
}

0 commit comments

Comments
 (0)