1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
21 "golang.org/x/tools/internal/fastwalk"
24 func formatFileModes(m map[string]os.FileMode) string {
27 keys = append(keys, k)
31 for _, k := range keys {
32 fmt.Fprintf(&buf, "%-20s: %v\n", k, m[k])
37 func testFastWalk(t *testing.T, files map[string]string, callback func(path string, typ os.FileMode) error, want map[string]os.FileMode) {
38 tempdir, err := ioutil.TempDir("", "test-fast-walk")
42 defer os.RemoveAll(tempdir)
44 symlinks := map[string]string{}
45 for path, contents := range files {
46 file := filepath.Join(tempdir, "/src", path)
47 if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
51 if strings.HasPrefix(contents, "LINK:") {
52 symlinks[file] = filepath.FromSlash(strings.TrimPrefix(contents, "LINK:"))
54 err = ioutil.WriteFile(file, []byte(contents), 0644)
61 // Create symlinks after all other files. Otherwise, directory symlinks on
62 // Windows are unusable (see https://golang.org/issue/39183).
63 for file, dst := range symlinks {
64 err = os.Symlink(dst, file)
66 if writeErr := ioutil.WriteFile(file, []byte(dst), 0644); writeErr == nil {
67 // Couldn't create symlink, but could write the file.
68 // Probably this filesystem doesn't support symlinks.
69 // (Perhaps we are on an older Windows and not running as administrator.)
70 t.Skipf("skipping because symlinks appear to be unsupported: %v", err)
75 got := map[string]os.FileMode{}
77 err = fastwalk.Walk(tempdir, func(path string, typ os.FileMode) error {
80 if !strings.HasPrefix(path, tempdir) {
81 t.Errorf("bogus prefix on %q, expect %q", path, tempdir)
83 key := filepath.ToSlash(strings.TrimPrefix(path, tempdir))
84 if old, dup := got[key]; dup {
85 t.Errorf("callback called twice for key %q: %v -> %v", key, old, typ)
88 return callback(path, typ)
92 t.Fatalf("callback returned: %v", err)
94 if !reflect.DeepEqual(got, want) {
95 t.Errorf("walk mismatch.\n got:\n%v\nwant:\n%v", formatFileModes(got), formatFileModes(want))
99 func TestFastWalk_Basic(t *testing.T) {
100 testFastWalk(t, map[string]string{
103 "skip/skip.go": "skip",
105 func(path string, typ os.FileMode) error {
108 map[string]os.FileMode{
111 "/src/bar": os.ModeDir,
112 "/src/bar/bar.go": 0,
113 "/src/foo": os.ModeDir,
114 "/src/foo/foo.go": 0,
115 "/src/skip": os.ModeDir,
116 "/src/skip/skip.go": 0,
120 func TestFastWalk_LongFileName(t *testing.T) {
121 longFileName := strings.Repeat("x", 255)
123 testFastWalk(t, map[string]string{
126 func(path string, typ os.FileMode) error {
129 map[string]os.FileMode{
132 "/src/" + longFileName: 0,
137 func TestFastWalk_Symlink(t *testing.T) {
138 testFastWalk(t, map[string]string{
140 "bar/bar.go": "LINK:../foo/foo.go",
141 "symdir": "LINK:foo",
142 "broken/broken.go": "LINK:../nonexistent",
144 func(path string, typ os.FileMode) error {
147 map[string]os.FileMode{
150 "/src/bar": os.ModeDir,
151 "/src/bar/bar.go": os.ModeSymlink,
152 "/src/foo": os.ModeDir,
153 "/src/foo/foo.go": 0,
154 "/src/symdir": os.ModeSymlink,
155 "/src/broken": os.ModeDir,
156 "/src/broken/broken.go": os.ModeSymlink,
160 func TestFastWalk_SkipDir(t *testing.T) {
161 testFastWalk(t, map[string]string{
164 "skip/skip.go": "skip",
166 func(path string, typ os.FileMode) error {
167 if typ == os.ModeDir && strings.HasSuffix(path, "skip") {
168 return filepath.SkipDir
172 map[string]os.FileMode{
175 "/src/bar": os.ModeDir,
176 "/src/bar/bar.go": 0,
177 "/src/foo": os.ModeDir,
178 "/src/foo/foo.go": 0,
179 "/src/skip": os.ModeDir,
183 func TestFastWalk_SkipFiles(t *testing.T) {
184 // Directory iteration order is undefined, so there's no way to know
185 // which file to expect until the walk happens. Rather than mess
186 // with the test infrastructure, just mutate want.
188 want := map[string]os.FileMode{
191 "/src/zzz": os.ModeDir,
195 testFastWalk(t, map[string]string{
196 "a_skipfiles.go": "a",
197 "b_skipfiles.go": "b",
200 func(path string, typ os.FileMode) error {
201 if strings.HasSuffix(path, "_skipfiles.go") {
204 want["/src/"+filepath.Base(path)] = 0
205 return fastwalk.ErrSkipFiles
211 t.Errorf("saw too many files: wanted 5, got %v (%v)", len(want), want)
215 func TestFastWalk_TraverseSymlink(t *testing.T) {
216 testFastWalk(t, map[string]string{
219 "skip/skip.go": "skip",
220 "symdir": "LINK:foo",
222 func(path string, typ os.FileMode) error {
223 if typ == os.ModeSymlink {
224 return fastwalk.ErrTraverseLink
228 map[string]os.FileMode{
231 "/src/bar": os.ModeDir,
232 "/src/bar/bar.go": 0,
233 "/src/foo": os.ModeDir,
234 "/src/foo/foo.go": 0,
235 "/src/skip": os.ModeDir,
236 "/src/skip/skip.go": 0,
237 "/src/symdir": os.ModeSymlink,
238 "/src/symdir/foo.go": 0,
242 var benchDir = flag.String("benchdir", runtime.GOROOT(), "The directory to scan for BenchmarkFastWalk")
244 func BenchmarkFastWalk(b *testing.B) {
246 for i := 0; i < b.N; i++ {
247 err := fastwalk.Walk(*benchDir, func(path string, typ os.FileMode) error { return nil })