fork/gdu

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

Daniel Mildefix: StoreDir fixes (#644)35cba00

master
7.0 KiB311 linesraw
1package analyze
2
3import (
4	"bytes"
5	"encoding/gob"
6	"fmt"
7	"slices"
8	"testing"
9
10	"github.com/dundee/gdu/v5/internal/testdir"
11	"github.com/dundee/gdu/v5/pkg/fs"
12	"github.com/stretchr/testify/assert"
13)
14
15func TestEncDec(t *testing.T) {
16	var d fs.Item = &StoredDir{
17		Dir: &Dir{
18			File: &File{
19				Name: "xxx",
20			},
21			BasePath: "/yyy",
22		},
23	}
24
25	b := &bytes.Buffer{}
26	enc := gob.NewEncoder(b)
27	err := enc.Encode(d)
28	assert.NoError(t, err)
29
30	var x fs.Item = &StoredDir{}
31	dec := gob.NewDecoder(b)
32	err = dec.Decode(x)
33	assert.NoError(t, err)
34
35	fmt.Println(d, x)
36	assert.Equal(t, d.GetName(), x.GetName())
37}
38
39func TestStoredAnalyzer(t *testing.T) {
40	fin := testdir.CreateTestDir()
41	defer fin()
42
43	a := CreateStoredAnalyzer("/tmp/badger")
44	dir := a.AnalyzeDir(
45		"test_dir", func(_, _ string) bool { return false }, func(_ string) bool { return false },
46	).(*StoredDir)
47
48	a.GetDone().Wait()
49
50	dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems))
51
52	// test dir info; must match the parallel analyzer on the same fixture
53	// (see TestAnalyzeDir in dir_test.go)
54	assert.Equal(t, "test_dir", dir.Name)
55	assert.Equal(t, int64(7), dir.Size)
56	assert.Equal(t, int64(5), dir.ItemCount)
57	assert.True(t, dir.IsDir())
58
59	// test dir tree
60	files := slices.Collect(dir.GetFiles(fs.SortByName, fs.SortAsc))
61	assert.Equal(t, "nested", files[0].GetName())
62
63	nested := files[0].(*StoredDir)
64	nestedFiles := slices.Collect(nested.GetFiles(fs.SortByName, fs.SortAsc))
65	assert.Equal(t, "subnested", nestedFiles[1].GetName())
66
67	// test file
68	assert.Equal(t, "file2", nestedFiles[0].GetName())
69	assert.Equal(t, int64(2), nestedFiles[0].GetSize())
70	assert.True(t, int64(4096) <= nestedFiles[0].GetUsage())
71
72	subnested := nestedFiles[1].(*StoredDir)
73	subnestedFiles := slices.Collect(subnested.GetFiles(fs.SortByName, fs.SortAsc))
74	assert.Equal(t, "file", subnestedFiles[0].GetName())
75	assert.Equal(t, int64(5), subnestedFiles[0].GetSize())
76}
77
78func TestRemoveStoredFile(t *testing.T) {
79	fin := testdir.CreateTestDir()
80	defer fin()
81
82	a := CreateStoredAnalyzer("/tmp/badger")
83	dir := a.AnalyzeDir(
84		"test_dir", func(_, _ string) bool { return false }, func(_ string) bool { return false },
85	).(*StoredDir)
86
87	a.GetDone().Wait()
88	a.ResetProgress()
89
90	dir.UpdateStats(make(fs.HardLinkedItems))
91
92	// test dir info
93	assert.Equal(t, "test_dir", dir.Name)
94	assert.Equal(t, int64(7), dir.Size)
95	assert.Equal(t, int64(5), dir.ItemCount)
96	assert.True(t, dir.IsDir())
97
98	dirFiles := slices.Collect(dir.GetFiles(fs.SortByName, fs.SortAsc))
99	subdir := dirFiles[0].(*StoredDir)
100	subdirFiles := slices.Collect(subdir.GetFiles(fs.SortByName, fs.SortAsc))
101	subdir.RemoveFile(subdirFiles[0])
102
103	closeFn := DefaultStorage.Open()
104	defer closeFn()
105	stored, err := DefaultStorage.GetDirForPath("test_dir")
106	assert.NoError(t, err)
107
108	assert.Equal(t, int64(4), stored.GetItemCount())
109	assert.Equal(t, int64(5), stored.GetSize())
110
111	storedFiles := slices.Collect(stored.GetFiles(fs.SortByName, fs.SortAsc))
112	storedNested := storedFiles[0].(*StoredDir)
113	storedNestedFiles := slices.Collect(storedNested.GetFiles(fs.SortByName, fs.SortAsc))
114	storedSubnested := storedNestedFiles[0].(*StoredDir)
115	storedSubnestedFiles := slices.Collect(storedSubnested.GetFiles(fs.SortByName, fs.SortAsc))
116	file := storedSubnestedFiles[0]
117	assert.Equal(t, false, file.IsDir())
118	assert.Equal(t, "file", file.GetName())
119	assert.Equal(t, "test_dir/nested/subnested", file.GetParent().GetPath())
120}
121
122func TestParentDirGetNamePanics(t *testing.T) {
123	defer func() {
124		if r := recover(); r != nil {
125			assert.Equal(t, "must not be called", r)
126		}
127	}()
128	dir := &ParentDir{}
129	dir.GetName()
130}
131
132func TestParentDirGetFlagPanics(t *testing.T) {
133	defer func() {
134		if r := recover(); r != nil {
135			assert.Equal(t, "must not be called", r)
136		}
137	}()
138	dir := &ParentDir{}
139	dir.GetFlag()
140}
141
142func TestParentDirIsDirPanics(t *testing.T) {
143	defer func() {
144		if r := recover(); r != nil {
145			assert.Equal(t, "must not be called", r)
146		}
147	}()
148	dir := &ParentDir{}
149	dir.IsDir()
150}
151
152func TestParentDirGetSizePanics(t *testing.T) {
153	defer func() {
154		if r := recover(); r != nil {
155			assert.Equal(t, "must not be called", r)
156		}
157	}()
158	dir := &ParentDir{}
159	dir.GetSize()
160}
161
162func TestParentDirGetTypePanics(t *testing.T) {
163	defer func() {
164		if r := recover(); r != nil {
165			assert.Equal(t, "must not be called", r)
166		}
167	}()
168	dir := &ParentDir{}
169	dir.GetType()
170}
171
172func TestParentDirGetUsagePanics(t *testing.T) {
173	defer func() {
174		if r := recover(); r != nil {
175			assert.Equal(t, "must not be called", r)
176		}
177	}()
178	dir := &ParentDir{}
179	dir.GetUsage()
180}
181
182func TestParentDirGetMtimePanics(t *testing.T) {
183	defer func() {
184		if r := recover(); r != nil {
185			assert.Equal(t, "must not be called", r)
186		}
187	}()
188	dir := &ParentDir{}
189	dir.GetMtime()
190}
191
192func TestParentDirGetItemCountPanics(t *testing.T) {
193	defer func() {
194		if r := recover(); r != nil {
195			assert.Equal(t, "must not be called", r)
196		}
197	}()
198	dir := &ParentDir{}
199	dir.GetItemCount()
200}
201
202func TestParentDirGetParentPanics(t *testing.T) {
203	defer func() {
204		if r := recover(); r != nil {
205			assert.Equal(t, "must not be called", r)
206		}
207	}()
208	dir := &ParentDir{}
209	dir.GetParent()
210}
211
212func TestParentDirSetParentPanics(t *testing.T) {
213	defer func() {
214		if r := recover(); r != nil {
215			assert.Equal(t, "must not be called", r)
216		}
217	}()
218	dir := &ParentDir{}
219	dir.SetParent(nil)
220}
221
222func TestParentDirGetMultiLinkedInodePanics(t *testing.T) {
223	defer func() {
224		if r := recover(); r != nil {
225			assert.Equal(t, "must not be called", r)
226		}
227	}()
228	dir := &ParentDir{}
229	dir.GetMultiLinkedInode()
230}
231
232func TestParentDirEncodeJSONPanics(t *testing.T) {
233	defer func() {
234		if r := recover(); r != nil {
235			assert.Equal(t, "must not be called", r)
236		}
237	}()
238	dir := &ParentDir{}
239	err := dir.EncodeJSON(nil, false, nil)
240	assert.NoError(t, err)
241}
242
243func TestParentDirUpdateStatsPanics(t *testing.T) {
244	defer func() {
245		if r := recover(); r != nil {
246			assert.Equal(t, "must not be called", r)
247		}
248	}()
249	dir := &ParentDir{}
250	dir.UpdateStats(nil)
251}
252
253func TestParentDirAddFilePanics(t *testing.T) {
254	defer func() {
255		if r := recover(); r != nil {
256			assert.Equal(t, "must not be called", r)
257		}
258	}()
259	dir := &ParentDir{}
260	dir.AddFile(nil)
261}
262
263func TestParentDirGetFilesPanics(t *testing.T) {
264	defer func() {
265		if r := recover(); r != nil {
266			assert.Equal(t, "must not be called", r)
267		}
268	}()
269	dir := &ParentDir{}
270	dir.GetFiles(fs.SortByName, fs.SortAsc)
271}
272
273func TestParentDirGetFilesLockedPanics(t *testing.T) {
274	defer func() {
275		if r := recover(); r != nil {
276			assert.Equal(t, "must not be called", r)
277		}
278	}()
279	dir := &ParentDir{}
280	dir.GetFilesLocked(fs.SortByName, fs.SortAsc)
281}
282
283func TestParentDirRLockPanics(t *testing.T) {
284	defer func() {
285		if r := recover(); r != nil {
286			assert.Equal(t, "must not be called", r)
287		}
288	}()
289	dir := &ParentDir{}
290	dir.RLock()
291}
292
293func TestParentDirRemoveFilePanics(t *testing.T) {
294	defer func() {
295		if r := recover(); r != nil {
296			assert.Equal(t, "must not be called", r)
297		}
298	}()
299	dir := &ParentDir{}
300	dir.RemoveFile(nil)
301}
302
303func TestParentDirGetItemStatsPanics(t *testing.T) {
304	defer func() {
305		if r := recover(); r != nil {
306			assert.Equal(t, "must not be called", r)
307		}
308	}()
309	dir := &ParentDir{}
310	dir.GetItemStats(nil, false)
311}