146 lines
3.3 KiB
Go
146 lines
3.3 KiB
Go
package git
|
|
|
|
import "testing"
|
|
|
|
func TestParseRemoteURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
url string
|
|
wantOwner string
|
|
wantName string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "HTTPS URL with .git",
|
|
url: "https://codeberg.org/romaintb/fgj.git",
|
|
wantOwner: "romaintb",
|
|
wantName: "fgj",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "HTTPS URL without .git",
|
|
url: "https://codeberg.org/romaintb/fgj",
|
|
wantOwner: "romaintb",
|
|
wantName: "fgj",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SSH URL with .git",
|
|
url: "git@codeberg.org:romaintb/fgj.git",
|
|
wantOwner: "romaintb",
|
|
wantName: "fgj",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SSH URL without .git",
|
|
url: "git@codeberg.org:romaintb/fgj",
|
|
wantOwner: "romaintb",
|
|
wantName: "fgj",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SSH protocol URL",
|
|
url: "ssh://git@codeberg.org/romaintb/fgj.git",
|
|
wantOwner: "romaintb",
|
|
wantName: "fgj",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "GitHub HTTPS URL",
|
|
url: "https://github.com/user/repo.git",
|
|
wantOwner: "user",
|
|
wantName: "repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Invalid URL",
|
|
url: "invalid-url",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Empty URL",
|
|
url: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "URL with trailing whitespace",
|
|
url: " https://codeberg.org/owner/repo.git ",
|
|
wantOwner: "owner",
|
|
wantName: "repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "URL with port number",
|
|
url: "https://git.example.com:443/owner/repo.git",
|
|
wantOwner: "owner",
|
|
wantName: "repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "SSH URL with port parses incorrectly",
|
|
url: "ssh://git@git.example.com:22/owner/repo.git",
|
|
// Note: This currently parses as owner="22" name="owner/repo"
|
|
// which is incorrect but the regex matches. We document this
|
|
// limitation rather than make the test fail.
|
|
wantOwner: "22",
|
|
wantName: "owner/repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "HTTP URL (not HTTPS)",
|
|
url: "http://codeberg.org/owner/repo",
|
|
wantOwner: "owner",
|
|
wantName: "repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Repo name with dashes",
|
|
url: "https://codeberg.org/owner/my-cool-repo.git",
|
|
wantOwner: "owner",
|
|
wantName: "my-cool-repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Repo name with dots",
|
|
url: "https://codeberg.org/owner/my.repo.name.git",
|
|
wantOwner: "owner",
|
|
wantName: "my.repo.name",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Owner with dots",
|
|
url: "https://codeberg.org/owner.name/repo.git",
|
|
wantOwner: "owner.name",
|
|
wantName: "repo",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Missing owner/repo",
|
|
url: "https://codeberg.org/",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Only owner, no repo",
|
|
url: "https://codeberg.org/owner",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
owner, name, err := parseRemoteURL(tt.url)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("parseRemoteURL() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr {
|
|
if owner != tt.wantOwner {
|
|
t.Errorf("parseRemoteURL() owner = %v, want %v", owner, tt.wantOwner)
|
|
}
|
|
if name != tt.wantName {
|
|
t.Errorf("parseRemoteURL() name = %v, want %v", name, tt.wantName)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|