fix(trash): keep tests off the machine's real per-mount trash dirs (#3100)

setupHomeTrash redirects HOME and XDG_DATA_HOME, but allTrashDirs also
walks readMountPoints() looking for <mount>/.Trash/<uid> and
<mount>/.Trash-<uid>, and no environment variable affects that walk. So
Count() and List() included whatever the developer had trashed on a
second filesystem, and Empty() deleted it for real: with an orphaned
file under a mount trash (no matching .trashinfo, so the count assert
still saw 3), TestEmptyClearsAll passed and removed the file.

Route the walk through a package-level mountPoints variable and have
setupHomeTrash swap in an empty list, restoring it via t.Cleanup.
Production behavior is unchanged.
This commit is contained in:
Marlon Kranz authored and GitHub committed 2026-08-18 12:46:09 -04:00
1 parent 978203cd52
commit f44b7b9851
2 files changed
+12 -1

No files matched your search

+5 -1
View File
@@ -250,6 +250,10 @@ func Put(path string) (Entry, error) {
}, nil
}
// mountPoints is a variable so tests can keep the mount walk off the real
// filesystem; HOME and XDG_DATA_HOME alone do not redirect it.
var mountPoints = readMountPoints
// allTrashDirs returns the home trash plus every per-mountpoint trash dir
// that exists (and passes the spec's safety checks for $topdir/.Trash).
func allTrashDirs() []string {
@@ -259,7 +263,7 @@ func allTrashDirs() []string {
}
uid := strconv.Itoa(os.Getuid())
for _, mount := range readMountPoints() {
for _, mount := range mountPoints() {
shared := filepath.Join(mount, ".Trash")
if isValidSharedTrash(shared) {
candidate := filepath.Join(shared, uid)
+7
View File
@@ -17,6 +17,13 @@ func setupHomeTrash(t *testing.T) (homeRoot string, trashDir string) {
}
t.Setenv("XDG_DATA_HOME", xdg)
t.Setenv("HOME", homeRoot)
// Without this, Count() and Empty() reach the machine's real per-mount
// trash dirs, and Empty() deletes what it finds there.
prev := mountPoints
mountPoints = func() []string { return nil }
t.Cleanup(func() { mountPoints = prev })
trashDir = filepath.Join(xdg, "Trash")
return homeRoot, trashDir
}