hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

filename_filter_fs_test.go (2412B)

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