-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk_test.go
173 lines (144 loc) · 3.58 KB
/
walk_test.go
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
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
//"io"
"iter"
"os"
"path/filepath"
"testing"
)
// test the walk iterator
func PrintAll[V any](seq iter.Seq[V]) {
for v := range seq {
fmt.Println(v)
}
}
func TestWalkDirsDFSIter(t *testing.T) {
// So the linux source tree had 4597 leaf directories.
//root := "github.com/torvalds/linux"
// We don't have linux all checked out everywhere,
// and it takes 3 seconds. Just scan locally.
root := "."
limit := 100000
di := NewDirIter()
k := 0
next, stop := iter.Pull2(di.DirsDepthFirstLeafOnly(root))
defer stop()
for {
dir, ok, valid := next()
_ = dir
if !valid {
//vv("not valid, breaking, ok = %v", ok)
break
}
if !ok {
break
}
k++
//fmt.Println(dir)
if k > limit {
//vv("break on limit")
break
}
}
vv("total leaf dir = %v", k)
}
func TestWalkDirs_FollowSymlinks(t *testing.T) {
root := "test_root"
os.RemoveAll(root) // cleanup any prior test output.
defer os.RemoveAll(root)
panicOn(os.MkdirAll(filepath.Join(root, "a/b/c/d"), 0700))
panicOn(os.MkdirAll(filepath.Join(root, "z"), 0700))
// two symlinks in a row
panicOn(os.Symlink("../a", filepath.Join(root, "z/symlink2")))
panicOn(os.Symlink("symlink2", filepath.Join(root, "z/symlink")))
fd, err := os.Create(filepath.Join(root, "a/b/c/d/file0.txt"))
panicOn(err)
fd.Close()
di := NewDirIter()
di.FollowSymlinks = true
next, stop := iter.Pull2(di.FilesOnly(filepath.Join(root, "z")))
defer stop()
seen := 0
paths := make(map[string]bool)
for {
path, ok, valid := next()
if !valid {
//vv("not valid, breaking, ok = %v", ok)
break
}
if !ok {
break
}
//vv("path = '%v'", path)
if fileExists(path) {
seen++
paths[path] = true
}
}
if want, got := 2, seen; want != got {
// there are 2 paths to file0.txt when resolving symlinks:
// test_root/z/symlink/symlink2/a/b/c/d/file0.txt
// test_root/z/symlink2/a/b/c/d/file0.txt
//
// both of which resolve ultimately to:
//
// test_root/a/b/c/d/file0.txt
t.Fatalf("want %v, got %v seen files", want, got)
}
if len(paths) != 1 {
t.Fatalf("expected only 1 unique path in paths")
}
if !paths["test_root/a/b/c/d/file0.txt"] {
t.Fatalf("expected 'test_root/a/b/c/d/file0.txt' in paths")
}
}
func TestWalkDirs_MaxDepth(t *testing.T) {
// same test but if we cut off at max depth 4, we should get no files.
root := "test_root"
os.RemoveAll(root) // cleanup any prior test output.
defer os.RemoveAll(root)
panicOn(os.MkdirAll(filepath.Join(root, "a/b/c/d"), 0700))
panicOn(os.MkdirAll(filepath.Join(root, "z"), 0700))
// two symlinks in a row
panicOn(os.Symlink("../a", filepath.Join(root, "z/symlink2")))
panicOn(os.Symlink("symlink2", filepath.Join(root, "z/symlink")))
fd, err := os.Create(filepath.Join(root, "a/b/c/d/file0.txt"))
panicOn(err)
fd.Close()
di := NewDirIter()
di.FollowSymlinks = true
di.MaxDepth = 4
next, stop := iter.Pull2(di.FilesOnly(filepath.Join(root, "z")))
defer stop()
seen := 0
paths := make(map[string]bool)
for {
path, ok, valid := next()
if !valid {
//vv("not valid, breaking, ok = %v", ok)
break
}
if !ok {
break
}
//vv("path = '%v'", path)
if fileExists(path) {
seen++
paths[path] = true
}
}
if want, got := 0, seen; want != got {
// there are 2 paths to file0.txt when resolving symlinks:
// test_root/z/symlink/symlink2/a/b/c/d/file0.txt
// test_root/z/symlink2/a/b/c/d/file0.txt
//
// both of which resolve ultimately to:
//
// test_root/a/b/c/d/file0.txt
t.Fatalf("want %v, got %v seen files", want, got)
}
if len(paths) != 0 {
t.Fatalf("expected only 0 unique path in paths")
}
}