git.greeks.studio/lethews/hugo@v0.47.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 "os" 18 "runtime" 19 "testing" 20 21 "github.com/gohugoio/hugo/helpers" 22 "github.com/gohugoio/hugo/hugofs" 23 24 "github.com/spf13/viper" 25 ) 26 27 func TestEmptySourceFilesystem(t *testing.T) { 28 ss := newTestSourceSpec() 29 src := ss.NewFilesystem("Empty") 30 if len(src.Files()) != 0 { 31 t.Errorf("new filesystem should contain 0 files.") 32 } 33 } 34 35 type TestPath struct { 36 filename string 37 logical string 38 content string 39 section string 40 dir string 41 } 42 43 func TestUnicodeNorm(t *testing.T) { 44 if runtime.GOOS != "darwin" { 45 // Normalization code is only for Mac OS, since it is not necessary for other OSes. 46 return 47 } 48 49 paths := []struct { 50 NFC string 51 NFD string 52 }{ 53 {NFC: "å", NFD: "\x61\xcc\x8a"}, 54 {NFC: "é", NFD: "\x65\xcc\x81"}, 55 } 56 57 ss := newTestSourceSpec() 58 var fi os.FileInfo 59 60 for _, path := range paths { 61 src := ss.NewFilesystem("base") 62 _ = src.add(path.NFD, fi) 63 f := src.Files()[0] 64 if f.BaseFileName() != path.NFC { 65 t.Fatalf("file name in NFD form should be normalized (%s)", path.NFC) 66 } 67 } 68 69 } 70 71 func newTestConfig() *viper.Viper { 72 v := viper.New() 73 v.Set("contentDir", "content") 74 v.Set("dataDir", "data") 75 v.Set("i18nDir", "i18n") 76 v.Set("layoutDir", "layouts") 77 v.Set("archetypeDir", "archetypes") 78 v.Set("resourceDir", "resources") 79 v.Set("publishDir", "public") 80 v.Set("assetDir", "assets") 81 return v 82 } 83 84 func newTestSourceSpec() *SourceSpec { 85 v := newTestConfig() 86 fs := hugofs.NewMem(v) 87 ps, err := helpers.NewPathSpec(fs, v) 88 if err != nil { 89 panic(err) 90 } 91 return NewSourceSpec(ps, fs.Source) 92 }