| 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 |
"fmt" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/config" |
| 24 |
"github.com/gohugoio/hugo/config/allconfig" |
| 25 |
"github.com/gohugoio/hugo/config/testconfig" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/deps" |
| 28 |
|
| 29 |
"github.com/gohugoio/hugo/hugolib" |
| 30 |
|
| 31 |
"github.com/gohugoio/hugo/hugofs" |
| 32 |
|
| 33 |
qt "github.com/frankban/quicktest" |
| 34 |
"github.com/gohugoio/hugo/create" |
| 35 |
"github.com/gohugoio/hugo/helpers" |
| 36 |
"github.com/spf13/afero" |
| 37 |
) |
| 38 |
|
| 39 |
// TODO(bep) clean this up. Export the test site builder in Hugolib or something. |
| 40 |
func TestNewContentFromFile(t *testing.T) { |
| 41 |
cases := []struct { |
| 42 |
name string |
| 43 |
kind string |
| 44 |
path string |
| 45 |
expected any |
| 46 |
}{ |
| 47 |
{"Post", "post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}}, |
| 48 |
{"Post org-mode", "post", "post/org-1.org", []string{`#+title: ORG-1`}}, |
| 49 |
{"Post, unknown content filetype", "post", "post/sample-1.pdoc", false}, |
| 50 |
{"Empty date", "emptydate", "post/sample-ed.md", []string{`title = "Empty Date Arch title"`, `test = "test1"`}}, |
| 51 |
{"Archetype file not found", "stump", "stump/sample-2.md", []string{`title: "Sample 2"`}}, // no archetype file |
| 52 |
{"No archetype", "", "sample-3.md", []string{`title: "Sample 3"`}}, // no archetype |
| 53 |
{"Empty archetype", "product", "product/sample-4.md", []string{`title = "SAMPLE-4"`}}, // empty archetype front matter |
| 54 |
{"Filenames", "filenames", "content/mypage/index.md", []string{"title = \"INDEX\"\n+++\n\n\nContentBaseName: mypage"}}, |
| 55 |
{"Branch Name", "name", "content/tags/tag-a/_index.md", []string{"+++\ntitle = 'Tag A'\n+++"}}, |
| 56 |
|
| 57 |
{"Lang 1", "lang", "post/lang-1.md", []string{`Site Lang: en|Name: Lang 1|i18n: Hugo Rocks!`}}, |
| 58 |
{"Lang 2", "lang", "post/lang-2.en.md", []string{`Site Lang: en|Name: Lang 2|i18n: Hugo Rocks!`}}, |
| 59 |
{"Lang nn file", "lang", "content/post/lang-3.nn.md", []string{`Site Lang: nn|Name: Lang 3|i18n: Hugo Rokkar!`}}, |
| 60 |
{"Lang nn dir", "lang", "content_nn/post/lang-4.md", []string{`Site Lang: nn|Name: Lang 4|i18n: Hugo Rokkar!`}}, |
| 61 |
{"Lang en in nn dir", "lang", "content_nn/post/lang-5.en.md", []string{`Site Lang: en|Name: Lang 5|i18n: Hugo Rocks!`}}, |
| 62 |
{"Lang en default", "lang", "post/my-bundle/index.md", []string{`Site Lang: en|Name: My Bundle|i18n: Hugo Rocks!`}}, |
| 63 |
{"Lang en file", "lang", "post/my-bundle/index.en.md", []string{`Site Lang: en|Name: My Bundle|i18n: Hugo Rocks!`}}, |
| 64 |
{"Lang nn bundle", "lang", "content/post/my-bundle/index.nn.md", []string{`Site Lang: nn|Name: My Bundle|i18n: Hugo Rokkar!`}}, |
| 65 |
{"Site", "site", "content/mypage/index.md", []string{"RegularPages .Site: 10", "RegularPages site: 10"}}, |
| 66 |
{"Shortcodes", "shortcodes", "shortcodes/go.md", []string{ |
| 67 |
`title = "GO"`, |
| 68 |
"{{< myshortcode >}}", |
| 69 |
"{{% myshortcode %}}", |
| 70 |
"{{</* comment */>}}\n{{%/* comment */%}}", |
| 71 |
}}, // shortcodes |
| 72 |
} |
| 73 |
|
| 74 |
c := qt.New(t) |
| 75 |
|
| 76 |
for i, cas := range cases { |
| 77 |
c.Run(cas.name, func(c *qt.C) { |
| 78 |
c.Parallel() |
| 79 |
|
| 80 |
mm := afero.NewMemMapFs() |
| 81 |
c.Assert(initFs(mm), qt.IsNil) |
| 82 |
cfg, fs := newTestCfg(c, mm) |
| 83 |
conf := testconfig.GetTestConfigs(fs.Source, cfg) |
| 84 |
h, err := hugolib.NewHugoSites(deps.DepsCfg{Configs: conf, Fs: fs}) |
| 85 |
c.Assert(err, qt.IsNil) |
| 86 |
err = create.NewContent(h, cas.kind, cas.path, false) |
| 87 |
|
| 88 |
if b, ok := cas.expected.(bool); ok && !b { |
| 89 |
if !b { |
| 90 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 91 |
} |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
c.Assert(err, qt.IsNil) |
| 96 |
|
| 97 |
fname := filepath.FromSlash(cas.path) |
| 98 |
if !strings.HasPrefix(fname, "content") { |
| 99 |
fname = filepath.Join("content", fname) |
| 100 |
} |
| 101 |
|
| 102 |
content := readFileFromFs(c, fs.Source, fname) |
| 103 |
|
| 104 |
for _, v := range cas.expected.([]string) { |
| 105 |
found := strings.Contains(content, v) |
| 106 |
if !found { |
| 107 |
c.Fatalf("[%d] %q missing from output:\n%q", i, v, content) |
| 108 |
} |
| 109 |
} |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestNewContentFromDirSiteFunction(t *testing.T) { |
| 115 |
mm := afero.NewMemMapFs() |
| 116 |
c := qt.New(t) |
| 117 |
|
| 118 |
archetypeDir := filepath.Join("archetypes", "my-bundle") |
| 119 |
defaultArchetypeDir := filepath.Join("archetypes", "default") |
| 120 |
c.Assert(mm.MkdirAll(archetypeDir, 0o755), qt.IsNil) |
| 121 |
c.Assert(mm.MkdirAll(defaultArchetypeDir, 0o755), qt.IsNil) |
| 122 |
|
| 123 |
contentFile := ` |
| 124 |
File: %s |
| 125 |
site RegularPages: {{ len site.RegularPages }} |
| 126 |
|
| 127 |
` |
| 128 |
|
| 129 |
c.Assert(afero.WriteFile(mm, filepath.Join(archetypeDir, "index.md"), fmt.Appendf(nil, contentFile, "index.md"), 0o755), qt.IsNil) |
| 130 |
c.Assert(afero.WriteFile(mm, filepath.Join(defaultArchetypeDir, "index.md"), []byte("default archetype index.md"), 0o755), qt.IsNil) |
| 131 |
|
| 132 |
c.Assert(initFs(mm), qt.IsNil) |
| 133 |
cfg, fs := newTestCfg(c, mm) |
| 134 |
|
| 135 |
conf := testconfig.GetTestConfigs(fs.Source, cfg) |
| 136 |
|
| 137 |
h := func() *hugolib.HugoSites { |
| 138 |
h, err := hugolib.NewHugoSites(deps.DepsCfg{Configs: conf, Fs: fs}) |
| 139 |
c.Assert(err, qt.IsNil) |
| 140 |
c.Assert(len(h.Sites), qt.Equals, 2) |
| 141 |
return h |
| 142 |
} |
| 143 |
|
| 144 |
c.Assert(create.NewContent(h(), "my-bundle", "post/my-post", false), qt.IsNil) |
| 145 |
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post/index.md")), `site RegularPages: 10`) |
| 146 |
|
| 147 |
// Default bundle archetype |
| 148 |
c.Assert(create.NewContent(h(), "", "post/my-post2", false), qt.IsNil) |
| 149 |
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/my-post2/index.md")), `default archetype index.md`) |
| 150 |
|
| 151 |
// Regular file with bundle kind. |
| 152 |
c.Assert(create.NewContent(h(), "my-bundle", "post/foo.md", false), qt.IsNil) |
| 153 |
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "post/foo.md")), `draft: true`) |
| 154 |
|
| 155 |
// Regular files should fall back to the default archetype (we have no regular file archetype). |
| 156 |
c.Assert(create.NewContent(h(), "my-bundle", "mypage.md", false), qt.IsNil) |
| 157 |
cContains(c, readFileFromFs(t, fs.Source, filepath.Join("content", "mypage.md")), `draft: true`) |
| 158 |
} |
| 159 |
|
| 160 |
// See issue 15078. |
| 161 |
func TestNewContentWithBuildCascade(t *testing.T) { |
| 162 |
t.Parallel() |
| 163 |
|
| 164 |
mm := afero.NewMemMapFs() |
| 165 |
c := qt.New(t) |
| 166 |
|
| 167 |
c.Assert(initFs(mm), qt.IsNil) |
| 168 |
c.Assert(mm.MkdirAll(filepath.Join("content", "posts", "drafts"), 0o755), qt.IsNil) |
| 169 |
c.Assert(afero.WriteFile(mm, filepath.Join("content", "posts", "drafts", "_index.md"), []byte(`--- |
| 170 |
title: Drafts |
| 171 |
build: |
| 172 |
render: never |
| 173 |
cascade: |
| 174 |
draft: true |
| 175 |
build: |
| 176 |
render: link |
| 177 |
draft: true |
| 178 |
--- |
| 179 |
`), 0o755), qt.IsNil) |
| 180 |
|
| 181 |
cfg, fs := newTestCfg(c, mm) |
| 182 |
conf := testconfig.GetTestConfigs(fs.Source, cfg) |
| 183 |
h, err := hugolib.NewHugoSites(deps.DepsCfg{Configs: conf, Fs: fs}) |
| 184 |
c.Assert(err, qt.IsNil) |
| 185 |
|
| 186 |
const target = "posts/drafts/trip-to-puebla/index.md" |
| 187 |
c.Assert(create.NewContent(h, "", target, false), qt.IsNil) |
| 188 |
c.Assert(readFileFromFs(c, fs.Source, filepath.Join("content", target)), qt.Contains, `title: "Trip to Puebla"`) |
| 189 |
} |
| 190 |
|
| 191 |
func initFs(fs afero.Fs) error { |
| 192 |
perm := os.FileMode(0o755) |
| 193 |
var err error |
| 194 |
|
| 195 |
// create directories |
| 196 |
dirs := []string{ |
| 197 |
"archetypes", |
| 198 |
"content", |
| 199 |
filepath.Join("themes", "sample", "archetypes"), |
| 200 |
} |
| 201 |
for _, dir := range dirs { |
| 202 |
err = fs.Mkdir(dir, perm) |
| 203 |
if err != nil && !os.IsExist(err) { |
| 204 |
return err |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// create some dummy content |
| 209 |
for i := 1; i <= 10; i++ { |
| 210 |
filename := filepath.Join("content", fmt.Sprintf("page%d.md", i)) |
| 211 |
afero.WriteFile(fs, filename, []byte(`--- |
| 212 |
title: Test |
| 213 |
--- |
| 214 |
`), 0o666) |
| 215 |
} |
| 216 |
|
| 217 |
// create archetype files |
| 218 |
for _, v := range []struct { |
| 219 |
path string |
| 220 |
content string |
| 221 |
}{ |
| 222 |
{ |
| 223 |
path: filepath.Join("archetypes", "post.md"), |
| 224 |
content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"Post Arch title\"\ntest = \"test1\"\n+++\n", |
| 225 |
}, |
| 226 |
{ |
| 227 |
path: filepath.Join("archetypes", "post.org"), |
| 228 |
content: "#+title: {{ .BaseFileName | upper }}", |
| 229 |
}, |
| 230 |
{ |
| 231 |
path: filepath.Join("archetypes", "name.md"), |
| 232 |
content: `+++ |
| 233 |
title = '{{ replace .Name "-" " " | title }}' |
| 234 |
+++`, |
| 235 |
}, |
| 236 |
{ |
| 237 |
path: filepath.Join("archetypes", "product.md"), |
| 238 |
content: `+++ |
| 239 |
title = "{{ .BaseFileName | upper }}" |
| 240 |
+++`, |
| 241 |
}, |
| 242 |
{ |
| 243 |
path: filepath.Join("archetypes", "filenames.md"), |
| 244 |
content: `... |
| 245 |
title = "{{ .BaseFileName | upper }}" |
| 246 |
+++ |
| 247 |
|
| 248 |
|
| 249 |
ContentBaseName: {{ .File.ContentBaseName }} |
| 250 |
|
| 251 |
`, |
| 252 |
}, |
| 253 |
{ |
| 254 |
path: filepath.Join("archetypes", "site.md"), |
| 255 |
content: `... |
| 256 |
title = "{{ .BaseFileName | upper }}" |
| 257 |
+++ |
| 258 |
|
| 259 |
Len RegularPages .Site: {{ len .Site.RegularPages }} |
| 260 |
Len RegularPages site: {{ len site.RegularPages }} |
| 261 |
|
| 262 |
|
| 263 |
`, |
| 264 |
}, |
| 265 |
{ |
| 266 |
path: filepath.Join("archetypes", "emptydate.md"), |
| 267 |
content: "+++\ndate =\"\"\ntitle = \"Empty Date Arch title\"\ntest = \"test1\"\n+++\n", |
| 268 |
}, |
| 269 |
{ |
| 270 |
path: filepath.Join("archetypes", "lang.md"), |
| 271 |
content: `Site Lang: {{ site.Language.Lang }}|Name: {{ replace .Name "-" " " | title }}|i18n: {{ T "hugo" }}`, |
| 272 |
}, |
| 273 |
// #3623x |
| 274 |
{ |
| 275 |
path: filepath.Join("archetypes", "shortcodes.md"), |
| 276 |
content: `+++ |
| 277 |
title = "{{ .BaseFileName | upper }}" |
| 278 |
+++ |
| 279 |
|
| 280 |
{{< myshortcode >}} |
| 281 |
|
| 282 |
Some text. |
| 283 |
|
| 284 |
{{% myshortcode %}} |
| 285 |
{{</* comment */>}} |
| 286 |
{{%/* comment */%}} |
| 287 |
|
| 288 |
|
| 289 |
`, |
| 290 |
}, |
| 291 |
} { |
| 292 |
f, err := fs.Create(v.path) |
| 293 |
if err != nil { |
| 294 |
return err |
| 295 |
} |
| 296 |
defer f.Close() |
| 297 |
|
| 298 |
_, err = f.Write([]byte(v.content)) |
| 299 |
if err != nil { |
| 300 |
return err |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return nil |
| 305 |
} |
| 306 |
|
| 307 |
func cContains(c *qt.C, v any, matches ...string) { |
| 308 |
for _, m := range matches { |
| 309 |
c.Assert(v, qt.Contains, m) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// TODO(bep) extract common testing package with this and some others |
| 314 |
func readFileFromFs(t testing.TB, fs afero.Fs, filename string) string { |
| 315 |
t.Helper() |
| 316 |
filename = filepath.FromSlash(filename) |
| 317 |
b, err := afero.ReadFile(fs, filename) |
| 318 |
if err != nil { |
| 319 |
// Print some debug info |
| 320 |
root, _, _ := strings.Cut(filename, helpers.FilePathSeparator) |
| 321 |
afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error { |
| 322 |
if info != nil && !info.IsDir() { |
| 323 |
fmt.Println(" ", path) |
| 324 |
} |
| 325 |
|
| 326 |
return nil |
| 327 |
}) |
| 328 |
t.Fatalf("Failed to read file: %s", err) |
| 329 |
} |
| 330 |
return string(b) |
| 331 |
} |
| 332 |
|
| 333 |
func newTestCfg(c *qt.C, mm afero.Fs) (config.Provider, *hugofs.Fs) { |
| 334 |
cfg := ` |
| 335 |
|
| 336 |
theme = "mytheme" |
| 337 |
[security] |
| 338 |
allowContent = ['.*'] |
| 339 |
[languages] |
| 340 |
[languages.en] |
| 341 |
weight = 1 |
| 342 |
label = "English" |
| 343 |
[languages.nn] |
| 344 |
weight = 2 |
| 345 |
label = "Nynorsk" |
| 346 |
|
| 347 |
[module] |
| 348 |
[[module.mounts]] |
| 349 |
source = 'archetypes' |
| 350 |
target = 'archetypes' |
| 351 |
[[module.mounts]] |
| 352 |
source = 'content' |
| 353 |
target = 'content' |
| 354 |
[module.mounts.sites.matrix] |
| 355 |
languages = 'en' |
| 356 |
[[module.mounts]] |
| 357 |
source = 'content_nn' |
| 358 |
target = 'content' |
| 359 |
[module.mounts.sites.matrix] |
| 360 |
languages = 'nn' |
| 361 |
` |
| 362 |
if mm == nil { |
| 363 |
mm = afero.NewMemMapFs() |
| 364 |
} |
| 365 |
|
| 366 |
mm.MkdirAll(filepath.FromSlash("content_nn"), 0o777) |
| 367 |
|
| 368 |
mm.MkdirAll(filepath.FromSlash("themes/mytheme"), 0o777) |
| 369 |
|
| 370 |
c.Assert(afero.WriteFile(mm, filepath.Join("i18n", "en.toml"), []byte(`[hugo] |
| 371 |
other = "Hugo Rocks!"`), 0o755), qt.IsNil) |
| 372 |
c.Assert(afero.WriteFile(mm, filepath.Join("i18n", "nn.toml"), []byte(`[hugo] |
| 373 |
other = "Hugo Rokkar!"`), 0o755), qt.IsNil) |
| 374 |
|
| 375 |
c.Assert(afero.WriteFile(mm, "config.toml", []byte(cfg), 0o755), qt.IsNil) |
| 376 |
|
| 377 |
res, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: mm, Filename: "config.toml"}) |
| 378 |
c.Assert(err, qt.IsNil) |
| 379 |
|
| 380 |
return res.LoadingInfo.Cfg, hugofs.NewFrom(mm, res.LoadingInfo.BaseConfig) |
| 381 |
} |