1 // Copyright 2020 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.
13 "golang.org/x/tools/internal/lsp/fake"
14 "golang.org/x/tools/internal/lsp/source"
15 "golang.org/x/tools/internal/span"
18 func TestCaseInsensitiveFilesystem(t *testing.T) {
19 base, err := ioutil.TempDir("", t.Name())
24 inner := filepath.Join(base, "a/B/c/DEFgh")
25 if err := os.MkdirAll(inner, 0777); err != nil {
28 file := filepath.Join(inner, "f.go")
29 if err := ioutil.WriteFile(file, []byte("hi"), 0777); err != nil {
32 if _, err := os.Stat(filepath.Join(inner, "F.go")); err != nil {
33 t.Skip("filesystem is case-sensitive")
41 {filepath.Join(inner, "F.go"), true},
42 {filepath.Join(base, "a/b/c/defgh/f.go"), true},
44 for _, tt := range tests {
45 err := checkPathCase(tt.path)
46 if err != nil != tt.err {
47 t.Errorf("checkPathCase(%q) = %v, wanted error: %v", tt.path, err, tt.err)
52 func TestFindWorkspaceRoot(t *testing.T) {
63 module d-goplsworkspace
69 dir, err := fake.Tempdir(workspace)
73 defer os.RemoveAll(dir)
79 {"", "", false}, // no module at root, and more than one nested module
82 {"b/c", "b/c", false},
85 {"d/e", "d/e", false},
91 for _, test := range tests {
92 ctx := context.Background()
93 rel := fake.RelativeTo(dir)
94 folderURI := span.URIFromPath(rel.AbsPath(test.folder))
95 excludeNothing := func(string) bool { return false }
96 got, err := findWorkspaceRoot(ctx, folderURI, &osFileSource{}, excludeNothing, test.experimental)
100 if gotf, wantf := filepath.Clean(got.Filename()), rel.AbsPath(test.want); gotf != wantf {
101 t.Errorf("findWorkspaceRoot(%q, %t) = %q, want %q", test.folder, test.experimental, gotf, wantf)
106 func TestInVendor(t *testing.T) {
107 for _, tt := range []struct {
112 path: "foo/vendor/x.go",
116 path: "foo/vendor/x/x.go",
124 if got := inVendor(span.URIFromPath(tt.path)); got != tt.inVendor {
125 t.Errorf("expected %s inVendor %v, got %v", tt.path, tt.inVendor, got)
130 func TestFilters(t *testing.T) {
137 included: []string{"x"},
140 filters: []string{"-"},
141 excluded: []string{"x", "x/a"},
144 filters: []string{"-x", "+y"},
145 included: []string{"y", "y/a", "z"},
146 excluded: []string{"x", "x/a"},
149 filters: []string{"-x", "+x/y", "-x/y/z"},
150 included: []string{"x/y", "x/y/a", "a"},
151 excluded: []string{"x", "x/a", "x/y/z/a"},
154 filters: []string{"+foobar", "-foo"},
155 included: []string{"foobar", "foobar/a"},
156 excluded: []string{"foo", "foo/a"},
160 for _, tt := range tests {
161 opts := &source.Options{}
162 opts.DirectoryFilters = tt.filters
163 for _, inc := range tt.included {
164 if pathExcludedByFilter(inc, opts) {
165 t.Errorf("filters %q excluded %v, wanted included", tt.filters, inc)
168 for _, exc := range tt.excluded {
169 if !pathExcludedByFilter(exc, opts) {
170 t.Errorf("filters %q included %v, wanted excluded", tt.filters, exc)