Skip to content

Commit

Permalink
fix(tests): properly silence log output
Browse files Browse the repository at this point in the history
Using `init` allows it to also work for benchmarks.
And `log.Silence` was sometimes getting overridden by `log.init`.
  • Loading branch information
ThinkChaos committed Nov 18, 2023
1 parent 94663ee commit a47e8d1
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 16 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ issues:
linters:
- dupl
- funlen
- gochecknoinits
- gochecknoglobals
- gosec
5 changes: 4 additions & 1 deletion api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)

func TestResolver(t *testing.T) {
func init() {
log.Silence()
}

func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "API Suite")
}
5 changes: 4 additions & 1 deletion cache/expirationcache/expiration_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
func init() {
log.Silence()
}

func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Expiration cache suite")
}
5 changes: 4 additions & 1 deletion cache/stringcache/string_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
func init() {
log.Silence()
}

func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "String cache suite")
}
5 changes: 4 additions & 1 deletion cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestCmd(t *testing.T) {
func init() {
log.Silence()
}

func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Command Suite")
}
5 changes: 4 additions & 1 deletion config/config_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ var (
hook *log.MockLoggerHook
)

func TestConfig(t *testing.T) {
func init() {
log.Silence()
}

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
Expand Down
5 changes: 4 additions & 1 deletion e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import (
"github.com/testcontainers/testcontainers-go"
)

func TestLists(t *testing.T) {
func init() {
log.Silence()
}

func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "e2e Suite", Label("e2e"))
}
Expand Down
5 changes: 4 additions & 1 deletion lists/list_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestLists(t *testing.T) {
func init() {
log.Silence()
}

func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Lists Suite")
}
5 changes: 4 additions & 1 deletion lists/parsers/parsers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestLists(t *testing.T) {
func init() {
log.Silence()
}

func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Parsers Suite")
}
25 changes: 23 additions & 2 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package log
import (
"fmt"
"io"
"math"
"strings"
"sync"
"sync/atomic"

"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
Expand All @@ -19,7 +21,10 @@ const prefixField = "prefix"
// Logger is the global logging instance
//
//nolint:gochecknoglobals
var logger *logrus.Logger
var (
logger *logrus.Logger
initDone atomic.Bool
)

// FormatType format for logging ENUM(
// text // logging as text
Expand Down Expand Up @@ -47,6 +52,10 @@ type Config struct {

//nolint:gochecknoinits
func init() {
if !initDone.CompareAndSwap(false, true) {
return
}

logger = logrus.New()

defaultConfig := &Config{
Expand Down Expand Up @@ -122,7 +131,19 @@ func ConfigureLogger(cfg *Config) {

// Silence disables the logger output
func Silence() {
logger.Out = io.Discard
initDone.Store(true)

logger.SetFormatter(discardFormatter{}) // skip expensive formatting

// not actually needed but doesn't hurt
logger.SetOutput(io.Discard)
logger.SetLevel(math.MaxUint32)
}

type discardFormatter struct{}

func (f discardFormatter) Format(*logrus.Entry) ([]byte, error) {
return nil, nil
}

func WithIndent(log *logrus.Entry, prefix string, callback func(*logrus.Entry)) {
Expand Down
5 changes: 4 additions & 1 deletion querylog/querylog_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestResolver(t *testing.T) {
func init() {
log.Silence()
}

func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Querylog Suite")
}
5 changes: 4 additions & 1 deletion redis/redis_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
. "github.com/onsi/gomega"
)

func TestRedisClient(t *testing.T) {
func init() {
log.Silence()
redis.SetLogger(NoLogs{})
}

func TestRedisClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Redis Suite")
}
Expand Down
5 changes: 4 additions & 1 deletion resolver/resolver_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import (
. "github.com/onsi/gomega"
)

func TestResolver(t *testing.T) {
func init() {
log.Silence()
redis.SetLogger(NoLogs{})
}

func TestResolver(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Resolver Suite")
}
Expand Down
5 changes: 4 additions & 1 deletion server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)

func TestDNSServer(t *testing.T) {
func init() {
log.Silence()
}

func TestDNSServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Server Suite")
}
5 changes: 4 additions & 1 deletion trie/trie_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
. "github.com/onsi/gomega"
)

func TestTrie(t *testing.T) {
func init() {
log.Silence()
}

func TestTrie(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Trie Suite")
}
5 changes: 4 additions & 1 deletion util/util_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
. "github.com/onsi/gomega"
)

func TestLists(t *testing.T) {
func init() {
log.Silence()
}

func TestLists(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Util Suite")
}

0 comments on commit a47e8d1

Please sign in to comment.