-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
142 lines (138 loc) · 4.63 KB
/
log_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
131
132
133
134
135
136
137
138
139
140
141
142
package cmd_toolkit_test
import (
"bytes"
"fmt"
cmdtoolkit "github.com/majohn-r/cmd-toolkit"
"github.com/majohn-r/output"
"io"
"io/fs"
"testing"
)
func TestInitLogging(t *testing.T) {
tests := map[string]struct {
logWriterInitFn func(output.Bus, string) (io.Writer, string)
want bool
wantLogPath string
logsDebug bool
logsError bool
logsFatal bool
logsInfo bool
logsPanic bool
logsTrace bool
logsWarning bool
}{
"no writer available": {
logWriterInitFn: func(o output.Bus, _ string) (io.Writer, string) {
return nil, ""
},
want: false,
wantLogPath: "",
},
"success": {
logWriterInitFn: func(o output.Bus, _ string) (io.Writer, string) {
return &bytes.Buffer{}, ""
},
want: true,
logsDebug: false,
logsError: true,
logsFatal: true,
logsInfo: true,
logsPanic: true,
logsTrace: false,
logsWarning: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
originalLogWriterInitFn := cmdtoolkit.LogWriterInitFn
defer func() {
cmdtoolkit.LogWriterInitFn = originalLogWriterInitFn
}()
cmdtoolkit.LogWriterInitFn = tt.logWriterInitFn
got := cmdtoolkit.InitLogging(nil, "")
if got != tt.want {
t.Errorf("InitLogging() = %v, want %v", got, tt.want)
}
if got {
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Debug); truth != tt.logsDebug {
t.Errorf("InitLogging() will log at debug, got %t, want %t", truth, tt.logsDebug)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Error); truth != tt.logsError {
t.Errorf("InitLogging() will log at error, got %t, want %t", truth, tt.logsError)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Fatal); truth != tt.logsFatal {
t.Errorf("InitLogging() will log at fatal, got %t, want %t", truth, tt.logsFatal)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Info); truth != tt.logsInfo {
t.Errorf("InitLogging() will log at info, got %t, want %t", truth, tt.logsInfo)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Panic); truth != tt.logsPanic {
t.Errorf("InitLogging() will log at panic, got %t, want %t", truth, tt.logsPanic)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Trace); truth != tt.logsTrace {
t.Errorf("InitLogging() will log at trace, got %t, want %t", truth, tt.logsTrace)
}
if truth := cmdtoolkit.ProductionLogger.WillLog(output.Warning); truth != tt.logsWarning {
t.Errorf("InitLogging() will log at warning, got %t, want %t", truth, tt.logsWarning)
}
}
})
}
}
func TestInitLoggingWithLevel(t *testing.T) {
originalLogWriterInitFn := cmdtoolkit.LogWriterInitFn
defer func() {
cmdtoolkit.LogWriterInitFn = originalLogWriterInitFn
}()
cmdtoolkit.LogWriterInitFn = func(_ output.Bus, _ string) (io.Writer, string) {
return &bytes.Buffer{}, "testingLogPath"
}
// only going to vary the logging level - TestInitLogging handles the error
// cases where a writer cannot be obtained. This ensures that we don't
// introduce a programming error when the underlying log implementation
// cannot be initialized with the specified log level. Also, the various
// Test_simpleLogger_[Level] tests verify the expected behavior as to what
// is and is not logged after initialization with each log level.
tests := map[string]struct {
l output.Level
wantOk bool
}{
"panic": {l: output.Panic, wantOk: true},
"fatal": {l: output.Fatal, wantOk: true},
"error": {l: output.Error, wantOk: true},
"warn": {l: output.Warning, wantOk: true},
"info": {l: output.Info, wantOk: true},
"debug": {l: output.Debug, wantOk: true},
"trace": {l: output.Trace, wantOk: true},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if gotOk := cmdtoolkit.InitLoggingWithLevel(nil, tt.l, ""); gotOk != tt.wantOk {
t.Errorf("InitLoggingWithLevel() = %t, want %t", gotOk, tt.wantOk)
}
if got := cmdtoolkit.LogPath(); got != "testingLogPath" {
t.Errorf("LogPath() = %v, want %v", got, "testingLogPath")
}
})
}
}
func TestErrorToString(t *testing.T) {
tests := map[string]struct {
e error
want string
}{
"nil": {e: nil, want: "'nil error'"},
"typical": {e: fmt.Errorf("test error"), want: "'test error'"},
"special": {e: &fs.PathError{
Op: "read",
Path: "bad path",
Err: fmt.Errorf("nil ptr")}, want: "'*fs.PathError: read bad path: nil ptr'"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := cmdtoolkit.ErrorToString(tt.e); got != tt.want {
t.Errorf("ErrorToString() = %v, want %v", got, tt.want)
}
})
}
}