fork/gdu

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

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

master
2.3 KiB86 linesraw
1package webui
2
3import (
4	"path/filepath"
5	"testing"
6
7	"github.com/dundee/gdu/v5/pkg/analyze"
8	"github.com/dundee/gdu/v5/pkg/fs"
9)
10
11func TestAnalyzePathsGroupsRootsUnderVirtualDir(t *testing.T) {
12	ui := newTestUI()
13	first := makeTree(t)
14	second := makeTree(t)
15
16	if err := ui.AnalyzePaths([]string{first, second}); err != nil {
17		t.Fatalf("AnalyzePaths: %v", err)
18	}
19	waitDone(t, ui)
20
21	if !analyze.IsVirtualRootDir(ui.topDir) {
22		t.Fatalf("expected virtual root, got %T named %q", ui.topDir, ui.topDir.GetName())
23	}
24	if ui.topDirPath != analyze.VirtualRootName {
25		t.Errorf("topDirPath = %q, want %q", ui.topDirPath, analyze.VirtualRootName)
26	}
27
28	// both roots are reachable and keep their real paths
29	for _, path := range []string{first, second} {
30		if _, err := ui.findNode(path); err != nil {
31			t.Errorf("findNode(%q): %v", path, err)
32		}
33	}
34
35	// so is a directory inside one of them
36	if _, err := ui.findNode(filepath.Join(first, "sub")); err != nil {
37		t.Errorf("findNode(sub of first root): %v", err)
38	}
39
40	// and the virtual root itself, as reached from a breadcrumb
41	node, err := ui.findNode(analyze.VirtualRootName)
42	if err != nil {
43		t.Fatalf("findNode(virtual root): %v", err)
44	}
45	resp := buildNodeResponse(node, fs.SortBySize, fs.SortDesc)
46	if len(resp.Children) != 2 {
47		t.Fatalf("virtual root has %d children, want 2", len(resp.Children))
48	}
49
50	// roots are labelled by absolute path, as their base names can collide
51	for _, child := range resp.Children {
52		if child.Name != child.Path {
53			t.Errorf("root labelled %q, want its path %q", child.Name, child.Path)
54		}
55	}
56}
57
58func TestFindNodeRejectsPathOutsideEveryRoot(t *testing.T) {
59	ui := newTestUI()
60
61	if err := ui.AnalyzePaths([]string{makeTree(t), makeTree(t)}); err != nil {
62		t.Fatalf("AnalyzePaths: %v", err)
63	}
64	waitDone(t, ui)
65
66	if _, err := ui.findNode(filepath.Join(t.TempDir(), "elsewhere")); err == nil {
67		t.Error("expected an error for a path outside every scanned root")
68	}
69}
70
71func TestAnalyzePathsWithSinglePathHasNoVirtualDir(t *testing.T) {
72	ui := newTestUI()
73	root := makeTree(t)
74
75	if err := ui.AnalyzePaths([]string{root}); err != nil {
76		t.Fatalf("AnalyzePaths: %v", err)
77	}
78	waitDone(t, ui)
79
80	if analyze.IsVirtualRootDir(ui.topDir) {
81		t.Error("single path must not be wrapped in a virtual root")
82	}
83	if ui.topDirPath != root {
84		t.Errorf("topDirPath = %q, want %q", ui.topDirPath, root)
85	}
86}