Skip to content

Commit d8ad628

Browse files
committed
refactor all getLogger function to a common package
the function were spread out everywhere in the code, use the same one everywhere. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 47d6237 commit d8ad628

File tree

12 files changed

+32
-51
lines changed

12 files changed

+32
-51
lines changed

pkg/adapter/adapter_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@ import (
1414
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
1616
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
17-
"go.uber.org/zap"
18-
zapobserver "go.uber.org/zap/zaptest/observer"
17+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1918
"gotest.tools/v3/assert"
2019
rtesting "knative.dev/pkg/reconciler/testing"
2120
)
2221

23-
func getLogger() *zap.SugaredLogger {
24-
observer, _ := zapobserver.New(zap.InfoLevel)
25-
logger := zap.New(observer).Sugar()
26-
return logger
27-
}
28-
2922
func TestHandleEvent(t *testing.T) {
3023
ctx, _ := rtesting.SetupFakeContext(t)
3124
cs, _ := testclient.SeedTestData(t, ctx, testclient.Data{})
25+
logger, _ := logger.GetLogger()
3226

3327
l := listener{
3428
run: &params.Run{
@@ -43,7 +37,7 @@ func TestHandleEvent(t *testing.T) {
4337
},
4438
},
4539
},
46-
logger: getLogger(),
40+
logger: logger,
4741
}
4842

4943
ts := httptest.NewServer(l.handleEvent())
@@ -128,8 +122,9 @@ func TestHandleEvent(t *testing.T) {
128122
}
129123

130124
func TestWhichProvider(t *testing.T) {
125+
logger, _ := logger.GetLogger()
131126
l := listener{
132-
logger: getLogger(),
127+
logger: logger,
133128
}
134129
tests := []struct {
135130
name string

pkg/provider/bitbucketcloud/bitbucket_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
1111
bbcloudtest "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/test"
1212
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types"
13-
"go.uber.org/zap"
14-
zapobserver "go.uber.org/zap/zaptest/observer"
1513
"gotest.tools/v3/assert"
1614
rtesting "knative.dev/pkg/reconciler/testing"
1715
)
@@ -285,9 +283,3 @@ func TestCreateStatus(t *testing.T) {
285283
})
286284
}
287285
}
288-
289-
func getLogger() *zap.SugaredLogger {
290-
observer, _ := zapobserver.New(zap.InfoLevel)
291-
logger := zap.New(observer).Sugar()
292-
return logger
293-
}

pkg/provider/bitbucketcloud/detect_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types"
10+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1011
"gotest.tools/v3/assert"
1112
)
1213

