glob_test.go (1738B)
1 // Copyright 2019 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 hugofs 15 16 import ( 17 "path/filepath" 18 "testing" 19 20 "github.com/spf13/afero" 21 22 qt "github.com/frankban/quicktest" 23 ) 24 25 func TestGlob(t *testing.T) { 26 c := qt.New(t) 27 28 fs := NewBaseFileDecorator(afero.NewMemMapFs()) 29 30 create := func(filename string) { 31 err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte("content "+filename), 0777) 32 c.Assert(err, qt.IsNil) 33 } 34 35 collect := func(pattern string) []string { 36 var paths []string 37 h := func(fi FileMetaInfo) (bool, error) { 38 paths = append(paths, fi.Meta().Path) 39 return false, nil 40 } 41 err := Glob(fs, pattern, h) 42 c.Assert(err, qt.IsNil) 43 return paths 44 } 45 46 create("root.json") 47 create("jsonfiles/d1.json") 48 create("jsonfiles/d2.json") 49 create("jsonfiles/sub/d3.json") 50 create("jsonfiles/d1.xml") 51 create("a/b/c/e/f.json") 52 53 c.Assert(collect("**.json"), qt.HasLen, 5) 54 c.Assert(collect("**"), qt.HasLen, 6) 55 c.Assert(collect(""), qt.HasLen, 0) 56 c.Assert(collect("jsonfiles/*.json"), qt.HasLen, 2) 57 c.Assert(collect("*.json"), qt.HasLen, 1) 58 c.Assert(collect("**.xml"), qt.HasLen, 1) 59 c.Assert(collect(filepath.FromSlash("/jsonfiles/*.json")), qt.HasLen, 2) 60 }