github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csplugin/listfiles_test.go (about) 1 package csplugin 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/crowdsecurity/go-cs-lib/cstest" 12 ) 13 14 func TestListFilesAtPath(t *testing.T) { 15 dir, err := os.MkdirTemp("", "test-listfiles") 16 require.NoError(t, err) 17 t.Cleanup(func() { 18 os.RemoveAll(dir) 19 }) 20 _, err = os.Create(filepath.Join(dir, "notification-gitter")) 21 require.NoError(t, err) 22 _, err = os.Create(filepath.Join(dir, "slack")) 23 require.NoError(t, err) 24 err = os.Mkdir(filepath.Join(dir, "somedir"), 0755) 25 require.NoError(t, err) 26 _, err = os.Create(filepath.Join(dir, "somedir", "inner")) 27 require.NoError(t, err) 28 29 tests := []struct { 30 name string 31 path string 32 want []string 33 expectedErr string 34 }{ 35 { 36 name: "valid directory", 37 path: dir, 38 want: []string{ 39 filepath.Join(dir, "notification-gitter"), 40 filepath.Join(dir, "slack"), 41 }, 42 }, 43 { 44 name: "invalid directory", 45 path: "./foo/bar/", 46 expectedErr: "open ./foo/bar/: " + cstest.PathNotFoundMessage, 47 }, 48 } 49 for _, tc := range tests { 50 tc := tc 51 t.Run(tc.name, func(t *testing.T) { 52 got, err := listFilesAtPath(tc.path) 53 cstest.RequireErrorContains(t, err, tc.expectedErr) 54 assert.ElementsMatch(t, tc.want, got) 55 }) 56 } 57 }