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