From 563828bf6a163420045fa77e1425bd230b432796 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sun, 31 Mar 2024 18:25:19 +0200 Subject: [PATCH] Handle some permission errors in test cleanup --- vfst/testfs.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/vfst/testfs.go b/vfst/testfs.go index 7aabf48..b5f3b9f 100644 --- a/vfst/testfs.go +++ b/vfst/testfs.go @@ -1,6 +1,8 @@ package vfst import ( + "errors" + "io/fs" "os" vfs "github.com/twpayne/go-vfs/v5" @@ -54,6 +56,23 @@ func (t *TestFS) TempDir() string { func (t *TestFS) cleanup() { if !t.keep { - os.RemoveAll(t.tempDir) + for { + // Remove t.tempDir but try to recover from permission denied errors + // by chmod'ing the path that causes the error. + err := os.RemoveAll(t.tempDir) + if err == nil { + break + } + if !errors.Is(err, fs.ErrPermission) { + break + } + var pathErr *os.PathError + if !errors.As(err, &pathErr) { + break + } + if err := os.Chmod(pathErr.Path, 0o777); err != nil { + break + } + } } }