Debian Codegraph @

zapcore/increase_level_test.go

raw symbols SCIP
1 // Copyright (c) 2020 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package zapcore_testscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore_test`/
22
23 import (
24 "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/"
25 "testingscip-go gomod github.com/golang/go/src go1.19 testing/"
26
27 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/"
28 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/"
29 "go.uber.org/zapscip-go gomod go.uber.org/zap . `go.uber.org/zap`/"
30
31 //revive:disable:dot-imports
32 . "go.uber.org/zap/zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/"
33 "go.uber.org/zap/zaptest/observerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/"
34 )
35
36 func TestIncreaseLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore_test`/TestIncreaseLevel().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
37 tests := []struct {
38 coreLevel Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#
39 increaseLevel Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Level#
40 wantErr bool
41 with []Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Field#
42 }{
43 {
44 coreLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
45 increaseLevel: DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel.,
46 wantErr: true,
47 },
48 {
49 coreLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
50 increaseLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
51 },
52 {
53 coreLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
54 increaseLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
55 },
56 {
57 coreLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
58 increaseLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
59 with: []Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Field#{zapscip-go gomod go.uber.org/zap . `go.uber.org/zap`/.Stringscip-go gomod go.uber.org/zap . `go.uber.org/zap`/String().("k", "v")},
60 },
61 {
62 coreLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
63 increaseLevel: DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel.,
64 wantErr: true,
65 },
66 {
67 coreLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
68 increaseLevel: InfoLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/InfoLevel.,
69 wantErr: true,
70 },
71 {
72 coreLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
73 increaseLevel: WarnLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WarnLevel.,
74 wantErr: true,
75 },
76 {
77 coreLevel: ErrorLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/ErrorLevel.,
78 increaseLevel: PanicLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/PanicLevel.,
79 },
80 }
81
82 for _, tt := range tests {
83 msg := fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("increase %v to %v", tt.coreLevel, tt.increaseLevel)
84 t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().(msg, func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
85 logger, logs := observerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/.Newscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/New().(tt.coreLevel)
86
87 // sanity check
88 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/Equal().(t, tt.coreLevel, LevelOfscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelOf().(logger), "Original logger has the wrong level")
89
90 filteredLogger, err := NewIncreaseLevelCorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/NewIncreaseLevelCore().(logger, tt.increaseLevel)
91 if tt.wantErr {
92 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.ErrorContainsscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/ErrorContains().(t, err, "invalid increase level")
93 return
94 }
95
96 if len(tt.with) > 0 {
97 filteredLogger = filteredLogger.Withscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#With.(tt.with)
98 }
99
100 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, err)
101
102 t.Runscip-go gomod github.com/golang/go/src go1.19 testing/T#Run().("LevelOf", func(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
103 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.increaseLevel, LevelOfscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelOf().(filteredLogger), "Filtered logger has the wrong level")
104 })
105
106 for l := DebugLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/DebugLevel.; l <= FatalLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/FatalLevel.; l++ {
107 enabled := filteredLogger.Enabledscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#Enabled.(l)
108 entry := Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#{Levelscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#Level.: l}
109 ce := filteredLogger.Checkscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#Check.(entry, nil)
110 ce.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckedEntry#Write().()
111 entries := logs.TakeAllscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/ObservedLogs#TakeAll().()
112
113 if l >= tt.increaseLevel {
114 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Truescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/True().(t, enabled, "expect %v to be enabled", l)
115 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.NotNilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/NotNil().(t, ce, "expect non-nil Check")
116 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/NotEmpty().(t, entries, "Expect log to be written")
117 } else {
118 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Falsescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/False().(t, enabled, "expect %v to be disabled", l)
119 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Nil().(t, ce, "expect nil Check")
120 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.Emptyscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/Empty().(t, entries, "No logs should have been written")
121 }
122
123 // Write should always log the entry as per the Core interface
124 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, filteredLogger.Writescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#Write.(entry, nil), "Write failed")
125 requirescip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/require`/NoError().(t, filteredLogger.Syncscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#Sync.(), "Sync failed")
126 assertscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.8.1 `github.com/stretchr/testify/assert`/NotEmpty().(t, logs.TakeAllscip-go gomod go.uber.org/zap . `go.uber.org/zap/zaptest/observer`/ObservedLogs#TakeAll().(), "Write should always log")
127 }
128 })
129 }
130 }