-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtree_test.go
More file actions
164 lines (139 loc) · 3.98 KB
/
tree_test.go
File metadata and controls
164 lines (139 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"os"
"path/filepath"
"testing"
)
func TestGetRelativePath(t *testing.T) {
testCases := []struct {
absolute string
root string
expected string
}{
{"/home/user/project/file.txt", "/home/user/", "project/file.txt"},
{"/home/user/project/", "/home/user/", "project"},
{"/home/user/project", "/home/user", "project"},
{"/other/path", "/home/user/", "/other/path"},
{"project/file.txt", "/home/user/", "project/file.txt"},
}
for _, tc := range testCases {
result := getRelativePath(tc.absolute, tc.root)
if result != tc.expected {
t.Errorf("For %s and %s, expected %s, but got %s", tc.absolute, tc.root, tc.expected, result)
}
}
}
func TestTreeNode(t *testing.T) {
// Create a test tree
root := &TreeNode{
Name: "root",
IsDir: true,
Depth: 0,
}
child1 := &TreeNode{
Name: "child1",
IsDir: true,
Depth: 1,
}
child2 := &TreeNode{
Name: "child2",
IsDir: false,
Depth: 1,
}
grandchild1 := &TreeNode{
Name: "grandchild1",
IsDir: false,
Depth: 2,
}
// Build tree structure
root.Children = append(root.Children, child1, child2)
child1.Children = append(child1.Children, grandchild1)
// Test node count
nodes := root.getAllNodes()
if len(nodes) != 4 {
t.Errorf("Expected 4 nodes, but got %d", len(nodes))
}
// Test specific node presence in result
found := false
for _, node := range nodes {
if node.Name == "grandchild1" {
found = true
break
}
}
if !found {
t.Error("Did not find grandchild1 in all nodes")
}
}
func TestGetTreeNode(t *testing.T) {
// Create test directory structure
testDir := "test_dir_structure"
defer os.RemoveAll(testDir)
// Create directory structure
os.MkdirAll(filepath.Join(testDir, "dir1", "subdir"), 0755)
os.MkdirAll(filepath.Join(testDir, "dir2"), 0755)
os.MkdirAll(filepath.Join(testDir, ".hidden_dir"), 0755)
// Create some files
os.WriteFile(filepath.Join(testDir, "file1.txt"), []byte("test"), 0644)
os.WriteFile(filepath.Join(testDir, "dir1", "file2.txt"), []byte("test"), 0644)
os.WriteFile(filepath.Join(testDir, ".hidden_file"), []byte("test"), 0644)
// Get current working directory as basePath
cwd, err := os.Getwd()
if err != nil {
t.Fatal("Failed to get current working directory")
}
// Test basic tree generation
filter := NewFilter("", false)
node, err := getTreeNode(testDir, 1, cwd, 0, filter, false, false)
if err != nil {
t.Fatalf("getTreeNode error: %v", err)
}
// Check root node
if node.Name != testDir || !node.IsDir {
t.Errorf("Root node error: name=%s, isDir=%v", node.Name, node.IsDir)
}
// Check child node count (should include 5: dir1, dir2, .hidden_dir, file1.txt, .hidden_file)
if len(node.Children) != 5 {
t.Errorf("Expected 5 child nodes, but got %d", len(node.Children))
}
// Test hidden file filtering
node, err = getTreeNode(testDir, 1, cwd, 0, filter, true, false)
if err != nil {
t.Fatalf("getTreeNode error: %v", err)
}
// Check child node count (should exclude .hidden_dir and .hidden_file)
if len(node.Children) != 3 {
t.Errorf("Expected 3 child nodes after filtering hidden files, but got %d", len(node.Children))
}
// Test maximum depth
node, err = getTreeNode(testDir, 1, cwd, 1, filter, false, false)
if err != nil {
t.Fatalf("getTreeNode error: %v", err)
}
// Find dir1 node
var dir1Node *TreeNode
for _, child := range node.Children {
if child.Name == "dir1" && child.IsDir {
dir1Node = child
break
}
}
if dir1Node == nil {
t.Fatal("Did not find dir1 node")
}
// Check if dir1 node has no children (due to depth limit)
if len(dir1Node.Children) != 0 {
t.Errorf("With depth limit 1, dir1 should have no children, but got %d", len(dir1Node.Children))
}
// Test only directories
node, err = getTreeNode(testDir, 1, cwd, 0, filter, false, true)
if err != nil {
t.Fatalf("getTreeNode error: %v", err)
}
// Check if only directories are included
for _, child := range node.Children {
if !child.IsDir {
t.Errorf("In dirs-only mode, node %s should not be a file", child.Name)
}
}
}