Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safely check if the directory exists #7353

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions pkg/utils/fsutils/fs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fsutils

import (
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -59,18 +58,13 @@ func CopyFile(src, dst string) (int64, error) {
}

func DirExists(path string) bool {
if f, err := os.Stat(path); os.IsNotExist(err) || !f.IsDir() {
return false
}
return true
f, err := os.Stat(path)
return err == nil && f.IsDir()
}

func FileExists(filename string) bool {
_, err := os.Stat(filename)
if errors.Is(err, os.ErrNotExist) {
return false
}
return err == nil
f, err := os.Stat(filename)
return err == nil && !f.IsDir()
}

type WalkDirRequiredFunc func(path string, d fs.DirEntry) bool
Expand Down
62 changes: 45 additions & 17 deletions pkg/utils/fsutils/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@ package fsutils

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func touch(t *testing.T, name string) {
f, err := os.Create(name)
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}

func write(t *testing.T, name, content string) {
err := os.WriteFile(name, []byte(content), 0666)
if err != nil {
t.Fatal(err)
}
}

func TestCopyFile(t *testing.T) {
type args struct {
src string
Expand Down Expand Up @@ -72,3 +56,47 @@ func TestCopyFile(t *testing.T) {
})
}
}

func TestDirExists(t *testing.T) {
t.Run("invalid path", func(t *testing.T) {
assert.False(t, DirExists("\000invalid:path"))
})

t.Run("valid path", func(t *testing.T) {
assert.True(t, DirExists(t.TempDir()))
})

t.Run("dir not exist", func(t *testing.T) {
assert.False(t, DirExists(filepath.Join(t.TempDir(), "tmp")))
})

t.Run("file path", func(t *testing.T) {
filePath := filepath.Join(t.TempDir(), "tmp")
f, err := os.Create(filePath)
require.NoError(t, f.Close())
require.NoError(t, err)
assert.False(t, DirExists(filePath))
})
}

func TestFileExists(t *testing.T) {
t.Run("invalid path", func(t *testing.T) {
assert.False(t, FileExists("\000invalid:path"))
})

t.Run("valid path", func(t *testing.T) {
filePath := filepath.Join(t.TempDir(), "tmp")
f, err := os.Create(filePath)
require.NoError(t, f.Close())
require.NoError(t, err)
assert.True(t, FileExists(filePath))
})

t.Run("file not exist", func(t *testing.T) {
assert.False(t, FileExists(filepath.Join(t.TempDir(), "tmp")))
})

t.Run("dir path", func(t *testing.T) {
assert.False(t, FileExists(t.TempDir()))
})
}