github.com/kubeshop/testkube@v1.17.23/pkg/tcl/expressionstcl/libs/fs_test.go (about) 1 // Copyright 2024 Testkube. 2 // 3 // Licensed as a Testkube Pro file under the Testkube Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt 8 9 package libs 10 11 import ( 12 "testing" 13 14 "github.com/spf13/afero" 15 "github.com/stretchr/testify/assert" 16 17 "github.com/kubeshop/testkube/pkg/tcl/expressionstcl" 18 ) 19 20 func MustCall(m expressionstcl.Machine, name string, args ...interface{}) interface{} { 21 list := make([]expressionstcl.StaticValue, len(args)) 22 for i, v := range args { 23 if vv, ok := v.(expressionstcl.StaticValue); ok { 24 list[i] = vv 25 } else { 26 list[i] = expressionstcl.NewValue(v) 27 } 28 } 29 v, ok, err := m.Call(name, list...) 30 if err != nil { 31 panic(err) 32 } 33 if !ok { 34 panic("not recognized") 35 } 36 return v.Static().Value() 37 } 38 39 func TestFsLibGlob(t *testing.T) { 40 fsys := &afero.IOFS{Fs: afero.NewMemMapFs()} 41 _ = afero.WriteFile(fsys.Fs, "etc/file1.txt", nil, 0644) 42 _ = afero.WriteFile(fsys.Fs, "else/file1.txt", nil, 0644) 43 _ = afero.WriteFile(fsys.Fs, "another-file.txt", nil, 0644) 44 _ = afero.WriteFile(fsys.Fs, "etc/nested/file2.json", nil, 0644) 45 machine := NewFsMachine(fsys, "/etc") 46 assert.Equal(t, []string{"/etc/file1.txt", "/etc/nested/file2.json"}, MustCall(machine, "glob", "**/*")) 47 assert.Equal(t, []string{"/etc/file1.txt"}, MustCall(machine, "glob", "*")) 48 assert.Equal(t, []string{"/etc/nested/file2.json"}, MustCall(machine, "glob", "**/*.json")) 49 assert.Equal(t, []string{"/etc/file1.txt", "/etc/nested/file2.json"}, MustCall(machine, "glob", "**/*.json", "*.txt")) 50 assert.Equal(t, []string{"/another-file.txt", "/else/file1.txt", "/etc/file1.txt"}, MustCall(machine, "glob", "/**/*.txt")) 51 assert.Equal(t, []string{"/another-file.txt", "/etc/file1.txt"}, MustCall(machine, "glob", "/**/*.txt", "!/else/**/*")) 52 } 53 54 func TestFsLibRead(t *testing.T) { 55 fsys := &afero.IOFS{Fs: afero.NewMemMapFs()} 56 _ = afero.WriteFile(fsys.Fs, "etc/file1.txt", []byte("foo"), 0644) 57 _ = afero.WriteFile(fsys.Fs, "another-file.txt", []byte("bar"), 0644) 58 machine := NewFsMachine(fsys, "/etc") 59 assert.Equal(t, "foo", MustCall(machine, "file", "file1.txt")) 60 assert.Equal(t, "foo", MustCall(machine, "file", "/etc/file1.txt")) 61 assert.Equal(t, "bar", MustCall(machine, "file", "../another-file.txt")) 62 assert.Equal(t, "bar", MustCall(machine, "file", "/another-file.txt")) 63 }