Skip to content

Commit 1a3a54f

Browse files
committed
update coverage
1 parent b6828c4 commit 1a3a54f

File tree

8 files changed

+151
-94
lines changed

8 files changed

+151
-94
lines changed

cmd/browser_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"runtime"
45
"testing"
56
)
67

@@ -54,3 +55,31 @@ func Test_openURLInBrowser(t *testing.T) {
5455
})
5556
}
5657
}
58+
59+
func Test_openWithXdgOpen(t *testing.T) {
60+
// This test is for Linux-specific functionality
61+
if runtime.GOOS != "linux" {
62+
t.Skip("Skipping test on non-Linux platforms")
63+
}
64+
65+
tests := []struct {
66+
name string
67+
url string
68+
wantErr bool
69+
}{
70+
{
71+
name: "success",
72+
url: "https://github.com/zhaochunqi/git-open",
73+
wantErr: false,
74+
},
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
err := openWithXdgOpen(tt.url)
80+
if (err != nil) != tt.wantErr {
81+
t.Errorf("openWithXdgOpen() error = %v, wantErr %v", err, tt.wantErr)
82+
}
83+
})
84+
}
85+
}

cmd/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ func buildBranchURL(baseURL, branchName, remoteURL string) string {
118118
// Default to GitHub-like path for unknown services or if no specific path is needed
119119
return fmt.Sprintf("%s/tree/%s", baseURL, branchName)
120120
}
121-
}
121+
}

