From 5e2a3f9d8de6f403f0d9f039eaa717d469172b5b Mon Sep 17 00:00:00 2001 From: Rumen Vasilev Date: Mon, 13 Nov 2023 00:44:04 +0100 Subject: [PATCH] add test for GetYamlFiles and fix a bug Signed-off-by: Rumen Vasilev --- internal/util/io.go | 10 ++++++---- internal/util/io_test.go | 24 ++++++++++++++++++++++++ testfixtures/yamlfiles/file-2.yml | 5 +++++ testfixtures/yamlfiles/file1.yaml | 4 ++++ testfixtures/yamlfiles/notyaml.file | 4 ++++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 testfixtures/yamlfiles/file-2.yml create mode 100644 testfixtures/yamlfiles/file1.yaml create mode 100644 testfixtures/yamlfiles/notyaml.file diff --git a/internal/util/io.go b/internal/util/io.go index c55b5f7..eb06993 100644 --- a/internal/util/io.go +++ b/internal/util/io.go @@ -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 } diff --git a/internal/util/io_test.go b/internal/util/io_test.go index 144079f..68731d7 100644 --- a/internal/util/io_test.go +++ b/internal/util/io_test.go @@ -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) + }) +} diff --git a/testfixtures/yamlfiles/file-2.yml b/testfixtures/yamlfiles/file-2.yml new file mode 100644 index 0000000..a85353f --- /dev/null +++ b/testfixtures/yamlfiles/file-2.yml @@ -0,0 +1,5 @@ +-- +root: + node: + - list + - file2 diff --git a/testfixtures/yamlfiles/file1.yaml b/testfixtures/yamlfiles/file1.yaml new file mode 100644 index 0000000..e21ba2c --- /dev/null +++ b/testfixtures/yamlfiles/file1.yaml @@ -0,0 +1,4 @@ +-- +root: + node: + - list diff --git a/testfixtures/yamlfiles/notyaml.file b/testfixtures/yamlfiles/notyaml.file new file mode 100644 index 0000000..9c69f41 --- /dev/null +++ b/testfixtures/yamlfiles/notyaml.file @@ -0,0 +1,4 @@ +-- +#this file is expected to be ignored +root: + child: bla