From 694ca30e0da9ce29c42fb10df25138976ef60d4c Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 29 Sep 2021 14:30:17 +0200 Subject: [PATCH] fix zap.Logger in webtest --- web/main.go | 5 +++++ web/main_test.go | 7 ++++--- web/webtest/main.go | 5 ++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/web/main.go b/web/main.go index e9c887d..af03743 100644 --- a/web/main.go +++ b/web/main.go @@ -45,6 +45,11 @@ type Service struct { modules []ModuleRegisterFunc } +// SetLog - set new logger +func (s *Service) SetLog(l *zap.Logger) { + s.log = l +} + // Log - get current logger func (s *Service) Log() *zap.Logger { return s.log diff --git a/web/main_test.go b/web/main_test.go index c043195..7a8dffc 100644 --- a/web/main_test.go +++ b/web/main_test.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" + "go.uber.org/zap" ) var ( @@ -17,13 +18,13 @@ func TestRun(t *testing.T) { s := &Service{AccessLog: true, Listen: "8.8.8.8:80"} s.ModuleRegister(func(_ *gin.Engine, _ *Service) {}) // HTTP - failed - err := s.Run() + err := s.Run(zap.L()) assert.Error(err) s.ACME.Enable = true // acme with listen port - panic assert.Panics(func() { - s.Run() + s.Run(zap.L()) }) if TestRunTLS == "false" { @@ -31,6 +32,6 @@ func TestRun(t *testing.T) { } s.Listen = "" // httpS - failed - err = s.Run() + err = s.Run(zap.L()) assert.Error(err) } diff --git a/web/webtest/main.go b/web/webtest/main.go index 11afa64..d529aac 100644 --- a/web/webtest/main.go +++ b/web/webtest/main.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "go.uber.org/zap" "dev.sum7.eu/genofire/golang-lib/database" "dev.sum7.eu/genofire/golang-lib/mailer" @@ -63,8 +64,10 @@ func NewWithDBSetup(modules web.ModuleRegisterFunc, dbCall func(db *database.Dat // New allows to configure WebService for testing func (option Option) New() (*TestServer, error) { + log := zap.L() ws := &web.Service{} + ws.SetLog(log) ws.Session.Name = "mysession" ws.Session.Secret = "hidden" @@ -100,7 +103,7 @@ func (option Option) New() (*TestServer, error) { } if option.Mailer { - mock, mail := mailer.NewFakeServer() + mock, mail := mailer.NewFakeServer(log) if err := mail.Setup(); err != nil { return nil, err }