Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / internal / lsp / cache / view_test.go
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.
4 package cache
5
6 import (
7         "context"
8         "io/ioutil"
9         "os"
10         "path/filepath"
11         "testing"
12
13         "golang.org/x/tools/internal/lsp/fake"
14         "golang.org/x/tools/internal/span"
15 )
16
17 func TestCaseInsensitiveFilesystem(t *testing.T) {
18         base, err := ioutil.TempDir("", t.Name())
19         if err != nil {
20                 t.Fatal(err)
21         }
22
23         inner := filepath.Join(base, "a/B/c/DEFgh")
24         if err := os.MkdirAll(inner, 0777); err != nil {
25                 t.Fatal(err)
26         }
27         file := filepath.Join(inner, "f.go")
28         if err := ioutil.WriteFile(file, []byte("hi"), 0777); err != nil {
29                 t.Fatal(err)
30         }
31         if _, err := os.Stat(filepath.Join(inner, "F.go")); err != nil {
32                 t.Skip("filesystem is case-sensitive")
33         }
34
35         tests := []struct {
36                 path string
37                 err  bool
38         }{
39                 {file, false},
40                 {filepath.Join(inner, "F.go"), true},
41                 {filepath.Join(base, "a/b/c/defgh/f.go"), true},
42         }
43         for _, tt := range tests {
44                 err := checkPathCase(tt.path)
45                 if err != nil != tt.err {
46                         t.Errorf("checkPathCase(%q) = %v, wanted error: %v", tt.path, err, tt.err)
47                 }
48         }
49 }
50
51 func TestFindWorkspaceRoot(t *testing.T) {
52         workspace := `
53 -- a/go.mod --
54 module a
55 -- a/x/x.go
56 package x
57 -- b/go.mod --
58 module b
59 -- b/c/go.mod --
60 module bc
61 -- d/gopls.mod --
62 module d-goplsworkspace
63 -- d/e/go.mod
64 module de
65 `
66         dir, err := fake.Tempdir(workspace)
67         if err != nil {
68                 t.Fatal(err)
69         }
70         defer os.RemoveAll(dir)
71
72         tests := []struct {
73                 folder, want string
74         }{
75                 // no module at root.
76                 {"", ""},
77                 {"a", "a"},
78                 {"a/x", "a"},
79                 {"b/c", "b/c"},
80                 {"d", "d"},
81                 {"d/e", "d"},
82         }
83
84         for _, test := range tests {
85                 ctx := context.Background()
86                 rel := fake.RelativeTo(dir)
87                 folderURI := span.URIFromPath(rel.AbsPath(test.folder))
88                 got, err := findWorkspaceRoot(ctx, folderURI, osFileSource{})
89                 if err != nil {
90                         t.Fatal(err)
91                 }
92                 if rel.RelPath(got.Filename()) != test.want {
93                         t.Errorf("fileWorkspaceRoot(%q) = %q, want %q", test.folder, got, test.want)
94                 }
95         }
96 }
97
98 func TestInVendor(t *testing.T) {
99         for _, tt := range []struct {
100                 path     string
101                 inVendor bool
102         }{
103                 {
104                         path:     "foo/vendor/x.go",
105                         inVendor: false,
106                 },
107                 {
108                         path:     "foo/vendor/x/x.go",
109                         inVendor: true,
110                 },
111                 {
112                         path:     "foo/x.go",
113                         inVendor: false,
114                 },
115         } {
116                 if got := inVendor(span.URIFromPath(tt.path)); got != tt.inVendor {
117                         t.Errorf("expected %s inVendor %v, got %v", tt.path, tt.inVendor, got)
118                 }
119         }
120 }