github.com/gohugoio/hugo@v0.88.1/source/filesystem_test.go (about) 1 // Copyright 2015 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 source 15 16 import ( 17 "fmt" 18 "path/filepath" 19 "runtime" 20 "testing" 21 22 "github.com/gohugoio/hugo/config" 23 24 "github.com/gohugoio/hugo/modules" 25 26 "github.com/gohugoio/hugo/langs" 27 28 "github.com/spf13/afero" 29 30 qt "github.com/frankban/quicktest" 31 "github.com/gohugoio/hugo/helpers" 32 "github.com/gohugoio/hugo/hugofs" 33 ) 34 35 func TestEmptySourceFilesystem(t *testing.T) { 36 c := qt.New(t) 37 ss := newTestSourceSpec() 38 src := ss.NewFilesystem("") 39 files, err := src.Files() 40 c.Assert(err, qt.IsNil) 41 if len(files) != 0 { 42 t.Errorf("new filesystem should contain 0 files.") 43 } 44 } 45 46 func TestUnicodeNorm(t *testing.T) { 47 if runtime.GOOS != "darwin" { 48 // Normalization code is only for Mac OS, since it is not necessary for other OSes. 49 return 50 } 51 52 c := qt.New(t) 53 54 paths := []struct { 55 NFC string 56 NFD string 57 }{ 58 {NFC: "å", NFD: "\x61\xcc\x8a"}, 59 {NFC: "é", NFD: "\x65\xcc\x81"}, 60 } 61 62 ss := newTestSourceSpec() 63 fi := hugofs.NewFileMetaInfo(nil, hugofs.NewFileMeta()) 64 65 for i, path := range paths { 66 base := fmt.Sprintf("base%d", i) 67 c.Assert(afero.WriteFile(ss.Fs.Source, filepath.Join(base, path.NFD), []byte("some data"), 0777), qt.IsNil) 68 src := ss.NewFilesystem(base) 69 _ = src.add(path.NFD, fi) 70 files, err := src.Files() 71 c.Assert(err, qt.IsNil) 72 f := files[0] 73 if f.BaseFileName() != path.NFC { 74 t.Fatalf("file %q name in NFD form should be normalized (%s)", f.BaseFileName(), path.NFC) 75 } 76 } 77 } 78 79 func newTestConfig() config.Provider { 80 v := config.New() 81 v.Set("contentDir", "content") 82 v.Set("dataDir", "data") 83 v.Set("i18nDir", "i18n") 84 v.Set("layoutDir", "layouts") 85 v.Set("archetypeDir", "archetypes") 86 v.Set("resourceDir", "resources") 87 v.Set("publishDir", "public") 88 v.Set("assetDir", "assets") 89 _, err := langs.LoadLanguageSettings(v, nil) 90 if err != nil { 91 panic(err) 92 } 93 mod, err := modules.CreateProjectModule(v) 94 if err != nil { 95 panic(err) 96 } 97 v.Set("allModules", modules.Modules{mod}) 98 99 return v 100 } 101 102 func newTestSourceSpec() *SourceSpec { 103 v := newTestConfig() 104 fs := hugofs.NewFrom(hugofs.NewBaseFileDecorator(afero.NewMemMapFs()), v) 105 ps, err := helpers.NewPathSpec(fs, v, nil) 106 if err != nil { 107 panic(err) 108 } 109 return NewSourceSpec(ps, fs.Source) 110 }