GoPLS Viewer

Home|gopls/internal/gopathwalk/walk_test.go
1// Copyright 2018 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.
4
5package gopathwalk
6
7import (
8    "io/ioutil"
9    "log"
10    "os"
11    "path/filepath"
12    "reflect"
13    "runtime"
14    "strings"
15    "sync"
16    "testing"
17)
18
19func TestShouldTraverse(t *testing.T) {
20    switch runtime.GOOS {
21    case "windows""plan9":
22        t.Skipf("skipping symlink-requiring test on %s"runtime.GOOS)
23    }
24
25    direrr := ioutil.TempDir("""goimports-")
26    if err != nil {
27        t.Fatal(err)
28    }
29    defer os.RemoveAll(dir)
30
31    // Note: mapToDir prepends "src" to each element, since
32    // mapToDir was made for creating GOPATHs.
33    if err := mapToDir(dir, map[string]string{
34        "foo/foo2/file.txt":        "",
35        "foo/foo2/link-to-src":     "LINK:../../",
36        "foo/foo2/link-to-src-foo""LINK:../../foo",
37        "foo/foo2/link-to-dot":     "LINK:.",
38        "bar/bar2/file.txt":        "",
39        "bar/bar2/link-to-src-foo""LINK:../../foo",
40
41        "a/b/c""LINK:../../a/d",
42        "a/d/e""LINK:../../a/b",
43    }); err != nil {
44        t.Fatal(err)
45    }
46    tests := []struct {
47        dir  string
48        file string
49        want bool
50    }{
51        {
52            dir:  "src/foo/foo2",
53            file"link-to-src-foo",
54            wantfalse// loop
55        },
56        {
57            dir:  "src/foo/foo2",
58            file"link-to-src",
59            wantfalse// loop
60        },
61        {
62            dir:  "src/foo/foo2",
63            file"link-to-dot",
64            wantfalse// loop
65        },
66        {
67            dir:  "src/bar/bar2",
68            file"link-to-src-foo",
69            wanttrue// not a loop
70        },
71        {
72            dir:  "src/a/b/c",
73            file"e",
74            wantfalse// loop: "e" is the same as "b".
75        },
76    }
77    for itt := range tests {
78        fierr := os.Stat(filepath.Join(dirtt.dirtt.file))
79        if err != nil {
80            t.Errorf("%d. Stat = %v"ierr)
81            continue
82        }
83        var w walker
84        got := w.shouldTraverse(filepath.Join(dirtt.dirfi.Name()))
85        if got != tt.want {
86            t.Errorf("%d. shouldTraverse(%q, %q) = %v; want %v"itt.dirtt.filegottt.want)
87        }
88    }
89}
90
91// TestSkip tests that various goimports rules are followed in non-modules mode.
92func TestSkip(t *testing.T) {
93    direrr := ioutil.TempDir("""goimports-")
94    if err != nil {
95        t.Fatal(err)
96    }
97    defer os.RemoveAll(dir)
98
99    if err := mapToDir(dir, map[string]string{
100        "ignoreme/f.go":     "package ignoreme",     // ignored by .goimportsignore
101        "node_modules/f.go""package nodemodules;"// ignored by hardcoded node_modules filter
102        "v/f.go":            "package v;",           // ignored by hardcoded vgo cache rule
103        "mod/f.go":          "package mod;",         // ignored by hardcoded vgo cache rule
104        "shouldfind/f.go":   "package shouldfind;",  // not ignored
105
106        ".goimportsignore""ignoreme\n",
107    }); err != nil {
108        t.Fatal(err)
109    }
110
111    var found []string
112    var mu sync.Mutex
113    walkDir(Root{filepath.Join(dir"src"), RootGOPATH},
114        func(root Rootdir string) {
115            mu.Lock()
116            defer mu.Unlock()
117            found = append(founddir[len(root.Path)+1:])
118        }, func(root Rootdir stringbool {
119            return false
120        }, Options{ModulesEnabledfalseLogflog.Printf})
121    if want := []string{"shouldfind"}; !reflect.DeepEqual(foundwant) {
122        t.Errorf("expected to find only %v, got %v"wantfound)
123    }
124}
125
126// TestSkipFunction tests that scan successfully skips directories from user callback.
127func TestSkipFunction(t *testing.T) {
128    direrr := ioutil.TempDir("""goimports-")
129    if err != nil {
130        t.Fatal(err)
131    }
132    defer os.RemoveAll(dir)
133
134    if err := mapToDir(dir, map[string]string{
135        "ignoreme/f.go":           "package ignoreme",    // ignored by skip
136        "ignoreme/subignore/f.go""package subignore",   // also ignored by skip
137        "shouldfind/f.go":         "package shouldfind;"// not ignored
138    }); err != nil {
139        t.Fatal(err)
140    }
141
142    var found []string
143    var mu sync.Mutex
144    walkDir(Root{filepath.Join(dir"src"), RootGOPATH},
145        func(root Rootdir string) {
146            mu.Lock()
147            defer mu.Unlock()
148            found = append(founddir[len(root.Path)+1:])
149        }, func(root Rootdir stringbool {
150            return strings.HasSuffix(dir"ignoreme")
151        },
152        Options{ModulesEnabledfalse})
153    if want := []string{"shouldfind"}; !reflect.DeepEqual(foundwant) {
154        t.Errorf("expected to find only %v, got %v"wantfound)
155    }
156}
157
158func mapToDir(destDir stringfiles map[string]stringerror {
159    for pathcontents := range files {
160        file := filepath.Join(destDir"src"path)
161        if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
162            return err
163        }
164        var err error
165        if strings.HasPrefix(contents"LINK:") {
166            err = os.Symlink(strings.TrimPrefix(contents"LINK:"), file)
167        } else {
168            err = ioutil.WriteFile(file, []byte(contents), 0644)
169        }
170        if err != nil {
171            return err
172        }
173    }
174    return nil
175}
176
MembersX
mapToDir.RangeStmt_4157.contents
TestShouldTraverse.t
TestSkipFunction.dir
TestSkipFunction.want
mapToDir.RangeStmt_4157.BlockStmt.file
TestShouldTraverse.RangeStmt_1546.BlockStmt.fi
TestSkip.mu
TestSkip.want
TestSkipFunction
TestShouldTraverse.RangeStmt_1546.tt
TestShouldTraverse.RangeStmt_1546.BlockStmt.got
TestSkip.found
mapToDir.destDir
TestSkipFunction.found
reflect
testing
TestShouldTraverse.tests
TestShouldTraverse.RangeStmt_1546.BlockStmt.w
TestSkip
TestSkipFunction.mu
mapToDir.files
mapToDir.RangeStmt_4157.path
sync
TestShouldTraverse.err
TestShouldTraverse.RangeStmt_1546.i
TestSkip.t
TestSkipFunction.err
TestShouldTraverse
TestSkip.dir
mapToDir
mapToDir.RangeStmt_4157.BlockStmt.err
runtime
TestShouldTraverse.dir
TestShouldTraverse.RangeStmt_1546.BlockStmt.err
TestSkip.err
TestSkipFunction.t
Members
X