mirror of
https://github.com/golang/go
synced 2024-11-05 16:16:11 -07:00
19e2aca3fd
Extract goimports' logic for walking Go source directories into a separate package, suitable for use in go/packages. No functional changes. Added a convenience feature to fastwalk, allowing the user to say that they're done with a directory and stop receiving callbacks for it. Testing is a little light; I expect goimports' tests to cover most everything we care about. Change-Id: If047ada4414f5f282637d11fd07e8342fadc9c33 Reviewed-on: https://go-review.googlesource.com/c/138877 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
136 lines
3.3 KiB
Go
136 lines
3.3 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gopathwalk
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestShouldTraverse(t *testing.T) {
|
|
switch runtime.GOOS {
|
|
case "windows", "plan9":
|
|
t.Skipf("skipping symlink-requiring test on %s", runtime.GOOS)
|
|
}
|
|
|
|
dir, err := ioutil.TempDir("", "goimports-")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
// Note: mapToDir prepends "src" to each element, since
|
|
// mapToDir was made for creating GOPATHs.
|
|
if err := mapToDir(dir, map[string]string{
|
|
"foo/foo2/file.txt": "",
|
|
"foo/foo2/link-to-src": "LINK:../../",
|
|
"foo/foo2/link-to-src-foo": "LINK:../../foo",
|
|
"foo/foo2/link-to-dot": "LINK:.",
|
|
"bar/bar2/file.txt": "",
|
|
"bar/bar2/link-to-src-foo": "LINK:../../foo",
|
|
|
|
"a/b/c": "LINK:../../a/d",
|
|
"a/d/e": "LINK:../../a/b",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := []struct {
|
|
dir string
|
|
file string
|
|
want bool
|
|
}{
|
|
{
|
|
dir: "src/foo/foo2",
|
|
file: "link-to-src-foo",
|
|
want: false, // loop
|
|
},
|
|
{
|
|
dir: "src/foo/foo2",
|
|
file: "link-to-src",
|
|
want: false, // loop
|
|
},
|
|
{
|
|
dir: "src/foo/foo2",
|
|
file: "link-to-dot",
|
|
want: false, // loop
|
|
},
|
|
{
|
|
dir: "src/bar/bar2",
|
|
file: "link-to-src-foo",
|
|
want: true, // not a loop
|
|
},
|
|
{
|
|
dir: "src/a/b/c",
|
|
file: "e",
|
|
want: false, // loop: "e" is the same as "b".
|
|
},
|
|
}
|
|
for i, tt := range tests {
|
|
fi, err := os.Stat(filepath.Join(dir, tt.dir, tt.file))
|
|
if err != nil {
|
|
t.Errorf("%d. Stat = %v", i, err)
|
|
continue
|
|
}
|
|
var w walker
|
|
got := w.shouldTraverse(filepath.Join(dir, tt.dir), fi)
|
|
if got != tt.want {
|
|
t.Errorf("%d. shouldTraverse(%q, %q) = %v; want %v", i, tt.dir, tt.file, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestSkip tests that various goimports rules are followed in non-modules mode.
|
|
func TestSkip(t *testing.T) {
|
|
dir, err := ioutil.TempDir("", "goimports-")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dir)
|
|
|
|
if err := mapToDir(dir, map[string]string{
|
|
"ignoreme/f.go": "package ignoreme", // ignored by .goimportsignore
|
|
"node_modules/f.go": "package nodemodules;", // ignored by hardcoded node_modules filter
|
|
"v/f.go": "package v;", // ignored by hardcoded vgo cache rule
|
|
"mod/f.go": "package mod;", // ignored by hardcoded vgo cache rule
|
|
"shouldfind/f.go": "package shouldfind;", // not ignored
|
|
|
|
".goimportsignore": "ignoreme\n",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var found []string
|
|
walkDir(filepath.Join(dir, "src"), func(srcDir string, dir string) {
|
|
found = append(found, dir[len(srcDir)+1:])
|
|
}, Options{ModulesEnabled: false})
|
|
if want := []string{"shouldfind"}; !reflect.DeepEqual(found, want) {
|
|
t.Errorf("expected to find only %v, got %v", want, found)
|
|
}
|
|
}
|
|
|
|
func mapToDir(destDir string, files map[string]string) error {
|
|
for path, contents := range files {
|
|
file := filepath.Join(destDir, "src", path)
|
|
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
|
|
return err
|
|
}
|
|
var err error
|
|
if strings.HasPrefix(contents, "LINK:") {
|
|
err = os.Symlink(strings.TrimPrefix(contents, "LINK:"), file)
|
|
} else {
|
|
err = ioutil.WriteFile(file, []byte(contents), 0644)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|