Skip to content

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
djjuhasz committed Nov 6, 2024
1 parent 0994e1a commit 628176a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
18 changes: 11 additions & 7 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ func BaseNoExt(path string) string {
return base
}

// FileExists returns true if path references an existing file. FileExists will
// return false if path doesn't reference an existing file, or if
// `os.Stat(path)` returns an error (e.g. ErrPermission).
func FileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
// Exists returns true if a file or directory exist at path and false if one
// doesn't. If a filesystem error occurs doing the check then Exists returns
// false and the error.
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false
return false, err
}

// Move moves a file or directory. It first tries to rename src to dst. If the
Expand Down
29 changes: 26 additions & 3 deletions fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,38 @@ func TestBaseNoExt(t *testing.T) {
}
}

func TestFileExists(t *testing.T) {
func TestExists(t *testing.T) {
t.Parallel()

td := tfs.NewDir(t, "preprocessing-sfa-test",
tfs.WithDir("a", tfs.WithFile("needle", "")),
)

assert.Assert(t, fsutil.FileExists(td.Join("a", "needle")))
assert.Assert(t, !fsutil.FileExists(td.Join("b")))
type test struct {
name string
path string
want bool
}
for _, tt := range []test{
{
name: "file exists",
path: td.Join("a", "needle"),
want: true,
},
{
name: "file doesn't exist",
path: td.Join("b"),
want: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := fsutil.Exists(tt.path)
assert.NilError(t, err)
assert.Equal(t, got, tt.want)
})
}
}

func TestSetFileModes(t *testing.T) {
Expand Down

0 comments on commit 628176a

Please sign in to comment.