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