package common_test import ( "testing" "src.opensuse.org/autogits/common" ) func TestGitUrlParse(t *testing.T) { tests := []struct { name string inputUrl string url common.GitUrl error bool }{ { name: "Empty string", error: true, }, { name: "OpenSUSE HTTPS Url", url: common.GitUrl{ Org: "foo", Repo: "b", }, inputUrl: "https://src.opensuse.org/foo/b.git", }, { name: "OpenSUSE HTTPS Url", url: common.GitUrl{ Org: "a", Repo: "b", }, inputUrl: "https://src.opensuse.org/a/b", }, { name: "OpenSUSE HTTPS Url", url: common.GitUrl{ Org: "foo", Repo: "bar", Commit: "main", }, inputUrl: "https://src.opensuse.org/foo/bar.git#main", }, { name: "invalid OpenSUSE HTTPS Url", inputUrl: "https://src.opensuse.org/bar.git#main", error: true, }, { name: "OpenSUSE SSH Url", url: common.GitUrl{ Org: "foo", Repo: "bar", Commit: "main", }, inputUrl: "ssh://src.opensuse.org/foo/bar.git#main", }, { name: "SSH native OpenSUSE Url", inputUrl: "gitea@src.opensuse.org:foo/bar.git#main", url: common.GitUrl{ Org: "foo", Repo: "bar", Commit: "main", }, }, { name: "SSH native OpenSUSE Url without user", inputUrl: "src.opensuse.org:foo/bar.git#main", url: common.GitUrl{ Org: "foo", Repo: "bar", Commit: "main", }, }, { name: "invalid SSH native OpenSUSE Url without user", inputUrl: "src.opensuse.org:/br.it", error: true, }, { name: "SSH native OpenSUSE Url without user", inputUrl: "src.opensuse.org:foo/bar#main", url: common.GitUrl{ Org: "foo", Repo: "bar", Commit: "main", }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { url, err := common.ParseGitRemoteUrl(test.inputUrl) if test.error && err != nil { return } if test.error && err == nil { t.Fatal("Expected an error but received", *url) } else if !test.error && err != nil { t.Error(err) } if url == nil { t.Fatal("Recieved nil. Expected", test.url) } else if *url != test.url { t.Fatalf("Expected %v but received %v", test.url, *url) } }) } }