Files
autogits/common/utils_test.go
Adam Majer 0e06ba5993 common: classifying rm branches on name
Branches with suffixes

  -rm
  -removed
  -deleted

are now classified as removed. This is important in case project
config refers to default branch names which must exist so we need
to be able to classify such branches to either use them or ignore
them
2025-11-04 18:00:21 +01:00

223 lines
4.3 KiB
Go

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)
}
})
}
}
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)
}
})
}
}