cmd/git_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,31 @@ func Test_getRemoteURL(t *testing.T) {
7979
wantErr bool
8080
}{
8181
{
82-
name: "github https url",
82+
name: "github https url",
8383
remoteURL: "https://github.com/zhaochunqi/git-open.git",
84-
want: "https://github.com/zhaochunqi/git-open.git",
85-
wantErr: false,
84+
want: "https://github.com/zhaochunqi/git-open.git",
85+
wantErr: false,
8686
},
8787
{
88-
name: "github ssh url",
88+
name: "github ssh url",
8989
remoteURL: "git@github.com:zhaochunqi/git-open.git",
90-
want: "git@github.com:zhaochunqi/git-open.git",
91-
wantErr: false,
90+
want: "git@github.com:zhaochunqi/git-open.git",
91+
wantErr: false,
9292
},
9393
{
94-
name: "gitlab https url",
94+
name: "gitlab https url",
9595
remoteURL: "https://gitlab.com/user/repo.git",
96-
want: "https://gitlab.com/user/repo.git",
97-
wantErr: false,
96+
want: "https://gitlab.com/user/repo.git",
97+
wantErr: false,
9898
},
9999
{
100-
name: "no remote url",
100+
name: "no remote url",
101101
remoteURL: "",
102-
want: "",
103-
wantErr: true,
102+
want: "",
103+
wantErr: true,
104104
},
105105
{
106-
name: "empty remote urls",
106+
name: "empty remote urls",
107107
remoteURL: "https://github.com/zhaochunqi/git-open.git",
108108
setup: func(t *testing.T, repo *git.Repository) {
109109
// Remove all remotes
@@ -120,7 +120,6 @@ func Test_getRemoteURL(t *testing.T) {
120120
want: "",
121121
wantErr: true,
122122
},
123-
124123
}
125124

126125
for _, tt := range tests {
@@ -250,7 +249,6 @@ func Test_getBranchName(t *testing.T) {
250249
branchName: "feature-branch",
251250
wantErr: false,
252251
},
253-
254252
}
255253

256254
for _, tt := range tests {
@@ -439,4 +437,4 @@ func Test_getRemoteURL_EmptyURLs(t *testing.T) {
439437
if err != nil && err.Error() != "remote URL not found" {
440438
t.Errorf("Expected error message 'remote URL not found', got '%s'", err.Error())
441439
}
442-
}
440+
}

cmd/root_test.go

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"github.com/go-git/go-git/v5"
1010
"github.com/spf13/cobra"
11-
"path/filepath"
1211
"github.com/zhaochunqi/git-open/internal/testhelper"
12+
"path/filepath"
1313
)
1414

1515
func Test_rootCmd(t *testing.T) {
@@ -46,7 +46,7 @@ func Test_rootCmd(t *testing.T) {
4646
cmd := &cobra.Command{}
4747
buf := new(bytes.Buffer)
4848
cmd.SetOut(buf)
49-
49+
5050
// Set flags
5151
cmd.Flags().Bool("plain", false, "")
5252
if err := cmd.Flags().Set("plain", "true"); err != nil {
@@ -69,33 +69,33 @@ func Test_rootCmd(t *testing.T) {
6969

7070
func Test_Execute(t *testing.T) {
7171
tests := []struct {
72-
name string
73-
args []string
74-
wantError bool
72+
name string
73+
args []string
74+
wantError bool
7575
expectedURL string // New field to store the expected URL
7676
}{
7777
{
78-
name: "normal execution - github main branch",
79-
wantError: false,
78+
name: "normal execution - github main branch",
79+
wantError: false,
8080
expectedURL: "https://github.com/zhaochunqi/git-open",
8181
},
8282
{
83-
name: "normal execution - github feature branch",
84-
wantError: false,
83+
name: "normal execution - github feature branch",
84+
wantError: false,
8585
expectedURL: "https://github.com/zhaochunqi/git-open/tree/feature-branch",
8686
},
8787
{
88-
name: "normal execution - gitlab feature branch",
89-
wantError: false,
88+
name: "normal execution - gitlab feature branch",
89+
wantError: false,
9090
expectedURL: "https://gitlab.com/zhaochunqi/git-open/-/tree/feature-branch",
9191
},
9292
{
93-
name: "normal execution - bitbucket feature branch",
94-
wantError: false,
93+
name: "normal execution - bitbucket feature branch",
94+
wantError: false,
9595
expectedURL: "https://bitbucket.org/zhaochunqi/git-open/src/feature-branch",
9696
},
9797
{
98-
name: "no git repo",
98+
name: "no git repo",
9999
wantError: true,
100100
},
101101
}
@@ -105,7 +105,7 @@ func Test_Execute(t *testing.T) {
105105
// Save and restore os.Args
106106
oldArgs := os.Args
107107
defer func() { os.Args = oldArgs }()
108-
108+
109109
if tt.args != nil {
110110
os.Args = tt.args
111111
}
@@ -168,11 +168,11 @@ func Test_initConfig(t *testing.T) {
168168
wantErr bool
169169
}{
170170
{
171-
name: "normal config",
171+
name: "normal config",
172172
wantErr: false,
173173
},
174174
{
175-
name: "with config file",
175+
name: "with config file",
176176
wantErr: false,
177177
},
178178
}
@@ -189,18 +189,18 @@ func Test_initConfig(t *testing.T) {
189189
})
190190

191191
// Create temporary home directory
192-
tmpHome, err := os.MkdirTemp("", "home")
193-
if err != nil {
194-
t.Fatal(err)
195-
}
196-
t.Cleanup(func() {
197-
os.RemoveAll(tmpHome)
198-
})
192+
tmpHome, err := os.MkdirTemp("", "home")
193+
if err != nil {
194+
t.Fatal(err)
195+
}
196+
t.Cleanup(func() {
197+
os.RemoveAll(tmpHome)
198+
})
199199

200200
// Set temporary home directory
201-
if err := os.Setenv("HOME", tmpHome); err != nil {
202-
t.Fatal(err)
203-
}
201+
if err := os.Setenv("HOME", tmpHome); err != nil {
202+
t.Fatal(err)
203+
}
204204
case "with config file":
205205
// Save original home directory
206206
origHome := os.Getenv("HOME")
@@ -209,30 +209,30 @@ func Test_initConfig(t *testing.T) {
209209
})
210210

211211
// Create temporary home directory
212-
tmpHome, err := os.MkdirTemp("", "home")
213-
if err != nil {
214-
t.Fatal(err)
215-
}
216-
t.Cleanup(func() {
217-
os.RemoveAll(tmpHome)
218-
})
212+
tmpHome, err := os.MkdirTemp("", "home")
213+
if err != nil {
214+
t.Fatal(err)
215+
}
216+
t.Cleanup(func() {
217+
os.RemoveAll(tmpHome)
218+
})
219219

220220
// Create .git-open directory
221-
configDir := filepath.Join(tmpHome, ".git-open")
222-
if err := os.MkdirAll(configDir, 0755); err != nil {
223-
t.Fatal(err)
224-
}
221+
configDir := filepath.Join(tmpHome, ".git-open")
222+
if err := os.MkdirAll(configDir, 0755); err != nil {
223+
t.Fatal(err)
224+
}
225225

226226
// Create config file
227-
configFile := filepath.Join(configDir, "config.yaml")
228-
if err := os.WriteFile(configFile, []byte("browser: firefox"), 0644); err != nil {
229-
t.Fatal(err)
230-
}
227+
configFile := filepath.Join(configDir, "config.yaml")
228+
if err := os.WriteFile(configFile, []byte("browser: firefox"), 0644); err != nil {
229+
t.Fatal(err)
230+
}
231231

232232
// Set temporary home directory
233-
if err := os.Setenv("HOME", tmpHome); err != nil {
234-
t.Fatal(err)
235-
}
233+
if err := os.Setenv("HOME", tmpHome); err != nil {
234+
t.Fatal(err)
235+
}
236236
}
237237

238238
initConfig()
@@ -245,7 +245,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
245245
originalGetRemoteURLFunc := getRemoteURLFunc
246246
originalGetBranchNameFunc := getBranchNameFunc
247247
originalOpenURLInBrowser := OpenURLInBrowser
248-
248+
249249
defer func() {
250250
getCurrentGitDirectoryFunc = originalGetCurrentGitDirectoryFunc
251251
getRemoteURLFunc = originalGetRemoteURLFunc
@@ -272,7 +272,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
272272
setup: func() {
273273
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main")
274274
t.Cleanup(cleanup)
275-
275+
276276
getRemoteURLFunc = func(repo *git.Repository) (string, error) {
277277
return "", errors.New("remote URL error")
278278
}
@@ -284,7 +284,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
284284
setup: func() {
285285
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main")
286286
t.Cleanup(cleanup)
287-
287+
288288
getBranchNameFunc = func(repo *git.Repository) (string, error) {
289289
return "", errors.New("branch name error")
290290
}
@@ -296,7 +296,7 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
296296
setup: func() {
297297
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main")
298298
t.Cleanup(cleanup)
299-
299+
300300
OpenURLInBrowser = func(url string) error {
301301
return errors.New("browser error")
302302
}
@@ -312,18 +312,18 @@ func Test_rootCmd_ErrorHandling(t *testing.T) {
312312
getRemoteURLFunc = originalGetRemoteURLFunc
313313
getBranchNameFunc = originalGetBranchNameFunc
314314
OpenURLInBrowser = originalOpenURLInBrowser
315-
315+
316316
tt.setup()
317-
317+
318318
// Create a buffer to capture stderr
319319
buf := new(bytes.Buffer)
320320
cmd := &cobra.Command{}
321321
cmd.SetErr(buf)
322-
322+
323323
// Run the root command function
324324
runE := rootCmd.RunE
325325
err := runE(cmd, []string{})
326-
326+
327327
if (err != nil) != tt.wantErr {
328328
t.Errorf("rootCmd.RunE() error = %v, wantErr %v", err, tt.wantErr)
329329
}
@@ -355,7 +355,7 @@ func Test_initConfig_ErrorCases(t *testing.T) {
355355
t.Cleanup(func() {
356356
os.Remove(tmpFile.Name())
357357
})
358-
358+
359359
cfgFile = tmpFile.Name()
360360
},
361361
},
@@ -381,7 +381,7 @@ func Test_initConfig_ErrorCases(t *testing.T) {
381381
if err := os.Setenv("HOME", tmpHome); err != nil {
382382
t.Fatal(err)
383383
}
384-
384+
385385
cfgFile = ""
386386
},
387387
},
@@ -393,4 +393,4 @@ func Test_initConfig_ErrorCases(t *testing.T) {
393393
initConfig()
394394
})
395395
}
396-
}
396+
}

cmd/version_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ func Test_versionCmd(t *testing.T) {
1313
origVersion := Version
1414
origCommitHash := CommitHash
1515
origBuildDate := BuildDate
16-
16+
1717
// Restore original values after test
1818
defer func() {
1919
Version = origVersion
2020
CommitHash = origCommitHash
2121
BuildDate = origBuildDate
2222
}()
23-
23+
2424
// Set test values
2525
Version = "1.0.0"
2626
CommitHash = "abc123"
2727
BuildDate = "2025-01-19"
28-
28+
2929
// Create buffer to capture output
3030
buf := new(bytes.Buffer)
3131
cmd := &cobra.Command{}
@@ -36,7 +36,7 @@ func Test_versionCmd(t *testing.T) {
3636

3737
expected := fmt.Sprintf("Version: %s\nGit Commit: %s\nBuild Date: %s\n",
3838
Version, CommitHash, BuildDate)
39-
39+
4040
if got := buf.String(); got != expected {
4141
t.Errorf("version command output = %q, want %q", got, expected)
4242
}

0 commit comments

Comments
 (0)