|
| 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_repoNameFromWebURL(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + webURL string |
| 17 | + want string |
| 18 | + }{ |
| 19 | + {"https URL", "https://github.com/zhaochunqi/git-open", "github.com/zhaochunqi/git-open"}, |
| 20 | + {"http URL", "http://gitlab.com/user/repo", "gitlab.com/user/repo"}, |
| 21 | + {"trailing slash", "https://github.com/user/repo/", "github.com/user/repo"}, |
| 22 | + {"no scheme", "github.com/user/repo", "github.com/user/repo"}, |
| 23 | + } |
| 24 | + |
| 25 | + for _, tt := range tests { |
| 26 | + t.Run(tt.name, func(t *testing.T) { |
| 27 | + if got := repoNameFromWebURL(tt.webURL); got != tt.want { |
| 28 | + t.Errorf("repoNameFromWebURL(%q) = %q, want %q", tt.webURL, got, tt.want) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func Test_repoCmd(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + remoteURL string |
| 38 | + want string |
| 39 | + }{ |
| 40 | + {"https URL", "https://github.com/zhaochunqi/git-open.git", "github.com/zhaochunqi/git-open\n"}, |
| 41 | + {"ssh URL", "git@github.com:zhaochunqi/git-open.git", "github.com/zhaochunqi/git-open\n"}, |
| 42 | + {"gitlab URL", "https://gitlab.com/user/repo.git", "gitlab.com/user/repo\n"}, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + _, cleanup := testhelper.SetupTestRepo(t, tt.remoteURL, "main") |
| 48 | + defer cleanup() |
| 49 | + |
| 50 | + buf := new(bytes.Buffer) |
| 51 | + cmd := &cobra.Command{} |
| 52 | + cmd.SetOut(buf) |
| 53 | + |
| 54 | + if err := repoCmd.RunE(cmd, []string{}); err != nil { |
| 55 | + t.Fatalf("repoCmd.RunE() error = %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + if got := buf.String(); got != tt.want { |
| 59 | + t.Errorf("repoCmd output = %q, want %q", got, tt.want) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func Test_repoCmd_InvalidRemoteURLFormat(t *testing.T) { |
| 66 | + originalGetRemoteURLFunc := getRemoteURLFunc |
| 67 | + defer func() { getRemoteURLFunc = originalGetRemoteURLFunc }() |
| 68 | + |
| 69 | + _, cleanup := testhelper.SetupTestRepo(t, "https://github.com/test/repo.git", "main") |
| 70 | + defer cleanup() |
| 71 | + |
| 72 | + getRemoteURLFunc = func(repo *git.Repository) (string, error) { |
| 73 | + return "invalid-remote", nil |
| 74 | + } |
| 75 | + |
| 76 | + cmd := &cobra.Command{} |
| 77 | + cmd.SetOut(new(bytes.Buffer)) |
| 78 | + |
| 79 | + err := repoCmd.RunE(cmd, []string{}) |
| 80 | + if err == nil { |
| 81 | + t.Fatal("repoCmd.RunE() expected error for invalid remote URL format, got nil") |
| 82 | + } |
| 83 | + if !strings.Contains(err.Error(), "unsupported remote URL format") { |
| 84 | + t.Fatalf("repoCmd.RunE() error = %v, want message containing 'unsupported remote URL format'", err) |
| 85 | + } |
| 86 | +} |
0 commit comments