Repository navigation
Expand file tree
/
Copy pathlogs_test.go
More file actions
138 lines (113 loc) · 3.13 KB
/
Copy pathlogs_test.go
File metadata and controls
138 lines (113 loc) · 3.13 KB
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
package fswatcher
import (
"bytes"
"context"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHandler is a simple slog.Handler that writes to a buffer
type mockHandler struct {
buf *bytes.Buffer
}
func (h *mockHandler) Enabled(ctx context.Context, level slog.Level) bool {
return true
}
func (h *mockHandler) Handle(ctx context.Context, r slog.Record) error {
h.buf.WriteString(r.Message)
r.Attrs(func(a slog.Attr) bool {
h.buf.WriteString(" ")
h.buf.WriteString(a.Key)
h.buf.WriteString("=")
h.buf.WriteString(a.Value.String())
return true
})
h.buf.WriteString("\n")
return nil
}
func (h *mockHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return h
}
func (h *mockHandler) WithGroup(name string) slog.Handler {
return h
}
func TestLogger_Output(t *testing.T) {
buf := &bytes.Buffer{}
handler := &mockHandler{buf: buf}
logger := slog.New(handler)
w := &watcher{
logger: logger,
severity: SeverityDebug,
}
t.Run("logInfo", func(t *testing.T) {
buf.Reset()
w.logInfo("info message", "key", "value")
assert.Contains(t, buf.String(), "info message key=value")
})
t.Run("logError", func(t *testing.T) {
buf.Reset()
w.logError("error message", "err", "some error")
assert.Contains(t, buf.String(), "error message err=some error")
})
t.Run("logWarn", func(t *testing.T) {
buf.Reset()
w.logWarn("warn message", "retry", 1)
assert.Contains(t, buf.String(), "warn message retry=1")
})
t.Run("logDebug", func(t *testing.T) {
buf.Reset()
w.logDebug("debug message", "detail", true)
assert.Contains(t, buf.String(), "debug message detail=true")
})
}
func TestLogger_NilLogger(t *testing.T) {
w := &watcher{logger: nil}
// None of these should panic when logger is nil
w.logDebug("debug")
w.logInfo("info")
w.logWarn("warn")
w.logError("error")
w.log(SeverityInfo, "generic")
}
func TestLogger_GenericLog(t *testing.T) {
buf := &bytes.Buffer{}
handler := &mockHandler{buf: buf}
logger := slog.New(handler)
w := &watcher{
logger: logger,
severity: SeverityDebug,
}
w.log(SeverityWarn, "generic warn", "code", 42)
assert.Contains(t, buf.String(), "generic warn code=42")
}
func TestLogger_LevelFiltering(t *testing.T) {
// Use a real TextHandler to test actual level filtering
buf := &bytes.Buffer{}
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
}
handler := slog.NewTextHandler(buf, opts)
logger := slog.New(handler)
w := &watcher{
logger: logger,
severity: SeverityInfo,
}
w.logInfo("should be logged")
w.logDebug("should be ignored")
assert.Contains(t, buf.String(), "level=INFO msg=\"should be logged\"")
assert.NotContains(t, buf.String(), "should be ignored")
}
func TestLogger_InitDisabled(t *testing.T) {
t.Run("empty log path disables logger", func(t *testing.T) {
w := &watcher{logPath: "", severity: SeverityWarn}
assert.NoError(t, w.initLogger())
assert.Nil(t, w.logger)
assert.Nil(t, w.logFile)
})
t.Run("severity none disables logger even with stdout", func(t *testing.T) {
w := &watcher{logPath: "stdout", severity: SeverityNone}
assert.NoError(t, w.initLogger())
assert.Nil(t, w.logger)
assert.Nil(t, w.logFile)
})
}