forked from malashin/hoi4treesnap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
77 lines (67 loc) · 2.06 KB
/
logger_test.go
File metadata and controls
77 lines (67 loc) · 2.06 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestWriteDiagnosticLogCreatesDatedFile(t *testing.T) {
t.Setenv("APPIMAGE", "")
tempDir := t.TempDir()
oldBinPath := binPath
binPath = tempDir
defer func() { binPath = oldBinPath }()
now := time.Date(2026, time.April, 6, 15, 4, 5, 0, time.Local)
path, err := writeDiagnosticLogAt(now, "error.log", "boom")
if err != nil {
t.Fatalf("writeDiagnosticLogAt: %v", err)
}
expectedDir := filepath.Join(tempDir, "logs", "06-04-2026")
if filepath.Dir(path) != expectedDir {
t.Fatalf("diagnostic dir = %q, want %q", filepath.Dir(path), expectedDir)
}
if filepath.Base(path) != "03-04-05-06-04-2026-error.log" {
t.Fatalf("diagnostic file = %q", filepath.Base(path))
}
content, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read diagnostic log: %v", err)
}
text := string(content)
if !strings.Contains(text, "boom") {
t.Fatalf("diagnostic log missing content: %q", text)
}
if !strings.Contains(text, "[2026-04-06T15:04:05") {
t.Fatalf("diagnostic log missing timestamp header: %q", text)
}
}
func TestWriteDiagnosticLogAvoidsSameSecondCollisions(t *testing.T) {
t.Setenv("APPIMAGE", "")
tempDir := t.TempDir()
oldBinPath := binPath
binPath = tempDir
defer func() { binPath = oldBinPath }()
now := time.Date(2026, time.April, 6, 15, 4, 5, 0, time.Local)
firstPath, err := writeDiagnosticLogAt(now, "error.log", "first")
if err != nil {
t.Fatalf("first writeDiagnosticLogAt: %v", err)
}
secondPath, err := writeDiagnosticLogAt(now, "error.log", "second")
if err != nil {
t.Fatalf("second writeDiagnosticLogAt: %v", err)
}
if firstPath == secondPath {
t.Fatalf("expected unique paths, got %q", firstPath)
}
if filepath.Base(secondPath) != "03-04-05-06-04-2026-error-01.log" {
t.Fatalf("collision file = %q", filepath.Base(secondPath))
}
content, err := os.ReadFile(firstPath)
if err != nil {
t.Fatalf("read first diagnostic log: %v", err)
}
if strings.Contains(string(content), "second") {
t.Fatalf("first diagnostic log was overwritten: %q", string(content))
}
}