Report temporary metadata cleanup failures

This commit is contained in:
2026-11-02 03:24:57 +00:00
parent 0666b68ff6
commit ef47356541
7 changed files with 197 additions and 25 deletions

View File

@@ -1,12 +1,81 @@
package tempdir
import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestOutsideRemovesAllocatedPathWhenResolutionFails(t *testing.T) {
for _, fallback := range []bool{false, true} {
for _, operation := range []string{"absolute", "symlinks"} {
name := operation
if fallback {
name += "-fallback"
}
t.Run(name, func(t *testing.T) {
parent := t.TempDir()
repository := filepath.Join(parent, "repository")
local := filepath.Join(repository, "tmp")
if err := os.MkdirAll(local, 0o700); err != nil {
t.Fatal(err)
}
if fallback {
setTemporaryDirectory(t, local)
} else {
setTemporaryDirectory(t, parent)
}
resolutionErr := errors.New("resolution failed")
removalErr := errors.New("removal failed")
calls := 0
failed := false
var removed string
failAt := 2
if fallback {
failAt = 3
}
abs := func(path string) (string, error) {
if operation == "absolute" {
calls++
if calls == failAt {
failed = true
return "", resolutionErr
}
}
return filepath.Abs(path)
}
resolve := func(path string) (string, error) {
if operation == "symlinks" {
calls++
if calls == failAt {
failed = true
return "", resolutionErr
}
}
return filepath.EvalSymlinks(path)
}
remove := func(path string) error {
if err := os.RemoveAll(path); err != nil {
return err
}
if failed {
removed = path
return removalErr
}
return nil
}
_, err := outside(repository, "owned-", abs, resolve, remove)
if !errors.Is(err, resolutionErr) || !errors.Is(err, removalErr) || removed == "" || !strings.Contains(err.Error(), removed) || !strings.HasPrefix(filepath.Base(removed), "owned-") {
t.Fatalf("error = %v, removed = %q", err, removed)
}
})
}
}
}
func TestOutsideHonorsRelativeTemporaryDirectory(t *testing.T) {
parent := t.TempDir()
repository := t.TempDir()