-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
58 lines (45 loc) · 1.05 KB
/
logger_test.go
File metadata and controls
58 lines (45 loc) · 1.05 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
package main
import (
"bytes"
"os"
"strings"
"testing"
)
func TestLogDebug(t *testing.T) {
// Capture stderr
old := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Test with debug mode off
InitLogger(false)
LogDebug("This should not appear")
// Test with debug mode on
InitLogger(true)
LogDebug("Test message %s", "with args")
_ = w.Close()
os.Stderr = old
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
if strings.Contains(output, "This should not appear") {
t.Error("Debug message appeared when debug mode was off")
}
if !strings.Contains(output, "[DEBUG] Test message with args") {
t.Error("Debug message did not appear when debug mode was on")
}
}
func TestLogError(t *testing.T) {
// Capture stderr
old := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
LogError("Error message %d", 42)
_ = w.Close()
os.Stderr = old
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
if !strings.Contains(output, "[ERROR] Error message 42") {
t.Error("Error message did not appear correctly")
}
}