fork/gdu

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

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

master
2.9 KiB104 linesraw
1package tui
2
3import (
4	"bytes"
5	"testing"
6
7	"github.com/dundee/gdu/v5/internal/testapp"
8	"github.com/dundee/gdu/v5/pkg/analyze"
9)
10
11func newSymlinkTestFile() *analyze.File {
12	dir := &analyze.Dir{
13		File: &analyze.File{
14			Name:  "test_dir",
15			Usage: 100,
16		},
17	}
18	return &analyze.File{
19		Name:    "bin",
20		Parent:  dir,
21		Usage:   4,
22		Size:    7,
23		Flag:    '@',
24		Symlink: "usr/bin",
25	}
26}
27
28func TestFormatSymlinkRow(t *testing.T) {
29	simScreen := testapp.CreateSimScreen()
30	defer simScreen.Fini()
31
32	app := testapp.CreateMockedApp(true)
33	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false)
34	ui.SetShowSymlinkTarget(true)
35
36	symlinkRow := ui.formatFileRow(newSymlinkTestFile(), rowMaxima{usage: 100, size: 100}, false, false)
37
38	if !bytes.Contains([]byte(symlinkRow), []byte("[aqua::b]")) {
39		t.Error("symlink row should contain [aqua::b]")
40	}
41	if !bytes.Contains([]byte(symlinkRow), []byte("-> usr/bin")) {
42		t.Error("symlink row should contain '-> usr/bin'")
43	}
44}
45
46func TestFormatSymlinkRowDisabledByDefault(t *testing.T) {
47	simScreen := testapp.CreateSimScreen()
48	defer simScreen.Fini()
49
50	app := testapp.CreateMockedApp(true)
51	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false)
52
53	symlinkRow := ui.formatFileRow(newSymlinkTestFile(), rowMaxima{usage: 100, size: 100}, false, false)
54
55	if bytes.Contains([]byte(symlinkRow), []byte("-> usr/bin")) {
56		t.Error("symlink target should not be shown when the option is disabled")
57	}
58	if !bytes.Contains([]byte(symlinkRow), []byte("bin")) {
59		t.Error("symlink row should still show the name")
60	}
61}
62
63func TestSymlinkTargetInfoLine(t *testing.T) {
64	simScreen := testapp.CreateSimScreen()
65	defer simScreen.Fini()
66
67	app := testapp.CreateMockedApp(true)
68	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false)
69	ui.SetShowSymlinkTarget(true)
70
71	line := ui.symlinkTargetInfoLine(newSymlinkTestFile())
72	if !bytes.Contains([]byte(line), []byte("Target:")) {
73		t.Errorf("info line should contain 'Target:', got %q", line)
74	}
75	if !bytes.Contains([]byte(line), []byte("usr/bin")) {
76		t.Errorf("info line should contain 'usr/bin', got %q", line)
77	}
78}
79
80func TestSymlinkTargetInfoLineDisabledByDefault(t *testing.T) {
81	simScreen := testapp.CreateSimScreen()
82	defer simScreen.Fini()
83
84	app := testapp.CreateMockedApp(true)
85	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false)
86
87	if line := ui.symlinkTargetInfoLine(newSymlinkTestFile()); line != "" {
88		t.Errorf("info line should be empty when the option is disabled, got %q", line)
89	}
90}
91
92func TestSymlinkTargetInfoLineForNonSymlink(t *testing.T) {
93	simScreen := testapp.CreateSimScreen()
94	defer simScreen.Fini()
95
96	app := testapp.CreateMockedApp(true)
97	ui := CreateUI(app, simScreen, &bytes.Buffer{}, true, false, false, false)
98	ui.SetShowSymlinkTarget(true)
99
100	regular := &analyze.File{Name: "file", Size: 5}
101	if line := ui.symlinkTargetInfoLine(regular); line != "" {
102		t.Errorf("non-symlink should produce empty info line, got %q", line)
103	}
104}