diff --git a/internal/util/io.go b/internal/util/io.go index eb06993..31352d5 100644 --- a/internal/util/io.go +++ b/internal/util/io.go @@ -92,13 +92,17 @@ func IsTestFileOrPath(fullPath string) bool { var r *regexp.Regexp for _, pattern := range testPathRegex { r = regexp.MustCompile(pattern) - return r.MatchString(fullPath) + if r.MatchString(fullPath) { + return true + } } fName := filepath.Base(fullPath) for _, pattern := range testFileRegex { r = regexp.MustCompile(pattern) - return r.MatchString(fName) + if r.MatchString(fName) { + return true + } } return false diff --git a/internal/util/io_test.go b/internal/util/io_test.go index 68731d7..f6e5ded 100644 --- a/internal/util/io_test.go +++ b/internal/util/io_test.go @@ -130,3 +130,112 @@ func TestGetYamlFiles(t *testing.T) { assert.Empty(t, got) }) } + +func TestCopyFiles(t *testing.T) { + t.Run("ok", func(t *testing.T) { + src := "../../testfixtures/yamlfiles" + dst, err := os.MkdirTemp("", "TestCopyFilesOK*") + defer os.RemoveAll(dst) + require.NoError(t, err) + + err = CopyFiles(src, dst) + assert.NoError(t, err) + }) + + t.Run("err", func(t *testing.T) { + src := "none/existing/dir" + dst, err := os.MkdirTemp("", "TestCopyFilesErr*") + defer os.RemoveAll(dst) + require.NoError(t, err) + + err = CopyFiles(src, dst) + assert.Error(t, err) + }) +} + +func TestIsMaxFileSize(t *testing.T) { + t.Run("ok", func(t *testing.T) { + got, msg := IsMaxFileSize("./io.go", 1) + assert.False(t, got) + assert.Empty(t, msg) + }) + + t.Run("file does not exist", func(t *testing.T) { + got, msg := IsMaxFileSize("./no-file", 1) + assert.False(t, got) + assert.Equal(t, "stat ./no-file: no such file or directory", msg) + }) + + t.Run("negative input, too large file", func(t *testing.T) { + got, msg := IsMaxFileSize("./io.go", -1) + assert.True(t, got) + assert.Equal(t, "is too large", msg) + }) + +} + +func TestWriteToFile(t *testing.T) { + t.Run("ok", func(t *testing.T) { + dst, err := os.MkdirTemp("", "TestWriteToFileOK*") + defer os.RemoveAll(dst) + require.NoError(t, err) + + fh := dst + "/sample.txt" + err = WriteToFile(fh, []byte("just-testing-meh")) + assert.NoError(t, err) + }) + + t.Run("no input", func(t *testing.T) { + dst, err := os.MkdirTemp("", "TestWriteToFileOKNoInput*") + defer os.RemoveAll(dst) + require.NoError(t, err) + + fh := dst + "/sample.txt" + err = WriteToFile(fh, []byte{}) + assert.NoError(t, err) + }) + + t.Run("incorrect path", func(t *testing.T) { + err := WriteToFile("", []byte("just-testing-meh")) + assert.Error(t, err) + assert.EqualError(t, err, "open : no such file or directory") + }) +} + +func TestSetHomeDir(t *testing.T) { + t.Skip() +} + +func TestMakeHomeDir(t *testing.T) { + t.Skip() +} + +func Test_IsTestFileOrPath(t *testing.T) { + tests := []struct { + input string + want bool + }{ + {"foo/test/bar", true}, + {"test/foo/bar", true}, + {"foo/test-secrets/bar", true}, + {"ghTestlk", true}, + {"Testllfhe", true}, + {"Test", true}, + {"foo_test.go", true}, + {"foo_test_baz", true}, + {"../../testfixtures/yamlfiles", true}, + {"/testfixtures/yamlfiles", true}, + {"thisShouldnt_Match.zip", false}, + {"/root/user/must/not.match", false}, + } + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + got := IsTestFileOrPath(tt.input) + if tt.want { + assert.True(t, got) + } else { + assert.False(t, got) + } + }) + } +}