Skip to content

Commit 3a07b04

Browse files
committed
fix coverage problem
1 parent e5bf7f9 commit 3a07b04

4 files changed

Lines changed: 17 additions & 13 deletions

File tree

cmd/git_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func Test_getCurrentGitDirectory(t *testing.T) {
1313
// Use setupTestRepo for setup
14-
_, cleanup := setupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
14+
_, cleanup := SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
1515
defer cleanup()
1616

1717
tests := []struct {
@@ -152,7 +152,7 @@ func Test_getRemoteURL(t *testing.T) {
152152
t.Run(tt.name, func(t *testing.T) {
153153
// If we have a custom test, run it instead of the standard test
154154
if tt.customTest != nil {
155-
_, cleanup := setupTestRepo(t, tt.remoteURL, "main")
155+
_, cleanup := SetupTestRepo(t, tt.remoteURL, "main")
156156
defer cleanup()
157157

158158
repo, err := getCurrentGitDirectory()
@@ -170,7 +170,7 @@ func Test_getRemoteURL(t *testing.T) {
170170
}
171171

172172
// Standard test path
173-
_, cleanup := setupTestRepo(t, tt.remoteURL, "main")
173+
_, cleanup := SetupTestRepo(t, tt.remoteURL, "main")
174174
defer cleanup()
175175

176176
repo, err := getCurrentGitDirectory()

cmd/root_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111

1212
func Test_rootCmd(t *testing.T) {
1313
// Setup test repository
14-
_, cleanup := setupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
14+
_, cleanup := SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
1515
defer cleanup()
1616

17-
// Save original openURLInBrowser function
17+
// Save original openURLInBrowser function and restore after test
1818
original := OpenURLInBrowser
1919
defer func() {
2020
OpenURLInBrowser = original
@@ -56,7 +56,7 @@ func Test_rootCmd(t *testing.T) {
5656
t.Errorf("rootCmd.RunE() error = %v", err)
5757
return
5858
}
59-
59+
6060
if got := buf.String(); got != tt.wantOutput {
6161
t.Errorf("root command output = %q, want %q", got, tt.wantOutput)
6262
}
@@ -118,16 +118,16 @@ func Test_Execute(t *testing.T) {
118118
// Setup logic moved directly into the test case
119119
switch tt.name {
120120
case "normal execution - github main branch":
121-
_, cleanup := setupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
121+
_, cleanup := SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "main")
122122
t.Cleanup(cleanup)
123123
case "normal execution - github feature branch":
124-
_, cleanup := setupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "feature-branch")
124+
_, cleanup := SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "feature-branch")
125125
t.Cleanup(cleanup)
126126
case "normal execution - gitlab feature branch":
127-
_, cleanup := setupTestRepo(t, "https://gitlab.com/zhaochunqi/git-open.git", "feature-branch")
127+
_, cleanup := SetupTestRepo(t, "https://gitlab.com/zhaochunqi/git-open.git", "feature-branch")
128128
t.Cleanup(cleanup)
129129
case "normal execution - bitbucket feature branch":
130-
_, cleanup := setupTestRepo(t, "https://bitbucket.org/zhaochunqi/git-open.git", "feature-branch")
130+
_, cleanup := SetupTestRepo(t, "https://bitbucket.org/zhaochunqi/git-open.git", "feature-branch")
131131
t.Cleanup(cleanup)
132132
case "no git repo":
133133
// Create and change to temp dir without git repo

cmd/testing_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// setupTestRepo creates a temporary git repository for testing.
1616
// It returns the temporary directory path and a cleanup function.
17-
func setupTestRepo(t *testing.T, remoteURL string, branchName string) (string, func()) {
17+
func SetupTestRepo(t *testing.T, remoteURL string, branchName string) (string, func()) {
1818
t.Helper()
1919

2020
// Create temporary directory

main_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ func Test_main(t *testing.T) {
3737
{
3838
name: "default behavior",
3939
args: []string{"git-open"},
40-
wantURL: "https://github.com/zhaochunqi/git-open",
40+
wantURL: "https://github.com/zhaochunqi/git-open/tree/feat/open-branch",
4141
wantError: false,
4242
},
4343
}
4444

4545
for _, tt := range tests {
4646
t.Run(tt.name, func(t *testing.T) {
47+
// Setup test repository for this test case
48+
_, cleanup := cmd.SetupTestRepo(t, "https://github.com/zhaochunqi/git-open.git", "feat/open-branch")
49+
t.Cleanup(cleanup)
50+
4751
os.Args = tt.args
4852
openedURL = ""
4953

@@ -73,4 +77,4 @@ func Test_main(t *testing.T) {
7377
}
7478
})
7579
}
76-
}
80+
}

0 commit comments

Comments
 (0)