-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging_test.go
More file actions
65 lines (57 loc) · 1.73 KB
/
Copy pathlogging_test.go
File metadata and controls
65 lines (57 loc) · 1.73 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
package main
import (
"bytes"
"encoding/json"
"log/slog"
"regexp"
"testing"
)
func TestNewLogHandlerJSON(t *testing.T) {
t.Setenv("LOG_FORMAT", "json")
var buf bytes.Buffer
logger := slog.New(newLogHandler(&buf))
logger.Error("aspell check failed", "commit", "abc12345", "file", "f.txt")
var entry map[string]any
if err := json.Unmarshal(buf.Bytes(), &entry); err != nil {
t.Fatalf("output is not JSON: %v (%q)", err, buf.String())
}
if entry["msg"] != "aspell check failed" || entry["commit"] != "abc12345" || entry["level"] != "ERROR" {
t.Errorf("unexpected entry: %v", entry)
}
}
func TestNewLogHandlerDefaultUses24hTime(t *testing.T) {
t.Setenv("LOG_FORMAT", "")
t.Setenv("NO_COLOR", "1") // stable output for assertions
var buf bytes.Buffer
logger := slog.New(newLogHandler(&buf))
logger.Info("checking local commits", "count", 2)
out := buf.String()
if !regexp.MustCompile(`^\d{2}:\d{2}:\d{2} INF checking local commits count=2`).MatchString(out) {
t.Errorf("unexpected tint output: %q", out)
}
if bytes.Contains(buf.Bytes(), []byte("\x1b[")) {
t.Errorf("ANSI codes present despite NO_COLOR: %q", out)
}
}
func TestNoColorPrecedence(t *testing.T) {
tests := []struct {
name string
noColor string
logColor string
want bool
}{
{"NO_COLOR wins over LOG_COLOR", "1", "always", true},
{"LOG_COLOR forces color", "", "1", false},
{"LOG_COLOR true forces color", "", "true", false},
{"default without TTY", "", "", true}, // test stderr is not a terminal
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("NO_COLOR", tt.noColor)
t.Setenv("LOG_COLOR", tt.logColor)
if got := noColor(); got != tt.want {
t.Errorf("noColor() = %v, want %v", got, tt.want)
}
})
}
}