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 TestProjectConfigMatcher(t *testing.T) { configs := common.AutogitConfigs{ { Organization: "test", GitProjectName: "test/prjgit#main", }, { Organization: "test", Branch: "main", GitProjectName: "test/prjgit#main", }, } tests := []struct { name string org string repo string branch string config int }{ { name: "invalid match", org: "foo", repo: "bar", config: -1, }, { name: "default branch", org: "test", repo: "foo", branch: "", config: 0, }, { name: "main branch", org: "test", repo: "foo", branch: "main", config: 1, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { c := configs.GetPrjGitConfig(test.org, test.repo, test.branch) if test.config < 0 { if c != nil { t.Fatal("Expected nil. Got:", *c) } } else if config := configs[test.config]; c != config { t.Fatal("Expected", *config, "got", *c) } }) } } 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) config, err := common.ReadWorkflowConfig(gitea, "foo/bar") if err != nil { t.Fatal(err) } if config.ManualMergeOnly != false { t.Fatal("This should be false") } }) } } 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) } }) } }