Debian Codegraph @

hugolib/rebuild_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package hugolib
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/fortytw2/leaktest"
11 qt "github.com/frankban/quicktest"
12 "github.com/gohugoio/hugo/common/types"
13 "github.com/gohugoio/hugo/htesting"
14 "github.com/gohugoio/hugo/markup/asciidocext"
15 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
16 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
17 )
18
19 const rebuildFilesSimple = `
20 -- hugo.toml --
21 baseURL = "https://example.com"
22 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
23 disableLiveReload = true
24 [outputFormats]
25 [outputFormats.rss]
26 weight = 10
27 [outputFormats.html]
28 weight = 20
29 [outputs]
30 home = ["rss", "html"]
31 section = ["html"]
32 page = ["html"]
33 -- content/mysection/_index.md --
34 ---
35 title: "My Section"
36 ---
37 -- content/mysection/mysectionbundle/index.md --
38 ---
39 title: "My Section Bundle"
40 ---
41 My Section Bundle Content.
42 -- content/mysection/mysectionbundle/mysectionbundletext.txt --
43 My Section Bundle Text 2 Content.
44 -- content/mysection/mysectionbundle/mysectionbundlecontent.md --
45 ---
46 title: "My Section Bundle Content"
47 ---
48 My Section Bundle Content Content.
49 -- content/mysection/_index.md --
50 ---
51 title: "My Section"
52 ---
53 -- content/mysection/mysectiontext.txt --
54 Content.
55 -- content/_index.md --
56 ---
57 title: "Home"
58 ---
59 Home Content.
60 -- content/hometext.txt --
61 Home Text Content.
62 -- content/myothersection/myothersectionpage.md --
63 ---
64 title: "myothersectionpage"
65 ---
66 myothersectionpage Content.
67 -- content/mythirdsection/mythirdsectionpage.md --
68 ---
69 title: "mythirdsectionpage"
70 ---
71 mythirdsectionpage Content.
72 {{< myshortcodetext >}}
73 §§§ myothertext
74 foo
75 §§§
76 -- assets/mytext.txt --
77 Assets My Text.
78 -- assets/myshortcodetext.txt --
79 Assets My Shortcode Text.
80 -- assets/myothertext.txt --
81 Assets My Other Text.
82 -- layouts/single.html --
83 Single: {{ .Title }}|{{ .Content }}$
84 Resources: {{ range $i, $e := .Resources }}{{ $i }}:{{ .RelPermalink }}|{{ .Content }}|{{ end }}$
85 Len Resources: {{ len .Resources }}|
86 -- layouts/list.html --
87 List: {{ .Title }}|{{ .Content }}$
88 Len Resources: {{ len .Resources }}|
89 Resources: {{ range $i, $e := .Resources }}{{ $i }}:{{ .RelPermalink }}|{{ .Content }}|{{ end }}$
90 -- layouts/_shortcodes/foo.html --
91 Foo.
92 -- layouts/_shortcodes/myshortcodetext.html --
93 {{ warnf "mytext %s" now}}
94 {{ $r := resources.Get "myshortcodetext.txt" }}
95 My Shortcode Text: {{ $r.Content }}|{{ $r.Permalink }}|
96 -- layouts/_markup/render-codeblock-myothertext.html --
97 {{ $r := resources.Get "myothertext.txt" }}
98 My Other Text: {{ $r.Content }}|{{ $r.Permalink }}|
99
100 `
101
102 func TestRebuildEditLeafBundleHeaderOnly(t *testing.T) {
103 t.Parallel()
104 for range 3 {
105 b := TestRunning(t, rebuildFilesSimple)
106 b.AssertFileContent("public/mysection/mysectionbundle/index.html",
107 "My Section Bundle Content Content.")
108 b.EditFileReplaceAll("content/mysection/mysectionbundle/index.md", "My Section Bundle Content.", "My Section Bundle Content Edited.").Build()
109 b.AssertFileContent("public/mysection/mysectionbundle/index.html",
110 "My Section Bundle Content Edited.")
111 b.AssertRenderCountPage(2) // home (rss) + bundle.
112 b.AssertRenderCountContent(1)
113 }
114 }
115
116 func TestRebuildEditTextFileInLeafBundle(t *testing.T) {
117 b := TestRunning(t, rebuildFilesSimple)
118 b.AssertFileContent("public/mysection/mysectionbundle/index.html",
119 "Resources: 0:/mysection/mysectionbundle/mysectionbundletext.txt|My Section Bundle Text 2 Content.|1:|<p>My Section Bundle Content Content.</p>\n|$")
120
121 b.EditFileReplaceAll("content/mysection/mysectionbundle/mysectionbundletext.txt", "Content.", "Content Edited.").Build()
122 b.AssertFileContent("public/mysection/mysectionbundle/index.html",
123 "Text 2 Content Edited")
124 b.AssertRenderCountPage(1)
125 b.AssertRenderCountContent(0)
126 }
127
128 func TestRebuildAddingALeaffBundleIssue13925(t *testing.T) {
129 t.Parallel()
130
131 b := TestRunning(t, rebuildFilesSimple)
132
133 b.AddFiles(
134 "content/mysection/mysectionbundle2/index.md", "",
135 "content/mysection/mysectionbundle2/mysectionbundletext.txt", "mysectionbundletext.txt",
136 ).Build()
137
138 b.AssertFileContent("public/mysection/mysectionbundle2/index.html", "Len Resources: 1|")
139
140 b.AddFiles(
141 "content/mynewsection/_index.md", "",
142 "content/mynewsection/mynewsectiontext.txt", "foo",
143 ).Build()
144
145 b.AssertFileContent("public/mynewsection/index.html", "Len Resources: 1|")
146 }
147
148 func TestRebuildEditTextFileInShortcode(t *testing.T) {
149 t.Parallel()
150 for range 3 {
151 b := TestRunning(t, rebuildFilesSimple)
152 b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
153 "Text: Assets My Shortcode Text.")
154 b.EditFileReplaceAll("assets/myshortcodetext.txt", "My Shortcode Text", "My Shortcode Text Edited").Build()
155 fmt.Println(b.LogString())
156 b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
157 "Text: Assets My Shortcode Text Edited.")
158
159 }
160 }
161
162 func TestRebuildEditTextFileInHook(t *testing.T) {
163 t.Parallel()
164 for range 3 {
165 b := TestRunning(t, rebuildFilesSimple)
166 b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
167 "Text: Assets My Other Text.")
168 b.AssertFileContent("public/myothertext.txt", "Assets My Other Text.")
169 b.EditFileReplaceAll("assets/myothertext.txt", "My Other Text", "My Other Text Edited").Build()
170 b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
171 "Text: Assets My Other Text Edited.")
172
173 }
174 }
175
176 func TestRebuiEditUnmarshaledYamlFileInLeafBundle(t *testing.T) {
177 files := `
178 -- hugo.toml --
179 baseURL = "https://example.com"
180 disableLiveReload = true
181 disableKinds = ["taxonomy", "term", "sitemap", "robotsTXT", "404", "rss"]
182 -- content/mybundle/index.md --
183 -- content/mybundle/mydata.yml --
184 foo: bar
185 -- layouts/single.html --
186 MyData: {{ .Resources.Get "mydata.yml" | transform.Unmarshal }}|
187 `
188 b := TestRunning(t, files)
189
190 b.AssertFileContent("public/mybundle/index.html", "MyData: map[foo:bar]")
191
192 b.EditFileReplaceAll("content/mybundle/mydata.yml", "bar", "bar edited").Build()
193
194 b.AssertFileContent("public/mybundle/index.html", "MyData: map[foo:bar edited]")
195 }
196
197 func TestRebuildEditTextFileInHomeBundle(t *testing.T) {
198 b := TestRunning(t, rebuildFilesSimple)
199 b.AssertFileContent("public/index.html", "Home Content.")
200 b.AssertFileContent("public/index.html", "Home Text Content.")
201
202 b.EditFileReplaceAll("content/hometext.txt", "Content.", "Content Edited.").Build()
203 b.AssertFileContent("public/index.html", "Home Content.")
204 b.AssertFileContent("public/index.html", "Home Text Content Edited.")
205 b.AssertRenderCountPage(1)
206 b.AssertRenderCountContent(0)
207 }
208
209 func TestRebuildEditTextFileInBranchBundle(t *testing.T) {
210 b := TestRunning(t, rebuildFilesSimple)
211 b.AssertFileContent("public/mysection/index.html", "My Section", "0:/mysection/mysectiontext.txt|Content.|")
212
213 b.EditFileReplaceAll("content/mysection/mysectiontext.txt", "Content.", "Content Edited.").Build()
214 b.AssertFileContent("public/mysection/index.html", "My Section", "0:/mysection/mysectiontext.txt|Content Edited.|")
215 b.AssertRenderCountPage(1)
216 b.AssertRenderCountContent(0)
217 }
218
219 func testRebuildBothWatchingAndRunning(t *testing.T, files string, withB func(b *IntegrationTestBuilder)) {
220 t.Helper()
221 for _, opt := range []TestOpt{TestOptWatching(), TestOptRunning()} {
222 b := Test(t, files, opt)
223 withB(b)
224 }
225 }
226
227 func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
228 testRebuildBothWatchingAndRunning(t, rebuildFilesSimple, func(b *IntegrationTestBuilder) {
229 b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
230
231 b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
232 b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
233 b.AssertRenderCountPage(8)
234 b.AssertRenderCountContent(9)
235 })
236 }
237
238 func TestRebuilEditContentFileInLeafBundle(t *testing.T) {
239 b := TestRunning(t, rebuildFilesSimple)
240 b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content.")
241 b.EditFileReplaceAll("content/mysection/mysectionbundle/mysectionbundlecontent.md", "Content Content.", "Content Content Edited.").Build()
242 b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content Edited.")
243 }
244
245 func TestRebuilEditContentFileThenAnother(t *testing.T) {
246 b := TestRunning(t, rebuildFilesSimple)
247 b.EditFileReplaceAll("content/mysection/mysectionbundle/mysectionbundlecontent.md", "Content Content.", "Content Content Edited.").Build()
248 b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content Edited.")
249 b.AssertRenderCountPage(1)
250 b.AssertRenderCountContent(2)
251
252 b.EditFileReplaceAll("content/myothersection/myothersectionpage.md", "myothersectionpage Content.", "myothersectionpage Content Edited.").Build()
253 b.AssertFileContent("public/myothersection/myothersectionpage/index.html", "myothersectionpage Content Edited")
254 b.AssertRenderCountPage(2)
255 b.AssertRenderCountContent(2)
256 }
257
258 func TestRebuildRenameTextFileInBranchBundle(t *testing.T) {
259 b := TestRunning(t, rebuildFilesSimple)
260 b.AssertFileContent("public/mysection/index.html", "My Section")
261
262 b.RenameFile("content/mysection/mysectiontext.txt", "content/mysection/mysectiontext2.txt").Build()
263 b.AssertFileContent("public/mysection/index.html", "mysectiontext2", "My Section")
264 b.AssertRenderCountPage(3)
265 b.AssertRenderCountContent(2)
266 }
267
268 func TestRebuildRenameTextFileInHomeBundle(t *testing.T) {
269 b := TestRunning(t, rebuildFilesSimple)
270 b.AssertFileContent("public/index.html", "Home Text Content.")
271
272 b.RenameFile("content/hometext.txt", "content/hometext2.txt").Build()
273 b.AssertFileContent("public/index.html", "hometext2", "Home Text Content.")
274 b.AssertRenderCountPage(5)
275 }
276
277 func TestRebuildRenameDirectoryWithLeafBundle(t *testing.T) {
278 b := TestRunning(t, rebuildFilesSimple)
279 b.RenameDir("content/mysection/mysectionbundle", "content/mysection/mysectionbundlerenamed").Build()
280 b.AssertFileContent("public/mysection/mysectionbundlerenamed/index.html", "My Section Bundle")
281 b.AssertRenderCountPage(2)
282 }
283
284 func TestRebuildRenameDirectoryWithBranchBundle(t *testing.T) {
285 b := TestRunning(t, rebuildFilesSimple)
286 b.RenameDir("content/mysection", "content/mysectionrenamed").Build()
287 b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
288 b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
289 b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
290 b.AssertRenderCountPage(5)
291 }
292
293 func TestRebuildRenameDirectoryWithRegularPageUsedInHome(t *testing.T) {
294 files := `
295 -- hugo.toml --
296 baseURL = "https://example.com"
297 disableLiveReload = true
298 -- content/foo/p1.md --
299 ---
300 title: "P1"
301 ---
302 -- layouts/home.html --
303 Pages: {{ range .Site.RegularPages }}{{ .RelPermalink }}|{{ end }}$
304 `
305 b := TestRunning(t, files)
306
307 b.AssertFileContent("public/index.html", "Pages: /foo/p1/|$")
308
309 b.RenameDir("content/foo", "content/bar").Build()
310
311 b.AssertFileContent("public/index.html", "Pages: /bar/p1/|$")
312 }
313
314 func TestRebuildAddRegularFileRegularPageUsedInHomeMultilingual(t *testing.T) {
315 files := `
316 -- hugo.toml --
317 baseURL = "https://example.com"
318 disableLiveReload = true
319 [languages]
320 [languages.en]
321 weight = 1
322 [languages.nn]
323 weight = 2
324 [languages.fr]
325 weight = 3
326 [languages.a]
327 weight = 4
328 [languages.b]
329 weight = 5
330 [languages.c]
331 weight = 6
332 [languages.d]
333 weight = 7
334 [languages.e]
335 weight = 8
336 [languages.f]
337 weight = 9
338 [languages.g]
339 weight = 10
340 [languages.h]
341 weight = 11
342 [languages.i]
343 weight = 12
344 [languages.j]
345 weight = 13
346 -- content/foo/_index.md --
347 -- content/foo/data.txt --
348 -- content/foo/p1.md --
349 -- content/foo/p1.nn.md --
350 -- content/foo/p1.fr.md --
351 -- content/foo/p1.a.md --
352 -- content/foo/p1.b.md --
353 -- content/foo/p1.c.md --
354 -- content/foo/p1.d.md --
355 -- content/foo/p1.e.md --
356 -- content/foo/p1.f.md --
357 -- content/foo/p1.g.md --
358 -- content/foo/p1.h.md --
359 -- content/foo/p1.i.md --
360 -- content/foo/p1.j.md --
361 -- layouts/home.html --
362 RegularPages: {{ range .Site.RegularPages }}{{ .RelPermalink }}|{{ end }}$
363 `
364 b := TestRunning(t, files)
365
366 b.AssertFileContent("public/index.html", "RegularPages: /foo/p1/|$")
367 b.AssertFileContent("public/nn/index.html", "RegularPages: /nn/foo/p1/|$")
368 b.AssertFileContent("public/i/index.html", "RegularPages: /i/foo/p1/|$")
369
370 b.AddFiles("content/foo/p2.md", ``).Build()
371
372 b.AssertFileContent("public/index.html", "RegularPages: /foo/p1/|/foo/p2/|$")
373 b.AssertFileContent("public/fr/index.html", "RegularPages: /fr/foo/p1/|$")
374
375 b.AddFiles("content/foo/p2.fr.md", ``).Build()
376 b.AssertFileContent("public/fr/index.html", "RegularPages: /fr/foo/p1/|/fr/foo/p2/|$")
377
378 b.AddFiles("content/foo/p2.i.md", ``).Build()
379 b.AssertFileContent("public/i/index.html", "RegularPages: /i/foo/p1/|/i/foo/p2/|$")
380 }
381
382 func TestRebuildRenameDirectoryWithBranchBundleFastRender(t *testing.T) {
383 recentlyVisited := types.NewEvictingQueue[string](10).Add("/a/b/c/")
384 b := TestRunning(t, rebuildFilesSimple, func(cfg *IntegrationTestConfig) { cfg.BuildCfg = BuildCfg{RecentlyTouched: recentlyVisited} })
385 b.RenameDir("content/mysection", "content/mysectionrenamed").Build()
386 b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
387 b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
388 b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
389 b.AssertRenderCountPage(5)
390 }
391
392 func TestRebuilErrorRecovery(t *testing.T) {
393 b := TestRunning(t, rebuildFilesSimple)
394 _, err := b.EditFileReplaceAll("content/mysection/mysectionbundle/index.md", "My Section Bundle Content.", "My Section Bundle Content\n\n\n\n{{< foo }}.").BuildE()
395
396 b.Assert(err, qt.Not(qt.IsNil))
397 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`"/content/mysection/mysectionbundle/index.md:8:9": unrecognized character`))
398
399 // Fix the error
400 b.EditFileReplaceAll("content/mysection/mysectionbundle/index.md", "{{< foo }}", "{{< foo >}}").Build()
401 }
402
403 func TestRebuildEmptyParamsIssue14886(t *testing.T) {
404 t.Parallel()
405
406 files := `
407 -- hugo.toml --
408 baseURL = "https://example.com"
409 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404", "rss"]
410 disableLiveReload = true
411 -- content/news/_index.md --
412 ---
413 title: News
414 params:
415 featuredImage: featured.png
416 ---
417 News.
418 -- layouts/list.html --
419 List: {{ .Title }}|{{ .Params.featuredimage }}|
420 `
421
422 b := TestRunning(t, files)
423 b.AssertFileContent("public/news/index.html", "List: News|featured.png|")
424
425 b.EditFileReplaceAll("content/news/_index.md", " featuredImage", "featuredImage").Build()
426 b.AssertFileContent("public/news/index.html", "List: News|featured.png|")
427
428 b.EditFileReplaceAll("content/news/_index.md", "featuredImage", " featuredImage").Build()
429 b.AssertFileContent("public/news/index.html", "List: News|featured.png|")
430 }
431
432 // Issue 14573
433 func TestRebuildAddContentWithMultipleDirCreations(t *testing.T) {
434 t.Parallel()
435 files := `
436 -- hugo.toml --
437 baseURL = "https://example.com"
438 disableLiveReload = true
439 -- content/p1.md --
440 ---
441 title: "P1"
442 ---
443 -- layouts/page.html --
444 Single: {{ .Title }}|{{ .Content }}|
445 -- layouts/list.html --
446 Pages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
447 `
448 b := TestRunning(t, files)
449 b.AssertFileContent("public/index.html", "Pages: /p1/|$")
450 b.AddFiles(
451 "content/nesteddir/dir1/post.md", "---\ntitle: Post\n---",
452 ).CreateDirs(
453 "content/dir1",
454 "content/dir2",
455 ).Build()
456 b.AssertFileContent("public/nesteddir/dir1/post/index.html", "Single: Post|")
457 }
458
459 func TestRebuildAddPageListPagesInHome(t *testing.T) {
460 files := `
461 -- hugo.toml --
462 baseURL = "https://example.com"
463 disableLiveReload = true
464 -- content/asection/s1.md --
465 -- content/p1.md --
466 ---
467 title: "P1"
468 weight: 1
469 ---
470 -- layouts/single.html --
471 Single: {{ .Title }}|{{ .Content }}|
472 -- layouts/home.html --
473 Pages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
474 `
475
476 b := TestRunning(t, files)
477 b.AssertFileContent("public/index.html", "Pages: /p1/|$")
478 b.AddFiles("content/p2.md", ``).Build()
479 b.AssertFileContent("public/index.html", "Pages: /p1/|/p2/|$")
480 }
481
482 func TestRebuildAddPageWithSpaceListPagesInHome(t *testing.T) {
483 files := `
484 -- hugo.toml --
485 baseURL = "https://example.com"
486 disableLiveReload = true
487 -- content/asection/s1.md --
488 -- content/p1.md --
489 ---
490 title: "P1"
491 weight: 1
492 ---
493 -- layouts/single.html --
494 Single: {{ .Title }}|{{ .Content }}|
495 -- layouts/home.html --
496 Pages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
497 `
498
499 b := TestRunning(t, files)
500 b.AssertFileContent("public/index.html", "Pages: /p1/|$")
501 b.AddFiles("content/test test/index.md", ``).Build()
502 b.AssertFileContent("public/index.html", "Pages: /p1/|/test-test/|$")
503 }
504
505 func TestRebuildScopedToOutputFormat(t *testing.T) {
506 files := `
507 -- hugo.toml --
508 baseURL = "https://example.com"
509 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
510 disableLiveReload = true
511 -- content/p1.md --
512 ---
513 title: "P1"
514 outputs: ["html", "json"]
515 ---
516 P1 Content.
517
518 {{< myshort >}}
519 -- layouts/single.html --
520 Single HTML: {{ .Title }}|{{ .Content }}|
521 -- layouts/single.json --
522 Single JSON: {{ .Title }}|{{ .Content }}|
523 -- layouts/_shortcodes/myshort.html --
524 My short.
525 `
526 b := Test(t, files, TestOptRunning())
527 b.AssertRenderCountPage(3)
528 b.AssertRenderCountContent(1)
529 b.AssertFileContent("public/p1/index.html", "Single HTML: P1|<p>P1 Content.</p>\n")
530 b.AssertFileContent("public/p1/index.json", "Single JSON: P1|<p>P1 Content.</p>\n")
531 b.EditFileReplaceAll("layouts/single.html", "Single HTML", "Single HTML Edited").Build()
532 b.AssertFileContent("public/p1/index.html", "Single HTML Edited: P1|<p>P1 Content.</p>\n")
533 b.AssertRenderCountPage(1)
534
535 // Edit shortcode. Note that this is reused across all output formats.
536 b.EditFileReplaceAll("layouts/_shortcodes/myshort.html", "My short", "My short edited").Build()
537 b.AssertFileContent("public/p1/index.html", "My short edited")
538 b.AssertFileContent("public/p1/index.json", "My short edited")
539 b.AssertRenderCountPage(3) // rss (uses .Content) + 2 single pages.
540 }
541
542 func TestRebuildBaseof(t *testing.T) {
543 files := `
544 -- hugo.toml --
545 title = "Hugo Project"
546 baseURL = "https://example.com"
547 disableKinds = ["term", "taxonomy"]
548 disableLiveReload = true
549 -- layouts/baseof.html --
550 Baseof: {{ .Title }}|
551 {{ block "main" . }}default{{ end }}
552 -- layouts/home.html --
553 {{ define "main" }}
554 Home: {{ .Title }}|{{ .Content }}|
555 {{ end }}
556 `
557 testRebuildBothWatchingAndRunning(t, files, func(b *IntegrationTestBuilder) {
558 b.AssertFileContent("public/index.html", "Baseof: Hugo Project|", "Home: Hugo Project||")
559 b.EditFileReplaceFunc("layouts/baseof.html", func(s string) string {
560 return strings.Replace(s, "Baseof", "Baseof Edited", 1)
561 }).Build()
562 b.AssertFileContent("public/index.html", "Baseof Edited: Hugo Project|", "Home: Hugo Project||")
563 })
564 }
565
566 func TestRebuildSingle(t *testing.T) {
567 t.Parallel()
568
569 files := `
570 -- hugo.toml --
571 title = "Hugo Project"
572 baseURL = "https://example.com"
573 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
574 disableLiveReload = true
575 -- content/p1.md --
576 ---
577 title: "P1"
578 ---
579 P1 Content.
580 -- layouts/home.html --
581 Home.
582 -- layouts/single.html --
583 Single: {{ .Title }}|{{ .Content }}|
584 {{ with (templates.Defer (dict "key" "global")) }}
585 Defer.
586 {{ end }}
587 `
588 b := Test(t, files, TestOptRunning())
589 b.AssertFileContent("public/p1/index.html", "Single: P1|", "Defer.")
590 b.AssertRenderCountPage(3)
591 b.AssertRenderCountContent(1)
592 b.EditFileReplaceFunc("layouts/single.html", func(s string) string {
593 s = strings.Replace(s, "Single", "Single Edited", 1)
594 s = strings.Replace(s, "Defer.", "Defer Edited.", 1)
595 return s
596 }).Build()
597 b.AssertFileContent("public/p1/index.html", "Single Edited: P1|", "Defer Edited.")
598 b.AssertRenderCountPage(1)
599 b.AssertRenderCountContent(0)
600 }
601
602 func TestRebuildSingleWithBaseofEditSingle(t *testing.T) {
603 t.Parallel()
604
605 files := `
606 -- hugo.toml --
607 title = "Hugo Project"
608 baseURL = "https://example.com"
609 disableKinds = ["term", "taxonomy"]
610 disableLiveReload = true
611 -- content/p1.md --
612 ---
613 title: "P1"
614 ---
615 P1 Content.
616 [foo](/foo)
617 -- layouts/baseof.html --
618 Baseof: {{ .Title }}|
619 {{ block "main" . }}default{{ end }}
620 {{ with (templates.Defer (dict "foo" "bar")) }}
621 Defer.
622 {{ end }}
623 -- layouts/home.html --
624 Home.
625 -- layouts/single.html --
626 {{ define "main" }}
627 Single: {{ .Title }}|{{ .Content }}|
628 {{ end }}
629 `
630 b := Test(t, files, TestOptRunning())
631 b.AssertFileContent("public/p1/index.html", "Single: P1|")
632 b.EditFileReplaceFunc("layouts/single.html", func(s string) string {
633 return strings.Replace(s, "Single", "Single Edited", 1)
634 }).Build()
635 b.AssertFileContent("public/p1/index.html", "Single Edited")
636 }
637
638 func TestRebuildSingleWithBaseofEditBaseof(t *testing.T) {
639 t.Parallel()
640
641 files := `
642 -- hugo.toml --
643 title = "Hugo Project"
644 baseURL = "https://example.com"
645 disableKinds = ["term", "taxonomy"]
646 disableLiveReload = true
647 -- content/p1.md --
648 ---
649 title: "P1"
650 ---
651 P1 Content.
652 [foo](/foo)
653 -- layouts/baseof.html --
654 Baseof: {{ .Title }}|
655 {{ block "main" . }}default{{ end }}
656 {{ with (templates.Defer (dict "foo" "bar")) }}
657 Defer.
658 {{ end }}
659 -- layouts/home.html --
660 Home.
661 -- layouts/single.html --
662 {{ define "main" }}
663 Single: {{ .Title }}|{{ .Content }}|
664 {{ end }}
665 `
666 b := Test(t, files, TestOptRunning())
667 b.AssertFileContent("public/p1/index.html", "Single: P1|")
668 b.EditFileReplaceAll("layouts/baseof.html", "Baseof", "Baseof Edited").Build()
669 b.AssertFileContent("public/p1/index.html", "Baseof Edited")
670 }
671
672 func TestRebuildWithDeferEditRenderHook(t *testing.T) {
673 t.Parallel()
674
675 files := `
676 -- hugo.toml --
677 title = "Hugo Project"
678 baseURL = "https://example.com"
679 disableKinds = ["term", "taxonomy"]
680 disableLiveReload = true
681 -- content/p1.md --
682 ---
683 title: "P1"
684 ---
685 P1 Content.
686 [foo](/foo)
687 -- layouts/baseof.html --
688 Baseof: {{ .Title }}|
689 {{ block "main" . }}default{{ end }}
690 {{ with (templates.Defer (dict "foo" "bar")) }}
691 Defer.
692 {{ end }}
693 -- layouts/single.html --
694 {{ define "main" }}
695 Single: {{ .Title }}|{{ .Content }}|
696 {{ end }}
697 -- layouts/_markup/render-link.html --
698 Render Link.
699 `
700 b := Test(t, files, TestOptRunning())
701 // Edit render hook.
702 b.EditFileReplaceAll("layouts/_markup/render-link.html", "Render Link", "Render Link Edited").Build()
703
704 b.AssertFileContent("public/p1/index.html", "Render Link Edited")
705 }
706
707 func TestRebuildFromString(t *testing.T) {
708 files := `
709 -- hugo.toml --
710 baseURL = "https://example.com"
711 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
712 disableLiveReload = true
713 -- content/p1.md --
714 ---
715 title: "P1"
716 layout: "l1"
717 ---
718 P1 Content.
719 -- content/p2.md --
720 ---
721 title: "P2"
722 layout: "l2"
723 ---
724 P2 Content.
725 -- assets/mytext.txt --
726 My Text
727 -- layouts/l1.html --
728 {{ $r := partial "get-resource.html" . }}
729 L1: {{ .Title }}|{{ .Content }}|R: {{ $r.Content }}|
730 -- layouts/l2.html --
731 L2.
732 -- layouts/_partials/get-resource.html --
733 {{ $mytext := resources.Get "mytext.txt" }}
734 {{ $txt := printf "Text: %s" $mytext.Content }}
735 {{ $r := resources.FromString "r.txt" $txt }}
736 {{ return $r }}
737
738 `
739 b := TestRunning(t, files)
740
741 b.AssertFileContent("public/p1/index.html", "L1: P1|<p>P1 Content.</p>\n|R: Text: My Text|")
742
743 b.EditFileReplaceAll("assets/mytext.txt", "My Text", "My Text Edited").Build()
744
745 b.AssertFileContent("public/p1/index.html", "L1: P1|<p>P1 Content.</p>\n|R: Text: My Text Edited|")
746
747 b.AssertRenderCountPage(1)
748 }
749
750 func TestRebuildDeeplyNestedLink(t *testing.T) {
751 t.Parallel()
752
753 files := `
754 -- hugo.toml --
755 baseURL = "https://example.com/"
756 disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
757 disableLiveReload = true
758 -- content/s/p1.md --
759 ---
760 title: "P1"
761 ---
762 -- content/s/p2.md --
763 ---
764 title: "P2"
765 ---
766 -- content/s/p3.md --
767 ---
768 title: "P3"
769 ---
770 -- content/s/p4.md --
771 ---
772 title: "P4"
773 ---
774 -- content/s/p5.md --
775 ---
776 title: "P5"
777 ---
778 -- content/s/p6.md --
779 ---
780 title: "P6"
781 ---
782 -- content/s/p7.md --
783 ---
784 title: "P7"
785 ---
786 -- layouts/list.html --
787 List.
788 -- layouts/single.html --
789 Single.
790 -- layouts/single.html --
791 Next: {{ with .PrevInSection }}{{ .Title }}{{ end }}|
792 Prev: {{ with .NextInSection }}{{ .Title }}{{ end }}|
793
794
795 `
796
797 b := TestRunning(t, files)
798
799 b.AssertFileContent("public/s/p1/index.html", "Next: P2|")
800 b.EditFileReplaceAll("content/s/p7.md", "P7", "P7 Edited").Build()
801 b.AssertFileContent("public/s/p6/index.html", "Next: P7 Edited|")
802 }
803
804 const rebuildBasicFiles = `
805 -- hugo.toml --
806 baseURL = "https://example.com/"
807 disableLiveReload = true
808 disableKinds = ["sitemap", "robotstxt", "404", "rss"]
809 -- content/_index.md --
810 ---
811 title: "Home"
812 cascade:
813 target:
814 kind: "page"
815 date: 2019-05-06
816 params:
817 myparam: "myvalue"
818 ---
819 -- content/mysection/_index.md --
820 ---
821 title: "My Section"
822 date: 2024-02-01
823 ---
824 -- content/mysection/p1.md --
825 ---
826 title: "P1"
827 date: 2020-01-01
828 ---
829 -- content/mysection/p2.md --
830 ---
831 title: "P2"
832 date: 2021-02-01
833 ---
834 -- content/mysection/p3.md --
835 ---
836 title: "P3"
837 ---
838 -- layouts/all.html --
839 all. {{ .Title }}|Param: {{ .Params.myparam }}|Lastmod: {{ .Lastmod.Format "2006-01-02" }}|
840 `
841
842 func TestRebuildEditPageTitle(t *testing.T) {
843 t.Parallel()
844 b := TestRunning(t, rebuildBasicFiles)
845 b.AssertFileContent("public/index.html", "all. Home|Param: |Lastmod: 2024-02-01|")
846 b.AssertFileContent("public/mysection/p1/index.html", "P1|Param: myvalue|", "Lastmod: 2020-01-01|")
847 b.EditFileReplaceAll("content/mysection/p1.md", "P1", "P1edit").Build()
848 b.AssertRenderCountPage(2)
849 b.AssertFileContent("public/mysection/p1/index.html", "all. P1edit|Param: myvalue|")
850 }
851
852 func TestRebuildEditPageDate(t *testing.T) {
853 t.Parallel()
854 b := TestRunning(t, rebuildBasicFiles)
855 b.AssertFileContent("public/index.html", "all. Home|Param: |Lastmod: 2024-02-01|")
856 b.EditFileReplaceAll("content/mysection/p2.md", "2021-02-01", "2025-04-02").Build()
857 b.AssertRenderCountPage(2)
858 b.AssertFileContent("public/index.html", "all. Home|Param: |Lastmod: 2025-04-02|")
859 b.AssertFileContent("public/mysection/p2/index.html", "all. P2|Param: myvalue|Lastmod: 2025-04-02|")
860 }
861
862 func TestRebuildEditHomeCascadeEditParam(t *testing.T) {
863 t.Parallel()
864 b := TestRunning(t, rebuildBasicFiles)
865 b.AssertRenderCountPage(7)
866 b.EditFileReplaceAll("content/_index.md", "myvalue", "myvalue-edit").Build()
867 b.AssertRenderCountPage(7)
868 b.AssertFileContent("public/mysection/p1/index.html", "all. P1|Param: myvalue-edit|")
869 }
870
871 func TestRebuildEditHomeCascadeRemoveParam(t *testing.T) {
872 t.Parallel()
873 b := TestRunning(t, rebuildBasicFiles)
874 b.AssertRenderCountPage(7)
875 b.EditFileReplaceAll("content/_index.md", `myparam: "myvalue"`, "").Build()
876 b.AssertRenderCountPage(7)
877 b.AssertFileContent("public/mysection/p1/index.html", "all. P1|Param: |")
878 }
879
880 func TestRebuildEditHomeCascadeEditDate(t *testing.T) {
881 t.Parallel()
882 b := TestRunning(t, rebuildBasicFiles)
883 b.AssertRenderCountPage(7)
884 b.AssertFileContent("public/mysection/p3/index.html", "all. P3|Param: myvalue|Lastmod: 2019-05-06")
885 b.EditFileReplaceAll("content/_index.md", "2019-05-06", "2025-05-06").Build()
886 b.AssertRenderCountPage(7)
887 b.AssertFileContent("public/index.html", "all. Home|Param: |Lastmod: 2025-05-06|")
888 b.AssertFileContent("public/mysection/index.html", "all. My Section|Param: |Lastmod: 2024-02-01|")
889 b.AssertFileContent("public/mysection/p3/index.html", "all. P3|Param: myvalue|Lastmod: 2025-05-06")
890 }
891
892 func TestRebuildEditSectionRemoveDate(t *testing.T) {
893 t.Parallel()
894 b := TestRunning(t, rebuildBasicFiles)
895 b.AssertFileContent("public/mysection/index.html", "all. My Section|Param: |Lastmod: 2024-02-01|")
896 b.EditFileReplaceAll("content/mysection/_index.md", "date: 2024-02-01", "").Build()
897 b.AssertRenderCountPage(5)
898 b.AssertFileContent("public/mysection/index.html", "all. My Section|Param: |Lastmod: 2021-02-01|")
899 }
900
901 // Atomic save (write temp file, then rename into place) of a section's branch bundle.
902 // See issue 15130.
903 func TestRebuildEditAtomicSection(t *testing.T) {
904 t.Parallel()
905 b := TestRunning(t, rebuildBasicFiles)
906 b.AssertFileContent("public/mysection/index.html", "My Section")
907 b.EditFileAtomicReplaceAll("content/mysection/_index.md", "My Section", "My Changed Section").Build()
908 b.AssertRenderCountPage(5)
909 b.AssertFileContent("public/mysection/index.html", "My Changed Section")
910 }
911
912 func TestRebuildVariations(t *testing.T) {
913 // t.Parallel() not supported, see https://github.com/fortytw2/leaktest/issues/4
914 // This leaktest seems to be a little bit shaky on Travis.
915 if !htesting.IsRealCI() {
916 defer leaktest.CheckTimeout(t, 10*time.Second)()
917 }
918
919 files := `
920 -- hugo.toml --
921 baseURL = "https://example.com"
922 disableKinds = ["term", "taxonomy"]
923 disableLiveReload = true
924 defaultContentLanguage = "nn"
925 [pagination]
926 pagerSize = 20
927 [security]
928 enableInlineShortcodes = true
929 [languages]
930 [languages.en]
931 weight = 1
932 [languages.nn]
933 weight = 2
934 -- content/mysect/p1/index.md --
935 ---
936 title: "P1"
937 ---
938 P1 Content.
939 {{< include "mysect/p2" >}}
940 §§§go { page="mysect/p3" }
941 hello
942 §§§
943
944 {{< foo.inline >}}Foo{{< /foo.inline >}}
945 -- content/mysect/p2/index.md --
946 ---
947 title: "P2"
948 ---
949 P2 Content.
950 -- content/mysect/p3/index.md --
951 ---
952 title: "P3"
953 ---
954 P3 Content.
955 -- content/mysect/sub/_index.md --
956 -- content/mysect/sub/p4/index.md --
957 ---
958 title: "P4"
959 ---
960 P4 Content.
961 -- content/mysect/sub/p5/index.md --
962 ---
963 title: "P5"
964 lastMod: 2019-03-02
965 ---
966 P5 Content.
967 -- content/myothersect/_index.md --
968 ---
969 cascade:
970 - target:
971 cascadeparam: "cascadevalue"
972 ---
973 -- content/myothersect/sub/_index.md --
974 -- content/myothersect/sub/p6/index.md --
975 ---
976 title: "P6"
977 ---
978 P6 Content.
979 -- content/translations/p7.en.md --
980 ---
981 title: "P7 EN"
982 ---
983 P7 EN Content.
984 -- content/translations/p7.nn.md --
985 ---
986 title: "P7 NN"
987 ---
988 P7 NN Content.
989 -- layouts/home.html --
990 Home: {{ .Title }}|{{ .Content }}|
991 RegularPages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
992 Len RegularPagesRecursive: {{ len .RegularPagesRecursive }}
993 Site.Lastmod: {{ .Site.Lastmod.Format "2006-01-02" }}|
994 Paginate: {{ range (.Paginate .Site.RegularPages).Pages }}{{ .RelPermalink }}|{{ .Title }}|{{ end }}$
995 -- layouts/single.html --
996 Single: .Site: {{ .Site }}
997 Single: {{ .Title }}|{{ .Content }}|
998 Single Partial Cached: {{ partialCached "pcached" . }}|
999 Page.Lastmod: {{ .Lastmod.Format "2006-01-02" }}|
1000 Cascade param: {{ .Params.cascadeparam }}|
1001 -- layouts/list.html --
1002 List: {{ .Title }}|{{ .Content }}|
1003 RegularPages: {{ range .RegularPages }}{{ .Title }}|{{ end }}$
1004 Len RegularPagesRecursive: {{ len .RegularPagesRecursive }}
1005 RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .RelPermalink }}|{{ end }}$
1006 List Partial P1: {{ partial "p1" . }}|
1007 Page.Lastmod: {{ .Lastmod.Format "2006-01-02" }}|
1008 Cascade param: {{ .Params.cascadeparam }}|
1009 -- layouts/_partials/p1.html --
1010 Partial P1.
1011 -- layouts/_partials/pcached.html --
1012 Partial Pcached.
1013 -- layouts/_shortcodes/include.html --
1014 {{ $p := site.GetPage (.Get 0)}}
1015 .Page.Site: {{ .Page.Site }} :: site: {{ site }}
1016 {{ with $p }}
1017 Shortcode Include: {{ .Title }}|
1018 {{ end }}
1019 Shortcode .Page.Title: {{ .Page.Title }}|
1020 Shortcode Partial P1: {{ partial "p1" . }}|
1021 -- layouts/_markup/render-codeblock.html --
1022 {{ $p := site.GetPage (.Attributes.page)}}
1023 {{ with $p }}
1024 Codeblock Include: {{ .Title }}|
1025 {{ end }}
1026
1027 `
1028
1029 b := TestRunning(
1030 t, files,
1031 TestOptWarn(),
1032 TestOptWithConfig(func(c *IntegrationTestConfig) {
1033 c.BuildCfg = BuildCfg{testCounters: &buildCounters{}}
1034 }),
1035 )
1036
1037 // When running the server, this is done on shutdown.
1038 // Do this here to satisfy the leak detector above.
1039 defer func() {
1040 b.Assert(b.H.Close(), qt.IsNil)
1041 }()
1042
1043 contentRenderCount := b.counters.contentRenderCounter.Load()
1044 pageRenderCount := b.counters.pageRenderCounter.Load()
1045
1046 b.Assert(contentRenderCount > 0, qt.IsTrue)
1047 b.Assert(pageRenderCount > 0, qt.IsTrue)
1048
1049 // Test cases:
1050 // - Edit content file direct
1051 // - Edit content file transitive shortcode
1052 // - Edit content file transitive render hook
1053 // - Rename one language version of a content file
1054 // - Delete content file, check site.RegularPages and section.RegularPagesRecursive (length)
1055 // - Add content file (see above).
1056 // - Edit shortcode
1057 // - Edit inline shortcode
1058 // - Edit render hook
1059 // - Edit partial used in template
1060 // - Edit partial used in shortcode
1061 // - Edit partial cached.
1062 // - Edit lastMod date in content file, check site.Lastmod.
1063 editFile := func(filename string, replacementFunc func(s string) string) {
1064 b.EditFileReplaceFunc(filename, replacementFunc).Build()
1065 b.Assert(b.counters.contentRenderCounter.Load() < contentRenderCount, qt.IsTrue, qt.Commentf("count %d < %d", b.counters.contentRenderCounter.Load(), contentRenderCount))
1066 b.Assert(b.counters.pageRenderCounter.Load() < pageRenderCount, qt.IsTrue, qt.Commentf("count %d < %d", b.counters.pageRenderCounter.Load(), pageRenderCount))
1067 }
1068
1069 b.AssertFileContent("public/index.html", "RegularPages: $", "Len RegularPagesRecursive: 7", "Site.Lastmod: 2019-03-02")
1070
1071 b.AssertFileContent("public/mysect/p1/index.html",
1072 "Single: P1|<p>P1 Content.",
1073 "Shortcode Include: P2|",
1074 "Codeblock Include: P3|")
1075
1076 editFile("content/mysect/p1/index.md", func(s string) string {
1077 return strings.ReplaceAll(s, "P1", "P1 Edited")
1078 })
1079
1080 b.AssertFileContent("public/mysect/p1/index.html", "Single: P1 Edited|<p>P1 Edited Content.")
1081 b.AssertFileContent("public/index.html", "RegularPages: $", "Len RegularPagesRecursive: 7", "Paginate: /mysect/sub/p5/|P5|/mysect/p1/|P1 Edited")
1082 b.AssertFileContent("public/mysect/index.html", "RegularPages: P1 Edited|P2|P3|$", "Len RegularPagesRecursive: 5")
1083
1084 // p2 is included in p1 via shortcode.
1085 editFile("content/mysect/p2/index.md", func(s string) string {
1086 return strings.ReplaceAll(s, "P2", "P2 Edited")
1087 })
1088
1089 b.AssertFileContent("public/mysect/p1/index.html", "Shortcode Include: P2 Edited|")
1090
1091 // p3 is included in p1 via codeblock hook.
1092 editFile("content/mysect/p3/index.md", func(s string) string {
1093 return strings.ReplaceAll(s, "P3", "P3 Edited")
1094 })
1095
1096 b.AssertFileContent("public/mysect/p1/index.html", "Codeblock Include: P3 Edited|")
1097
1098 // Remove a content file in a nested section.
1099 b.RemoveFiles("content/mysect/sub/p4/index.md").Build()
1100 b.AssertFileContent("public/mysect/index.html", "RegularPages: P1 Edited|P2 Edited|P3 Edited", "Len RegularPagesRecursive: 4")
1101 b.AssertFileContent("public/mysect/sub/index.html", "RegularPages: P5|$", "RegularPagesRecursive: 1")
1102
1103 // Rename one of the translations.
1104 b.AssertFileContent("public/translations/index.html", "RegularPagesRecursive: /translations/p7/")
1105 b.AssertFileContent("public/en/translations/index.html", "RegularPagesRecursive: /en/translations/p7/")
1106 b.RenameFile("content/translations/p7.nn.md", "content/translations/p7rename.nn.md").Build()
1107 b.AssertFileContent("public/translations/index.html", "RegularPagesRecursive: /translations/p7rename/")
1108 b.AssertFileContent("public/en/translations/index.html", "RegularPagesRecursive: /en/translations/p7/")
1109
1110 // Edit shortcode
1111 editFile("layouts/_shortcodes/include.html", func(s string) string {
1112 return s + "\nShortcode Include Edited."
1113 })
1114 b.AssertFileContent("public/mysect/p1/index.html", "Shortcode Include Edited.")
1115
1116 // Edit render hook
1117 editFile("layouts/_markup/render-codeblock.html", func(s string) string {
1118 return s + "\nCodeblock Include Edited."
1119 })
1120 b.AssertFileContent("public/mysect/p1/index.html", "Codeblock Include Edited.")
1121
1122 // Edit partial p1
1123 editFile("layouts/_partials/p1.html", func(s string) string {
1124 return strings.Replace(s, "Partial P1", "Partial P1 Edited", 1)
1125 })
1126 b.AssertFileContent("public/mysect/index.html", "List Partial P1: Partial P1 Edited.")
1127 b.AssertFileContent("public/mysect/p1/index.html", "Shortcode Partial P1: Partial P1 Edited.")
1128
1129 // Edit partial cached.
1130 editFile("layouts/_partials/pcached.html", func(s string) string {
1131 return strings.Replace(s, "Partial Pcached", "Partial Pcached Edited", 1)
1132 })
1133 b.AssertFileContent("public/mysect/p1/index.html", "Pcached Edited.")
1134
1135 // Edit lastMod date in content file, check site.Lastmod.
1136 editFile("content/mysect/sub/p5/index.md", func(s string) string {
1137 return strings.Replace(s, "2019-03-02", "2020-03-10", 1)
1138 })
1139 b.AssertFileContent("public/index.html", "Site.Lastmod: 2020-03-10|")
1140 b.AssertFileContent("public/mysect/index.html", "Page.Lastmod: 2020-03-10|")
1141
1142 // Adjust the date back a few days.
1143 editFile("content/mysect/sub/p5/index.md", func(s string) string {
1144 return strings.Replace(s, "2020-03-10", "2019-03-08", 1)
1145 })
1146 b.AssertFileContent("public/mysect/index.html", "Page.Lastmod: 2019-03-08|")
1147 b.AssertFileContent("public/index.html", "Site.Lastmod: 2019-03-08|")
1148
1149 // Check cascade mods.
1150 b.AssertFileContent("public/myothersect/index.html", "Cascade param: cascadevalue|")
1151 b.AssertFileContent("public/myothersect/sub/index.html", "Cascade param: cascadevalue|")
1152 b.AssertFileContent("public/myothersect/sub/p6/index.html", "Cascade param: cascadevalue|")
1153
1154 editFile("content/myothersect/_index.md", func(s string) string {
1155 return strings.Replace(s, "cascadevalue", "cascadevalue edited", 1)
1156 })
1157 b.AssertFileContent("public/myothersect/index.html", "Cascade param: cascadevalue edited|")
1158 b.AssertFileContent("public/myothersect/sub/p6/index.html", "Cascade param: cascadevalue edited|")
1159
1160 // Repurpose the cascadeparam to set the title.
1161 editFile("content/myothersect/_index.md", func(s string) string {
1162 return strings.Replace(s, "cascadeparam:", "title:", 1)
1163 })
1164 b.AssertFileContent("public/myothersect/sub/index.html", "Cascade param: |", "List: cascadevalue edited|")
1165
1166 // Revert it.
1167 editFile("content/myothersect/_index.md", func(s string) string {
1168 return strings.Replace(s, "title:", "cascadeparam:", 1)
1169 })
1170 b.AssertFileContent("public/myothersect/sub/index.html", "Cascade param: cascadevalue edited|", "List: |")
1171 }
1172
1173 func TestRebuildVariationsJSNoneFingerprinted(t *testing.T) {
1174 t.Parallel()
1175
1176 files := `
1177 -- hugo.toml --
1178 baseURL = "https://example.com/"
1179 disableKinds = ["term", "taxonomy", "sitemap", "robotsTXT", "404", "rss"]
1180 disableLiveReload = true
1181 -- content/p1/index.md --
1182 ---
1183 title: "P1"
1184 ---
1185 P1.
1186 -- content/p2/index.md --
1187 ---
1188 title: "P2"
1189 ---
1190 P2.
1191 -- content/p3/index.md --
1192 ---
1193 title: "P3"
1194 ---
1195 P3.
1196 -- content/p4/index.md --
1197 ---
1198 title: "P4"
1199 ---
1200 P4.
1201 -- assets/main.css --
1202 body {
1203 background: red;
1204 }
1205 -- layouts/default/list.html --
1206 List.
1207 -- layouts/single.html --
1208 Single.
1209 {{ $css := resources.Get "main.css" | minify }}
1210 RelPermalink: {{ $css.RelPermalink }}|
1211
1212 `
1213
1214 b := TestRunning(t, files)
1215
1216 b.AssertFileContent("public/p1/index.html", "RelPermalink: /main.min.css|")
1217 b.AssertFileContent("public/main.min.css", "body{background:red}")
1218
1219 b.EditFileReplaceAll("assets/main.css", "red", "blue")
1220 b.RemoveFiles("content/p2/index.md")
1221 b.RemoveFiles("content/p3/index.md")
1222 b.Build()
1223
1224 b.AssertFileContent("public/main.min.css", "body{background:blue}")
1225 }
1226
1227 func TestRebuildVariationsJSInNestedCachedPartialFingerprinted(t *testing.T) {
1228 t.Parallel()
1229
1230 files := `
1231 -- hugo.toml --
1232 baseURL = "https://example.com/"
1233 disableKinds = ["term", "taxonomy", "sitemap", "robotsTXT", "404", "rss"]
1234 disableLiveReload = true
1235 -- content/p1/index.md --
1236 ---
1237 title: "P1"
1238 ---
1239 P1.
1240 -- content/p2/index.md --
1241 ---
1242 title: "P2"
1243 ---
1244 P2.
1245 -- content/p3/index.md --
1246 ---
1247 title: "P3"
1248 ---
1249 P3.
1250 -- content/p4/index.md --
1251 ---
1252 title: "P4"
1253 ---
1254 P4.
1255 -- assets/js/main.js --
1256 console.log("Hello");
1257 -- layouts/list.html --
1258 List. {{ partial "head.html" . }}$
1259 -- layouts/single.html --
1260 Single. {{ partial "head.html" . }}$
1261 -- layouts/_partials/head.html --
1262 {{ partialCached "js.html" . }}$
1263 -- layouts/_partials/js.html --
1264 {{ $js := resources.Get "js/main.js" | js.Build | fingerprint }}
1265 RelPermalink: {{ $js.RelPermalink }}|
1266 `
1267
1268 b := TestRunning(t, files, TestOptOsFs())
1269
1270 b.AssertFileContent("public/p1/index.html", "/js/main.712a50b59d0f0dedb4e3606eaa3860b1f1a5305f6c42da30a2985e473ba314eb.js")
1271 b.AssertFileContent("public/index.html", "/js/main.712a50b59d0f0dedb4e3606eaa3860b1f1a5305f6c42da30a2985e473ba314eb.js")
1272
1273 b.EditFileReplaceAll("assets/js/main.js", "Hello", "Hello is Edited").Build()
1274
1275 for i := 1; i < 5; i++ {
1276 b.AssertFileContent(fmt.Sprintf("public/p%d/index.html", i), "/js/main.6535698cec9a21875f40ae03e96f30c4bee41a01e979224761e270b9034b2424.js")
1277 }
1278
1279 b.AssertFileContent("public/index.html", "/js/main.6535698cec9a21875f40ae03e96f30c4bee41a01e979224761e270b9034b2424.js")
1280 }
1281
1282 func TestRebuildVariationsJSInNestedPartialFingerprintedInBase(t *testing.T) {
1283 t.Parallel()
1284
1285 files := `
1286 -- hugo.toml --
1287 baseURL = "https://example.com/"
1288 disableKinds = ["term", "taxonomy", "sitemap", "robotsTXT", "404", "rss"]
1289 disableLiveReload = true
1290 -- assets/js/main.js --
1291 console.log("Hello");
1292 -- layouts/baseof.html --
1293 Base. {{ partial "common/head.html" . }}$
1294 {{ block "main" . }}default{{ end }}
1295 -- layouts/list.html --
1296 {{ define "main" }}main{{ end }}
1297 -- layouts/_partials/common/head.html --
1298 {{ partial "myfiles/js.html" . }}$
1299 -- layouts/_partials/myfiles/js.html --
1300 {{ $js := resources.Get "js/main.js" | js.Build | fingerprint }}
1301 RelPermalink: {{ $js.RelPermalink }}|
1302 `
1303
1304 b := TestRunning(t, files, TestOptOsFs())
1305
1306 b.AssertFileContent("public/index.html", "/js/main.712a50b59d0f0dedb4e3606eaa3860b1f1a5305f6c42da30a2985e473ba314eb.js")
1307
1308 b.EditFileReplaceAll("assets/js/main.js", "Hello", "Hello is Edited").Build()
1309
1310 b.AssertFileContent("public/index.html", "/js/main.6535698cec9a21875f40ae03e96f30c4bee41a01e979224761e270b9034b2424.js")
1311 }
1312
1313 func TestRebuildVariationsJSBundled(t *testing.T) {
1314 t.Parallel()
1315
1316 files := `
1317 -- hugo.toml --
1318 baseURL = "https://example.com"
1319 disableKinds = ["term", "taxonomy", "sitemap", "robotsTXT", "404", "rss"]
1320 disableLiveReload = true
1321 -- content/_index.md --
1322 ---
1323 title: "Home"
1324 ---
1325 -- content/p1.md --
1326 ---
1327 title: "P1"
1328 layout: "main"
1329 ---
1330 -- content/p2.md --
1331 ---
1332 title: "P2"
1333 ---
1334 {{< jsfingerprinted >}}
1335 -- content/p3.md --
1336 ---
1337 title: "P3"
1338 layout: "plain"
1339 ---
1340 {{< jsfingerprinted >}}
1341 -- content/main.js --
1342 console.log("Hello");
1343 -- content/foo.js --
1344 console.log("Foo");
1345 -- layouts/home.html --
1346 Home.
1347 {{ $js := site.Home.Resources.Get "main.js" }}
1348 {{ with $js }}
1349 <script src="{{ .RelPermalink }}"></script>
1350 {{ end }}
1351 -- layouts/single.html --
1352 Single. Deliberately no .Content in here.
1353 -- layouts/plain.html --
1354 Content: {{ .Content }}|
1355 -- layouts/main.html --
1356 {{ $js := site.Home.Resources.Get "main.js" }}
1357 {{ with $js }}
1358 <script>
1359 {{ .Content }}
1360 </script>
1361 {{ end }}
1362 -- layouts/_shortcodes/jsfingerprinted.html --
1363 {{ $js := site.Home.Resources.Get "foo.js" | fingerprint }}
1364 <script src="{{ $js.RelPermalink }}"></script>
1365 `
1366
1367 testCounters := &buildCounters{}
1368
1369 b := TestRunning(
1370 t, files,
1371 TestOptWithConfig(func(c *IntegrationTestConfig) {
1372 c.BuildCfg = BuildCfg{testCounters: testCounters}
1373 }),
1374 )
1375
1376 b.AssertFileContent("public/index.html", `<script src="/main.js"></script>`)
1377 b.AssertFileContent("public/p1/index.html", "<script>\n\"console.log(\\\"Hello\\\");\"\n</script>")
1378 b.AssertFileContent("public/p2/index.html", "Single. Deliberately no .Content in here.")
1379 b.AssertFileContent("public/p3/index.html", "foo.57b4465b908531b43d4e4680ab1063d856b475cb1ae81ad43e0064ecf607bec1.js")
1380 b.AssertRenderCountPage(4)
1381
1382 // Edit JS file.
1383 b.EditFileReplaceFunc("content/main.js", func(s string) string {
1384 return strings.Replace(s, "Hello", "Hello is Edited", 1)
1385 }).Build()
1386
1387 b.AssertFileContent("public/p1/index.html", "<script>\n\"console.log(\\\"Hello is Edited\\\");\"\n</script>")
1388 // The p1 (the one inlining the JS) should be rebuilt.
1389 b.AssertRenderCountPage(2)
1390 // But not the content file.
1391 b.AssertRenderCountContent(0)
1392
1393 // This is included with RelPermalink in a shortcode used in p3, but it's fingerprinted
1394 // so we need to rebuild on change.
1395 b.EditFileReplaceFunc("content/foo.js", func(s string) string {
1396 return strings.Replace(s, "Foo", "Foo Edited", 1)
1397 }).Build()
1398
1399 // Verify that the hash has changed.
1400 b.AssertFileContent("public/p3/index.html", "foo.3a332a088521231e5fc9bd22f15e0ccf507faa7b373fbff22959005b9a80481c.js")
1401
1402 b.AssertRenderCountPage(1)
1403 b.AssertRenderCountContent(1)
1404 }
1405
1406 func TestRebuildEditData(t *testing.T) {
1407 t.Parallel()
1408
1409 files := `
1410 -- hugo.toml --
1411 disableLiveReload = true
1412 [security]
1413 enableInlineShortcodes=true
1414 -- data/mydata.yaml --
1415 foo: bar
1416 -- content/_index.md --
1417 ---
1418 title: "Home"
1419 ---
1420 {{< data "mydata.foo" >}}}
1421 -- content/p1.md --
1422 ---
1423 title: "P1"
1424 ---
1425
1426 Foo inline: {{< foo.inline >}}{{ hugo.Data.mydata.foo }}|{{< /foo.inline >}}
1427 -- layouts/_shortcodes/data.html --
1428 {{ $path := split (.Get 0) "." }}
1429 {{ $data := index hugo.Data $path }}
1430 Foo: {{ $data }}|
1431 -- layouts/home.html --
1432 Content: {{ .Content }}|
1433 -- layouts/single.html --
1434 Single: {{ .Content }}|
1435 `
1436 b := TestRunning(t, files)
1437
1438 b.AssertFileContent("public/index.html", "Foo: bar|")
1439 b.AssertFileContent("public/p1/index.html", "Foo inline: bar|")
1440 b.EditFileReplaceFunc("data/mydata.yaml", func(s string) string {
1441 return strings.Replace(s, "bar", "bar edited", 1)
1442 }).Build()
1443 b.AssertFileContent("public/index.html", "Foo: bar edited|")
1444 b.AssertFileContent("public/p1/index.html", "Foo inline: bar edited|")
1445 }
1446
1447 func TestRebuildEditHomeContent(t *testing.T) {
1448 t.Parallel()
1449
1450 files := `
1451 -- hugo.toml --
1452 baseURL = "https://example.com"
1453 disableLiveReload = true
1454 -- content/_index.md --
1455 ---
1456 title: "Home"
1457 ---
1458 Home.
1459 -- layouts/home.html --
1460 Content: {{ .Content }}
1461 `
1462 b := TestRunning(t, files)
1463
1464 b.AssertFileContent("public/index.html", "Content: <p>Home.</p>")
1465 b.EditFileReplaceAll("content/_index.md", "Home.", "Home").Build()
1466 b.AssertFileContent("public/index.html", "Content: <p>Home</p>")
1467 }
1468
1469 // Issue #13014.
1470 func TestRebuildEditNotPermalinkableCustomOutputFormatTemplateInFastRenderMode(t *testing.T) {
1471 t.Parallel()
1472
1473 files := `
1474 -- hugo.toml --
1475 baseURL = "https://example.com/docs/"
1476 disableLiveReload = true
1477 [internal]
1478 fastRenderMode = true
1479 disableKinds = ["taxonomy", "term", "sitemap", "robotsTXT", "404"]
1480 [outputFormats]
1481 [outputFormats.SearchIndex]
1482 baseName = 'Search'
1483 isPlainText = true
1484 mediaType = 'text/plain'
1485 noAlternative = true
1486 permalinkable = false
1487
1488 [outputs]
1489 home = ['HTML', 'SearchIndex']
1490 -- content/_index.md --
1491 ---
1492 title: "Home"
1493 ---
1494 Home.
1495 -- layouts/home.html --
1496 Home.
1497 -- layouts/index.searchindex.txt --
1498 Text. {{ .Title }}|{{ .RelPermalink }}|
1499
1500 `
1501 b := TestRunning(t, files, TestOptInfo())
1502
1503 b.AssertFileContent("public/search.txt", "Text.")
1504
1505 b.EditFileReplaceAll("layouts/index.searchindex.txt", "Text.", "Text Edited.").Build()
1506
1507 b.BuildPartial("/docs/search.txt")
1508
1509 b.AssertFileContent("public/search.txt", "Text Edited.")
1510 }
1511
1512 func TestRebuildVariationsAssetsJSImport(t *testing.T) {
1513 t.Parallel()
1514 files := `
1515 -- hugo.toml --
1516 baseURL = "https://example.com"
1517 disableKinds = ["term", "taxonomy"]
1518 disableLiveReload = true
1519 -- layouts/home.html --
1520 Home. {{ now }}
1521 {{ with (resources.Get "js/main.js" | js.Build | fingerprint) }}
1522 <script>{{ .Content | safeJS }}</script>
1523 {{ end }}
1524 -- assets/js/lib/foo.js --
1525 export function foo() {
1526 console.log("Foo");
1527 }
1528 -- assets/js/main.js --
1529 import { foo } from "./lib/foo.js";
1530 console.log("Hello");
1531 foo();
1532 `
1533
1534 b := TestRunning(
1535 t, files,
1536 TestOptOsFs(),
1537 )
1538
1539 b.AssertFileContent("public/index.html", "Home.", "Hello", "Foo")
1540 // Edit the imported file.
1541 b.EditFileReplaceAll("assets/js/lib/foo.js", "Foo", "Foo Edited").Build()
1542 b.AssertFileContent("public/index.html", "Home.", "Hello", "Foo Edited")
1543 }
1544
1545 func TestRebuildVariationsAssetsPostCSSImport(t *testing.T) {
1546 if !htesting.IsCI() {
1547 t.Skip("skip CI only")
1548 }
1549
1550 files := `
1551 -- hugo.toml --
1552 baseURL = "https://example.com"
1553 disableKinds = ["term", "taxonomy", "sitemap", "rss"]
1554 disableLiveReload = true
1555 -- assets/css/lib/foo.css --
1556 body {
1557 background: red;
1558 }
1559 -- assets/css/main.css --
1560 @import "lib/foo.css";
1561 -- package.json --
1562 {
1563 "devDependencies": {
1564 "postcss-cli": "^9.0.1"
1565 }
1566 }
1567 -- content/p1.md --
1568 ---
1569 title: "P1"
1570 ---
1571 -- content/p2.md --
1572 ---
1573 title: "P2"
1574 layout: "foo"
1575 ---
1576 {{< fingerprinted >}}
1577 -- content/p3.md --
1578 ---
1579 title: "P3"
1580 layout: "foo"
1581 ---
1582 {{< notfingerprinted >}}
1583 -- layouts/_shortcodes/fingerprinted.html --
1584 Fingerprinted.
1585 {{ $opts := dict "inlineImports" true "noMap" true }}
1586 {{ with (resources.Get "css/main.css" | postCSS $opts | fingerprint) }}
1587 <style src="{{ .RelPermalink }}"></style>
1588 {{ end }}
1589 -- layouts/_shortcodes/notfingerprinted.html --
1590 Fingerprinted.
1591 {{ $opts := dict "inlineImports" true "noMap" true }}
1592 {{ with (resources.Get "css/main.css" | postCSS $opts) }}
1593 <style src="{{ .RelPermalink }}"></style>
1594 {{ end }}
1595 -- layouts/home.html --
1596 Home.
1597 {{ $opts := dict "inlineImports" true "noMap" true }}
1598 {{ with (resources.Get "css/main.css" | postCSS $opts) }}
1599 <style>{{ .Content | safeCSS }}</style>
1600 {{ end }}
1601 -- layouts/foo.html --
1602 Foo.
1603 {{ .Title }}|{{ .Content }}|
1604 -- layouts/single.html --
1605 Single.
1606 {{ $opts := dict "inlineImports" true "noMap" true }}
1607 {{ with (resources.Get "css/main.css" | postCSS $opts) }}
1608 <style src="{{ .RelPermalink }}"></style>
1609 {{ end }}
1610 `
1611
1612 b := TestRunning(
1613 t, files,
1614 TestOptOsFs(),
1615 TestOptWithNpmInstall(),
1616 )
1617
1618 b.AssertFileContent("public/index.html", "Home.", "<style>body {\n\tbackground: red;\n}</style>")
1619 b.AssertFileContent("public/p1/index.html", "Single.", "/css/main.css")
1620 b.AssertRenderCountPage(4)
1621
1622 // Edit the imported file.
1623 b.EditFileReplaceFunc("assets/css/lib/foo.css", func(s string) string {
1624 return strings.Replace(s, "red", "blue", 1)
1625 }).Build()
1626
1627 b.AssertRenderCountPage(4)
1628
1629 b.AssertFileContent("public/index.html", "Home.", "<style>body {\n\tbackground: blue;\n}</style>")
1630 }
1631
1632 func TestRebuildI18n(t *testing.T) {
1633 t.Parallel()
1634
1635 files := `
1636 -- hugo.toml --
1637 baseURL = "https://example.com"
1638 disableLiveReload = true
1639 -- i18n/en.toml --
1640 hello = "Hello"
1641 -- layouts/home.html --
1642 Hello: {{ i18n "hello" }}
1643 `
1644
1645 b := TestRunning(t, files)
1646
1647 b.AssertFileContent("public/index.html", "Hello: Hello")
1648
1649 b.EditFileReplaceAll("i18n/en.toml", "Hello", "Hugo").Build()
1650
1651 b.AssertFileContent("public/index.html", "Hello: Hugo")
1652 }
1653
1654 func TestRebuildEditContentNonDefaultLanguage(t *testing.T) {
1655 t.Parallel()
1656
1657 files := `
1658 -- hugo.toml --
1659 baseURL = "https://example.com"
1660 disableLiveReload = true
1661 defaultContentLanguage = "en"
1662 defaultContentLanguageInSubdir = true
1663 [languages]
1664 [languages.en]
1665 weight = 1
1666 [languages.nn]
1667 weight = 2
1668 -- content/p1/index.en.md --
1669 ---
1670 title: "P1 en"
1671 ---
1672 P1 en.
1673 -- content/p1/b.en.md --
1674 ---
1675 title: "B en"
1676 ---
1677 B en.
1678 -- content/p1/f1.en.txt --
1679 F1 en
1680 -- content/p1/index.nn.md --
1681 ---
1682 title: "P1 nn"
1683 ---
1684 P1 nn.
1685 -- content/p1/b.nn.md --
1686 ---
1687 title: "B nn"
1688 ---
1689 B nn.
1690 -- content/p1/f1.nn.txt --
1691 F1 nn
1692 -- layouts/all.html --
1693 All: {{ .Title }}|{{ .Kind }}|{{ .Content }}|Bundled File: {{ with .Resources.GetMatch "f1.*" }}{{ .Content }}{{ end }}|Bundled Page: {{ with .Resources.GetMatch "b.*" }}{{ .Content }}{{ end }}|
1694 `
1695
1696 b := TestRunning(t, files)
1697
1698 b.AssertFileContent("public/nn/p1/index.html", "All: P1 nn|page|<p>P1 nn.</p>", "F1 nn|")
1699 b.EditFileReplaceAll("content/p1/index.nn.md", "P1 nn.", "P1 nn edit.").Build()
1700 b.AssertFileContent("public/nn/p1/index.html", "All: P1 nn|page|<p>P1 nn edit.</p>")
1701 b.EditFileReplaceAll("content/p1/f1.nn.txt", "F1 nn", "F1 nn edit.").Build()
1702 b.AssertFileContent("public/nn/p1/index.html", "All: P1 nn|page|<p>P1 nn edit.</p>\n|Bundled File: F1 nn edit.|")
1703 b.EditFileReplaceAll("content/p1/b.nn.md", "B nn.", "B nn edit.").Build()
1704 b.AssertFileContent("public/nn/p1/index.html", "B nn edit.")
1705 }
1706
1707 func TestRebuildEditContentNonDefaultLanguageDifferentBundles(t *testing.T) {
1708 t.Parallel()
1709
1710 files := `
1711 -- hugo.toml --
1712 baseURL = "https://example.com"
1713 disableLiveReload = true
1714 defaultContentLanguage = "en"
1715 defaultContentLanguageInSubdir = true
1716 [languages]
1717 [languages.en]
1718 weight = 1
1719 contentDir = "content/en"
1720 [languages.nn]
1721 weight = 2
1722 contentDir = "content/nn"
1723 -- content/en/p1en/index.md --
1724 ---
1725 title: "P1 en"
1726 ---
1727 -- content/nn/p1nn/index.md --
1728 ---
1729 title: "P1 nn"
1730 ---
1731 P1 nn.
1732 -- layouts/single.html --
1733 Single: {{ .Title }}|{{ .Content }}|
1734 `
1735
1736 b := TestRunning(t, files)
1737 b.AssertFileContent("public/nn/p1nn/index.html", "Single: P1 nn|<p>P1 nn.</p>")
1738 b.EditFileReplaceAll("content/nn/p1nn/index.md", "P1 nn.", "P1 nn edit.").Build()
1739 b.AssertFileContent("public/nn/p1nn/index.html", "Single: P1 nn|<p>P1 nn edit.</p>\n|")
1740 b.AssertFileContent("public/nn/p1nn/index.html", "P1 nn edit.")
1741 }
1742
1743 func TestRebuildVariationsAssetsSassImport(t *testing.T) {
1744 if !htesting.IsCI() {
1745 t.Skip("skip CI only")
1746 }
1747
1748 filesTemplate := `
1749 -- hugo.toml --
1750 baseURL = "https://example.com"
1751 disableKinds = ["term", "taxonomy"]
1752 disableLiveReload = true
1753 -- assets/css/lib/foo.scss --
1754 body {
1755 background: red;
1756 }
1757 -- assets/css/main.scss --
1758 @import "lib/foo";
1759 -- layouts/home.html --
1760 Home.
1761 {{ $opts := dict "transpiler" "TRANSPILER" }}
1762 {{ with (resources.Get "css/main.scss" | toCSS $opts) }}
1763 <style>{{ .Content | safeCSS }}</style>
1764 {{ end }}
1765 `
1766
1767 runTest := func(transpiler string) {
1768 t.Run(transpiler, func(t *testing.T) {
1769 files := strings.Replace(filesTemplate, "TRANSPILER", transpiler, 1)
1770 b := TestRunning(
1771 t, files,
1772 TestOptOsFs(),
1773 )
1774
1775 b.AssertFileContent("public/index.html", "Home.", "background: red")
1776
1777 // Edit the imported file.
1778 b.EditFileReplaceFunc("assets/css/lib/foo.scss", func(s string) string {
1779 return strings.Replace(s, "red", "blue", 1)
1780 }).Build()
1781
1782 b.AssertFileContent("public/index.html", "Home.", "background: blue")
1783 })
1784 }
1785
1786 if scss.Supports() {
1787 runTest("libsass")
1788 }
1789
1790 if dartsass.Supports() {
1791 runTest("dartsass")
1792 }
1793 }
1794
1795 func benchmarkFilesEdit(count int) string {
1796 var files strings.Builder
1797 files.WriteString(`
1798 -- hugo.toml --
1799 baseURL = "https://example.com"
1800 disableKinds = ["term", "taxonomy"]
1801 disableLiveReload = true
1802 -- layouts/single.html --
1803 Single: {{ .Title }}|{{ .Content }}|
1804 -- layouts/list.html --
1805 List: {{ .Title }}|{{ .Content }}|
1806 -- content/mysect/_index.md --
1807 ---
1808 title: "My Sect"
1809 ---
1810 `)
1811
1812 contentTemplate := `
1813 ---
1814 title: "P%d"
1815 ---
1816 P%d Content.
1817 `
1818
1819 for i := range count {
1820 files.WriteString(fmt.Sprintf("-- content/mysect/p%d/index.md --\n%s", i, fmt.Sprintf(contentTemplate, i, i)))
1821 }
1822
1823 return files.String()
1824 }
1825
1826 func BenchmarkRebuildContentFileChange(b *testing.B) {
1827 files := benchmarkFilesEdit(500)
1828
1829 cfg := IntegrationTestConfig{
1830 T: b,
1831 TxtarString: files,
1832 Running: true,
1833 }
1834
1835 for b.Loop() {
1836 b.StopTimer()
1837 bb := NewIntegrationTestBuilder(cfg).Build()
1838 b.StartTimer()
1839 bb.EditFileReplaceFunc("content/mysect/p123/index.md", func(s string) string {
1840 return s + "... Edited"
1841 }).Build()
1842 }
1843 }
1844
1845 func TestRebuildConcat(t *testing.T) {
1846 files := `
1847 -- hugo.toml --
1848 baseURL = "https://example.com"
1849 disableLiveReload = true
1850 disableKinds = ["taxonomy", "term", "sitemap", "robotsTXT", "404", "rss"]
1851 -- assets/a.css --
1852 a
1853 -- assets/b.css --
1854 b
1855 -- assets/c.css --
1856 c
1857 -- assets/common/c1.css --
1858 c1
1859 -- assets/common/c2.css --
1860 c2
1861 -- layouts/home.html --
1862 {{ $a := resources.Get "a.css" }}
1863 {{ $b := resources.Get "b.css" }}
1864 {{ $common := resources.Match "common/*.css" | resources.Concat "common.css" | minify }}
1865 {{ $ab := slice $a $b $common | resources.Concat "ab.css" }}
1866 all: {{ $ab.RelPermalink }}
1867 `
1868 b := TestRunning(t, files)
1869
1870 b.AssertFileContent("public/ab.css", "abc1c2")
1871 b.EditFileReplaceAll("assets/common/c2.css", "c2", "c2 edited").Build()
1872 b.AssertFileContent("public/ab.css", "abc1c2 edited")
1873 b.AddFiles("assets/common/c3.css", "c3").Build()
1874 b.AssertFileContent("public/ab.css", "abc1c2 editedc3")
1875 }
1876
1877 func TestRebuildEditArchetypeFile(t *testing.T) {
1878 t.Parallel()
1879
1880 files := `
1881 -- hugo.toml --
1882 baseURL = "https://example.com"
1883 disableLiveReload = true
1884 -- archetypes/default.md --
1885 ---
1886 title: "Default"
1887 ---
1888 `
1889
1890 b := TestRunning(t, files)
1891 // Just make sure that it doesn't panic.
1892 b.EditFileReplaceAll("archetypes/default.md", "Default", "Default Edited").Build()
1893 }
1894
1895 func TestRebuildEditMixedCaseTemplateFileIssue12165(t *testing.T) {
1896 t.Parallel()
1897
1898 files := `
1899 -- hugo.toml --
1900 baseURL = "https://example.com"
1901 disableLiveReload = true
1902 -- layouts/_partials/MyTemplate.html --
1903 MyTemplate
1904 -- layouts/home.html --
1905 MyTemplate: {{ partial "MyTemplate.html" . }}|
1906
1907
1908 `
1909
1910 b := TestRunning(t, files)
1911
1912 b.AssertFileContent("public/index.html", "MyTemplate: MyTemplate")
1913
1914 b.EditFileReplaceAll("layouts/_partials/MyTemplate.html", "MyTemplate", "MyTemplate Edited").Build()
1915
1916 b.AssertFileContent("public/index.html", "MyTemplate: MyTemplate Edited")
1917 }
1918
1919 func TestRebuildEditInlinePartial13723(t *testing.T) {
1920 t.Parallel()
1921
1922 files := `
1923 -- hugo.toml --
1924 baseURL = "https://example.com"
1925 disableLiveReload = true
1926 title = "Foo"
1927 -- layouts/baseof.html --
1928 {{ block "main" . }}Main.{{ end }}
1929 {{ partial "myinlinepartialinbaseof.html" . }}|
1930 {{- define "_partials/myinlinepartialinbaseof.html" }}
1931 My inline partial in baseof.
1932 {{ end }}
1933 -- layouts/_partials/mypartial.html --
1934 Mypartial.
1935 {{ partial "myinlinepartial.html" . }}|
1936 {{- define "_partials/myinlinepartial.html" }}
1937 Mypartial Inline.|{{ .Title }}|
1938 {{ end }}
1939 -- layouts/_partials/myotherpartial.html --
1940 Myotherpartial.
1941 {{ partial "myotherinlinepartial.html" . }}|
1942 {{- define "_partials/myotherinlinepartial.html" }}
1943 Myotherpartial Inline.|{{ .Title }}|
1944 {{ return "myotherinlinepartial" }}
1945 {{ end }}
1946 -- layouts/all.html --
1947 {{ define "main" }}
1948 {{ partial "mypartial.html" . }}|
1949 {{ partial "myotherpartial.html" . }}|
1950 {{ partial "myinlinepartialinall.html" . }}|
1951 {{ end }}
1952 {{- define "_partials/myinlinepartialinall.html" }}
1953 My inline partial in all.
1954 {{ end }}
1955
1956 `
1957 b := TestRunning(t, files)
1958 b.AssertFileContent("public/index.html", "Mypartial.", "Mypartial Inline.|Foo")
1959
1960 // Edit inline partial in partial.
1961 b.EditFileReplaceAll("layouts/_partials/mypartial.html", "Mypartial Inline.", "Mypartial Inline Edited.").Build()
1962 b.AssertFileContent("public/index.html", "Mypartial Inline Edited.|Foo")
1963
1964 // Edit inline partial in baseof.
1965 b.EditFileReplaceAll("layouts/baseof.html", "My inline partial in baseof.", "My inline partial in baseof Edited.").Build()
1966 b.AssertFileContent("public/index.html", "My inline partial in baseof Edited.")
1967
1968 // Edit inline partial in all.
1969 b.EditFileReplaceAll("layouts/all.html", "My inline partial in all.", "My inline partial in all Edited.").Build()
1970 b.AssertFileContent("public/index.html", "My inline partial in all Edited.")
1971 }
1972
1973 func TestRebuildEditAsciiDocContentFile(t *testing.T) {
1974 if ok, err := asciidocext.Supports(); !ok {
1975 t.Skip(err)
1976 }
1977 files := `
1978 -- hugo.toml --
1979 baseURL = "https://example.com"
1980 disableLiveReload = true
1981 disableKinds = ["taxonomy", "term", "sitemap", "robotsTXT", "404", "rss", "home", "section"]
1982 [security]
1983 [security.exec]
1984 allow = ['^python$', '^rst2html.*', '^asciidoctor$']
1985 -- content/posts/p1.adoc --
1986 ---
1987 title: "P1"
1988 ---
1989 P1 Content.
1990 -- content/posts/p2.adoc --
1991 ---
1992 title: "P2"
1993 ---
1994 P2 Content.
1995 -- layouts/single.html --
1996 Single: {{ .Title }}|{{ .Content }}|
1997 `
1998 b := TestRunning(t, files)
1999 b.AssertFileContent("public/posts/p1/index.html",
2000 "Single: P1|<div class=\"paragraph\">\n<p>P1 Content.</p>\n</div>\n|")
2001 b.AssertRenderCountPage(2)
2002 b.AssertRenderCountContent(2)
2003
2004 b.EditFileReplaceAll("content/posts/p1.adoc", "P1 Content.", "P1 Content Edited.").Build()
2005
2006 b.AssertFileContent("public/posts/p1/index.html", "Single: P1|<div class=\"paragraph\">\n<p>P1 Content Edited.</p>\n</div>\n|")
2007 b.AssertRenderCountPage(1)
2008 b.AssertRenderCountContent(1)
2009 }
2010
2011 func TestRebuildEditSingleListChangeUbuntuIssue12362(t *testing.T) {
2012 t.Parallel()
2013
2014 files := `
2015 -- hugo.toml --
2016 disableKinds = ['rss','section','sitemap','taxonomy','term']
2017 disableLiveReload = true
2018 -- layouts/list.html --
2019 {{ range .Pages }}{{ .Title }}|{{ end }}
2020 -- layouts/single.html --
2021 {{ .Title }}
2022 -- content/p1.md --
2023 ---
2024 title: p1
2025 ---
2026 `
2027
2028 b := TestRunning(t, files)
2029 b.AssertFileContent("public/index.html", "p1|")
2030
2031 b.AddFiles("content/p2.md", "---\ntitle: p2\n---").Build()
2032 b.AssertFileContent("public/index.html", "p1|p2|") // this test passes, which doesn't match reality
2033 }
2034
2035 func TestRebuildHomeThenPageIssue12436(t *testing.T) {
2036 t.Parallel()
2037
2038 files := `
2039 -- hugo.toml --
2040 baseURL = "https://example.com"
2041 disableKinds = ['sitemap','taxonomy','term']
2042 disableLiveReload = true
2043 -- layouts/list.html --
2044 {{ .Content }}
2045 -- layouts/single.html --
2046 {{ .Content }}
2047 -- content/_index.md --
2048 ---
2049 title: home
2050 ---
2051 home-content|
2052 -- content/p1/index.md --
2053 ---
2054 title: p1
2055 ---
2056 p1-content|
2057 `
2058
2059 b := TestRunning(t, files)
2060
2061 b.AssertFileContent("public/index.html", "home-content|")
2062 b.AssertFileContent("public/p1/index.html", "p1-content|")
2063 b.AssertRenderCountPage(3)
2064
2065 b.EditFileReplaceAll("content/_index.md", "home-content", "home-content-foo").Build()
2066 b.AssertFileContent("public/index.html", "home-content-foo")
2067 b.AssertRenderCountPage(2) // Home page rss + html
2068
2069 b.EditFileReplaceAll("content/p1/index.md", "p1-content", "p1-content-foo").Build()
2070 b.AssertFileContent("public/p1/index.html", "p1-content-foo")
2071 }
2072
2073 func TestRebuildEditTagIssue13648(t *testing.T) {
2074 t.Parallel()
2075
2076 files := `
2077 -- hugo.toml --
2078 baseURL = "https://example.com"
2079 disableLiveReload = true
2080 -- layouts/all.html --
2081 All. {{ range .Pages }}{{ .Title }}|{{ end }}$
2082 -- content/p1.md --
2083 ---
2084 title: "P1"
2085 tags: ["tag1"]
2086 ---
2087
2088 `
2089 b := TestRunning(t, files)
2090
2091 b.AssertFileContent("public/tags/index.html", "All. Tag1|")
2092 b.EditFileReplaceAll("content/p1.md", "tag1", "tag2").Build()
2093
2094 b.AssertFileContent("public/tags/index.html", "All. Tag1|$")
2095 }
2096
2097 func TestRebuildPageWithChineseTag14240(t *testing.T) {
2098 t.Parallel()
2099
2100 files := `
2101 -- hugo.toml --
2102 baseURL = "https://example.com"
2103 disableLiveReload = true
2104 -- layouts/all.html --
2105 All. {{ range .Pages }}{{ .RelPermalink }}: {{ .Title }}|{{ end }}$
2106 -- content/p1.md --
2107 ---
2108 title: "P1"
2109 tags: ["欢迎"]
2110 ---
2111
2112 `
2113 b := TestRunning(t, files)
2114
2115 b.AssertFileContent("public/tags/欢迎/index.html", "All. /p1/: P1|$")
2116 b.EditFileReplaceAll("content/p1.md", "P1", "P1 edit").Build()
2117 b.AssertFileContent("public/tags/欢迎/index.html", "All. /p1/: P1 edit|$")
2118 }
2119
2120 func TestRebuildEditNonReferencedResourceIssue13748(t *testing.T) {
2121 t.Parallel()
2122
2123 files := `
2124 -- hugo.toml --
2125 baseURL = "https://example.com"
2126 disableLiveReload = true
2127 -- content/mybundle/index.md --
2128 -- content/mybundle/resource.txt --
2129 This is a resource file.
2130 -- layouts/all.html --
2131 All.
2132 `
2133 b := TestRunning(t, files)
2134
2135 b.AssertFileContent("public/mybundle/resource.txt", "This is a resource file.")
2136 b.EditFileReplaceAll("content/mybundle/resource.txt", "This is a resource file.", "This is an edited resource file.").Build()
2137 b.AssertFileContent("public/mybundle/resource.txt", "This is an edited resource file.")
2138 }
2139
2140 // Issue #14207
2141 func TestRebuildAddShortcodeThenEditContentUsingIt(t *testing.T) {
2142 t.Parallel()
2143
2144 files := `
2145 -- hugo.toml --
2146 baseURL = "https://example.com"
2147 disableLiveReload = true
2148 -- layouts/single.html --
2149 Single: {{ .Title }}|{{ .Content }}|
2150 -- content/p1.md --
2151 ---
2152 title: "P1"
2153 ---
2154 Content before shortcode.
2155 `
2156 b := TestRunning(t, files)
2157 b.AssertFileContent("public/p1/index.html", "Single: P1|", "Content before shortcode.")
2158
2159 // Add a new shortcode file
2160 b.AddFiles(
2161 "layouts/_shortcodes/year.html", `{{- now.Format "2006" -}}`,
2162 ).Build()
2163
2164 // Verify shortcode was registered
2165 ts := b.H.Sites[0].Deps.TemplateStore
2166 if ti := ts.LookupShortcodeByName("year"); ti == nil {
2167 t.Fatal("shortcode 'year' not found in cache after adding")
2168 }
2169
2170 // Now edit content to use the shortcode - this should find the shortcode
2171 currentYear := time.Now().Format("2006")
2172 b.EditFileReplaceAll("content/p1.md", "Content before shortcode.", "This is {{< year >}} wow.").Build()
2173 b.AssertFileContent("public/p1/index.html", "Single: P1|", fmt.Sprintf("This is %s wow.", currentYear))
2174 }
2175
2176 // Issue 14740.
2177 func TestRebuildEditTagsListLayout(t *testing.T) {
2178 files := `
2179 -- hugo.toml --
2180 baseURL = "https://example.com"
2181 -- layouts/tags/list.html --
2182 Foo.
2183 `
2184
2185 b := TestRunning(t, files)
2186 b.AssertFileContent("public/tags/index.html", "Foo.")
2187 b.EditFileReplaceAll("layouts/tags/list.html", "Foo", "Bar").Build()
2188 b.AssertFileContent("public/tags/index.html", "Bar.")
2189 }
2190
2191 // Atomic save (write temp file, then rename into place) of a content backed taxonomy root.
2192 // See issue 15130.
2193 func TestRebuildEditAtomicTaxonomyRoot(t *testing.T) {
2194 t.Parallel()
2195 files := `
2196 -- hugo.toml --
2197 baseURL = "https://example.com"
2198 disableLiveReload = true
2199 [taxonomies]
2200 object = "objects"
2201 -- content/objects/_index.md --
2202 ---
2203 title: "Objects"
2204 ---
2205 Objects content.
2206 -- content/p1.md --
2207 ---
2208 title: "P1"
2209 objects: ["o1"]
2210 ---
2211 -- layouts/all.html --
2212 {{ .Title }}|{{ .Kind }}|{{ .Content }}
2213 `
2214 b := TestRunning(t, files)
2215 b.AssertFileContent("public/objects/index.html", "Objects|taxonomy|<p>Objects content.</p>")
2216 b.EditFileAtomicReplaceAll("content/objects/_index.md", "Objects content.", "Objects content edited.").Build()
2217 b.AssertFileContent("public/objects/index.html", "Objects|taxonomy|<p>Objects content edited.</p>")
2218 b.AssertFileContent("public/objects/o1/index.html", "O1|term|")
2219 }
2220
2221 func TestRebuildEditMountedAssetOnlyRelPermalinkUsed(t *testing.T) {
2222 files := `
2223 -- hugo.toml --
2224 baseURL = "https://example.com"
2225 disableLiveReload = true
2226 disableKinds = ["taxonomy", "term", "rss", "sitemap", "section"]
2227 [module]
2228 [[module.mounts]]
2229 source = "assets"
2230 target = "assets"
2231 [[module.mounts]]
2232 source = "out"
2233 target = "assets/out"
2234 -- out/data.json --
2235 {"version": "v1"}
2236 -- content/_index.md --
2237 ---
2238 title: "Home"
2239 ---
2240 -- content/mytext.txt --
2241 mytext v1
2242 -- layouts/home.html --
2243 {{ $data := resources.Get "out/data.json" | minify }}
2244 Data: {{ $data.RelPermalink }}|
2245 `
2246 b := TestRunning(t, files)
2247 b.AssertFileContent("public/index.html", "Data: /out/data.min.json|")
2248 b.AssertFileContent("public/out/data.min.json", `{"version":"v1"}`)
2249
2250 // Simulate the periodic data refresh.
2251 b.EditFileReplaceAll("out/data.json", "v1", "v2").Build()
2252 b.AssertFileContent("public/out/data.min.json", `{"version":"v2"}`)
2253
2254 // An unrelated edit to a home bundle resource must not revert the
2255 // published data to an older version.
2256 b.EditFileReplaceAll("content/mytext.txt", "mytext v1", "mytext v2").Build()
2257 b.AssertFileContent("public/out/data.min.json", `{"version":"v2"}`)
2258
2259 // One more refresh round.
2260 b.EditFileReplaceAll("out/data.json", "v2", "v3").Build()
2261 b.AssertFileContent("public/out/data.min.json", `{"version":"v3"}`)
2262 }
2263
2264 func TestRebuildEditMountedAssetContentUsed(t *testing.T) {
2265 files := `
2266 -- hugo.toml --
2267 baseURL = "https://example.com"
2268 disableLiveReload = true
2269 disableKinds = ["taxonomy", "term", "rss", "sitemap", "section"]
2270 [module]
2271 [[module.mounts]]
2272 source = "assets"
2273 target = "assets"
2274 [[module.mounts]]
2275 source = "out"
2276 target = "assets/out"
2277 -- out/data.json --
2278 {"version": "v1"}
2279 -- content/_index.md --
2280 ---
2281 title: "Home"
2282 ---
2283 -- content/mytext.txt --
2284 mytext v1
2285 -- layouts/home.html --
2286 {{ $data := resources.Get "out/data.json" | minify }}
2287 Data: {{ $data.RelPermalink }}|{{ $data.Content | safeHTML }}|
2288 `
2289 b := TestRunning(t, files)
2290 b.AssertFileContent("public/index.html", "Data: /out/data.min.json|")
2291 b.AssertFileContent("public/out/data.min.json", `{"version":"v1"}`)
2292
2293 b.EditFileReplaceAll("out/data.json", "v1", "v2").Build()
2294 b.AssertFileContent("public/out/data.min.json", `{"version":"v2"}`)
2295 b.AssertFileContent("public/index.html", `{"version":"v2"}`)
2296
2297 b.EditFileReplaceAll("content/mytext.txt", "mytext v1", "mytext v2").Build()
2298 b.AssertFileContent("public/out/data.min.json", `{"version":"v2"}`)
2299
2300 b.EditFileReplaceAll("out/data.json", "v2", "v3").Build()
2301 b.AssertFileContent("public/out/data.min.json", `{"version":"v3"}`)
2302 b.AssertFileContent("public/index.html", `{"version":"v3"}`)
2303 }
2304
2305 func TestRebuildFastRenderEditMountedAssetNotRecentlyVisited(t *testing.T) {
2306 files := `
2307 -- hugo.toml --
2308 baseURL = "https://example.com"
2309 disableLiveReload = true
2310 disableKinds = ["taxonomy", "term", "rss", "sitemap", "section"]
2311 [module]
2312 [[module.mounts]]
2313 source = "assets"
2314 target = "assets"
2315 [[module.mounts]]
2316 source = "out"
2317 target = "assets/out"
2318 -- out/data.json --
2319 {"version": "v1"}
2320 -- content/_index.md --
2321 ---
2322 title: "Home"
2323 ---
2324 -- content/p1.md --
2325 ---
2326 title: "P1"
2327 ---
2328 -- layouts/home.html --
2329 {{ $data := resources.Get "out/data.json" | minify }}
2330 Data: {{ $data.RelPermalink }}|
2331 -- layouts/single.html --
2332 Single: {{ .Title }}|
2333 `
2334 recentlyVisited := types.NewEvictingQueue[string](20).Add("/p1/")
2335 b := TestRunning(t, files, func(cfg *IntegrationTestConfig) {
2336 cfg.FastRenderMode = true
2337 cfg.BuildCfg = BuildCfg{RecentlyTouched: recentlyVisited}
2338 })
2339
2340 b.AssertFileContent("public/index.html", "Data: /out/data.min.json|")
2341 b.AssertFileContent("public/out/data.min.json", `{"version":"v1"}`)
2342
2343 // Simulate the periodic data refresh.
2344 b.EditFileReplaceAll("out/data.json", "v1", "v2").Build()
2345 b.AssertFileContent("public/out/data.min.json", `{"version":"v2"}`)
2346 }
2347
2348 // See issue 15330.
2349 func TestRebuildEditTwoContentFilesInSameDirSameBatch(t *testing.T) {
2350 t.Parallel()
2351
2352 files := `
2353 -- hugo.toml --
2354 disableKinds = ["taxonomy", "term", "rss", "sitemap", "home", "section"]
2355 disableLiveReload = true
2356 -- content/a.md --
2357 ---
2358 title: a
2359 ---
2360 page a
2361 -- content/c.md --
2362 ---
2363 title: c
2364 ---
2365 page c
2366 -- layouts/page.html --
2367 {{ .Content }}|Summary: {{ .Summary }}|
2368 `
2369 b := TestRunning(t, files)
2370 b.AssertFileContent("public/c/index.html", "<p>page c</p>")
2371
2372 // a.md rewritten with identical content, c.md grows, same batch.
2373 b.EditFiles("content/a.md", "---\ntitle: a\n---\npage a\n")
2374 b.EditFiles("content/c.md", "---\ntitle: c\n---\npage c\nappended line\n")
2375 b.Build()
2376 b.AssertFileContent("public/c/index.html", "<p>page c\nappended line</p>")
2377
2378 // c.md shrinks.
2379 b.EditFiles("content/a.md", "---\ntitle: a\n---\npage a\n")
2380 b.EditFiles("content/c.md", "---\ntitle: c\n---\nc\n")
2381 b.Build()
2382 b.AssertFileContent("public/c/index.html", "<p>c</p>", "|Summary: <p>c</p>|")
2383 }