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