Debian Codegraph @

hugolib/rebuild_test.go

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