Skip to content

Commit

Permalink
add more tests and fix another 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 13, 2023
1 parent 5e2a3f9 commit d7fdffd
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
109 changes: 109 additions & 0 deletions internal/util/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit d7fdffd

Please sign in to comment.