Files
autogits/common/config_test.go

131 lines
2.9 KiB
Go

package common_test
import (
"slices"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestConfigWorkflowParser(t *testing.T) {
tests := []struct {
name string
config_json string
repo models.Repository
}{
{
name: "Regular workflow file",
config_json: `{
"Workflows": ["direct", "pr"],
"Organization": "testing",
"ReviewGroups": [
{
"Name": "gnuman1",
"Reviewers": ["adamm"]
}
]
}`,
repo: models.Repository{
DefaultBranch: "master",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGiteaFileContentAndRepoFetcher(ctl)
gitea.EXPECT().GetRepositoryFileContent("foo", "bar", "", "workflow.config").Return([]byte(test.config_json), "abc", nil)
gitea.EXPECT().GetRepository("foo", "bar").Return(&test.repo, nil)
_, err := common.ReadWorkflowConfig(gitea, "foo/bar")
if err != nil {
t.Fatal(err)
}
})
}
}
func TestProjectGitParser(t *testing.T) {
tests := []struct {
name string
prjgit string
org string
branch string
res [3]string
}{
{
name: "repo only",
prjgit: "repo.git",
org: "org",
branch: "br",
res: [3]string{"org", "repo.git", "master"},
},
{
name: "default",
org: "org",
res: [3]string{"org", common.DefaultGitPrj, "master"},
},
{
name: "repo with branch",
org: "org2",
prjgit: "repo.git#somebranch",
res: [3]string{"org2", "repo.git", "somebranch"},
},
{
name: "repo org and branch",
org: "org3",
prjgit: "oorg/foo.bar#point",
res: [3]string{"oorg", "foo.bar", "point"},
},
{
name: "whitespace shouldn't matter",
prjgit: " oorg / \nfoo.bar\t # point ",
res: [3]string{"oorg", "foo.bar", "point"},
},
{
name: "repo org and empty branch",
org: "org3",
prjgit: "oorg/foo.bar#",
res: [3]string{"oorg", "foo.bar", "master"},
},
{
name: "only branch defined",
org: "org3",
prjgit: "#mybranch",
res: [3]string{"org3", "_ObsPrj", "mybranch"},
},
{
name: "only org and branch defined",
org: "org3",
prjgit: "org1/#mybranch",
res: [3]string{"org1", "_ObsPrj", "mybranch"},
},
{
name: "empty org and repo",
org: "org3",
prjgit: "/repo#",
res: [3]string{"org3", "repo", "master"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := &common.AutogitConfig{
Organization: test.org,
Branch: test.branch,
GitProjectName: test.prjgit,
}
i, j, k := c.GetPrjGit()
res := []string{i, j, k}
if !slices.Equal(res, test.res[:]) {
t.Error("Expected", test.res, "but received", res)
}
})
}
}