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 DeleteAll() on non-existing directory #3974

Merged
merged 2 commits into from
Apr 1, 2021
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
4 changes: 3 additions & 1 deletion pkg/runutil/runutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ func ExhaustCloseWithErrCapture(err *error, r io.ReadCloser, format string, a ..

// DeleteAll deletes all files and directories inside the given
// dir except for the ignoreDirs directories.
// NOTE: DeleteAll is not idempotent.
func DeleteAll(dir string, ignoreDirs ...string) error {
entries, err := ioutil.ReadDir(dir)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return errors.Wrap(err, "read dir")
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/runutil/runutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,12 @@ func TestDeleteAll(t *testing.T) {
_, err = os.Stat(filepath.Join(dir, "c", "innerc"))
testutil.Ok(t, err)
}

func TestDeleteAll_ShouldReturnNoErrorIfDirectoryDoesNotExists(t *testing.T) {
dir, err := ioutil.TempDir("", "example")
testutil.Ok(t, err)
testutil.Ok(t, os.RemoveAll(dir))

// Calling DeleteAll() on a non-existent directory should return no error.
testutil.Ok(t, DeleteAll(dir))
}