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.
19 func TestShouldTraverse(t *testing.T) {
21 case "windows", "plan9":
22 t.Skipf("skipping symlink-requiring test on %s", runtime.GOOS)
25 dir, err := ioutil.TempDir("", "goimports-")
29 defer os.RemoveAll(dir)
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",
41 "a/b/c": "LINK:../../a/d",
42 "a/d/e": "LINK:../../a/b",
53 file: "link-to-src-foo",
68 file: "link-to-src-foo",
69 want: true, // not a loop
74 want: false, // loop: "e" is the same as "b".
77 for i, tt := range tests {
78 fi, err := os.Stat(filepath.Join(dir, tt.dir, tt.file))
80 t.Errorf("%d. Stat = %v", i, err)
84 got := w.shouldTraverse(filepath.Join(dir, tt.dir), fi)
86 t.Errorf("%d. shouldTraverse(%q, %q) = %v; want %v", i, tt.dir, tt.file, got, tt.want)
91 // TestSkip tests that various goimports rules are followed in non-modules mode.
92 func TestSkip(t *testing.T) {
93 dir, err := ioutil.TempDir("", "goimports-")
97 defer os.RemoveAll(dir)
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
106 ".goimportsignore": "ignoreme\n",
113 walkDir(Root{filepath.Join(dir, "src"), RootGOPATH},
114 func(root Root, dir string) {
117 found = append(found, dir[len(root.Path)+1:])
118 }, func(root Root, dir string) bool {
120 }, Options{ModulesEnabled: false, Logf: log.Printf})
121 if want := []string{"shouldfind"}; !reflect.DeepEqual(found, want) {
122 t.Errorf("expected to find only %v, got %v", want, found)
126 // TestSkipFunction tests that scan successfully skips directories from user callback.
127 func TestSkipFunction(t *testing.T) {
128 dir, err := ioutil.TempDir("", "goimports-")
132 defer os.RemoveAll(dir)
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
144 walkDir(Root{filepath.Join(dir, "src"), RootGOPATH},
145 func(root Root, dir string) {
148 found = append(found, dir[len(root.Path)+1:])
149 }, func(root Root, dir string) bool {
150 return strings.HasSuffix(dir, "ignoreme")
152 Options{ModulesEnabled: false})
153 if want := []string{"shouldfind"}; !reflect.DeepEqual(found, want) {
154 t.Errorf("expected to find only %v, got %v", want, found)
158 func mapToDir(destDir string, files map[string]string) error {
159 for path, contents := range files {
160 file := filepath.Join(destDir, "src", path)
161 if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
165 if strings.HasPrefix(contents, "LINK:") {
166 err = os.Symlink(strings.TrimPrefix(contents, "LINK:"), file)
168 err = ioutil.WriteFile(file, []byte(contents), 0644)