package repoclone import "testing" func TestCloneURL(t *testing.T) { tests := []struct { name string provider string owner string repo string protocol string want string wantErr bool }{ { "github SSH", "github", "owner", "repo", ProtocolSSH, "git@github.com:owner/repo.git", false, }, { "github HTTPS", "github", "owner", "repo", ProtocolHTTPS, "https://github.com/owner/repo.git", false, }, { "empty provider defaults to github SSH", "", "owner", "repo", ProtocolSSH, "git@github.com:owner/repo.git", false, }, { "gitlab SSH", "gitlab", "owner", "repo", ProtocolSSH, "git@gitlab.com:owner/repo.git", false, }, { "bitbucket HTTPS", "bitbucket", "owner", "repo", ProtocolHTTPS, "https://bitbucket.org/owner/repo.git", false, }, { "unsupported provider", "unknown", "owner", "repo", ProtocolSSH, "", true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := CloneURL(tt.provider, tt.owner, tt.repo, tt.protocol) if tt.wantErr { if err == nil { t.Errorf("expected error, got nil") } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if got != tt.want { t.Errorf("got %q, want %q", got, tt.want) } }) } } func TestCloneURLWithHost(t *testing.T) { tests := []struct { name string provider string host string owner string repo string protocol string want string }{ { "gitlab self-managed SSH", "gitlab", "https://gitlab.acme.corp", "team", "service", ProtocolSSH, "git@gitlab.acme.corp:team/service.git", }, { "gitlab self-managed HTTPS", "gitlab", "https://gitlab.acme.corp/", "team", "service", ProtocolHTTPS, "https://gitlab.acme.corp/team/service.git", }, { "empty host falls back to provider default", "gitlab", "", "team", "service", ProtocolSSH, "git@gitlab.com:team/service.git", }, { "http scheme honored", "gitlab", "http://gitlab.local", "team", "service", ProtocolHTTPS, "https://gitlab.local/team/service.git", }, { // scp-style "git@host:path" can't carry a port; ssh:// URL // form is the only correct shape when one is present. "self-managed SSH with port falls back to ssh:// URL", "gitlab", "https://gitlab.acme.corp:2222", "team", "service", ProtocolSSH, "ssh://git@gitlab.acme.corp:2222/team/service.git", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := CloneURLWithHost(tt.provider, tt.host, tt.owner, tt.repo, tt.protocol) if err != nil { t.Fatalf("err = %v", err) } if got != tt.want { t.Errorf("got %q, want %q", got, tt.want) } }) } } func TestProviderHost(t *testing.T) { tests := []struct { provider string want string wantErr bool }{ {"github", "github.com", false}, {"GitHub", "github.com", false}, {"", "github.com", false}, {"gitlab", "gitlab.com", false}, {"bitbucket", "bitbucket.org", false}, {"unknown", "", true}, } for _, tt := range tests { t.Run(tt.provider, func(t *testing.T) { got, err := providerHost(tt.provider) if tt.wantErr { if err == nil { t.Error("expected error") } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if got != tt.want { t.Errorf("got %q, want %q", got, tt.want) } }) } } func TestDetectGitProtocol(t *testing.T) { got := DetectGitProtocol() if got != ProtocolSSH && got != ProtocolHTTPS { t.Errorf("expected %q or %q, got %q", ProtocolSSH, ProtocolHTTPS, got) } }