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) } }) } } func TestRemoteName(t *testing.T) { tests := []struct { name string giturl *common.GitUrl remotename string }{ { name: "empty", remotename: "origin", }, { name: "regular repo", giturl: &common.GitUrl{ Org: "org1", Repo: "repo2", Commit: "main", }, remotename: "org1_repo2", }, { name: "regular repo with upper case", giturl: &common.GitUrl{ Org: "Org1", Repo: "REPO2", }, remotename: "org1_repo2", }, { name: "no org", giturl: &common.GitUrl{ Repo: "REPO2", }, remotename: "origin", }, { name: "no repo", giturl: &common.GitUrl{ Org: "ORG2", }, remotename: "origin", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { n := test.giturl.RemoteName() if n != test.remotename { t.Errorf("Expected '%s' but received '%s'", test.remotename, n) } }) } }