fork/gdu

git clone https://git.julia.blue/fork/gdu

cosmolifeat: move items to XDG trash with D key (#12) (#608)9ca5c60

ignore
8.8 KiB360 linesraw
1//go:build !windows && !darwin
2
3package remove
4
5import (
6	"bytes"
7	"errors"
8	"io"
9	"os"
10	"path/filepath"
11	"testing"
12
13	"github.com/stretchr/testify/assert"
14	"github.com/stretchr/testify/require"
15
16	"github.com/dundee/gdu/v5/pkg/analyze"
17)
18
19type stubTrashInfoFile struct {
20	writeErr error
21	closeErr error
22}
23
24func (f *stubTrashInfoFile) WriteString(s string) (int, error) {
25	if f.writeErr != nil {
26		return 0, f.writeErr
27	}
28	return len(s), nil
29}
30
31func (f *stubTrashInfoFile) Close() error {
32	return f.closeErr
33}
34
35type stubWriteCloser struct {
36	bytes.Buffer
37	closeErr error
38}
39
40func (w *stubWriteCloser) Close() error {
41	return w.closeErr
42}
43
44func TestMoveItemToTrashSetupErrors(t *testing.T) {
45	sentinel := errors.New("sentinel")
46
47	tests := []struct {
48		name      string
49		configure func(*trashOSOps)
50		unsetXDG  bool
51	}{
52		{
53			name: "home directory",
54			configure: func(ops *trashOSOps) {
55				ops.userHomeDir = func() (string, error) { return "", sentinel }
56			},
57			unsetXDG: true,
58		},
59		{
60			name: "files directory",
61			configure: func(ops *trashOSOps) {
62				ops.mkdirAll = func(string, os.FileMode) error { return sentinel }
63			},
64		},
65		{
66			name: "info directory",
67			configure: func(ops *trashOSOps) {
68				calls := 0
69				ops.mkdirAll = func(string, os.FileMode) error {
70					calls++
71					if calls == 2 {
72						return sentinel
73					}
74					return nil
75				}
76			},
77		},
78		{
79			name: "absolute source path",
80			configure: func(ops *trashOSOps) {
81				ops.abs = func(string) (string, error) { return "", sentinel }
82			},
83		},
84		{
85			name: "trashinfo reservation",
86			configure: func(ops *trashOSOps) {
87				ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
88					return nil, sentinel
89				}
90			},
91		},
92	}
93
94	for _, tt := range tests {
95		t.Run(tt.name, func(t *testing.T) {
96			if tt.unsetXDG {
97				t.Setenv("XDG_DATA_HOME", "")
98			} else {
99				t.Setenv("XDG_DATA_HOME", t.TempDir())
100			}
101			mockTrashOS(t, tt.configure)
102			parent := &analyze.Dir{
103				File:     &analyze.File{Name: "parent"},
104				BasePath: ".",
105			}
106			item := &analyze.File{Name: "item", Parent: parent}
107
108			err := MoveItemToTrash(nil, item)
109
110			require.ErrorIs(t, err, sentinel)
111		})
112	}
113}
114
115func TestMoveItemToTrashExhaustsDestinationRetries(t *testing.T) {
116	t.Setenv("XDG_DATA_HOME", t.TempDir())
117	mockTrashOS(t, func(ops *trashOSOps) {
118		ops.lstat = func(string) (os.FileInfo, error) { return nil, os.ErrNotExist }
119		ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
120			return &stubTrashInfoFile{}, nil
121		}
122		ops.rename = func(string, string) error { return os.ErrExist }
123		ops.remove = func(string) error { return nil }
124	})
125	parent := &analyze.Dir{
126		File:     &analyze.File{Name: "parent"},
127		BasePath: ".",
128	}
129	item := &analyze.File{Name: "item", Parent: parent}
130
131	err := MoveItemToTrash(nil, item)
132
133	require.Error(t, err)
134	assert.Contains(t, err.Error(), "could not find unique trash name")
135}
136
137func TestReserveTrashInfoErrorPaths(t *testing.T) {
138	sentinel := errors.New("sentinel")
139	info, err := os.Stat(t.TempDir())
140	require.NoError(t, err)
141
142	t.Run("initial lstat", func(t *testing.T) {
143		mockTrashOS(t, func(ops *trashOSOps) {
144			ops.lstat = func(string) (os.FileInfo, error) { return nil, sentinel }
145		})
146
147		_, _, err := reserveTrashInfo("files", "info", "item", "/item")
148
149		require.ErrorIs(t, err, sentinel)
150	})
151
152	t.Run("post-reservation lstat", func(t *testing.T) {
153		calls := 0
154		removed := false
155		mockTrashOS(t, func(ops *trashOSOps) {
156			ops.lstat = func(string) (os.FileInfo, error) {
157				calls++
158				if calls == 1 {
159					return nil, os.ErrNotExist
160				}
161				return nil, sentinel
162			}
163			ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
164				return &stubTrashInfoFile{}, nil
165			}
166			ops.remove = func(string) error {
167				removed = true
168				return nil
169			}
170		})
171
172		_, _, err := reserveTrashInfo("files", "info", "item", "/item")
173
174		require.ErrorIs(t, err, sentinel)
175		assert.True(t, removed)
176	})
177
178	t.Run("destination appears after reservation", func(t *testing.T) {
179		calls := 0
180		removed := false
181		mockTrashOS(t, func(ops *trashOSOps) {
182			ops.lstat = func(string) (os.FileInfo, error) {
183				calls++
184				switch calls {
185				case 1, 3, 4:
186					return nil, os.ErrNotExist
187				default:
188					return info, nil
189				}
190			}
191			ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
192				return &stubTrashInfoFile{}, nil
193			}
194			ops.remove = func(string) error {
195				removed = true
196				return nil
197			}
198		})
199
200		name, _, err := reserveTrashInfo("files", "info", "item", "/item")
201
202		require.NoError(t, err)
203		assert.Equal(t, "item.2", name)
204		assert.True(t, removed)
205	})
206
207	t.Run("all names occupied", func(t *testing.T) {
208		mockTrashOS(t, func(ops *trashOSOps) {
209			ops.lstat = func(string) (os.FileInfo, error) { return info, nil }
210		})
211
212		_, _, err := reserveTrashInfo("files", "info", "item", "/item")
213
214		require.Error(t, err)
215		assert.Contains(t, err.Error(), "could not find unique trash name")
216	})
217}
218
219func TestWriteTrashInfoErrors(t *testing.T) {
220	sentinel := errors.New("sentinel")
221
222	t.Run("write", func(t *testing.T) {
223		removed := false
224		mockTrashOS(t, func(ops *trashOSOps) {
225			ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
226				return &stubTrashInfoFile{writeErr: sentinel}, nil
227			}
228			ops.remove = func(string) error {
229				removed = true
230				return nil
231			}
232		})
233
234		err := writeTrashInfo("item.trashinfo", "/item")
235
236		require.ErrorIs(t, err, sentinel)
237		assert.True(t, removed)
238	})
239
240	t.Run("close", func(t *testing.T) {
241		removed := false
242		mockTrashOS(t, func(ops *trashOSOps) {
243			ops.openTrashInfo = func(string, int, os.FileMode) (trashInfoFile, error) {
244				return &stubTrashInfoFile{closeErr: sentinel}, nil
245			}
246			ops.remove = func(string) error {
247				removed = true
248				return nil
249			}
250		})
251
252		err := writeTrashInfo("item.trashinfo", "/item")
253
254		require.ErrorIs(t, err, sentinel)
255		assert.True(t, removed)
256	})
257}
258
259func TestCopyRecursivelyErrorPaths(t *testing.T) {
260	sentinel := errors.New("sentinel")
261
262	t.Run("read directory", func(t *testing.T) {
263		src := t.TempDir()
264		mockTrashOS(t, func(ops *trashOSOps) {
265			ops.readDir = func(string) ([]os.DirEntry, error) { return nil, sentinel }
266		})
267
268		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
269
270		require.ErrorIs(t, err, sentinel)
271	})
272
273	t.Run("child copy", func(t *testing.T) {
274		src := t.TempDir()
275		require.NoError(t, os.WriteFile(filepath.Join(src, "child"), []byte("data"), 0o600))
276		mockTrashOS(t, func(ops *trashOSOps) {
277			ops.openSource = func(string) (io.ReadCloser, error) { return nil, sentinel }
278		})
279
280		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
281
282		require.ErrorIs(t, err, sentinel)
283	})
284
285	t.Run("readlink", func(t *testing.T) {
286		src := filepath.Join(t.TempDir(), "link")
287		require.NoError(t, os.Symlink("target", src))
288		mockTrashOS(t, func(ops *trashOSOps) {
289			ops.readlink = func(string) (string, error) { return "", sentinel }
290		})
291
292		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
293
294		require.ErrorIs(t, err, sentinel)
295	})
296
297	t.Run("symlink", func(t *testing.T) {
298		src := filepath.Join(t.TempDir(), "link")
299		require.NoError(t, os.Symlink("target", src))
300		mockTrashOS(t, func(ops *trashOSOps) {
301			ops.symlink = func(string, string) error { return sentinel }
302		})
303
304		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
305
306		require.ErrorIs(t, err, sentinel)
307	})
308
309	t.Run("open source", func(t *testing.T) {
310		src := filepath.Join(t.TempDir(), "file")
311		require.NoError(t, os.WriteFile(src, []byte("data"), 0o600))
312		mockTrashOS(t, func(ops *trashOSOps) {
313			ops.openSource = func(string) (io.ReadCloser, error) { return nil, sentinel }
314		})
315
316		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
317
318		require.ErrorIs(t, err, sentinel)
319	})
320
321	t.Run("open destination", func(t *testing.T) {
322		src := filepath.Join(t.TempDir(), "file")
323		require.NoError(t, os.WriteFile(src, []byte("data"), 0o600))
324		mockTrashOS(t, func(ops *trashOSOps) {
325			ops.openDestination = func(string, int, os.FileMode) (io.WriteCloser, error) {
326				return nil, sentinel
327			}
328		})
329
330		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
331
332		require.ErrorIs(t, err, sentinel)
333	})
334
335	t.Run("copy", func(t *testing.T) {
336		src := filepath.Join(t.TempDir(), "file")
337		require.NoError(t, os.WriteFile(src, []byte("data"), 0o600))
338		mockTrashOS(t, func(ops *trashOSOps) {
339			ops.copy = func(io.Writer, io.Reader) (int64, error) { return 0, sentinel }
340		})
341
342		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
343
344		require.ErrorIs(t, err, sentinel)
345	})
346
347	t.Run("close destination", func(t *testing.T) {
348		src := filepath.Join(t.TempDir(), "file")
349		require.NoError(t, os.WriteFile(src, []byte("data"), 0o600))
350		mockTrashOS(t, func(ops *trashOSOps) {
351			ops.openDestination = func(string, int, os.FileMode) (io.WriteCloser, error) {
352				return &stubWriteCloser{closeErr: sentinel}, nil
353			}
354		})
355
356		err := copyRecursively(src, filepath.Join(t.TempDir(), "dst"))
357
358		require.ErrorIs(t, err, sentinel)
359	})
360}