From f44b7b9851bd972c75cfff24ddfa96ced6113185 Mon Sep 17 00:00:00 2001 From: Marlon Kranz Date: Tue, 18 Aug 2026 18:46:09 +0200 Subject: [PATCH] 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 /.Trash/ and /.Trash-, 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. --- core/internal/trash/trash.go | 6 +++++- core/internal/trash/trash_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/internal/trash/trash.go b/core/internal/trash/trash.go index f130f8623..3b0b4c0da 100644 --- a/core/internal/trash/trash.go +++ b/core/internal/trash/trash.go @@ -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) diff --git a/core/internal/trash/trash_test.go b/core/internal/trash/trash_test.go index fc533564d..04f3c9352 100644 --- a/core/internal/trash/trash_test.go +++ b/core/internal/trash/trash_test.go @@ -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 }