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.
14 "golang.org/x/mod/modfile"
15 "golang.org/x/tools/internal/lsp/fake"
16 "golang.org/x/tools/internal/lsp/protocol"
17 "golang.org/x/tools/internal/lsp/source"
18 "golang.org/x/tools/internal/span"
21 func TestCaseInsensitiveFilesystem(t *testing.T) {
22 base, err := ioutil.TempDir("", t.Name())
27 inner := filepath.Join(base, "a/B/c/DEFgh")
28 if err := os.MkdirAll(inner, 0777); err != nil {
31 file := filepath.Join(inner, "f.go")
32 if err := ioutil.WriteFile(file, []byte("hi"), 0777); err != nil {
35 if _, err := os.Stat(filepath.Join(inner, "F.go")); err != nil {
36 t.Skip("filesystem is case-sensitive")
44 {filepath.Join(inner, "F.go"), true},
45 {filepath.Join(base, "a/b/c/defgh/f.go"), true},
47 for _, tt := range tests {
48 err := checkPathCase(tt.path)
49 if err != nil != tt.err {
50 t.Errorf("checkPathCase(%q) = %v, wanted error: %v", tt.path, err, tt.err)
55 func TestFindWorkspaceRoot(t *testing.T) {
66 module d-goplsworkspace
72 dir, err := fake.Tempdir(workspace)
76 defer os.RemoveAll(dir)
82 {"", "", false}, // no module at root, and more than one nested module
85 {"b/c", "b/c", false},
88 {"d/e", "d/e", false},
94 for _, test := range tests {
95 ctx := context.Background()
96 rel := fake.RelativeTo(dir)
97 folderURI := span.URIFromPath(rel.AbsPath(test.folder))
98 excludeNothing := func(string) bool { return false }
99 got, err := findWorkspaceRoot(ctx, folderURI, &osFileSource{}, excludeNothing, test.experimental)
103 if gotf, wantf := filepath.Clean(got.Filename()), rel.AbsPath(test.want); gotf != wantf {
104 t.Errorf("findWorkspaceRoot(%q, %t) = %q, want %q", test.folder, test.experimental, gotf, wantf)
109 // This tests the logic used to extract positions from parse and other Go
111 func TestExtractPositionFromError(t *testing.T) {
124 dir, err := fake.Tempdir(workspace)
128 defer os.RemoveAll(dir)
132 wantRng protocol.Range
135 filename: "a/go.mod",
136 wantRng: protocol.Range{},
139 filename: "b/go.mod",
140 wantRng: protocol.Range{
141 Start: protocol.Position{Line: 2},
142 End: protocol.Position{Line: 2},
146 filename: "c/go.mod",
147 wantRng: protocol.Range{
148 Start: protocol.Position{Line: 2},
149 End: protocol.Position{Line: 2},
153 for _, test := range tests {
154 ctx := context.Background()
155 rel := fake.RelativeTo(dir)
156 uri := span.URIFromPath(rel.AbsPath(test.filename))
157 if source.DetectLanguage("", uri.Filename()) != source.Mod {
158 t.Fatalf("expected only go.mod files")
160 // Try directly parsing the given, invalid go.mod file. Then, extract a
161 // position from the error message.
162 src := &osFileSource{}
163 modFH, err := src.GetFile(ctx, uri)
167 content, err := modFH.Read()
171 _, parseErr := modfile.Parse(uri.Filename(), content, nil)
173 t.Fatalf("%s: expected an unparseable go.mod file", uri.Filename())
175 srcErr := extractErrorWithPosition(ctx, parseErr.Error(), src)
177 t.Fatalf("unable to extract positions from %v", parseErr.Error())
179 if srcErr.URI != uri {
180 t.Errorf("unexpected URI: got %s, wanted %s", srcErr.URI, uri)
182 if protocol.CompareRange(test.wantRng, srcErr.Range) != 0 {
183 t.Errorf("unexpected range: got %s, wanted %s", srcErr.Range, test.wantRng)
185 if !strings.HasSuffix(parseErr.Error(), srcErr.Message) {
186 t.Errorf("unexpected message: got %s, wanted %s", srcErr.Message, parseErr)
191 func TestInVendor(t *testing.T) {
192 for _, tt := range []struct {
197 path: "foo/vendor/x.go",
201 path: "foo/vendor/x/x.go",
209 if got := inVendor(span.URIFromPath(tt.path)); got != tt.inVendor {
210 t.Errorf("expected %s inVendor %v, got %v", tt.path, tt.inVendor, got)
215 func TestFilters(t *testing.T) {
222 included: []string{"x"},
225 filters: []string{"-"},
226 excluded: []string{"x", "x/a"},
229 filters: []string{"-x", "+y"},
230 included: []string{"y", "y/a", "z"},
231 excluded: []string{"x", "x/a"},
234 filters: []string{"-x", "+x/y", "-x/y/z"},
235 included: []string{"x/y", "x/y/a", "a"},
236 excluded: []string{"x", "x/a", "x/y/z/a"},
239 filters: []string{"+foobar", "-foo"},
240 included: []string{"foobar", "foobar/a"},
241 excluded: []string{"foo", "foo/a"},
245 for _, tt := range tests {
246 opts := &source.Options{}
247 opts.DirectoryFilters = tt.filters
248 for _, inc := range tt.included {
249 if pathExcludedByFilter(inc, opts) {
250 t.Errorf("filters %q excluded %v, wanted included", tt.filters, inc)
253 for _, exc := range tt.excluded {
254 if !pathExcludedByFilter(exc, opts) {
255 t.Errorf("filters %q included %v, wanted excluded", tt.filters, exc)