github.com/SuCicada/su-hugo@v1.0.0/hugofs/glob/filename_filter_test.go (about) 1 // Copyright 2021 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package glob 15 16 import ( 17 "path/filepath" 18 "strings" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func TestFilenameFilter(t *testing.T) { 25 c := qt.New(t) 26 27 excludeAlmostAllJSON, err := NewFilenameFilter([]string{"/a/b/c/foo.json"}, []string{"**.json"}) 28 c.Assert(err, qt.IsNil) 29 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/data/my.json"), false), qt.Equals, false) 30 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/a/b/c/foo.json"), false), qt.Equals, true) 31 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/a/b/c/foo.bar"), false), qt.Equals, false) 32 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/a/b/c"), true), qt.Equals, true) 33 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/a/b"), true), qt.Equals, true) 34 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/a"), true), qt.Equals, true) 35 c.Assert(excludeAlmostAllJSON.Match(filepath.FromSlash("/"), true), qt.Equals, true) 36 c.Assert(excludeAlmostAllJSON.Match("", true), qt.Equals, true) 37 38 excludeAllButFooJSON, err := NewFilenameFilter([]string{"/a/**/foo.json"}, []string{"**.json"}) 39 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/data/my.json"), false), qt.Equals, false) 40 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/c/d/e/foo.json"), false), qt.Equals, true) 41 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/c"), true), qt.Equals, true) 42 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/"), true), qt.Equals, true) 43 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/"), true), qt.Equals, true) 44 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/b"), true), qt.Equals, false) 45 46 excludeAllButFooJSONMixedCasePattern, err := NewFilenameFilter([]string{"/**/Foo.json"}, nil) 47 c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/foo.json"), false), qt.Equals, true) 48 c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/FOO.json"), false), qt.Equals, true) 49 50 c.Assert(err, qt.IsNil) 51 52 nopFilter, err := NewFilenameFilter(nil, nil) 53 c.Assert(err, qt.IsNil) 54 c.Assert(nopFilter.Match("ab.txt", false), qt.Equals, true) 55 56 includeOnlyFilter, err := NewFilenameFilter([]string{"**.json", "**.jpg"}, nil) 57 c.Assert(err, qt.IsNil) 58 c.Assert(includeOnlyFilter.Match("ab.json", false), qt.Equals, true) 59 c.Assert(includeOnlyFilter.Match("ab.jpg", false), qt.Equals, true) 60 c.Assert(includeOnlyFilter.Match("ab.gif", false), qt.Equals, false) 61 62 exlcudeOnlyFilter, err := NewFilenameFilter(nil, []string{"**.json", "**.jpg"}) 63 c.Assert(err, qt.IsNil) 64 c.Assert(exlcudeOnlyFilter.Match("ab.json", false), qt.Equals, false) 65 c.Assert(exlcudeOnlyFilter.Match("ab.jpg", false), qt.Equals, false) 66 c.Assert(exlcudeOnlyFilter.Match("ab.gif", false), qt.Equals, true) 67 68 var nilFilter *FilenameFilter 69 c.Assert(nilFilter.Match("ab.gif", false), qt.Equals, true) 70 71 funcFilter := NewFilenameFilterForInclusionFunc(func(s string) bool { return strings.HasSuffix(s, ".json") }) 72 c.Assert(funcFilter.Match("ab.json", false), qt.Equals, true) 73 c.Assert(funcFilter.Match("ab.bson", false), qt.Equals, false) 74 75 }