| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package cache |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"golang.org/x/tools/gopls/internal/protocol" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCaseInsensitiveFilesystem(t *testing.T) { |
| 15 |
base := t.TempDir() |
| 16 |
|
| 17 |
inner := filepath.Join(base, "a/B/c/DEFgh") |
| 18 |
if err := os.MkdirAll(inner, 0777); err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
file := filepath.Join(inner, "f.go") |
| 22 |
if err := os.WriteFile(file, []byte("hi"), 0777); err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
if _, err := os.Stat(filepath.Join(inner, "F.go")); err != nil { |
| 26 |
t.Skip("filesystem is case-sensitive") |
| 27 |
} |
| 28 |
|
| 29 |
tests := []struct { |
| 30 |
path string |
| 31 |
err bool |
| 32 |
}{ |
| 33 |
{file, false}, |
| 34 |
{filepath.Join(inner, "F.go"), true}, |
| 35 |
{filepath.Join(base, "a/b/c/defgh/f.go"), true}, |
| 36 |
} |
| 37 |
for _, tt := range tests { |
| 38 |
err := checkPathValid(tt.path) |
| 39 |
if err != nil != tt.err { |
| 40 |
t.Errorf("checkPathValid(%q) = %v, wanted error: %v", tt.path, err, tt.err) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestInVendor(t *testing.T) { |
| 46 |
for _, tt := range []struct { |
| 47 |
path string |
| 48 |
inVendor bool |
| 49 |
}{ |
| 50 |
{"foo/vendor/x.go", false}, |
| 51 |
{"foo/vendor/x/x.go", true}, |
| 52 |
{"foo/x.go", false}, |
| 53 |
{"foo/vendor/foo.txt", false}, |
| 54 |
{"foo/vendor/modules.txt", false}, |
| 55 |
} { |
| 56 |
if got := inVendor(protocol.URIFromPath(tt.path)); got != tt.inVendor { |
| 57 |
t.Errorf("expected %s inVendor %v, got %v", tt.path, tt.inVendor, got) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestFilters(t *testing.T) { |
| 63 |
tests := []struct { |
| 64 |
filters []string |
| 65 |
included []string |
| 66 |
excluded []string |
| 67 |
}{ |
| 68 |
{ |
| 69 |
included: []string{"x"}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
filters: []string{"-"}, |
| 73 |
excluded: []string{"x", "x/a"}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
filters: []string{"-x", "+y"}, |
| 77 |
included: []string{"y", "y/a", "z"}, |
| 78 |
excluded: []string{"x", "x/a"}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
filters: []string{"-x", "+x/y", "-x/y/z"}, |
| 82 |
included: []string{"x/y", "x/y/a", "a"}, |
| 83 |
excluded: []string{"x", "x/a", "x/y/z/a"}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
filters: []string{"+foobar", "-foo"}, |
| 87 |
included: []string{"foobar", "foobar/a"}, |
| 88 |
excluded: []string{"foo", "foo/a"}, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tt := range tests { |
| 93 |
pathIncluded := PathIncludeFunc(tt.filters) |
| 94 |
for _, inc := range tt.included { |
| 95 |
if relPathExcludedByFilter(inc, pathIncluded) { |
| 96 |
t.Errorf("filters %q excluded %v, wanted included", tt.filters, inc) |
| 97 |
} |
| 98 |
} |
| 99 |
for _, exc := range tt.excluded { |
| 100 |
if !relPathExcludedByFilter(exc, pathIncluded) { |
| 101 |
t.Errorf("filters %q included %v, wanted excluded", tt.filters, exc) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestSuffixes(t *testing.T) { |
| 108 |
type file struct { |
| 109 |
path string |
| 110 |
want bool |
| 111 |
} |
| 112 |
type cases struct { |
| 113 |
option []string |
| 114 |
files []file |
| 115 |
} |
| 116 |
tests := []cases{ |
| 117 |
{[]string{"tmpl", "gotmpl"}, []file{ |
| 118 |
{"foo", false}, |
| 119 |
{"foo.tmpl", true}, |
| 120 |
{"foo.gotmpl", true}, |
| 121 |
{"tmpl", false}, |
| 122 |
{"tmpl.go", false}}, |
| 123 |
}, |
| 124 |
{[]string{"tmpl", "gotmpl", "html", "gohtml"}, []file{ |
| 125 |
{"foo.gotmpl", true}, |
| 126 |
{"foo.html", true}, |
| 127 |
{"foo.gohtml", true}, |
| 128 |
{"html", false}}, |
| 129 |
}, |
| 130 |
{[]string{"tmpl", "gotmpl", ""}, []file{ |
| 131 |
{"foo.gotmpl", true}, |
| 132 |
{"foo.go", false}, |
| 133 |
{"foo", false}}, |
| 134 |
}, |
| 135 |
} |
| 136 |
for _, a := range tests { |
| 137 |
suffixes := a.option |
| 138 |
for _, b := range a.files { |
| 139 |
got := fileHasExtension(b.path, suffixes) |
| 140 |
if got != b.want { |
| 141 |
t.Errorf("got %v, want %v, option %q, file %q (%+v)", |
| 142 |
got, b.want, a.option, b.path, b) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestIgnoreFilter(t *testing.T) { |
| 149 |
tests := []struct { |
| 150 |
dirs []string |
| 151 |
path string |
| 152 |
want bool |
| 153 |
}{ |
| 154 |
{[]string{"a"}, "a/testdata/foo", true}, |
| 155 |
{[]string{"a"}, "a/_ignore/foo", true}, |
| 156 |
{[]string{"a"}, "a/.ignore/foo", true}, |
| 157 |
{[]string{"a"}, "b/testdata/foo", false}, |
| 158 |
{[]string{"a"}, "testdata/foo", false}, |
| 159 |
{[]string{"a", "b"}, "b/testdata/foo", true}, |
| 160 |
{[]string{"a"}, "atestdata/foo", false}, |
| 161 |
} |
| 162 |
|
| 163 |
for _, test := range tests { |
| 164 |
|
| 165 |
for i, dir := range test.dirs { |
| 166 |
test.dirs[i] = filepath.FromSlash(dir) |
| 167 |
} |
| 168 |
test.path = filepath.FromSlash(test.path) |
| 169 |
|
| 170 |
f := newIgnoreFilter(test.dirs) |
| 171 |
if got := f.ignored(test.path); got != test.want { |
| 172 |
t.Errorf("newIgnoreFilter(%q).ignore(%q) = %t, want %t", test.dirs, test.path, got, test.want) |
| 173 |
} |
| 174 |
} |
| 175 |
} |