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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
package testutil
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
// TempFile creates a temporary file with the given content and returns its path.
// The file is automatically cleaned up when the test ends.
func TempFile(t *testing.T, content string) string {
t.Helper()
tmpfile, err := os.CreateTemp("", "dtail-test-*.txt")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
if _, err := tmpfile.Write([]byte(content)); err != nil {
tmpfile.Close()
os.Remove(tmpfile.Name())
t.Fatalf("failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
os.Remove(tmpfile.Name())
t.Fatalf("failed to close temp file: %v", err)
}
t.Cleanup(func() {
os.Remove(tmpfile.Name())
})
return tmpfile.Name()
}
// TempDir creates a temporary directory and returns its path.
// The directory is automatically cleaned up when the test ends.
func TempDir(t *testing.T) string {
t.Helper()
tmpdir, err := os.MkdirTemp("", "dtail-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
t.Cleanup(func() {
os.RemoveAll(tmpdir)
})
return tmpdir
}
// CreateFileTree creates a directory structure with files based on the provided map.
// Keys are relative file paths, values are file contents.
func CreateFileTree(t *testing.T, baseDir string, files map[string]string) {
t.Helper()
for path, content := range files {
fullPath := filepath.Join(baseDir, path)
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("failed to create directory %s: %v", dir, err)
}
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
t.Fatalf("failed to write file %s: %v", fullPath, err)
}
}
}
// AssertFileContents checks that a file contains the expected content.
func AssertFileContents(t *testing.T, path, expected string) {
t.Helper()
actual, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read file %s: %v", path, err)
}
if string(actual) != expected {
t.Errorf("file content mismatch:\nexpected: %q\nactual: %q", expected, string(actual))
}
}
// CaptureOutput captures stdout during the execution of a function.
func CaptureOutput(t *testing.T, f func()) string {
t.Helper()
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stdout = w
outCh := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outCh <- buf.String()
}()
f()
w.Close()
os.Stdout = old
return <-outCh
}
// AssertError checks that an error is not nil and contains the expected substring.
func AssertError(t *testing.T, err error, contains string) {
t.Helper()
if err == nil {
t.Errorf("expected error containing %q, got nil", contains)
return
}
if !strings.Contains(err.Error(), contains) {
t.Errorf("expected error containing %q, got %q", contains, err.Error())
}
}
// AssertNoError checks that an error is nil.
func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
}
// AssertEqual checks that two values are equal.
func AssertEqual(t *testing.T, expected, actual interface{}) {
t.Helper()
if expected != actual {
t.Errorf("expected %v, got %v", expected, actual)
}
}
// AssertContains checks that a string contains a substring.
func AssertContains(t *testing.T, s, substr string) {
t.Helper()
if !strings.Contains(s, substr) {
t.Errorf("expected %q to contain %q", s, substr)
}
}
// AssertNotContains checks that a string does not contain a substring.
func AssertNotContains(t *testing.T, s, substr string) {
t.Helper()
if strings.Contains(s, substr) {
t.Errorf("expected %q not to contain %q", s, substr)
}
}
// GenerateTestData generates test data of the specified size.
func GenerateTestData(lines int, lineLength int) string {
var builder strings.Builder
line := strings.Repeat("x", lineLength-1) + "\n"
for i := 0; i < lines; i++ {
builder.WriteString(fmt.Sprintf("%d: %s", i+1, line))
}
return builder.String()
}
// GenerateLogLines generates realistic log lines for testing.
func GenerateLogLines(count int) []string {
levels := []string{"INFO", "WARN", "ERROR", "DEBUG"}
messages := []string{
"Server started successfully",
"Connection established",
"Processing request",
"Request completed",
"Connection closed",
"Error processing file",
"Timeout occurred",
"Retrying operation",
}
lines := make([]string, count)
for i := 0; i < count; i++ {
level := levels[i%len(levels)]
msg := messages[i%len(messages)]
lines[i] = fmt.Sprintf("2024-01-15 10:00:%02d [%s] %s", i%60, level, msg)
}
return lines
}
// TableTest is a generic structure for table-driven tests.
type TableTest[T any] struct {
Name string
Input T
Expected interface{}
WantErr bool
ErrMsg string
}
|