Skip to content

Commit 05a8297

Browse files
authored
fix: safely check if the directory exists (#7353)
Signed-off-by: nikpivkin <[email protected]>
1 parent db2c955 commit 05a8297

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

pkg/utils/fsutils/fs.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fsutils
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"io/fs"
@@ -59,18 +58,13 @@ func CopyFile(src, dst string) (int64, error) {
5958
}
6059

6160
func DirExists(path string) bool {
62-
if f, err := os.Stat(path); os.IsNotExist(err) || !f.IsDir() {
63-
return false
64-
}
65-
return true
61+
f, err := os.Stat(path)
62+
return err == nil && f.IsDir()
6663
}
6764

6865
func FileExists(filename string) bool {
69-
_, err := os.Stat(filename)
70-
if errors.Is(err, os.ErrNotExist) {
71-
return false
72-
}
73-
return err == nil
66+
f, err := os.Stat(filename)
67+
return err == nil && !f.IsDir()
7468
}
7569

7670
type WalkDirRequiredFunc func(path string, d fs.DirEntry) bool

pkg/utils/fsutils/fs_test.go

+45-17
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@ package fsutils
22

33
import (
44
"os"
5+
"path/filepath"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
)
1011

11-
func touch(t *testing.T, name string) {
12-
f, err := os.Create(name)
13-
if err != nil {
14-
t.Fatal(err)
15-
}
16-
if err := f.Close(); err != nil {
17-
t.Fatal(err)
18-
}
19-
}
20-
21-
func write(t *testing.T, name, content string) {
22-
err := os.WriteFile(name, []byte(content), 0666)
23-
if err != nil {
24-
t.Fatal(err)
25-
}
26-
}
27-
2812
func TestCopyFile(t *testing.T) {
2913
type args struct {
3014
src string
@@ -72,3 +56,47 @@ func TestCopyFile(t *testing.T) {
7256
})
7357
}
7458
}
59+
60+
func TestDirExists(t *testing.T) {
61+
t.Run("invalid path", func(t *testing.T) {
62+
assert.False(t, DirExists("\000invalid:path"))
63+
})
64+
65+
t.Run("valid path", func(t *testing.T) {
66+
assert.True(t, DirExists(t.TempDir()))
67+
})
68+
69+
t.Run("dir not exist", func(t *testing.T) {
70+
assert.False(t, DirExists(filepath.Join(t.TempDir(), "tmp")))
71+
})
72+
73+
t.Run("file path", func(t *testing.T) {
74+
filePath := filepath.Join(t.TempDir(), "tmp")
75+
f, err := os.Create(filePath)
76+
require.NoError(t, f.Close())
77+
require.NoError(t, err)
78+
assert.False(t, DirExists(filePath))
79+
})
80+
}
81+
82+
func TestFileExists(t *testing.T) {
83+
t.Run("invalid path", func(t *testing.T) {
84+
assert.False(t, FileExists("\000invalid:path"))
85+
})
86+
87+
t.Run("valid path", func(t *testing.T) {
88+
filePath := filepath.Join(t.TempDir(), "tmp")
89+
f, err := os.Create(filePath)
90+
require.NoError(t, f.Close())
91+
require.NoError(t, err)
92+
assert.True(t, FileExists(filePath))
93+
})
94+
95+
t.Run("file not exist", func(t *testing.T) {
96+
assert.False(t, FileExists(filepath.Join(t.TempDir(), "tmp")))
97+
})
98+
99+
t.Run("dir path", func(t *testing.T) {
100+
assert.False(t, FileExists(t.TempDir()))
101+
})
102+
}

0 commit comments

Comments
 (0)