github.com/jmooring/hugo@v0.47.1/hugofs/rootmapping_fs_test.go (about) 1 // Copyright 2018 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 "io/ioutil" 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/spf13/afero" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestRootMappingFsRealName(t *testing.T) { 27 assert := require.New(t) 28 fs := afero.NewMemMapFs() 29 30 rfs, err := NewRootMappingFs(fs, "f1", "f1t", "f2", "f2t") 31 assert.NoError(err) 32 33 assert.Equal(filepath.FromSlash("f1t/foo/file.txt"), rfs.realName(filepath.Join("f1", "foo", "file.txt"))) 34 35 } 36 37 func TestRootMappingFsDirnames(t *testing.T) { 38 assert := require.New(t) 39 fs := afero.NewMemMapFs() 40 41 testfile := "myfile.txt" 42 assert.NoError(fs.Mkdir("f1t", 0755)) 43 assert.NoError(fs.Mkdir("f2t", 0755)) 44 assert.NoError(fs.Mkdir("f3t", 0755)) 45 assert.NoError(afero.WriteFile(fs, filepath.Join("f2t", testfile), []byte("some content"), 0755)) 46 47 rfs, err := NewRootMappingFs(fs, "bf1", "f1t", "cf2", "f2t", "af3", "f3t") 48 assert.NoError(err) 49 50 fif, err := rfs.Stat(filepath.Join("cf2", testfile)) 51 assert.NoError(err) 52 assert.Equal("myfile.txt", fif.Name()) 53 54 root, err := rfs.Open(filepathSeparator) 55 assert.NoError(err) 56 57 dirnames, err := root.Readdirnames(-1) 58 assert.NoError(err) 59 assert.Equal([]string{"bf1", "cf2", "af3"}, dirnames) 60 61 } 62 63 func TestRootMappingFsOs(t *testing.T) { 64 assert := require.New(t) 65 fs := afero.NewOsFs() 66 67 d, err := ioutil.TempDir("", "hugo-root-mapping") 68 assert.NoError(err) 69 defer func() { 70 os.RemoveAll(d) 71 }() 72 73 testfile := "myfile.txt" 74 assert.NoError(fs.Mkdir(filepath.Join(d, "f1t"), 0755)) 75 assert.NoError(fs.Mkdir(filepath.Join(d, "f2t"), 0755)) 76 assert.NoError(fs.Mkdir(filepath.Join(d, "f3t"), 0755)) 77 assert.NoError(afero.WriteFile(fs, filepath.Join(d, "f2t", testfile), []byte("some content"), 0755)) 78 79 rfs, err := NewRootMappingFs(fs, "bf1", filepath.Join(d, "f1t"), "cf2", filepath.Join(d, "f2t"), "af3", filepath.Join(d, "f3t")) 80 assert.NoError(err) 81 82 fif, err := rfs.Stat(filepath.Join("cf2", testfile)) 83 assert.NoError(err) 84 assert.Equal("myfile.txt", fif.Name()) 85 86 root, err := rfs.Open(filepathSeparator) 87 assert.NoError(err) 88 89 dirnames, err := root.Readdirnames(-1) 90 assert.NoError(err) 91 assert.Equal([]string{"bf1", "cf2", "af3"}, dirnames) 92 93 }