forked from git-workflow/autogits
423 lines
11 KiB
Go
423 lines
11 KiB
Go
package common_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
"go.uber.org/mock/gomock"
|
|
|
|
"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 TestSplitStringNoEmpty(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
sep string
|
|
expected []string
|
|
}{
|
|
{"Empty string", "", ",", []string{}},
|
|
{"Only separators", ",,,", ",", []string{}},
|
|
{"Spaces and separators", " , , ", ",", []string{}},
|
|
{"Normal split", "a,b,c", ",", []string{"a", "b", "c"}},
|
|
{"Leading/trailing spaces", " a , b ", ",", []string{"a", "b"}},
|
|
{"Multiple separators", "a,,b", ",", []string{"a", "b"}},
|
|
{"Newlines", "line1\n\nline2", "\n", []string{"line1", "line2"}},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
res := common.SplitStringNoEmpty(test.input, test.sep)
|
|
if !reflect.DeepEqual(res, test.expected) {
|
|
t.Errorf("SplitStringNoEmpty(%q, %q) = %v; want %v", test.input, test.sep, res, test.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTranslateHttpsToSshUrl(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
err bool
|
|
}{
|
|
{"Opensuse HTTPS", "https://src.opensuse.org/org/repo", "ssh://gitea@src.opensuse.org/org/repo", false},
|
|
{"Suse HTTPS", "https://src.suse.de/org/repo", "ssh://gitea@src.suse.de/org/repo", false},
|
|
{"Already SSH", "ssh://gitea@src.opensuse.org/org/repo", "ssh://gitea@src.opensuse.org/org/repo", false},
|
|
{"Native SSH", "gitea@src.opensuse.org:org/repo", "gitea@src.opensuse.org:org/repo", false},
|
|
{"Unknown URL", "https://github.com/org/repo", "", true},
|
|
{"Empty URL", "", "", true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
res, err := common.TranslateHttpsToSshUrl(test.input)
|
|
if (err != nil) != test.err {
|
|
t.Errorf("TranslateHttpsToSshUrl(%q) error = %v; want error %v", test.input, err, test.err)
|
|
}
|
|
if res != test.expected {
|
|
t.Errorf("TranslateHttpsToSshUrl(%q) = %q; want %q", test.input, res, test.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetEnvOverride(t *testing.T) {
|
|
t.Run("GetEnvOverrideString", func(t *testing.T) {
|
|
tests := []struct {
|
|
envValue string
|
|
def string
|
|
expected string
|
|
}{
|
|
{"", "default", "default"},
|
|
{"override", "default", "override"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
if res := common.GetEnvOverrideString(test.envValue, test.def); res != test.expected {
|
|
t.Errorf("GetEnvOverrideString(%q, %q) = %q; want %q", test.envValue, test.def, res, test.expected)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("GetEnvOverrideBool", func(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
envValue string
|
|
def bool
|
|
expected bool
|
|
}{
|
|
{"Empty env value, default false", "", false, false},
|
|
{"Empty env value, default true", "", true, true},
|
|
{"Env '1', default false", "1", false, true},
|
|
{"Env '2', default false", "2", false, true},
|
|
{"Env '0', default false", "0", false, false},
|
|
{"Env 'invalid', default true", "abc", true, false},
|
|
{"Env 'true', default false", "true", false, true},
|
|
{"Env 'YES', default false", "YES", false, true},
|
|
{"Env '0', default true", "0", true, false},
|
|
{"Env 'false', default true", "false", true, false},
|
|
{"Env 'FALSE', default true", "FALSE", true, false},
|
|
{"Env ' true ', default false", " true ", false, true},
|
|
{"Env 'no', default true", "no", true, false},
|
|
{"Env 'NO', default true", "NO", true, false},
|
|
{"Env 'off', default true", "off", true, false},
|
|
{"Env 'on', default false", "on", false, true},
|
|
{"Env 'invalid', default false", "tbc", false, false},
|
|
{"Env 'garbage', default false", "!@#$", false, false},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if res := common.GetEnvOverrideBool(test.envValue, test.def); res != test.expected {
|
|
t.Errorf("GetEnvOverrideBool(%q, %v) = %v; want %v", test.envValue, test.def, res, test.expected)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func NewController(t *testing.T) *gomock.Controller {
|
|
common.SetTestLogger(t)
|
|
return gomock.NewController(t)
|
|
}
|