| 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 |
"errors" |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/hugofs/hglob" |
| 24 |
"github.com/spf13/afero" |
| 25 |
|
| 26 |
qt "github.com/frankban/quicktest" |
| 27 |
) |
| 28 |
|
| 29 |
func TestFilenameFilterFs(t *testing.T) { |
| 30 |
c := qt.New(t) |
| 31 |
|
| 32 |
base := filepath.FromSlash("/mybase") |
| 33 |
|
| 34 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 35 |
|
| 36 |
for _, letter := range []string{"a", "b", "c"} { |
| 37 |
for i := 1; i <= 3; i++ { |
| 38 |
c.Assert(afero.WriteFile(fs, filepath.Join(base, letter, fmt.Sprintf("my%d.txt", i)), []byte("some text file for"+letter), 0o755), qt.IsNil) |
| 39 |
c.Assert(afero.WriteFile(fs, filepath.Join(base, letter, fmt.Sprintf("my%d.json", i)), []byte("some json file for"+letter), 0o755), qt.IsNil) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
fs = NewBasePathFs(fs, base) |
| 44 |
|
| 45 |
filter, err := hglob.NewFilenameFilter(nil, []string{"/b/**.txt"}) |
| 46 |
c.Assert(err, qt.IsNil) |
| 47 |
|
| 48 |
fs = newFilenameFilterFs(fs, base, filter) |
| 49 |
|
| 50 |
assertExists := func(filename string, shouldExist bool) { |
| 51 |
filename = filepath.Clean(filename) |
| 52 |
_, err1 := fs.Stat(filename) |
| 53 |
f, err2 := fs.Open(filename) |
| 54 |
if shouldExist { |
| 55 |
c.Assert(err1, qt.IsNil) |
| 56 |
c.Assert(err2, qt.IsNil) |
| 57 |
defer f.Close() |
| 58 |
|
| 59 |
} else { |
| 60 |
for _, err := range []error{err1, err2} { |
| 61 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 62 |
c.Assert(errors.Is(err, os.ErrNotExist), qt.IsTrue) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
assertExists("/a/my1.txt", true) |
| 68 |
assertExists("/b/my1.txt", false) |
| 69 |
|
| 70 |
dirB, err := fs.Open("/b") |
| 71 |
c.Assert(err, qt.IsNil) |
| 72 |
defer dirB.Close() |
| 73 |
dirBEntries, err := dirB.Readdirnames(-1) |
| 74 |
c.Assert(err, qt.IsNil) |
| 75 |
c.Assert(dirBEntries, qt.DeepEquals, []string{"my1.json", "my2.json", "my3.json"}) |
| 76 |
|
| 77 |
dirC, err := fs.Open("/c") |
| 78 |
c.Assert(err, qt.IsNil) |
| 79 |
defer dirC.Close() |
| 80 |
dirCEntries, err := dirC.Readdirnames(-1) |
| 81 |
c.Assert(err, qt.IsNil) |
| 82 |
c.Assert(dirCEntries, qt.DeepEquals, []string{"my1.json", "my1.txt", "my2.json", "my2.txt", "my3.json", "my3.txt"}) |
| 83 |
} |