fork/gdu

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

Daniel Mildefeat: show bar for item count (#649)daaae96

master
5.8 KiB202 linesraw
1package tui
2
3import (
4	"bytes"
5	"strings"
6	"testing"
7
8	"github.com/dundee/gdu/v5/internal/testapp"
9	"github.com/dundee/gdu/v5/pkg/analyze"
10	"github.com/dundee/gdu/v5/pkg/fs"
11	"github.com/rivo/tview"
12	"github.com/stretchr/testify/assert"
13)
14
15func TestHelpMoveToTrash(t *testing.T) {
16	app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
17	defer simScreen.Fini()
18
19	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false)
20
21	helpText := ui.formatHelpTextFor()
22
23	assert.True(t, strings.Contains(helpText, "Move file or directory to trash"))
24	assert.False(t, strings.Contains(helpText, "Move file or directory to trash (disabled)"))
25}
26
27func TestHelpListsCopyPath(t *testing.T) {
28	app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
29	defer simScreen.Fini()
30
31	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false)
32
33	helpText := ui.formatHelpTextFor()
34
35	assert.True(t, strings.Contains(helpText, "Copy path of file or directory to clipboard"))
36}
37
38func TestHelpNoSpawnShell(t *testing.T) {
39	app, simScreen := testapp.CreateTestAppWithSimScreen(50, 50)
40	defer simScreen.Fini()
41
42	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, true, false, false)
43	ui.SetNoDelete()
44	ui.SetNoSpawnShell()
45	ui.SetNoViewFile()
46	ui.showHelp()
47
48	assert.True(t, ui.pages.HasPage("help"))
49
50	helpText := ui.formatHelpTextFor()
51
52	assert.True(t, strings.Contains(helpText, "Delete file or directory (disabled)"))
53	assert.True(t, strings.Contains(helpText, "Empty file or directory (disabled)"))
54	assert.True(t, strings.Contains(helpText, "Move file or directory to trash (disabled)"))
55	assert.True(t, strings.Contains(helpText, "Spawn shell in current directory (disabled)"))
56	assert.True(t, strings.Contains(helpText, "Open file or directory in external program (disabled)"))
57	assert.True(t, strings.Contains(helpText, "Show content of file (disabled)"))
58}
59
60func TestCollapsePathFlag(t *testing.T) {
61	app := testapp.CreateMockedApp(true)
62	simScreen := testapp.CreateSimScreen()
63	defer simScreen.Fini()
64	ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, false, false, false)
65
66	// Create a collapsible structure
67	deepestDir := &analyze.Dir{
68		File: &analyze.File{
69			Name:  "deepest",
70			Usage: 100,
71			Size:  100,
72		},
73		Files: []fs.Item{},
74	}
75	middleDir := &analyze.Dir{
76		File: &analyze.File{
77			Name:  "middle",
78			Usage: 100,
79			Size:  100,
80		},
81		Files: []fs.Item{deepestDir},
82	}
83	topDir := &analyze.Dir{
84		File: &analyze.File{
85			Name: "top",
86		},
87		Files: []fs.Item{middleDir},
88	}
89	deepestDir.SetParent(middleDir)
90	middleDir.SetParent(topDir)
91
92	ui.currentDir = topDir
93	ui.topDir = topDir
94	ui.topDirPath = "top"
95
96	// Default (flag false) -> Should NOT collapse
97	ui.showDir()
98	cell := ui.table.GetCell(0, 0)
99	assert.Contains(t, cell.Text, "middle")
100	assert.NotContains(t, cell.Text, "deepest")
101
102	// Enable flag -> Should collapse
103	ui.SetCollapsePath(true)
104	ui.showDir()
105	cell = ui.table.GetCell(0, 0)
106	assert.Contains(t, cell.Text, "middle/deepest")
107}
108
109// TestColumnsWidthMatchesRenderedRow pins ui.columnsWidth() to the width the
110// columns actually occupy on screen, for every combination of optional columns.
111// The "/.." row is padded with columnsWidth() spaces, so if the two drift apart
112// that row stops lining up with the listing below it.
113func TestColumnsWidthMatchesRenderedRow(t *testing.T) {
114	simScreen := testapp.CreateSimScreen()
115	defer simScreen.Fini()
116
117	app := testapp.CreateMockedApp(false)
118	ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false)
119
120	root := &analyze.Dir{
121		File:      &analyze.File{Name: "root", Usage: 1000, Size: 1000},
122		ItemCount: 1,
123		BasePath:  ".",
124	}
125	sub := &analyze.Dir{
126		File:      &analyze.File{Name: "subdir", Usage: 700, Size: 700, Parent: root, Flag: ' '},
127		ItemCount: 11,
128	}
129	root.Files = fs.Files{sub}
130
131	maxima := rowMaxima{usage: 1000, size: 1000, count: 10}
132
133	for _, tc := range []struct {
134		name                                               string
135		oldBar, percentage, count, countBar, mtime, marked bool
136	}{
137		{name: "defaults"},
138		{name: "old bar", oldBar: true},
139		{name: "percentage", percentage: true},
140		{name: "count", count: true},
141		{name: "count and bar", count: true, countBar: true},
142		{name: "count bar without count column", countBar: true},
143		{name: "count bar with old bar", count: true, countBar: true, oldBar: true},
144		{name: "mtime", mtime: true},
145		{name: "marked", marked: true},
146		{
147			name: "everything", oldBar: true, percentage: true,
148			count: true, countBar: true, mtime: true, marked: true,
149		},
150	} {
151		t.Run(tc.name, func(t *testing.T) {
152			ui.useOldSizeBar = tc.oldBar
153			ui.showBarPercentage = tc.percentage
154			ui.showItemCount = tc.count
155			ui.showItemCountBar = tc.countBar
156			ui.showMtime = tc.mtime
157			ui.markedRows = map[int]struct{}{}
158			if tc.marked {
159				ui.markedRows[0] = struct{}{}
160			}
161
162			rendered := ui.formatColumns(sub, maxima, false, false)
163
164			assert.Equal(t, ui.columnsWidth(), tview.TaggedStringWidth(rendered))
165		})
166	}
167}
168
169// TestParentRowIsPaddedToColumnsWidth checks the "/.." row is indented by
170// exactly the width of the columns it has none of.
171func TestParentRowIsPaddedToColumnsWidth(t *testing.T) {
172	simScreen := testapp.CreateSimScreen()
173	defer simScreen.Fini()
174
175	app := testapp.CreateMockedApp(false)
176	ui := CreateUI(app, simScreen, &bytes.Buffer{}, false, true, false, false)
177	ui.showItemCount = true
178	ui.showItemCountBar = true
179
180	root := &analyze.Dir{
181		File:      &analyze.File{Name: "root", Usage: 1000, Size: 1000},
182		ItemCount: 1,
183		BasePath:  ".",
184	}
185	root.Files = fs.Files{
186		&analyze.Dir{
187			File:      &analyze.File{Name: "subdir", Usage: 700, Size: 700, Parent: root, Flag: ' '},
188			ItemCount: 11,
189		},
190	}
191
192	ui.currentDir = root
193	ui.topDir = root
194	// differs from the current dir, so the "/.." row is rendered
195	ui.topDirPath = "/somewhere/else"
196
197	ui.showDir()
198
199	parentRow := ui.table.GetCell(0, 0).Text
200
201	assert.Equal(t, strings.Repeat(" ", ui.columnsWidth())+"[::b]/..", parentRow)
202}