| 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 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/spf13/afero" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
) |
| 25 |
|
| 26 |
func TestGlob(t *testing.T) { |
| 27 |
c := qt.New(t) |
| 28 |
|
| 29 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 30 |
|
| 31 |
create := func(filename string) { |
| 32 |
filename = filepath.FromSlash(filename) |
| 33 |
dir := filepath.Dir(filename) |
| 34 |
if dir != "." { |
| 35 |
err := fs.MkdirAll(dir, 0o777) |
| 36 |
c.Assert(err, qt.IsNil) |
| 37 |
} |
| 38 |
err := afero.WriteFile(fs, filename, []byte("content "+filename), 0o777) |
| 39 |
c.Assert(err, qt.IsNil) |
| 40 |
} |
| 41 |
|
| 42 |
collect := func(pattern string) []string { |
| 43 |
var paths []string |
| 44 |
h := func(fi FileMetaInfo) (bool, error) { |
| 45 |
p := fi.Meta().PathInfo.Path() |
| 46 |
paths = append(paths, p) |
| 47 |
return false, nil |
| 48 |
} |
| 49 |
err := Glob(fs, pattern, h) |
| 50 |
c.Assert(err, qt.IsNil) |
| 51 |
return paths |
| 52 |
} |
| 53 |
|
| 54 |
create("/root.json") |
| 55 |
create("/jsonfiles/d1.json") |
| 56 |
create("/jsonfiles/d2.json") |
| 57 |
create("/jsonfiles/sub/d3.json") |
| 58 |
create("/jsonfiles/d1.xml") |
| 59 |
create("/a/b/c/e/f.json") |
| 60 |
create("/UPPER/sub/style.css") |
| 61 |
create("/root/UPPER/sub/style.css") |
| 62 |
|
| 63 |
afero.Walk(fs, "/", func(path string, info os.FileInfo, err error) error { |
| 64 |
c.Assert(err, qt.IsNil) |
| 65 |
return nil |
| 66 |
}) |
| 67 |
|
| 68 |
c.Assert(collect(filepath.FromSlash("/jsonfiles/*.json")), qt.HasLen, 2) |
| 69 |
c.Assert(collect("/*.json"), qt.HasLen, 1) |
| 70 |
c.Assert(collect("**.json"), qt.HasLen, 5) |
| 71 |
c.Assert(collect("**"), qt.HasLen, 8) |
| 72 |
c.Assert(collect(""), qt.HasLen, 0) |
| 73 |
c.Assert(collect("jsonfiles/*.json"), qt.HasLen, 2) |
| 74 |
c.Assert(collect("*.json"), qt.HasLen, 1) |
| 75 |
c.Assert(collect("**.xml"), qt.HasLen, 1) |
| 76 |
|
| 77 |
c.Assert(collect("root/UPPER/sub/style.css"), qt.HasLen, 1) |
| 78 |
c.Assert(collect("UPPER/sub/style.css"), qt.HasLen, 1) |
| 79 |
} |