Skip to content

Commit 36d9813

Browse files
committed
test: improve coverage for branch and invalid remote handling
1 parent 3013c9d commit 36d9813

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

cmd/git.go

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

33
import (
4+
"errors"
45
"fmt"
56
"strings"
67

@@ -81,7 +82,10 @@ func convertToWebURL(url string) string {
8182
var getBranchNameFunc = func(repo *git.Repository) (string, error) {
8283
head, err := repo.Head()
8384
if err == nil {
84-
return head.Name().Short(), nil
85+
if head.Name().IsBranch() {
86+
return head.Name().Short(), nil
87+
}
88+
err = errors.New("detached HEAD")
8589
}
8690

8791
ref, refErr := repo.Reference("HEAD", true)

cmd/git_additional_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/go-git/go-git/v5/plumbing"
8+
"github.com/zhaochunqi/git-open/internal/testhelper"
9+
)
10+
11+
func Test_getBranchName_DetachedHEAD(t *testing.T) {
12+
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main")
13+
defer cleanup()
14+
15+
repo, err := getCurrentGitDirectory()
16+
if err != nil {
17+
t.Fatalf("getCurrentGitDirectory() error = %v", err)
18+
}
19+
20+
head, err := repo.Head()
21+
if err != nil {
22+
t.Fatalf("repo.Head() error = %v", err)
23+
}
24+
25+
if err := repo.Storer.SetReference(plumbing.NewHashReference(plumbing.HEAD, head.Hash())); err != nil {
26+
t.Fatalf("set detached HEAD failed: %v", err)
27+
}
28+
29+
branch, err := getBranchName(repo)
30+
if err == nil {
31+
t.Fatalf("getBranchName() expected error on detached HEAD, got branch=%q", branch)
32+
}
33+
if !strings.Contains(err.Error(), "error getting HEAD") {
34+
t.Fatalf("getBranchName() error = %v, want message containing 'error getting HEAD'", err)
35+
}
36+
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ and converts it to a web URL. The web URL is then printed to the console.`,
3838

3939
// Convert the remote URL to a web URL
4040
webURL := convertToWebURL(remoteURL)
41+
if webURL == "" {
42+
return fmt.Errorf("unsupported remote URL format: %s", remoteURL)
43+
}
4144

4245
// Open the web URL in the browser if the -o flag is provided
4346
plain, _ := cmd.Flags().GetBool("plain")

cmd/root_additional_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/go-git/go-git/v5"
9+
"github.com/spf13/cobra"
10+
"github.com/zhaochunqi/git-open/internal/testhelper"
11+
)
12+
13+
func Test_rootCmd_InvalidRemoteURLFormat(t *testing.T) {
14+
originalGetRemoteURLFunc := getRemoteURLFunc
15+
defer func() { getRemoteURLFunc = originalGetRemoteURLFunc }()
16+
17+
_, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main")
18+
defer cleanup()
19+
20+
getRemoteURLFunc = func(repo *git.Repository) (string, error) {
21+
return "invalid-remote", nil
22+
}
23+
24+
cmd := &cobra.Command{}
25+
cmd.SetOut(new(bytes.Buffer))
26+
cmd.Flags().Bool("plain", false, "")
27+
cmd.Flags().Bool("version", false, "")
28+
29+
err := rootCmd.RunE(cmd, []string{})
30+
if err == nil {
31+
t.Fatal("rootCmd.RunE() expected error for invalid remote URL format, got nil")
32+
}
33+
if !strings.Contains(err.Error(), "unsupported remote URL format") {
34+
t.Fatalf("rootCmd.RunE() error = %v, want message containing 'unsupported remote URL format'", err)
35+
}
36+
}

0 commit comments

Comments
 (0)