github.com/dominikszabo/hugo-ds-clean@v0.47.1/create/content_test.go (about) 1 // Copyright 2016 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 create_test 15 16 import ( 17 "os" 18 "path/filepath" 19 "strings" 20 "testing" 21 22 "github.com/gohugoio/hugo/deps" 23 24 "github.com/gohugoio/hugo/hugolib" 25 26 "fmt" 27 28 "github.com/gohugoio/hugo/hugofs" 29 30 "github.com/gohugoio/hugo/create" 31 "github.com/gohugoio/hugo/helpers" 32 "github.com/spf13/afero" 33 "github.com/spf13/viper" 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestNewContent(t *testing.T) { 38 v := viper.New() 39 initViper(v) 40 41 cases := []struct { 42 kind string 43 path string 44 expected []string 45 }{ 46 {"post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}}, 47 {"post", "post/org-1.org", []string{`#+title: ORG-1`}}, 48 {"emptydate", "post/sample-ed.md", []string{`title = "Empty Date Arch title"`, `test = "test1"`}}, 49 {"stump", "stump/sample-2.md", []string{`title: "Sample 2"`}}, // no archetype file 50 {"", "sample-3.md", []string{`title: "Sample 3"`}}, // no archetype 51 {"product", "product/sample-4.md", []string{`title = "SAMPLE-4"`}}, // empty archetype front matter 52 {"shortcodes", "shortcodes/go.md", []string{ 53 `title = "GO"`, 54 "{{< myshortcode >}}", 55 "{{% myshortcode %}}", 56 "{{</* comment */>}}\n{{%/* comment */%}}"}}, // shortcodes 57 } 58 59 for _, c := range cases { 60 cfg, fs := newTestCfg() 61 require.NoError(t, initFs(fs)) 62 h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) 63 require.NoError(t, err) 64 65 siteFactory := func(filename string, siteUsed bool) (*hugolib.Site, error) { 66 return h.Sites[0], nil 67 } 68 69 require.NoError(t, create.NewContent(h.PathSpec, siteFactory, c.kind, c.path)) 70 71 fname := filepath.Join("content", filepath.FromSlash(c.path)) 72 content := readFileFromFs(t, fs.Source, fname) 73 for i, v := range c.expected { 74 found := strings.Contains(content, v) 75 if !found { 76 t.Fatalf("[%d] %q missing from output:\n%q", i, v, content) 77 } 78 } 79 } 80 } 81 82 func initViper(v *viper.Viper) { 83 v.Set("metaDataFormat", "toml") 84 v.Set("archetypeDir", "archetypes") 85 v.Set("contentDir", "content") 86 v.Set("themesDir", "themes") 87 v.Set("layoutDir", "layouts") 88 v.Set("i18nDir", "i18n") 89 v.Set("theme", "sample") 90 v.Set("archetypeDir", "archetypes") 91 v.Set("resourceDir", "resources") 92 v.Set("publishDir", "public") 93 } 94 95 func initFs(fs *hugofs.Fs) error { 96 perm := os.FileMode(0755) 97 var err error 98 99 // create directories 100 dirs := []string{ 101 "archetypes", 102 "content", 103 filepath.Join("themes", "sample", "archetypes"), 104 } 105 for _, dir := range dirs { 106 err = fs.Source.Mkdir(dir, perm) 107 if err != nil { 108 return err 109 } 110 } 111 112 // create files 113 for _, v := range []struct { 114 path string 115 content string 116 }{ 117 { 118 path: filepath.Join("archetypes", "post.md"), 119 content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"Post Arch title\"\ntest = \"test1\"\n+++\n", 120 }, 121 { 122 path: filepath.Join("archetypes", "post.org"), 123 content: "#+title: {{ .BaseFileName | upper }}", 124 }, 125 { 126 path: filepath.Join("archetypes", "product.md"), 127 content: `+++ 128 title = "{{ .BaseFileName | upper }}" 129 +++`, 130 }, 131 { 132 path: filepath.Join("archetypes", "emptydate.md"), 133 content: "+++\ndate =\"\"\ntitle = \"Empty Date Arch title\"\ntest = \"test1\"\n+++\n", 134 }, 135 // #3623x 136 { 137 path: filepath.Join("archetypes", "shortcodes.md"), 138 content: `+++ 139 title = "{{ .BaseFileName | upper }}" 140 +++ 141 142 {{< myshortcode >}} 143 144 Some text. 145 146 {{% myshortcode %}} 147 {{</* comment */>}} 148 {{%/* comment */%}} 149 150 151 `, 152 }, 153 } { 154 f, err := fs.Source.Create(v.path) 155 if err != nil { 156 return err 157 } 158 defer f.Close() 159 160 _, err = f.Write([]byte(v.content)) 161 if err != nil { 162 return err 163 } 164 } 165 166 return nil 167 } 168 169 // TODO(bep) extract common testing package with this and some others 170 func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string { 171 filename = filepath.FromSlash(filename) 172 b, err := afero.ReadFile(fs, filename) 173 if err != nil { 174 // Print some debug info 175 root := strings.Split(filename, helpers.FilePathSeparator)[0] 176 afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error { 177 if info != nil && !info.IsDir() { 178 fmt.Println(" ", path) 179 } 180 181 return nil 182 }) 183 t.Fatalf("Failed to read file: %s", err) 184 } 185 return string(b) 186 } 187 188 func newTestCfg() (*viper.Viper, *hugofs.Fs) { 189 190 v := viper.New() 191 v.Set("contentDir", "content") 192 v.Set("dataDir", "data") 193 v.Set("i18nDir", "i18n") 194 v.Set("layoutDir", "layouts") 195 v.Set("archetypeDir", "archetypes") 196 v.Set("assetDir", "assets") 197 198 fs := hugofs.NewMem(v) 199 200 v.SetFs(fs.Source) 201 202 initViper(v) 203 204 return v, fs 205 206 }