@@ -132,7 +133,7 @@ func TestProvider_Detect(t *testing.T) {
132133
for _, tt := range tests {
133134
t.Run(tt.name, func(t *testing.T) {
134135
bprovider := Provider{}
135-
logger := getLogger()
136+
logger, _ := logger.GetLogger()
136137

137138
jeez, err := json.Marshal(tt.event)
138139
if err != nil {

pkg/provider/bitbucketserver/bitbucketserver_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
2121
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
2222
bbtest "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketserver/test"
23-
"go.uber.org/zap"
24-
zapobserver "go.uber.org/zap/zaptest/observer"
2523
"gotest.tools/v3/assert"
2624
rtesting "knative.dev/pkg/reconciler/testing"
2725
)
@@ -338,12 +336,6 @@ func TestGetConfig(t *testing.T) {
338336
assert.Equal(t, config.TaskStatusTMPL, taskStatusTemplate)
339337
}
340338

341-
func getLogger() *zap.SugaredLogger {
342-
observer, _ := zapobserver.New(zap.InfoLevel)
343-
logger := zap.New(observer).Sugar()
344-
return logger
345-
}
346-
347339
func TestValidate(t *testing.T) {
348340
tests := []struct {
349341
name string

pkg/provider/bitbucketserver/detect_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
bbv1 "github.com/gfleury/go-bitbucket-v1"
1010
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketserver/types"
11+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1112
"gotest.tools/v3/assert"
1213
)
1314

@@ -115,7 +116,7 @@ func TestProvider_Detect(t *testing.T) {
115116
for _, tt := range tests {
116117
t.Run(tt.name, func(t *testing.T) {
117118
bprovider := Provider{}
118-
logger := getLogger()
119+
logger, _ := logger.GetLogger()
119120

120121
jeez, err := json.Marshal(tt.event)
121122
if err != nil {

pkg/provider/github/detect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/google/go-github/v48/github"
10+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1011
"gotest.tools/v3/assert"
1112
)
1213

@@ -238,8 +239,7 @@ func TestProvider_Detect(t *testing.T) {
238239
for _, tt := range tests {
239240
t.Run(tt.name, func(t *testing.T) {
240241
gprovider := Provider{}
241-
logger := getLogger()
242-
242+
logger, _ := logger.GetLogger()
243243
jeez, err := json.Marshal(tt.event)
244244
if err != nil {
245245
assert.NilError(t, err)

pkg/provider/github/github_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,12 @@ import (
2121
"github.com/jonboulle/clockwork"
2222
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
2323
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
24-
"go.uber.org/zap"
25-
zapobserver "go.uber.org/zap/zaptest/observer"
24+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
2625
"gotest.tools/v3/assert"
2726
"knative.dev/pkg/ptr"
2827
rtesting "knative.dev/pkg/reconciler/testing"
2928
)
3029

31-
func getLogger() *zap.SugaredLogger {
32-
observer, _ := zapobserver.New(zap.InfoLevel)
33-
logger := zap.New(observer).Sugar()
34-
return logger
35-
}
36-
3730
func TestGithubSplitURL(t *testing.T) {
3831
tests := []struct {
3932
name string
@@ -512,7 +505,7 @@ func TestValidate(t *testing.T) {
512505
}
513506
for _, tt := range tests {
514507
t.Run(tt.name, func(t *testing.T) {
515-
logger := getLogger()
508+
logger, _ := logger.GetLogger()
516509
v := &Provider{Logger: logger}
517510

518511
hm := hmac.New(tt.hashFunc, []byte(tt.secret))

pkg/provider/github/parse_payload_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
1717
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
1818
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
19+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
1920
"gotest.tools/v3/assert"
2021
"gotest.tools/v3/env"
2122
corev1 "k8s.io/api/core/v1"
@@ -313,7 +314,7 @@ func TestParsePayLoad(t *testing.T) {
313314
fmt.Fprint(rw, string(bjeez))
314315
})
315316
}
316-
logger := getLogger()
317+
logger, _ := logger.GetLogger()
317318
gprovider := Provider{
318319
Client: tt.githubClient,
319320
Logger: logger,
@@ -505,7 +506,7 @@ func TestAppTokenGeneration(t *testing.T) {
505506
}
506507

507508
jeez, _ := json.Marshal(samplePRevent)
508-
logger := getLogger()
509+
logger, _ := logger.GetLogger()
509510
gprovider := Provider{
510511
Logger: logger,
511512
Client: fakeghclient,

pkg/provider/github/status_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
1919
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
2020
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
21+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
2122
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2223
"gotest.tools/v3/assert"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -283,7 +284,7 @@ func TestGithubProviderCreateStatus(t *testing.T) {
283284
ctx, _ := rtesting.SetupFakeContext(t)
284285
gcvs := New()
285286
gcvs.Client = fakeclient
286-
gcvs.Logger = getLogger()
287+
gcvs.Logger, _ = logger.GetLogger()
287288
gcvs.Run = params.New()
288289

289290
mux.HandleFunc("/repos/check/run/statuses/sha", func(rw http.ResponseWriter, r *http.Request) {})

pkg/provider/gitlab/detect_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
thelp "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab/test"
9+
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
910
"github.com/xanzy/go-gitlab"
1011
"gotest.tools/v3/assert"
1112
)
@@ -109,7 +110,7 @@ func TestProvider_Detect(t *testing.T) {
109110
for _, tt := range tests {
110111
t.Run(tt.name, func(t *testing.T) {
111112
gprovider := Provider{}
112-
logger := getLogger()
113+
logger, _ := logger.GetLogger()
113114

114115
header := http.Header{}
115116
header.Set("X-Gitlab-Event", string(tt.eventType))

0 commit comments

Comments
 (0)