Debian Codegraph @

hugofs/hglob/filename_filter_test.go

raw
1 // Copyright 2025 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 hglob
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(err, qt.IsNil)
40 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/data/my.json"), false), qt.Equals, false)
41 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/c/d/e/foo.json"), false), qt.Equals, true)
42 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/c"), true), qt.Equals, true)
43 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/a/b/"), true), qt.Equals, true)
44 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/"), true), qt.Equals, true)
45 c.Assert(excludeAllButFooJSON.Match(filepath.FromSlash("/b"), true), qt.Equals, false)
46
47 excludeAllButFooJSONMixedCasePattern, err := NewFilenameFilter([]string{"/**/Foo.json"}, nil)
48 c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/foo.json"), false), qt.Equals, true)
49 c.Assert(excludeAllButFooJSONMixedCasePattern.Match(filepath.FromSlash("/a/b/c/d/e/FOO.json"), false), qt.Equals, true)
50
51 c.Assert(err, qt.IsNil)
52
53 nopFilter, err := NewFilenameFilter(nil, nil)
54 c.Assert(err, qt.IsNil)
55 c.Assert(nopFilter.Match("ab.txt", false), qt.Equals, true)
56
57 includeOnlyFilter, err := NewFilenameFilter([]string{"**.json", "**.jpg"}, nil)
58 c.Assert(err, qt.IsNil)
59 c.Assert(includeOnlyFilter.Match("ab.json", false), qt.Equals, true)
60 c.Assert(includeOnlyFilter.Match("ab.jpg", false), qt.Equals, true)
61 c.Assert(includeOnlyFilter.Match("ab.gif", false), qt.Equals, false)
62
63 excludeOnlyFilter, err := NewFilenameFilter(nil, []string{"**.json", "**.jpg"})
64 c.Assert(err, qt.IsNil)
65 c.Assert(excludeOnlyFilter.Match("ab.json", false), qt.Equals, false)
66 c.Assert(excludeOnlyFilter.Match("ab.jpg", false), qt.Equals, false)
67 c.Assert(excludeOnlyFilter.Match("ab.gif", false), qt.Equals, true)
68
69 var nilFilter *FilenameFilter
70 c.Assert(nilFilter.Match("ab.gif", false), qt.Equals, true)
71
72 funcFilter := NewFilenameFilterForInclusionFunc(func(s string) bool { return strings.HasSuffix(s, ".json") })
73 c.Assert(funcFilter.Match("ab.json", false), qt.Equals, true)
74 c.Assert(funcFilter.Match("ab.bson", false), qt.Equals, false)
75 }