filename_filter_test.go (3396B)
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 c.Assert(err, qt.IsNil) 46 47 nopFilter, err := NewFilenameFilter(nil, nil) 48 c.Assert(err, qt.IsNil) 49 c.Assert(nopFilter.Match("ab.txt", false), qt.Equals, true) 50 51 includeOnlyFilter, err := NewFilenameFilter([]string{"**.json", "**.jpg"}, nil) 52 c.Assert(err, qt.IsNil) 53 c.Assert(includeOnlyFilter.Match("ab.json", false), qt.Equals, true) 54 c.Assert(includeOnlyFilter.Match("ab.jpg", false), qt.Equals, true) 55 c.Assert(includeOnlyFilter.Match("ab.gif", false), qt.Equals, false) 56 57 exlcudeOnlyFilter, err := NewFilenameFilter(nil, []string{"**.json", "**.jpg"}) 58 c.Assert(err, qt.IsNil) 59 c.Assert(exlcudeOnlyFilter.Match("ab.json", false), qt.Equals, false) 60 c.Assert(exlcudeOnlyFilter.Match("ab.jpg", false), qt.Equals, false) 61 c.Assert(exlcudeOnlyFilter.Match("ab.gif", false), qt.Equals, true) 62 63 var nilFilter *FilenameFilter 64 c.Assert(nilFilter.Match("ab.gif", false), qt.Equals, true) 65 66 funcFilter := NewFilenameFilterForInclusionFunc(func(s string) bool { return strings.HasSuffix(s, ".json") }) 67 c.Assert(funcFilter.Match("ab.json", false), qt.Equals, true) 68 c.Assert(funcFilter.Match("ab.bson", false), qt.Equals, false) 69 70 }