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