-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatch_test.go
More file actions
308 lines (273 loc) · 7.61 KB
/
Copy pathwatch_test.go
File metadata and controls
308 lines (273 loc) · 7.61 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"testing"
"time"
"gopkg.in/yaml.v3"
)
func TestWatch_SetDefaults(t *testing.T) {
t.Run("sets defaults for zero values", func(t *testing.T) {
w := &Vai{}
w.setDefaults()
if w.Config.BufferSize != 4096 {
t.Errorf("Expected BufferSize to be 4096, got %d", w.Config.BufferSize)
}
if w.Config.Severity != SeverityWarn.String() {
t.Errorf("Expected Severity to be 'warn', got '%s'", w.Config.Severity)
}
if w.Config.Cooldown != 100*time.Millisecond {
t.Errorf("Expected Cooldown to be 100ms, got %v", w.Config.Cooldown)
}
})
t.Run("does not override existing values", func(t *testing.T) {
w := &Vai{
Config: Config{
BufferSize: 512,
Severity: "debug",
Cooldown: 50 * time.Millisecond,
},
}
w.setDefaults()
if w.Config.BufferSize != 512 {
t.Errorf("Expected BufferSize to remain 512, got %d", w.Config.BufferSize)
}
if w.Config.Severity != "debug" {
t.Errorf("Expected Severity to remain 'debug', got '%s'", w.Config.Severity)
}
if w.Config.Cooldown != 50*time.Millisecond {
t.Errorf("Expected Cooldown to remain 50ms, got %v", w.Config.Cooldown)
}
})
}
func TestWatch_Save(t *testing.T) {
w := &Vai{
Config: Config{Severity: "info"},
Jobs: map[string]Job{
"test-job": {Cmd: "go", Params: []string{"test"}},
},
}
tempDir := t.TempDir()
filePath := filepath.Join(tempDir, "vai.yml")
err := w.save(filePath)
if err != nil {
t.Fatalf("save() returned an unexpected error: %v", err)
}
data, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read saved file: %v", err)
}
var loadedVai Vai
if err := yaml.Unmarshal(data, &loadedVai); err != nil {
t.Fatalf("Failed to unmarshal saved data: %v", err)
}
if !reflect.DeepEqual(w.Config, loadedVai.Config) {
t.Errorf("Saved config does not match original. Got %+v, want %+v", loadedVai.Config, w.Config)
}
if !reflect.DeepEqual(w.Jobs, loadedVai.Jobs) {
t.Errorf("Saved jobs do not match original. Got %+v, want %+v", loadedVai.Jobs, w.Jobs)
}
}
func TestAggregateRegex(t *testing.T) {
vai := &Vai{
Jobs: map[string]Job{
"job1": {
Trigger: &Trigger{Regex: []string{`\.go$`, `!\.test\.go$`, `\.mod$`}},
},
"job2": {
Trigger: &Trigger{Regex: []string{`\.html$`, `!\.test\.go$`}},
},
"job3": {},
"job4": {
Trigger: &Trigger{Regex: []string{`\.go$`}},
},
},
}
inc, exc := vai.aggregateRegex()
sort.Strings(inc)
sort.Strings(exc)
expectedInc := []string{`\.go$`, `\.html$`, `\.mod$`}
sort.Strings(expectedInc)
expectedExc := []string{`\.test\.go$`}
sort.Strings(expectedExc)
if !reflect.DeepEqual(inc, expectedInc) {
t.Errorf("Expected inclusion patterns %v, got %v", expectedInc, inc)
}
if !reflect.DeepEqual(exc, expectedExc) {
t.Errorf("Expected exclusion patterns %v, got %v", expectedExc, exc)
}
}
func TestMatchRegex(t *testing.T) {
testCases := []struct {
name string
path string
patterns []string
expected bool
}{
{
name: "Matches inclusion pattern",
path: "main.go",
patterns: []string{`\.go$`},
expected: true,
},
{
name: "Does not match inclusion pattern",
path: "image.png",
patterns: []string{`\.go$`},
expected: false,
},
{
name: "Matches exclusion pattern",
path: "main_test.go",
patterns: []string{`\.go$`, `!_test\.go$`},
expected: false,
},
{
name: "Matches inclusion but not exclusion",
path: "main.go",
patterns: []string{`\.go$`, `!_test\.go$`},
expected: true,
},
{
name: "Empty patterns match everything",
path: "anything",
patterns: []string{},
expected: true,
},
{
name: "Matches multiple inclusions",
path: "go.mod",
patterns: []string{`\.go$`, `go\.mod$`},
expected: true,
},
{
name: "Exclusion overrides match",
path: "vendor/foo.go",
patterns: []string{`\.go$`, `!vendor/`},
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := matchRegex(tc.path, tc.patterns)
if result != tc.expected {
t.Errorf("matchRegex(%q, %v) = %v, want %v", tc.path, tc.patterns, result, tc.expected)
}
})
}
}
func TestFileExists(t *testing.T) {
t.Run("returns true for existing file", func(t *testing.T) {
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if !fileExists(tmpfile.Name()) {
t.Errorf("Expected fileExists to return true for %s", tmpfile.Name())
}
})
t.Run("returns false for non-existent file", func(t *testing.T) {
if fileExists("non_existent_file.xyz") {
t.Error("Expected fileExists to return false for non-existent file")
}
})
t.Run("returns false for directory", func(t *testing.T) {
tmpdir := t.TempDir()
if fileExists(tmpdir) {
t.Error("Expected fileExists to return false for directory")
}
})
}
func TestParsePath(t *testing.T) {
t.Run("returns provided path if not empty", func(t *testing.T) {
path := parsePath("/custom/path")
if path != "/custom/path" {
t.Errorf("Expected '/custom/path', got '%s'", path)
}
})
t.Run("returns cwd if path is empty", func(t *testing.T) {
cwd, _ := os.Getwd()
path := parsePath("")
if path != cwd {
t.Errorf("Expected cwd '%s', got '%s'", cwd, path)
}
})
}
func TestParseRegex(t *testing.T) {
t.Run("parses comma-separated patterns", func(t *testing.T) {
patterns := parseRegex("p1, p2 ,p3")
expected := []string{"p1", "p2", "p3"}
if !reflect.DeepEqual(patterns, expected) {
t.Errorf("Expected %v, got %v", expected, patterns)
}
})
t.Run("returns default patterns when empty", func(t *testing.T) {
patterns := parseRegex("")
expected := []string{".*\\.go$", "^go\\.mod$", "^go\\.sum$"}
if !reflect.DeepEqual(patterns, expected) {
t.Errorf("Expected default patterns, got %v", patterns)
}
})
}
func TestParseEnv(t *testing.T) {
t.Run("parses comma-separated env vars", func(t *testing.T) {
envMap := parseEnv("K1=V1, K2=V2 , K3=V3")
expected := map[string]string{"K1": "V1", "K2": "V2", "K3": "V3"}
if !reflect.DeepEqual(envMap, expected) {
t.Errorf("Expected %v, got %v", expected, envMap)
}
})
t.Run("handles invalid pairs gracefully", func(t *testing.T) {
envMap := parseEnv("K1=V1,invalid,K3=V3")
expected := map[string]string{"K1": "V1", "K3": "V3"}
if !reflect.DeepEqual(envMap, expected) {
t.Errorf("Expected %v, got %v", expected, envMap)
}
})
}
func TestParseFlags(t *testing.T) {
t.Run("CmdFlags take precedence", func(t *testing.T) {
cmdFlags := []string{"cmd1", "cmd2"}
posArgs := []string{"pos1", "pos2"}
result := parseFlags(cmdFlags, posArgs)
if !reflect.DeepEqual(result, cmdFlags) {
t.Errorf("Expected %v, got %v", cmdFlags, result)
}
})
t.Run("PositionalArgs used if CmdFlags empty", func(t *testing.T) {
posArgs := []string{"echo", "hello"}
result := parseFlags(nil, posArgs)
expected := []string{"echo hello"}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
})
t.Run("Returns nil if both empty", func(t *testing.T) {
// Initialize logger to prevent panic if parseFlags logs
logger = newLogger(SeverityDebug)
result := parseFlags(nil, nil)
if result != nil {
t.Errorf("Expected nil, got %v", result)
}
})
}
func TestClearCLI(t *testing.T) {
t.Run("clears screen", func(t *testing.T) {
if runtime.GOOS != "windows" {
output := captureOutput(func() {
clearCLI()
})
expected := "\033[H\033[2J"
if output != expected {
t.Errorf("Expected %q, got %q", expected, output)
}
} else {
// On Windows, just run it to ensure no panic
clearCLI()
}
})
}