fork/gdu

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

Daniel Mildefix: StoreDir fixes (#644)35cba00

master
5.8 KiB182 linesraw
1package analyze
2
3import (
4	"os"
5	"path/filepath"
6	"testing"
7
8	"github.com/dundee/gdu/v5/internal/common"
9	"github.com/dundee/gdu/v5/pkg/fs"
10	"github.com/stretchr/testify/assert"
11)
12
13// itemStats is the reported accounting for one directory in a scanned tree.
14type itemStats struct {
15	size      int64
16	usage     int64
17	itemCount int64
18	flag      rune
19}
20
21// createEmptyDirTree builds a tree that exercises every branch of gdu's
22// empty-directory rule: a leaf empty dir, a dir holding only an empty dir, a
23// chain of them, and a dir with real content to keep the normal path covered.
24func createEmptyDirTree(t *testing.T) string {
25	t.Helper()
26
27	root := t.TempDir()
28	for _, dir := range []string{"empty", "onlyempty/sub", "deep/a/b/c", "withfile"} {
29		assert.NoError(t, os.MkdirAll(filepath.Join(root, dir), os.ModePerm))
30	}
31	assert.NoError(t, os.WriteFile(filepath.Join(root, "withfile", "f"), []byte("hello"), 0o600))
32
33	return root
34}
35
36// collectStats walks a scanned tree and records the stats of every directory,
37// keyed by path relative to the scan root.
38func collectStats(root fs.Item) map[string]itemStats {
39	stats := make(map[string]itemStats)
40	base := root.GetPath()
41
42	var walk func(item fs.Item)
43	walk = func(item fs.Item) {
44		key, err := filepath.Rel(base, item.GetPath())
45		if err != nil {
46			key = item.GetPath()
47		}
48		stats[key] = itemStats{
49			size:      item.GetSize(),
50			usage:     item.GetUsage(),
51			itemCount: item.GetItemCount(),
52			flag:      item.GetFlag(),
53		}
54		for child := range item.GetFiles(fs.SortByName, fs.SortAsc) {
55			if child.IsDir() {
56				walk(child)
57			}
58		}
59	}
60	walk(root)
61
62	return stats
63}
64
65// scanWith runs an unfiltered scan. The nil file type filter is significant:
66// per common.UI.CreateFileTypeFilter, nil is how "no filtering" is expressed,
67// and analyzers that finalize stats during the scan read it to pick between
68// UpdateStats and UpdateStatsWithFileFiltering accounting.
69func scanWith(t *testing.T, analyzer common.Analyzer, root string) map[string]itemStats {
70	t.Helper()
71
72	dir := analyzer.AnalyzeDir(root, func(_, _ string) bool { return false }, nil)
73	analyzer.GetDone().Wait()
74	dir.UpdateStats(make(fs.HardLinkedItems))
75
76	return collectStats(dir)
77}
78
79// scanFilteringAllFiles runs a scan whose type filter rejects every file, so
80// the whole tree becomes empty directories and the collapse branch of
81// resolveDirStats applies.
82func scanFilteringAllFiles(
83	t *testing.T, analyzer common.Analyzer, root string,
84) map[string]itemStats {
85	t.Helper()
86
87	dir := analyzer.AnalyzeDir(
88		root, func(_, _ string) bool { return false }, func(_ string) bool { return true },
89	)
90	analyzer.GetDone().Wait()
91	dir.UpdateStatsWithFileFiltering(make(fs.HardLinkedItems))
92
93	return collectStats(dir)
94}
95
96// TestEmptyDirStatsAcrossAnalyzers pins the empty-directory rule: a directory
97// with no counted children reports EmptyDirSize apparent size and zero disk
98// usage, and a tree of nothing but empty directories therefore uses no disk at
99// all. Every analyzer must agree, so the numbers gdu prints cannot depend on
100// whether --db was passed.
101func TestEmptyDirStatsAcrossAnalyzers(t *testing.T) {
102	root := createEmptyDirTree(t)
103
104	want := map[string]itemStats{
105		".":         {size: 1541, usage: 4096, itemCount: 10, flag: ' '},
106		"empty":     {size: EmptyDirSize, usage: 0, itemCount: 1, flag: 'e'},
107		"onlyempty": {size: EmptyDirSize, usage: 0, itemCount: 2, flag: ' '},
108		"onlyempty/sub": {
109			size: EmptyDirSize, usage: 0, itemCount: 1, flag: 'e',
110		},
111		"deep":       {size: EmptyDirSize, usage: 0, itemCount: 4, flag: ' '},
112		"deep/a":     {size: EmptyDirSize, usage: 0, itemCount: 3, flag: ' '},
113		"deep/a/b":   {size: EmptyDirSize, usage: 0, itemCount: 2, flag: ' '},
114		"deep/a/b/c": {size: EmptyDirSize, usage: 0, itemCount: 1, flag: 'e'},
115	}
116
117	parallel := scanWith(t, CreateAnalyzer(), root)
118
119	// The reference values above are asserted against the in-memory analyzer
120	// first, so a genuine change in the rule fails here rather than silently
121	// re-baselining every analyzer at once.
122	for path, expected := range want {
123		assert.Equal(t, expected, parallel[path], "parallel analyzer at %q", path)
124	}
125	assert.Equal(t, int64(0), parallel["empty"].usage, "an empty dir must use no disk")
126	assert.Equal(t, int64(0), parallel["deep"].usage, "a tree of empty dirs must use no disk")
127
128	// Under file filtering the whole tree collapses, because every directory
129	// ends up holding nothing but empty directories.
130	wantFiltered := scanFilteringAllFiles(t, CreateAnalyzer(), root)
131	assert.Equal(t, int64(1), wantFiltered["."].itemCount)
132	assert.Equal(t, int64(0), wantFiltered["."].usage)
133
134	for _, tc := range parityAnalyzers() {
135		t.Run(tc.name, func(t *testing.T) {
136			got := scanWith(t, tc.analyzer(t), root)
137			for path, expected := range want {
138				assert.Equal(t, expected, got[path], "%s analyzer at %q", tc.name, path)
139			}
140		})
141
142		t.Run(tc.name+" filtering files", func(t *testing.T) {
143			got := scanFilteringAllFiles(t, tc.analyzer(t), root)
144			for path, expected := range wantFiltered {
145				assert.Equal(t, expected, got[path], "%s analyzer at %q", tc.name, path)
146			}
147		})
148	}
149}
150
151type parityAnalyzer struct {
152	name     string
153	analyzer func(t *testing.T) common.Analyzer
154}
155
156func parityAnalyzers() []parityAnalyzer {
157	return []parityAnalyzer{
158		{
159			name:     "sequential",
160			analyzer: func(*testing.T) common.Analyzer { return CreateSeqAnalyzer() },
161		},
162		{
163			name:     "stable order",
164			analyzer: func(*testing.T) common.Analyzer { return CreateStableOrderAnalyzer() },
165		},
166		{
167			name: "stored (badger)",
168			analyzer: func(t *testing.T) common.Analyzer {
169				return CreateStoredAnalyzer(filepath.Join(t.TempDir(), "badger"))
170			},
171		},
172		{
173			name: "sqlite",
174			analyzer: func(t *testing.T) common.Analyzer {
175				analyzer, err := CreateSqliteAnalyzer(filepath.Join(t.TempDir(), "test.db"))
176				assert.NoError(t, err)
177				t.Cleanup(func() { _ = analyzer.storage.Close() })
178				return analyzer
179			},
180		},
181	}
182}