Skip to content

Commit 9bdedb9

Browse files
committed
Revert "Remove test for old getMountpoint"
This reverts commit 0593fe5
1 parent f9c3172 commit 9bdedb9

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

internal/xdg/trashdir.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,25 @@ func getAllMountpoints() ([]string, error) {
239239
return mountpoints, nil
240240
}
241241

242+
var mountinfo_Mounted = mountinfo.Mounted
243+
var realpath_Realpath = realpath.Realpath
244+
242245
// Obtain a mount point associated with a file.
243246
// Same as df <PATH>
244247
func getMountpoint(path string) (string, error) {
245248

246249
// iterate over the parents of the real (without symlinks) path until we find a mount point
247250

248-
candidate, err := realpath.Realpath(path)
251+
candidate, err := realpath_Realpath(path)
249252
if err != nil {
250253
return "", err
251254
}
252255

253-
OUTER:
254256
for {
255257
// root is always mounted
256258
if candidate == string(os.PathSeparator) {
257259
slog.Debug("root mountpoint is detected", "path", path)
258-
break OUTER
260+
break
259261
}
260262

261263
if candidate == "." {
@@ -264,8 +266,8 @@ OUTER:
264266
return "", errors.New("mountpoint is '.'")
265267
}
266268

267-
if mounted, err := mountinfo.Mounted(candidate); err == nil && mounted {
268-
break OUTER
269+
if mounted, err := mountinfo_Mounted(candidate); err == nil && mounted {
270+
break
269271
}
270272

271273
candidate = filepath.Dir(candidate)

internal/xdg/trashdir_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package xdg
2+
3+
import (
4+
"slices"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGetMountpoint(t *testing.T) {
11+
// replace to stub
12+
mountinfo_Mounted = func(fpath string) (bool, error) {
13+
mounts := []string{
14+
"/",
15+
"/foo/bar",
16+
"/foo",
17+
"/fooo/bar",
18+
"/ffoo/bar",
19+
}
20+
return slices.Contains(mounts, fpath), nil
21+
}
22+
23+
realpath_Realpath = func(path string) (string, error) {
24+
return path, nil
25+
}
26+
27+
testsNormal := []struct {
28+
path string
29+
want string
30+
}{
31+
{path: "/a.txt", want: "/"},
32+
{path: "/foo/bar/a.txt", want: "/foo/bar"},
33+
{path: "/foo/bar/aaa/b.txt", want: "/foo/bar"},
34+
{path: "/ffoo/bar/a.txt", want: "/ffoo/bar"},
35+
{path: "/aaa/bbb/ccc/ddd.txt", want: "/"},
36+
{path: "/", want: "/"},
37+
}
38+
39+
t.Run("normal", func(t *testing.T) {
40+
for _, tt := range testsNormal {
41+
got, err := getMountpoint(tt.path)
42+
require.NoError(t, err)
43+
if got != tt.want {
44+
t.Errorf("getMountpoint(%q) = %q, want %q", tt.path, got, tt.want)
45+
}
46+
}
47+
})
48+
49+
t.Run("error", func(t *testing.T) {
50+
got, err := getMountpoint("")
51+
require.Error(t, err, got)
52+
})
53+
}

0 commit comments

Comments
 (0)