Skip to content

Commit

Permalink
add test for GetYamlFiles and fix a bug
Browse files Browse the repository at this point in the history
Signed-off-by: Rumen Vasilev <[email protected]>
  • Loading branch information
rumenvasilev committed Nov 12, 2023
1 parent a0bc846 commit 5e2a3f9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internal/util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ func GetYamlFiles(dir string) ([]string, error) {
}

var sigs []string
var ext string
var ext, fullPath string
for _, f := range files {
ext = filepath.Ext(dir + "/" + f.Name())
if ext == "yml" || ext == "yaml" {
sigs = append(sigs, fmt.Sprintf("%s/%s", dir, f.Name()))
fullPath = fmt.Sprintf("%s/%s", dir, f.Name())
ext = filepath.Ext(fullPath)
if ext == ".yml" || ext == ".yaml" {
sigs = append(sigs, fullPath)
}
}

return sigs, nil
}

Expand Down
24 changes: 24 additions & 0 deletions internal/util/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,27 @@ func BenchmarkIsBinaryFile(b *testing.B) {
IsBinaryFile("../../bin/rvsecret-darwin")
}
}

func TestGetYamlFiles(t *testing.T) {
t.Run("ok", func(t *testing.T) {
dir := "../../testfixtures/yamlfiles"
want := []string{dir + "/file-2.yml", dir + "/file1.yaml"}
got, err := GetYamlFiles(dir)
assert.NoError(t, err)
assert.Equal(t, want, got)
})

t.Run("no files found", func(t *testing.T) {
dir := "./"
got, err := GetYamlFiles(dir)
assert.NoError(t, err)
assert.Empty(t, got)
})

t.Run("err", func(t *testing.T) {
dir := "none/existing/dir"
got, err := GetYamlFiles(dir)
assert.Error(t, err)
assert.Empty(t, got)
})
}
5 changes: 5 additions & 0 deletions testfixtures/yamlfiles/file-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--
root:
node:
- list
- file2
4 changes: 4 additions & 0 deletions testfixtures/yamlfiles/file1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--
root:
node:
- list
4 changes: 4 additions & 0 deletions testfixtures/yamlfiles/notyaml.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--
#this file is expected to be ignored
root:
child: bla

0 comments on commit 5e2a3f9

Please sign in to comment.