-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
134 lines (116 loc) · 3.27 KB
/
errors_test.go
File metadata and controls
134 lines (116 loc) · 3.27 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
package goconfig
import (
"bytes"
"errors"
"fmt"
"log/slog"
"strings"
"testing"
)
func TestConfigErrors(t *testing.T) {
t.Run("Basic operations", func(t *testing.T) {
ce := &ConfigErrors{}
if ce.HasErrors() {
t.Error("expected HasErrors to be false")
}
if ce.Len() != 0 {
t.Errorf("expected Len 0, got %d", ce.Len())
}
err1 := errors.New("error 1")
ce.Add("KEY1", err1)
if !ce.HasErrors() {
t.Error("expected HasErrors to be true")
}
if ce.Len() != 1 {
t.Errorf("expected Len 1, got %d", ce.Len())
}
err2 := errors.New("error 2")
ce.Add("KEY2", err2)
if ce.Len() != 2 {
t.Errorf("expected Len 2, got %d", ce.Len())
}
unwrapped := ce.Unwrap()
if len(unwrapped) != 2 {
t.Errorf("expected 2 unwrapped errors, got %d", len(unwrapped))
}
if unwrapped[0] != err1 || unwrapped[1] != err2 {
t.Error("unwrapped errors mismatch")
}
})
t.Run("Error formatting and prefix stripping", func(t *testing.T) {
ce := &ConfigErrors{}
ce.Add("PORT", errors.New("invalid port"))
ce.Add("HOST", fmt.Errorf("invalid value for HOST: empty not allowed"))
got := ce.Error()
// Expect prefix stripping for HOST
// "PORT: invalid port\nHOST: empty not allowed"
expected := "PORT: invalid port\nHOST: empty not allowed"
if got != expected {
t.Errorf("expected:\n%s\ngot:\n%s", expected, got)
}
})
t.Run("Empty ConfigErrors formatting", func(t *testing.T) {
ce := &ConfigErrors{}
if ce.Error() != "" {
t.Errorf("expected empty string for no errors, got %q", ce.Error())
}
})
}
func TestLogging(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove time to make it deterministic
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}}))
t.Run("LogAll", func(t *testing.T) {
buf.Reset()
ce := &ConfigErrors{}
ce.Add("KEY1", errors.New("err1"))
ce.LogAll(logger)
output := buf.String()
if !strings.Contains(output, "level=ERROR") {
t.Error("expected ERROR level")
}
if !strings.Contains(output, "msg=\"configuration error\"") {
t.Error("expected default message")
}
if !strings.Contains(output, "key=KEY1") || !strings.Contains(output, "error=err1") {
t.Error("missing key or error in log")
}
})
t.Run("LogAll with custom message", func(t *testing.T) {
buf.Reset()
ce := &ConfigErrors{}
ce.Add("KEY1", errors.New("err1"))
ce.LogAll(logger, WithLogMessage("custom_msg"))
output := buf.String()
if !strings.Contains(output, "msg=custom_msg") {
t.Error("expected custom message")
}
})
t.Run("LogError with ConfigErrors", func(t *testing.T) {
buf.Reset()
ce := &ConfigErrors{}
ce.Add("KEY1", errors.New("err1"))
LogError(logger, ce)
output := buf.String()
if !strings.Contains(output, "key=KEY1") {
t.Error("expected individual error logging via LogError")
}
})
t.Run("LogError with regular error", func(t *testing.T) {
buf.Reset()
err := errors.New("regular error")
LogError(logger, err, WithLogMessage("fail"))
output := buf.String()
if !strings.Contains(output, "msg=fail") {
t.Error("expected custom message for regular error")
}
if !strings.Contains(output, "error=\"regular error\"") {
t.Error("missing error in log")
}
})
}