fork/gdu

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

ShivamB25feat: customize JSON export attributes (#605)d062e88

subvolume
7.0 KiB310 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
53	assert.Equal(t, "test_dir", dir.Name)
54	assert.Equal(t, int64(7+4096*3), dir.Size)
55	assert.Equal(t, int64(5), dir.ItemCount)
56	assert.True(t, dir.IsDir())
57
58	// test dir tree
59	files := slices.Collect(dir.GetFiles(fs.SortByName, fs.SortAsc))
60	assert.Equal(t, "nested", files[0].GetName())
61
62	nested := files[0].(*StoredDir)
63	nestedFiles := slices.Collect(nested.GetFiles(fs.SortByName, fs.SortAsc))
64	assert.Equal(t, "subnested", nestedFiles[1].GetName())
65
66	// test file
67	assert.Equal(t, "file2", nestedFiles[0].GetName())
68	assert.Equal(t, int64(2), nestedFiles[0].GetSize())
69	assert.True(t, int64(4096) <= nestedFiles[0].GetUsage())
70
71	subnested := nestedFiles[1].(*StoredDir)
72	subnestedFiles := slices.Collect(subnested.GetFiles(fs.SortByName, fs.SortAsc))
73	assert.Equal(t, "file", subnestedFiles[0].GetName())
74	assert.Equal(t, int64(5), subnestedFiles[0].GetSize())
75}
76
77func TestRemoveStoredFile(t *testing.T) {
78	fin := testdir.CreateTestDir()
79	defer fin()
80
81	a := CreateStoredAnalyzer("/tmp/badger")
82	dir := a.AnalyzeDir(
83		"test_dir", func(_, _ string) bool { return false }, func(_ string) bool { return false },
84	).(*StoredDir)
85
86	a.GetDone().Wait()
87	a.ResetProgress()
88
89	dir.UpdateStats(make(fs.HardLinkedItems))
90
91	// test dir info
92	assert.Equal(t, "test_dir", dir.Name)
93	assert.Equal(t, int64(7+4096*3), dir.Size)
94	assert.Equal(t, int64(5), dir.ItemCount)
95	assert.True(t, dir.IsDir())
96
97	dirFiles := slices.Collect(dir.GetFiles(fs.SortByName, fs.SortAsc))
98	subdir := dirFiles[0].(*StoredDir)
99	subdirFiles := slices.Collect(subdir.GetFiles(fs.SortByName, fs.SortAsc))
100	subdir.RemoveFile(subdirFiles[0])
101
102	closeFn := DefaultStorage.Open()
103	defer closeFn()
104	stored, err := DefaultStorage.GetDirForPath("test_dir")
105	assert.NoError(t, err)
106
107	assert.Equal(t, int64(4), stored.GetItemCount())
108	assert.Equal(t, int64(5+4096*3), stored.GetSize())
109
110	storedFiles := slices.Collect(stored.GetFiles(fs.SortByName, fs.SortAsc))
111	storedNested := storedFiles[0].(*StoredDir)
112	storedNestedFiles := slices.Collect(storedNested.GetFiles(fs.SortByName, fs.SortAsc))
113	storedSubnested := storedNestedFiles[0].(*StoredDir)
114	storedSubnestedFiles := slices.Collect(storedSubnested.GetFiles(fs.SortByName, fs.SortAsc))
115	file := storedSubnestedFiles[0]
116	assert.Equal(t, false, file.IsDir())
117	assert.Equal(t, "file", file.GetName())
118	assert.Equal(t, "test_dir/nested/subnested", file.GetParent().GetPath())
119}
120
121func TestParentDirGetNamePanics(t *testing.T) {
122	defer func() {
123		if r := recover(); r != nil {
124			assert.Equal(t, "must not be called", r)
125		}
126	}()
127	dir := &ParentDir{}
128	dir.GetName()
129}
130
131func TestParentDirGetFlagPanics(t *testing.T) {
132	defer func() {
133		if r := recover(); r != nil {
134			assert.Equal(t, "must not be called", r)
135		}
136	}()
137	dir := &ParentDir{}
138	dir.GetFlag()
139}
140
141func TestParentDirIsDirPanics(t *testing.T) {
142	defer func() {
143		if r := recover(); r != nil {
144			assert.Equal(t, "must not be called", r)
145		}
146	}()
147	dir := &ParentDir{}
148	dir.IsDir()
149}
150
151func TestParentDirGetSizePanics(t *testing.T) {
152	defer func() {
153		if r := recover(); r != nil {
154			assert.Equal(t, "must not be called", r)
155		}
156	}()
157	dir := &ParentDir{}
158	dir.GetSize()
159}
160
161func TestParentDirGetTypePanics(t *testing.T) {
162	defer func() {
163		if r := recover(); r != nil {
164			assert.Equal(t, "must not be called", r)
165		}
166	}()
167	dir := &ParentDir{}
168	dir.GetType()
169}
170
171func TestParentDirGetUsagePanics(t *testing.T) {
172	defer func() {
173		if r := recover(); r != nil {
174			assert.Equal(t, "must not be called", r)
175		}
176	}()
177	dir := &ParentDir{}
178	dir.GetUsage()
179}
180
181func TestParentDirGetMtimePanics(t *testing.T) {
182	defer func() {
183		if r := recover(); r != nil {
184			assert.Equal(t, "must not be called", r)
185		}
186	}()
187	dir := &ParentDir{}
188	dir.GetMtime()
189}
190
191func TestParentDirGetItemCountPanics(t *testing.T) {
192	defer func() {
193		if r := recover(); r != nil {
194			assert.Equal(t, "must not be called", r)
195		}
196	}()
197	dir := &ParentDir{}
198	dir.GetItemCount()
199}
200
201func TestParentDirGetParentPanics(t *testing.T) {
202	defer func() {
203		if r := recover(); r != nil {
204			assert.Equal(t, "must not be called", r)
205		}
206	}()
207	dir := &ParentDir{}
208	dir.GetParent()
209}
210
211func TestParentDirSetParentPanics(t *testing.T) {
212	defer func() {
213		if r := recover(); r != nil {
214			assert.Equal(t, "must not be called", r)
215		}
216	}()
217	dir := &ParentDir{}
218	dir.SetParent(nil)
219}
220
221func TestParentDirGetMultiLinkedInodePanics(t *testing.T) {
222	defer func() {
223		if r := recover(); r != nil {
224			assert.Equal(t, "must not be called", r)
225		}
226	}()
227	dir := &ParentDir{}
228	dir.GetMultiLinkedInode()
229}
230
231func TestParentDirEncodeJSONPanics(t *testing.T) {
232	defer func() {
233		if r := recover(); r != nil {
234			assert.Equal(t, "must not be called", r)
235		}
236	}()
237	dir := &ParentDir{}
238	err := dir.EncodeJSON(nil, false, nil)
239	assert.NoError(t, err)
240}
241
242func TestParentDirUpdateStatsPanics(t *testing.T) {
243	defer func() {
244		if r := recover(); r != nil {
245			assert.Equal(t, "must not be called", r)
246		}
247	}()
248	dir := &ParentDir{}
249	dir.UpdateStats(nil)
250}
251
252func TestParentDirAddFilePanics(t *testing.T) {
253	defer func() {
254		if r := recover(); r != nil {
255			assert.Equal(t, "must not be called", r)
256		}
257	}()
258	dir := &ParentDir{}
259	dir.AddFile(nil)
260}
261
262func TestParentDirGetFilesPanics(t *testing.T) {
263	defer func() {
264		if r := recover(); r != nil {
265			assert.Equal(t, "must not be called", r)
266		}
267	}()
268	dir := &ParentDir{}
269	dir.GetFiles(fs.SortByName, fs.SortAsc)
270}
271
272func TestParentDirGetFilesLockedPanics(t *testing.T) {
273	defer func() {
274		if r := recover(); r != nil {
275			assert.Equal(t, "must not be called", r)
276		}
277	}()
278	dir := &ParentDir{}
279	dir.GetFilesLocked(fs.SortByName, fs.SortAsc)
280}
281
282func TestParentDirRLockPanics(t *testing.T) {
283	defer func() {
284		if r := recover(); r != nil {
285			assert.Equal(t, "must not be called", r)
286		}
287	}()
288	dir := &ParentDir{}
289	dir.RLock()
290}
291
292func TestParentDirRemoveFilePanics(t *testing.T) {
293	defer func() {
294		if r := recover(); r != nil {
295			assert.Equal(t, "must not be called", r)
296		}
297	}()
298	dir := &ParentDir{}
299	dir.RemoveFile(nil)
300}
301
302func TestParentDirGetItemStatsPanics(t *testing.T) {
303	defer func() {
304		if r := recover(); r != nil {
305			assert.Equal(t, "must not be called", r)
306		}
307	}()
308	dir := &ParentDir{}
309	dir.GetItemStats(nil, false)
310}