fork/gdu

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

Daniel Mildefeat: allow scanning multiple directories (#653)34f3996

master
6.7 KiB223 linesraw
1package tui
2
3import (
4	"bytes"
5	"os"
6	"path/filepath"
7	"testing"
8
9	"github.com/dundee/gdu/v5/internal/testapp"
10	"github.com/dundee/gdu/v5/pkg/analyze"
11	"github.com/gdamore/tcell/v2"
12	"github.com/stretchr/testify/assert"
13	"github.com/stretchr/testify/require"
14)
15
16// createMultiTestDirs creates two sibling directories to be scanned together.
17func createMultiTestDirs(t *testing.T) (first, second string) {
18	t.Helper()
19
20	require.NoError(t, os.MkdirAll("multi_a/nested", os.ModePerm))
21	require.NoError(t, os.MkdirAll("multi_b/other", os.ModePerm))
22	require.NoError(t, os.WriteFile("multi_a/nested/file", []byte("hello"), 0o600))
23	require.NoError(t, os.WriteFile("multi_b/other/file2", []byte("go"), 0o600))
24
25	t.Cleanup(func() {
26		assert.NoError(t, os.RemoveAll("multi_a"))
27		assert.NoError(t, os.RemoveAll("multi_b"))
28	})
29
30	return "multi_a", "multi_b"
31}
32
33// createNestedTestDirs creates two directories that share a base name but live
34// under different parents.
35func createNestedTestDirs(t *testing.T) (first, second string) {
36	first = filepath.Join("outer_p", "data")
37	second = filepath.Join("outer_q", "data")
38
39	for _, dir := range []string{first, second} {
40		require.NoError(t, os.MkdirAll(dir, os.ModePerm))
41		require.NoError(t, os.WriteFile(filepath.Join(dir, "file"), []byte("hello"), 0o600))
42	}
43
44	t.Cleanup(func() {
45		assert.NoError(t, os.RemoveAll("outer_p"))
46		assert.NoError(t, os.RemoveAll("outer_q"))
47	})
48
49	return first, second
50}
51
52// analyzeMultiplePaths runs a multi-root scan to completion and flushes the
53// queued UI updates, as the TUI tests do for single-path scans.
54func analyzeMultiplePaths(t *testing.T, paths []string) *UI {
55	t.Helper()
56
57	simScreen := testapp.CreateSimScreen()
58	t.Cleanup(simScreen.Fini)
59
60	app := testapp.CreateMockedApp(true)
61	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false)
62	ui.done = make(chan struct{})
63
64	require.NoError(t, ui.AnalyzePaths(paths))
65
66	<-ui.done
67	for _, draw := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
68		draw()
69	}
70
71	return ui
72}
73
74func TestAnalyzePathsGroupsRootsUnderVirtualDir(t *testing.T) {
75	first, second := createMultiTestDirs(t)
76
77	ui := analyzeMultiplePaths(t, []string{first, second})
78
79	assert.True(t, analyze.IsVirtualRootDir(ui.currentDir))
80	assert.Equal(t, analyze.VirtualRootName, ui.topDirPath)
81	// no "/.." row is shown at the top, so the rows are exactly the two roots
82	assert.Equal(t, 2, ui.table.GetRowCount())
83
84	names := []string{ui.table.GetCell(0, 0).Text, ui.table.GetCell(1, 0).Text}
85	assert.Contains(t, names[0]+names[1], first)
86	assert.Contains(t, names[0]+names[1], second)
87}
88
89// Roots from different parents can share a base name, so rows under the virtual
90// top level dir are labelled with the absolute path instead.
91func TestRootsAreLabelledWithTheirAbsolutePath(t *testing.T) {
92	first, second := createNestedTestDirs(t)
93
94	ui := analyzeMultiplePaths(t, []string{first, second})
95
96	rows := ui.table.GetCell(0, 0).Text + "\n" + ui.table.GetCell(1, 0).Text
97	assert.Contains(t, rows, first)
98	assert.Contains(t, rows, second)
99	// the path is not given a second leading separator by the directory marker
100	assert.NotContains(t, rows, string(filepath.Separator)+first)
101}
102
103func TestRootsKeepBaseNamesBelowTheVirtualDir(t *testing.T) {
104	first, second := createMultiTestDirs(t)
105	ui := analyzeMultiplePaths(t, []string{first, second})
106
107	// descend into the first root; its children are ordinary rows again
108	ui.table.Select(0, 0)
109	ui.handleRight()
110
111	assert.False(t, analyze.IsVirtualRootDir(ui.currentDir))
112	assert.Contains(t, ui.table.GetCell(1, 0).Text, "/nested")
113}
114
115func TestFilteringAtVirtualRootMatchesThePathShown(t *testing.T) {
116	first, second := createNestedTestDirs(t)
117	ui := analyzeMultiplePaths(t, []string{first, second})
118
119	// "outer_p" appears only in the path, never in the base name
120	ui.filterValue = "outer_p"
121	ui.showDir()
122
123	assert.Equal(t, 1, ui.table.GetRowCount())
124	assert.Contains(t, ui.table.GetCell(0, 0).Text, first)
125}
126
127func TestAnalyzePathsKeepsRealPathsOfRoots(t *testing.T) {
128	first, second := createMultiTestDirs(t)
129
130	ui := analyzeMultiplePaths(t, []string{first, second})
131
132	paths := ui.scannedRootPaths()
133	assert.ElementsMatch(t, []string{first, second}, paths)
134}
135
136func TestAnalyzePathsSumsRootStats(t *testing.T) {
137	first, second := createMultiTestDirs(t)
138
139	ui := analyzeMultiplePaths(t, []string{first, second})
140
141	var rootSize int64
142	for item := range ui.currentDir.GetFiles(0, 0) {
143		rootSize += item.GetSize()
144	}
145	assert.Equal(t, rootSize, ui.currentDir.GetSize())
146}
147
148func TestAnalyzePathsWithSinglePathHasNoVirtualDir(t *testing.T) {
149	first, _ := createMultiTestDirs(t)
150
151	ui := analyzeMultiplePaths(t, []string{first})
152
153	assert.False(t, analyze.IsVirtualRootDir(ui.currentDir))
154	assert.Equal(t, first, ui.currentDir.GetName())
155}
156
157func TestEnteringAndLeavingARootUnderTheVirtualDir(t *testing.T) {
158	first, second := createMultiTestDirs(t)
159	ui := analyzeMultiplePaths(t, []string{first, second})
160
161	// enter the first root
162	ui.table.Select(0, 0)
163	enteredName := ui.currentDir.GetName()
164	ui.handleRight()
165	assert.NotEqual(t, enteredName, ui.currentDir.GetName())
166	assert.False(t, analyze.IsVirtualRootDir(ui.currentDir))
167
168	// and back out to the virtual root via the "/.." row
169	ui.handleLeft()
170	assert.True(t, analyze.IsVirtualRootDir(ui.currentDir))
171}
172
173func TestHandleLeftAtVirtualRootDoesNotBrowseParent(t *testing.T) {
174	first, second := createMultiTestDirs(t)
175	ui := analyzeMultiplePaths(t, []string{first, second})
176	ui.SetBrowseParentDirs()
177
178	ui.handleLeft()
179
180	// the virtual root has no parent on disk, so nothing may be rescanned
181	assert.True(t, analyze.IsVirtualRootDir(ui.currentDir))
182	assert.Equal(t, analyze.VirtualRootName, ui.topDirPath)
183}
184
185func TestRescanAtVirtualRootRescansAllRoots(t *testing.T) {
186	first, second := createMultiTestDirs(t)
187	ui := analyzeMultiplePaths(t, []string{first, second})
188
189	ui.done = make(chan struct{})
190	ui.rescanDir()
191	<-ui.done
192	for _, draw := range ui.app.(*testapp.MockedApp).GetUpdateDraws() {
193		draw()
194	}
195
196	assert.True(t, analyze.IsVirtualRootDir(ui.currentDir))
197	assert.ElementsMatch(t, []string{first, second}, ui.scannedRootPaths())
198}
199
200func TestExportAtVirtualRootIsRejected(t *testing.T) {
201	first, second := createMultiTestDirs(t)
202	ui := analyzeMultiplePaths(t, []string{first, second})
203
204	ui.exportAnalysis()
205
206	assert.Contains(t, ui.header.GetText(false), "Export is not supported")
207	assert.False(t, ui.pages.HasPage("exporting"))
208}
209
210func TestSpawnShellAtVirtualRootIsRejected(t *testing.T) {
211	first, second := createMultiTestDirs(t)
212	ui := analyzeMultiplePaths(t, []string{first, second})
213	called := false
214	ui.exec = func(argv0 string, argv, envv []string) error {
215		called = true
216		return nil
217	}
218
219	assert.Nil(t, ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'b', 0)))
220
221	assert.False(t, called)
222	assert.Contains(t, ui.header.GetText(false), "Shell cannot be spawned")
223}