From 5d27dc1ad0407addfce0b919977211d4d3debdb6 Mon Sep 17 00:00:00 2001 From: Ryo Kitagawa Date: Wed, 10 Apr 2024 19:43:24 +0900 Subject: [PATCH 1/2] refactor: add gomock for test --- Makefile | 3 + go.mod | 16 +- go.sum | 27 +-- pkg/notifier/gitlab/comment_test.go | 137 +++++++----- pkg/notifier/gitlab/commits_test.go | 56 +++-- pkg/notifier/gitlab/gen/gitlab.go | 321 ++++++++++++++++++++++++++++ pkg/notifier/gitlab/gitlab_test.go | 82 ------- pkg/notifier/gitlab/notify_test.go | 96 +++++++-- 8 files changed, 549 insertions(+), 189 deletions(-) create mode 100644 Makefile create mode 100644 pkg/notifier/gitlab/gen/gitlab.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5c5072c --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +.PHONY: mockgen +mockgen: + go run go.uber.org/mock/mockgen -source=./pkg/notifier/gitlab/gitlab.go -destination=./pkg/notifier/gitlab/gen/gitlab.go -package gitlabmock diff --git a/go.mod b/go.mod index eb5837b..1d75545 100644 --- a/go.mod +++ b/go.mod @@ -6,15 +6,12 @@ require ( github.com/Masterminds/sprig/v3 v3.2.2 github.com/drone/envsubst v1.0.3 github.com/google/go-cmp v0.5.7 - github.com/google/go-github/v39 v39.2.0 github.com/mattn/go-colorable v0.1.12 - github.com/shurcooL/githubv4 v0.0.0-20220115235240-a14260e6f8a2 github.com/sirupsen/logrus v1.8.1 - github.com/suzuki-shunsuke/github-comment-metadata v0.1.0 - github.com/suzuki-shunsuke/go-ci-env v1.1.0 github.com/suzuki-shunsuke/go-findconfig v1.1.0 github.com/urfave/cli/v2 v2.4.0 - golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 + github.com/xanzy/go-gitlab v0.64.0 + go.uber.org/mock v0.4.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -35,13 +32,14 @@ require ( github.com/mitchellh/reflectwalk v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shopspring/decimal v1.2.0 // indirect - github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect github.com/spf13/cast v1.3.1 // indirect - github.com/xanzy/go-gitlab v0.64.0 // indirect golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect - golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect - golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + golang.org/x/mod v0.11.0 // indirect + golang.org/x/net v0.1.0 // indirect + golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect + golang.org/x/sys v0.1.0 // indirect golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect + golang.org/x/tools v0.2.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.25.0 // indirect diff --git a/go.sum b/go.sum index ef81ba4..c96e0e8 100644 --- a/go.sum +++ b/go.sum @@ -94,11 +94,8 @@ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= -github.com/google/go-github/v39 v39.2.0 h1:rNNM311XtPOz5rDdsJXAp2o8F67X9FnROXTvto3aSnQ= -github.com/google/go-github/v39 v39.2.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -118,6 +115,7 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= github.com/hashicorp/go-retryablehttp v0.6.8 h1:92lWxgpa+fF3FozM4B3UZtHZMJX8T5XT+TFdCxsPyWs= github.com/hashicorp/go-retryablehttp v0.6.8/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= @@ -154,10 +152,6 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/shurcooL/githubv4 v0.0.0-20220115235240-a14260e6f8a2 h1:82EIpiGB79OIPgSGa63Oj4Ipf+YAX1c6A9qjmEYoRXc= -github.com/shurcooL/githubv4 v0.0.0-20220115235240-a14260e6f8a2/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= -github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a h1:KikTa6HtAK8cS1qjvUvvq4QO21QnwC+EfvB+OAuZ/ZU= -github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= @@ -167,10 +161,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/suzuki-shunsuke/github-comment-metadata v0.1.0 h1:89uGvBINoWZ4p2dGj7S695bm/L1H175eMp/D47ltSPs= -github.com/suzuki-shunsuke/github-comment-metadata v0.1.0/go.mod h1:GNDhEmWAJ6Bbk9rIds0mAMF4noyPV3EqwqLetnEoNLg= -github.com/suzuki-shunsuke/go-ci-env v1.1.0 h1:eGpItM2bEDtHFXYYEm8zz+kDGxWlhOtwNK58cREa1QU= -github.com/suzuki-shunsuke/go-ci-env v1.1.0/go.mod h1:kO9UgcQIAH4Pu4ESkUJHPuQEtesxHPKkpQqeJHJzzdk= github.com/suzuki-shunsuke/go-findconfig v1.1.0 h1:kWZHF93LPZPLrvA/GToPZPKEFzgky3tNTrYEfMpr7f8= github.com/suzuki-shunsuke/go-findconfig v1.1.0/go.mod h1:u/0Zz6/GDE6G0gofzVhR9UPOIKLSUoDMjUoFWqOoVlg= github.com/urfave/cli/v2 v2.4.0 h1:m2pxjjDFgDxSPtO8WSdbndj17Wu2y8vOT86wE/tjr+I= @@ -185,6 +175,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= +go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -223,6 +215,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -249,9 +243,9 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -293,13 +287,12 @@ golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -351,6 +344,8 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE= +golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/notifier/gitlab/comment_test.go b/pkg/notifier/gitlab/comment_test.go index 8a92c2d..c8ce98a 100644 --- a/pkg/notifier/gitlab/comment_test.go +++ b/pkg/notifier/gitlab/comment_test.go @@ -4,20 +4,31 @@ import ( "reflect" "testing" + gitlabmock "github.com/hirosassa/tfcmt-gitlab/pkg/notifier/gitlab/gen" gitlab "github.com/xanzy/go-gitlab" + "go.uber.org/mock/gomock" ) func TestCommentPost(t *testing.T) { t.Parallel() + body := "body" testCases := []struct { - config Config - body string - opt PostOptions - ok bool + name string + config Config + createMockGitLabAPI func(ctrl *gomock.Controller) *gitlabmock.MockAPI + body string + opt PostOptions + ok bool }{ { + name: "should post", config: newFakeConfig(), - body: "", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(body)}).Return(&gitlab.Note{}, nil, nil) + return api + }, + body: body, opt: PostOptions{ Number: 1, Revision: "abcd", @@ -25,8 +36,16 @@ func TestCommentPost(t *testing.T) { ok: true, }, { + name: "should get mriid when PostOptions.Number is 0 and has PostOptions.Revision", config: newFakeConfig(), - body: "", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + mriid := 1 + api.EXPECT().ListMergeRequestsByCommit("abcd").Return([]*gitlab.MergeRequest{{IID: mriid}}, nil, nil) + api.EXPECT().CreateMergeRequestNote(mriid, &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(body)}).Return(&gitlab.Note{}, nil, nil) + return api + }, + body: body, opt: PostOptions{ Number: 0, Revision: "abcd", @@ -34,8 +53,14 @@ func TestCommentPost(t *testing.T) { ok: true, }, { + name: "should post number 2", config: newFakeConfig(), - body: "", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(2, &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(body)}).Return(&gitlab.Note{}, nil, nil) + return api + }, + body: body, opt: PostOptions{ Number: 2, Revision: "", @@ -43,8 +68,13 @@ func TestCommentPost(t *testing.T) { ok: true, }, { + name: "should error PostOptions number=0 and Revision is empty", config: newFakeConfig(), - body: "", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + return api + }, + body: "", opt: PostOptions{ Number: 0, Revision: "", @@ -54,16 +84,23 @@ func TestCommentPost(t *testing.T) { } for _, testCase := range testCases { - client, err := NewClient(testCase.config) - if err != nil { - t.Fatal(err) - } - api := newFakeAPI() - client.API = &api - err = client.Comment.Post(testCase.body, testCase.opt) - if (err == nil) != testCase.ok { - t.Errorf("got error %q", err) - } + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + client, err := NewClient(testCase.config) + if err != nil { + t.Fatal(err) + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + client.API = testCase.createMockGitLabAPI(mockCtrl) + + err = client.Comment.Post(testCase.body, testCase.opt) + if (err == nil) != testCase.ok { + t.Errorf("got error %q", err) + } + }) } } @@ -80,44 +117,48 @@ func TestCommentList(t *testing.T) { }, } testCases := []struct { - config Config - number int - ok bool - comments []*gitlab.Note + name string + config Config + createMockGitLabAPI func(ctrl *gomock.Controller) *gitlabmock.MockAPI + number int + ok bool + comments []*gitlab.Note }{ { - config: newFakeConfig(), + name: "should list comments", + config: newFakeConfig(), + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().ListMergeRequestNotes(1, gomock.Any()).Return(comments, &gitlab.Response{TotalPages: 1, NextPage: 1}, nil) + return api + }, number: 1, ok: true, comments: comments, }, - { - config: newFakeConfig(), - number: 12, - ok: true, - comments: comments, - }, - { - config: newFakeConfig(), - number: 123, - ok: true, - comments: comments, - }, } for _, testCase := range testCases { - client, err := NewClient(testCase.config) - if err != nil { - t.Fatal(err) - } - api := newFakeAPI() - client.API = &api - comments, err := client.Comment.List(testCase.number) - if (err == nil) != testCase.ok { - t.Errorf("got error %q", err) - } - if !reflect.DeepEqual(comments, testCase.comments) { - t.Errorf("got %v but want %v", comments, testCase.comments) - } + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + client, err := NewClient(testCase.config) + if err != nil { + t.Fatal(err) + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + client.API = testCase.createMockGitLabAPI(mockCtrl) + + comments, err := client.Comment.List(testCase.number) + if (err == nil) != testCase.ok { + t.Errorf("got error %q", err) + } + if !reflect.DeepEqual(comments, testCase.comments) { + t.Errorf("got %v but want %v", comments, testCase.comments) + } + }) } } diff --git a/pkg/notifier/gitlab/commits_test.go b/pkg/notifier/gitlab/commits_test.go index 6473986..3bf23e4 100644 --- a/pkg/notifier/gitlab/commits_test.go +++ b/pkg/notifier/gitlab/commits_test.go @@ -2,36 +2,64 @@ package gitlab import ( "testing" + + gitlabmock "github.com/hirosassa/tfcmt-gitlab/pkg/notifier/gitlab/gen" + gitlab "github.com/xanzy/go-gitlab" + "go.uber.org/mock/gomock" ) func TestCommitsList(t *testing.T) { t.Parallel() testCases := []struct { - revision string - ok bool + name string + createMockGitLabAPI func(ctrl *gomock.Controller) *gitlabmock.MockAPI + revision string + ok bool }{ { + name: "should list commits", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().ListCommits(gomock.Cond(func(x any) bool { + _, ok := x.(*gitlab.ListCommitsOptions) + return ok + })).Return([]*gitlab.Commit{}, nil, nil) + return api + }, revision: "04e0917e448b662c2b16330fad50e97af16ff27a", ok: true, }, { + name: "should return error when revision is empty", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + return api + }, revision: "", ok: false, }, } for _, testCase := range testCases { - cfg := newFakeConfig() - client, err := NewClient(cfg) - if err != nil { - t.Fatal(err) - } - api := newFakeAPI() - client.API = &api - _, err = client.Commits.List(testCase.revision) - if (err == nil) != testCase.ok { - t.Errorf("got error %q", err) - } + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + cfg := newFakeConfig() + client, err := NewClient(cfg) + if err != nil { + t.Fatal(err) + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + client.API = testCase.createMockGitLabAPI(mockCtrl) + + _, err = client.Commits.List(testCase.revision) + if (err == nil) != testCase.ok { + t.Errorf("got error %q", err) + } + }) } } @@ -80,8 +108,6 @@ func TestCommitsLastOne(t *testing.T) { if err != nil { t.Fatal(err) } - api := newFakeAPI() - client.API = &api commit, err := client.Commits.lastOne(testCase.commits, testCase.revision) if (err == nil) != testCase.ok { t.Errorf("got error %q", err) diff --git a/pkg/notifier/gitlab/gen/gitlab.go b/pkg/notifier/gitlab/gen/gitlab.go new file mode 100644 index 0000000..7f44257 --- /dev/null +++ b/pkg/notifier/gitlab/gen/gitlab.go @@ -0,0 +1,321 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./pkg/notifier/gitlab/gitlab.go +// +// Generated by this command: +// +// mockgen -source=./pkg/notifier/gitlab/gitlab.go -destination=./pkg/notifier/gitlab/gen/gitlab.go -package gitlabmock +// + +// Package gitlabmock is a generated GoMock package. +package gitlabmock + +import ( + reflect "reflect" + + gitlab "github.com/xanzy/go-gitlab" + gomock "go.uber.org/mock/gomock" +) + +// MockAPI is a mock of API interface. +type MockAPI struct { + ctrl *gomock.Controller + recorder *MockAPIMockRecorder +} + +// MockAPIMockRecorder is the mock recorder for MockAPI. +type MockAPIMockRecorder struct { + mock *MockAPI +} + +// NewMockAPI creates a new mock instance. +func NewMockAPI(ctrl *gomock.Controller) *MockAPI { + mock := &MockAPI{ctrl: ctrl} + mock.recorder = &MockAPIMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAPI) EXPECT() *MockAPIMockRecorder { + return m.recorder +} + +// AddMergeRequestLabels mocks base method. +func (m *MockAPI) AddMergeRequestLabels(labels *[]string, mergeRequest int) (gitlab.Labels, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddMergeRequestLabels", labels, mergeRequest) + ret0, _ := ret[0].(gitlab.Labels) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// AddMergeRequestLabels indicates an expected call of AddMergeRequestLabels. +func (mr *MockAPIMockRecorder) AddMergeRequestLabels(labels, mergeRequest any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddMergeRequestLabels", reflect.TypeOf((*MockAPI)(nil).AddMergeRequestLabels), labels, mergeRequest) +} + +// CreateMergeRequestNote mocks base method. +func (m *MockAPI) CreateMergeRequestNote(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "CreateMergeRequestNote", varargs...) + ret0, _ := ret[0].(*gitlab.Note) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// CreateMergeRequestNote indicates an expected call of CreateMergeRequestNote. +func (mr *MockAPIMockRecorder) CreateMergeRequestNote(mergeRequest, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateMergeRequestNote", reflect.TypeOf((*MockAPI)(nil).CreateMergeRequestNote), varargs...) +} + +// GetCommit mocks base method. +func (m *MockAPI) GetCommit(sha string, options ...gitlab.RequestOptionFunc) (*gitlab.Commit, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{sha} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetCommit", varargs...) + ret0, _ := ret[0].(*gitlab.Commit) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetCommit indicates an expected call of GetCommit. +func (mr *MockAPIMockRecorder) GetCommit(sha any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{sha}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCommit", reflect.TypeOf((*MockAPI)(nil).GetCommit), varargs...) +} + +// GetLabel mocks base method. +func (m *MockAPI) GetLabel(labelName string, options ...gitlab.RequestOptionFunc) (*gitlab.Label, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{labelName} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetLabel", varargs...) + ret0, _ := ret[0].(*gitlab.Label) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetLabel indicates an expected call of GetLabel. +func (mr *MockAPIMockRecorder) GetLabel(labelName any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{labelName}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLabel", reflect.TypeOf((*MockAPI)(nil).GetLabel), varargs...) +} + +// GetMergeRequest mocks base method. +func (m *MockAPI) GetMergeRequest(mergeRequest int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetMergeRequest", varargs...) + ret0, _ := ret[0].(*gitlab.MergeRequest) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetMergeRequest indicates an expected call of GetMergeRequest. +func (mr *MockAPIMockRecorder) GetMergeRequest(mergeRequest, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMergeRequest", reflect.TypeOf((*MockAPI)(nil).GetMergeRequest), varargs...) +} + +// ListCommits mocks base method. +func (m *MockAPI) ListCommits(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListCommits", varargs...) + ret0, _ := ret[0].([]*gitlab.Commit) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ListCommits indicates an expected call of ListCommits. +func (mr *MockAPIMockRecorder) ListCommits(opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListCommits", reflect.TypeOf((*MockAPI)(nil).ListCommits), varargs...) +} + +// ListMergeRequestLabels mocks base method. +func (m *MockAPI) ListMergeRequestLabels(mergeRequest int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (gitlab.Labels, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListMergeRequestLabels", varargs...) + ret0, _ := ret[0].(gitlab.Labels) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListMergeRequestLabels indicates an expected call of ListMergeRequestLabels. +func (mr *MockAPIMockRecorder) ListMergeRequestLabels(mergeRequest, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMergeRequestLabels", reflect.TypeOf((*MockAPI)(nil).ListMergeRequestLabels), varargs...) +} + +// ListMergeRequestNotes mocks base method. +func (m *MockAPI) ListMergeRequestNotes(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListMergeRequestNotes", varargs...) + ret0, _ := ret[0].([]*gitlab.Note) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ListMergeRequestNotes indicates an expected call of ListMergeRequestNotes. +func (mr *MockAPIMockRecorder) ListMergeRequestNotes(mergeRequest, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMergeRequestNotes", reflect.TypeOf((*MockAPI)(nil).ListMergeRequestNotes), varargs...) +} + +// ListMergeRequestsByCommit mocks base method. +func (m *MockAPI) ListMergeRequestsByCommit(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{sha} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListMergeRequestsByCommit", varargs...) + ret0, _ := ret[0].([]*gitlab.MergeRequest) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ListMergeRequestsByCommit indicates an expected call of ListMergeRequestsByCommit. +func (mr *MockAPIMockRecorder) ListMergeRequestsByCommit(sha any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{sha}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMergeRequestsByCommit", reflect.TypeOf((*MockAPI)(nil).ListMergeRequestsByCommit), varargs...) +} + +// PostCommitComment mocks base method. +func (m *MockAPI) PostCommitComment(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{sha, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "PostCommitComment", varargs...) + ret0, _ := ret[0].(*gitlab.CommitComment) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// PostCommitComment indicates an expected call of PostCommitComment. +func (mr *MockAPIMockRecorder) PostCommitComment(sha, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{sha, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PostCommitComment", reflect.TypeOf((*MockAPI)(nil).PostCommitComment), varargs...) +} + +// RemoveMergeRequestLabels mocks base method. +func (m *MockAPI) RemoveMergeRequestLabels(labels *[]string, mergeRequest int) (gitlab.Labels, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemoveMergeRequestLabels", labels, mergeRequest) + ret0, _ := ret[0].(gitlab.Labels) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RemoveMergeRequestLabels indicates an expected call of RemoveMergeRequestLabels. +func (mr *MockAPIMockRecorder) RemoveMergeRequestLabels(labels, mergeRequest any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveMergeRequestLabels", reflect.TypeOf((*MockAPI)(nil).RemoveMergeRequestLabels), labels, mergeRequest) +} + +// UpdateLabel mocks base method. +func (m *MockAPI) UpdateLabel(opt *gitlab.UpdateLabelOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Label, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "UpdateLabel", varargs...) + ret0, _ := ret[0].(*gitlab.Label) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// UpdateLabel indicates an expected call of UpdateLabel. +func (mr *MockAPIMockRecorder) UpdateLabel(opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateLabel", reflect.TypeOf((*MockAPI)(nil).UpdateLabel), varargs...) +} + +// UpdateMergeRequest mocks base method. +func (m *MockAPI) UpdateMergeRequest(mergeRequest int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "UpdateMergeRequest", varargs...) + ret0, _ := ret[0].(*gitlab.MergeRequest) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// UpdateMergeRequest indicates an expected call of UpdateMergeRequest. +func (mr *MockAPIMockRecorder) UpdateMergeRequest(mergeRequest, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateMergeRequest", reflect.TypeOf((*MockAPI)(nil).UpdateMergeRequest), varargs...) +} + +// UpdateMergeRequestNote mocks base method. +func (m *MockAPI) UpdateMergeRequestNote(mergeRequest, note int, opt *gitlab.UpdateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) { + m.ctrl.T.Helper() + varargs := []any{mergeRequest, note, opt} + for _, a := range options { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "UpdateMergeRequestNote", varargs...) + ret0, _ := ret[0].(*gitlab.Note) + ret1, _ := ret[1].(*gitlab.Response) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// UpdateMergeRequestNote indicates an expected call of UpdateMergeRequestNote. +func (mr *MockAPIMockRecorder) UpdateMergeRequestNote(mergeRequest, note, opt any, options ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{mergeRequest, note, opt}, options...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateMergeRequestNote", reflect.TypeOf((*MockAPI)(nil).UpdateMergeRequestNote), varargs...) +} diff --git a/pkg/notifier/gitlab/gitlab_test.go b/pkg/notifier/gitlab/gitlab_test.go index 239ec99..b9a922f 100644 --- a/pkg/notifier/gitlab/gitlab_test.go +++ b/pkg/notifier/gitlab/gitlab_test.go @@ -2,90 +2,8 @@ package gitlab import ( "github.com/hirosassa/tfcmt-gitlab/pkg/terraform" - gitlab "github.com/xanzy/go-gitlab" ) -type fakeAPI struct { - API - FakeCreateMergeRequestNote func(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) - FakeListMergeRequestNotes func(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error) - FakePostCommitComment func(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error) - FakeListCommits func(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error) - FakeListMergeRequestsByCommit func(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) -} - -func (g *fakeAPI) CreateMergeRequestNote(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) { - return g.FakeCreateMergeRequestNote(mergeRequest, opt, options...) -} - -func (g *fakeAPI) ListMergeRequestNotes(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error) { - return g.FakeListMergeRequestNotes(mergeRequest, opt, options...) -} - -func (g *fakeAPI) PostCommitComment(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error) { - return g.FakePostCommitComment(sha, opt, options...) -} - -func (g *fakeAPI) ListCommits(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error) { - return g.FakeListCommits(opt, options...) -} - -func (g *fakeAPI) ListMergeRequestsByCommit(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) { - return g.FakeListMergeRequestsByCommit(sha, options...) -} - -func newFakeAPI() fakeAPI { - return fakeAPI{ - FakeCreateMergeRequestNote: func(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) { - return &gitlab.Note{ - ID: 371748792, - Body: "comment 1", - }, nil, nil - }, - FakeListMergeRequestNotes: func(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error) { - var comments []*gitlab.Note - comments = []*gitlab.Note{ - { - ID: 371748792, - Body: "comment 1", - }, - { - ID: 371765743, - Body: "comment 2", - }, - } - return comments, nil, nil - }, - FakePostCommitComment: func(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error) { - return &gitlab.CommitComment{ - Note: "comment 1", - }, nil, nil - }, - FakeListCommits: func(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error) { - var commits []*gitlab.Commit - commits = []*gitlab.Commit{ - { - ID: "04e0917e448b662c2b16330fad50e97af16ff27a", - }, - { - ID: "04e0917e448b662c2b16330fad50e97af16ff27b", - }, - { - ID: "04e0917e448b662c2b16330fad50e97af16ff27c", - }, - } - return commits, nil, nil - }, - FakeListMergeRequestsByCommit: func(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) { - return []*gitlab.MergeRequest{ - { - IID: 1, - }, - }, nil, nil - }, - } -} - func newFakeConfig() Config { return Config{ Token: "token", diff --git a/pkg/notifier/gitlab/notify_test.go b/pkg/notifier/gitlab/notify_test.go index 2e9a8db..dfb1f90 100644 --- a/pkg/notifier/gitlab/notify_test.go +++ b/pkg/notifier/gitlab/notify_test.go @@ -4,20 +4,29 @@ import ( "testing" "github.com/hirosassa/tfcmt-gitlab/pkg/notifier" + gitlabmock "github.com/hirosassa/tfcmt-gitlab/pkg/notifier/gitlab/gen" "github.com/hirosassa/tfcmt-gitlab/pkg/terraform" + gitlab "github.com/xanzy/go-gitlab" + "go.uber.org/mock/gomock" ) -func TestNotifyNotify(t *testing.T) { +func TestNotifyNotify(t *testing.T) { //nolint:maintidx t.Parallel() testCases := []struct { - name string - config Config - ok bool - exitCode int - paramExec notifier.ParamExec + name string + createMockGitLabAPI func(ctrl *gomock.Controller) *gitlabmock.MockAPI + config Config + ok bool + exitCode int + paramExec notifier.ParamExec }{ { name: "invalid body (cannot parse)", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -40,6 +49,10 @@ func TestNotifyNotify(t *testing.T) { }, { name: "invalid mr", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -62,6 +75,11 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid, error", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -84,6 +102,11 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid, and isMR", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -106,6 +129,12 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid, and isRevision", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().ListMergeRequestsByCommit("revision-revision").Return([]*gitlab.MergeRequest{{IID: 1}}, nil, nil) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -128,6 +157,11 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid, and contains destroy", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -149,6 +183,11 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid with no change", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -170,6 +209,11 @@ func TestNotifyNotify(t *testing.T) { }, { name: "valid, contains destroy, but not to notify", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -191,6 +235,13 @@ func TestNotifyNotify(t *testing.T) { }, { name: "apply case", + createMockGitLabAPI: func(ctrl *gomock.Controller) *gitlabmock.MockAPI { + api := gitlabmock.NewMockAPI(ctrl) + api.EXPECT().ListCommits(gomock.Any()).Return([]*gitlab.Commit{{ID: "1"}, {ID: "2"}}, nil, nil) + api.EXPECT().ListMergeRequestsByCommit("2").Return([]*gitlab.MergeRequest{{IID: 1}}, nil, nil) + api.EXPECT().CreateMergeRequestNote(1, gomock.Any()).Return(nil, nil, nil) + return api + }, config: Config{ Token: "token", NameSpace: "namespace", @@ -214,18 +265,25 @@ func TestNotifyNotify(t *testing.T) { } for _, testCase := range testCases { - client, err := NewClient(testCase.config) - if err != nil { - t.Fatal(err) - } - api := newFakeAPI() - client.API = &api - exitCode, err := client.Notify.Notify(testCase.paramExec) - if (err == nil) != testCase.ok { - t.Errorf("test case: %s, got error %q", testCase.name, err) - } - if exitCode != testCase.exitCode { - t.Errorf("test case: %s, got %q but want %q", testCase.name, exitCode, testCase.exitCode) - } + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + client, err := NewClient(testCase.config) + if err != nil { + t.Fatal(err) + } + + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + client.API = testCase.createMockGitLabAPI(mockCtrl) + + exitCode, err := client.Notify.Notify(testCase.paramExec) + if (err == nil) != testCase.ok { + t.Errorf("test case: %s, got error %q", testCase.name, err) + } + if exitCode != testCase.exitCode { + t.Errorf("test case: %s, got %q but want %q", testCase.name, exitCode, testCase.exitCode) + } + }) } } From 59c4995e3579019be7b7362bd3223c7e60630bd9 Mon Sep 17 00:00:00 2001 From: Ryo Kitagawa Date: Wed, 10 Apr 2024 19:50:04 +0900 Subject: [PATCH 2/2] fix: update aqua-installer to v3.0.0 --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 88f9425..4f78cea 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,9 +20,9 @@ jobs: with: go-version-file: "go.mod" - - uses: aquaproj/aqua-installer@v2.1.2 + - uses: aquaproj/aqua-installer@v3.0.0 with: - aqua_version: v2.10.1 + aqua_version: v2.27.0 - uses: suzuki-shunsuke/github-action-golangci-lint@v0.1.4 - name: Test