package common_test import ( "reflect" "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) } }) } } func TestRemovedBranchName(t *testing.T) { tests := []struct { name string branchName string isRemoved bool regularName string }{ { name: "Empty branch", }, { name: "Removed suffix only", branchName: "-rm", isRemoved: false, regularName: "-rm", }, { name: "Capital suffix", branchName: "Foo-Rm", isRemoved: true, regularName: "Foo", }, { name: "Other suffixes", isRemoved: true, branchName: "Goo-Rm-DeleteD", regularName: "Goo-Rm", }, { name: "Other suffixes", isRemoved: true, branchName: "main-REMOVED", regularName: "main", }, { name: "Not removed separator", isRemoved: false, branchName: "main;REMOVED", regularName: "main;REMOVED", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if r := common.IsRemovedBranch(test.branchName); r != test.isRemoved { t.Error("Expecting isRemoved:", test.isRemoved, "but received", r) } if tn := common.TrimRemovedBranchSuffix(test.branchName); tn != test.regularName { t.Error("Expected stripped branch name to be:", test.regularName, "but have:", tn) } }) } } func TestNewPackageIssueParsing(t *testing.T) { tests := []struct { name string input string issues *common.NewRepos }{ { name: "Nothing", }, { name: "Basic repo", input: "org/repo#branch", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org", Repository: "repo", Branch: "branch", PackageName: "repo"}, }, }, }, { name: "Default branch and junk lines and approval for maintainership", input: "\n\nsome comments\n\norg1/repo2\n\nmaintainership: yes", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org1", Repository: "repo2", Branch: "", PackageName: "repo2"}, }, IsMaintainer: true, }, }, { name: "Default branch and junk lines and no maintainership", input: "\n\nsome comments\n\norg1/repo2\n\nmaintainership: NEVER", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org1", Repository: "repo2", Branch: "", PackageName: "repo2"}, }, }, }, { name: "3 repos with comments and maintainership", input: "\n\nsome comments for org1/repo2 are here and more\n\norg1/repo2#master\n org2/repo3#master\n some/repo3#m\nMaintainer ok", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org1", Repository: "repo2", Branch: "master", PackageName: "repo2"}, {Organization: "org2", Repository: "repo3", Branch: "master", PackageName: "repo3"}, {Organization: "some", Repository: "repo3", Branch: "m", PackageName: "repo3"}, }, IsMaintainer: true, }, }, { name: "Invalid repos with spaces", input: "or g/repo#branch\norg/r epo#branch\norg/repo#br anch\norg/repo#branch As foo ++", }, { name: "Valid repos with spaces", input: " org / repo # branch", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org", Repository: "repo", Branch: "branch", PackageName: "repo"}, }, }, }, { name: "Package name is not repo name", input: " org / repo # branch as repo++ \nmaintainer true", issues: &common.NewRepos{ Repos: []struct{ Organization, Repository, Branch, PackageName string }{ {Organization: "org", Repository: "repo", Branch: "branch", PackageName: "repo++"}, }, IsMaintainer: true, }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { issue := common.FindNewReposInIssueBody(test.input) if !reflect.DeepEqual(test.issues, issue) { t.Error("Expected", test.issues, "but have", issue) } }) } }