-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr_test.go
More file actions
400 lines (363 loc) · 11.5 KB
/
Copy pathexpr_test.go
File metadata and controls
400 lines (363 loc) · 11.5 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package expression_test
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jahvon/expression"
)
func TestIsTruthy(t *testing.T) {
tests := []struct {
name string
expr string
env expression.Data
expected bool
}{
{"true literal", "true", nil, true},
{"false literal", "false", nil, false},
{"numeric 1", "1", nil, true},
{"numeric 0", "0", nil, false},
{"string true", `"true"`, nil, true},
{"string false", `"false"`, nil, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.IsTruthy(test.expr, test.env)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestEvaluate(t *testing.T) {
tests := []struct {
name string
expr string
env expression.Data
expected interface{}
}{
{"addition", "1 + 1", nil, 2},
{"boolean and", "true && false", nil, false},
{"string concatenation", `"hello" + " " + "world"`, nil, "hello world"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, test.env)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestEvaluateString(t *testing.T) {
tests := []struct {
name string
expr string
env expression.Data
expected string
}{
{"string literal", `"hello"`, nil, "hello"},
{"string concatenation", `"foo" + "bar"`, nil, "foobar"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.EvaluateString(test.expr, test.env)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %s, got %s", test.expected, result)
}
})
}
}
func TestDataComplexExpressions(t *testing.T) {
data := map[string]interface{}{
"os": "linux",
"arch": "amd64",
"ctx": struct {
Workspace string `expr:"workspace"`
Namespace string `expr:"namespace"`
}{"workspace", "namespace"},
"store": map[string]string{
"key1": "value1",
"key2": "value2",
},
"env": map[string]string{
"ENV_VAR1": "env_value1",
"ENV_VAR2": "env_value2",
},
}
tests := []struct {
name string
expr string
expected interface{}
}{
{"addition", "1 + 1", 2},
{"boolean and", "true && false", false},
{"string concatenation", `"hello" + " " + "world"`, "hello world"},
{"map access", `store["key1"]`, "value1"},
{"env access", `env["ENV_VAR1"]`, "env_value1"},
{"os comparison", `os == "linux"`, true},
{"arch comparison", `arch == "amd64"`, true},
{"struct field access", `ctx.workspace == "workspace"`, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, data)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestBuildDataExec(t *testing.T) {
envMap := map[string]string{}
ctx := context.Background()
data, err := expression.BuildData(ctx, envMap)
if err != nil {
t.Fatalf("expected no error building data, got %v", err)
}
dataMap, ok := data.(map[string]interface{})
if !ok {
t.Fatalf("expected data to be a map[string]interface{}, got %T", data)
}
if _, exists := dataMap["$"]; !exists {
t.Fatal("exec function should exist in BuildData result")
}
result, err := expression.EvaluateString(`$("echo \"hello world\"")`, data)
if err != nil {
t.Fatalf("expected no error executing command, got %v", err)
}
expected := "hello world"
if result != expected {
t.Errorf("expected %q, got %q", expected, result)
}
}
func TestFileExistenceFunctions(t *testing.T) {
tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.txt")
testDir := filepath.Join(tempDir, "testdir")
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
if err := os.Mkdir(testDir, 0755); err != nil {
t.Fatalf("failed to create test directory: %v", err)
}
tests := []struct {
name string
expr string
expected bool
}{
{"fileExists with existing file", `fileExists("` + testFile + `")`, true},
{"fileExists with existing dir", `fileExists("` + testDir + `")`, true},
{"fileExists with non-existing", `fileExists("/non/existing/path")`, false},
{"dirExists with existing dir", `dirExists("` + testDir + `")`, true},
{"dirExists with file", `dirExists("` + testFile + `")`, false},
{"dirExists with non-existing", `dirExists("/non/existing/path")`, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestFileTypeFunctions(t *testing.T) {
tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.txt")
testDir := filepath.Join(tempDir, "testdir")
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
if err := os.Mkdir(testDir, 0755); err != nil {
t.Fatalf("failed to create test directory: %v", err)
}
tests := []struct {
name string
expr string
expected bool
}{
{"isFile with file", `isFile("` + testFile + `")`, true},
{"isFile with directory", `isFile("` + testDir + `")`, false},
{"isFile with non-existing", `isFile("/non/existing/path")`, false},
{"isDir with directory", `isDir("` + testDir + `")`, true},
{"isDir with file", `isDir("` + testFile + `")`, false},
{"isDir with non-existing", `isDir("/non/existing/path")`, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestPathOperationFunctions(t *testing.T) {
tests := []struct {
name string
expr string
expected string
}{
{"basename of file", `basename("/path/to/file.txt")`, "file.txt"},
{"basename of directory", `basename("/path/to/dir")`, "dir"},
{"basename of root", `basename("/")`, "/"},
{"basename of current", `basename(".")`, "."},
{"dirname of file", `dirname("/path/to/file.txt")`, "/path/to"},
{"dirname of directory", `dirname("/path/to/dir")`, "/path/to"},
{"dirname of root", `dirname("/")`, "/"},
{"dirname of current", `dirname(".")`, "."},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.EvaluateString(test.expr, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %q, got %q", test.expected, result)
}
})
}
}
func TestFileContentFunctions(t *testing.T) {
tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.txt")
testContent := "Hello, World!\nThis is a test file."
if err := os.WriteFile(testFile, []byte(testContent), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
tests := []struct {
name string
expr string
expected interface{}
}{
{"readFile success", `readFile("` + testFile + `")`, testContent},
{"fileSize success", `fileSize("` + testFile + `")`, int64(len(testContent))},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if result != test.expected {
t.Errorf("expected %v, got %v", test.expected, result)
}
})
}
}
func TestFileTimeFunctions(t *testing.T) {
tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.txt")
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
info, err := os.Stat(testFile)
if err != nil {
t.Fatalf("failed to stat test file: %v", err)
}
expectedModTime := info.ModTime()
time.Sleep(10 * time.Millisecond)
tests := []struct {
name string
expr string
test func(result interface{}) bool
}{
{
"fileModTime returns correct time",
`fileModTime("` + testFile + `")`,
func(result interface{}) bool {
modTime, ok := result.(time.Time)
return ok && modTime.Equal(expectedModTime)
},
},
{
"fileAge returns positive duration",
`fileAge("` + testFile + `")`,
func(result interface{}) bool {
age, ok := result.(time.Duration)
return ok && age > 0
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := expression.Evaluate(test.expr, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !test.test(result) {
t.Errorf("test failed for result: %v", result)
}
})
}
}
func TestFileOperationErrors(t *testing.T) {
tests := []struct {
name string
expr string
expectError bool
errorMsg string
}{
{"fileExists wrong args", `fileExists()`, true, "takes exactly 1 argument"},
{"fileExists wrong type", `fileExists(123)`, true, "requires string argument"},
{"dirExists wrong args", `dirExists("a", "b")`, true, "takes exactly 1 argument"},
{"dirExists wrong type", `dirExists(true)`, true, "requires string argument"},
{"isFile wrong args", `isFile()`, true, "takes exactly 1 argument"},
{"isFile wrong type", `isFile(123)`, true, "requires string argument"},
{"isDir wrong args", `isDir("a", "b")`, true, "takes exactly 1 argument"},
{"isDir wrong type", `isDir(false)`, true, "requires string argument"},
{"basename wrong args", `basename()`, true, "takes exactly 1 argument"},
{"basename wrong type", `basename(123)`, true, "requires string argument"},
{"dirname wrong args", `dirname("a", "b")`, true, "takes exactly 1 argument"},
{"dirname wrong type", `dirname(true)`, true, "requires string argument"},
{"readFile wrong args", `readFile()`, true, "takes exactly 1 argument"},
{"readFile wrong type", `readFile(123)`, true, "requires string argument"},
{"readFile non-existing", `readFile("/non/existing/file")`, true, "no such file"},
{"fileSize wrong args", `fileSize()`, true, "takes exactly 1 argument"},
{"fileSize wrong type", `fileSize(true)`, true, "requires string argument"},
{"fileSize non-existing", `fileSize("/non/existing/file")`, true, "no such file"},
{"fileModTime wrong args", `fileModTime()`, true, "takes exactly 1 argument"},
{"fileModTime wrong type", `fileModTime(123)`, true, "requires string argument"},
{"fileModTime non-existing", `fileModTime("/non/existing/file")`, true, "no such file"},
{"fileAge wrong args", `fileAge()`, true, "takes exactly 1 argument"},
{"fileAge wrong type", `fileAge(false)`, true, "requires string argument"},
{"fileAge non-existing", `fileAge("/non/existing/file")`, true, "no such file"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := expression.Evaluate(test.expr, nil)
if test.expectError {
if err == nil {
t.Fatalf("expected error but got none")
}
if !strings.Contains(err.Error(), test.errorMsg) {
t.Errorf("expected error to contain %q, got %v", test.errorMsg, err)
}
} else if err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
}
}