Skip to content

Commit

Permalink
Add MustCheckExists to filez.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Bertona committed Mar 16, 2022
1 parent 25dd795 commit a7e1283
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions filez/filez.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,15 @@ func WithMustCreateTempDir(pattern string, f func(dirPath string)) {

f(dirPath)
}

// MustCheckExists checks if the given path exists, panics on error (other than os.ErrNotExist).
func MustCheckExists(fileOrDirPath string) bool {
_, err := os.Stat(fileOrDirPath)
if err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
return true
}
18 changes: 18 additions & 0 deletions filez/filez_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ func TestFilesAndDirectories(t *testing.T) {
require.Error(t, err)
require.True(t, os.IsNotExist(err))
}

func TestMustCheckExists(t *testing.T) {
filez.WithMustCreateTempDir("golang-bites", func(dirPath string) {
require.True(t, filez.MustCheckExists(dirPath))
})

var deletedFilePath string
filez.WithMustWriteTempFile("golang-bites", []byte("temp"), func(filePath string) {
require.True(t, filez.MustCheckExists(filePath))
deletedFilePath = filePath
})

require.False(t, filez.MustCheckExists(deletedFilePath))

require.Panics(t, func() {
filez.MustCheckExists(string([]byte{0}))
})
}

0 comments on commit a7e1283

Please sign in to comment.