integration_test.go (3125B)
1 package tplimpl_test
2
3 import (
4 "path/filepath"
5 "testing"
6
7 qt "github.com/frankban/quicktest"
8 "github.com/gohugoio/hugo/hugolib"
9 "github.com/gohugoio/hugo/tpl"
10 )
11
12 func TestPrintUnusedTemplates(t *testing.T) {
13 t.Parallel()
14
15 files := `
16 -- config.toml --
17 baseURL = 'http://example.com/'
18 printUnusedTemplates=true
19 -- content/p1.md --
20 ---
21 title: "P1"
22 ---
23 {{< usedshortcode >}}
24 -- layouts/baseof.html --
25 {{ block "main" . }}{{ end }}
26 -- layouts/baseof.json --
27 {{ block "main" . }}{{ end }}
28 -- layouts/index.html --
29 {{ define "main" }}FOO{{ end }}
30 -- layouts/_default/single.json --
31 -- layouts/_default/single.html --
32 {{ define "main" }}MAIN{{ end }}
33 -- layouts/post/single.html --
34 {{ define "main" }}MAIN{{ end }}
35 -- layouts/partials/usedpartial.html --
36 -- layouts/partials/unusedpartial.html --
37 -- layouts/shortcodes/usedshortcode.html --
38 {{ partial "usedpartial.html" }}
39 -- layouts/shortcodes/unusedshortcode.html --
40
41 `
42
43 b := hugolib.NewIntegrationTestBuilder(
44 hugolib.IntegrationTestConfig{
45 T: t,
46 TxtarString: files,
47 NeedsOsFS: true,
48 },
49 )
50 b.Build()
51
52 unused := b.H.Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates()
53
54 var names []string
55 for _, tmpl := range unused {
56 names = append(names, tmpl.Name())
57 }
58
59 b.Assert(names, qt.DeepEquals, []string{"_default/single.json", "baseof.json", "partials/unusedpartial.html", "post/single.html", "shortcodes/unusedshortcode.html"})
60 b.Assert(unused[0].Filename(), qt.Equals, filepath.Join(b.Cfg.WorkingDir, "layouts/_default/single.json"))
61 }
62
63 // Verify that the new keywords in Go 1.18 is available.
64 func TestGo18Constructs(t *testing.T) {
65 t.Parallel()
66
67 files := `
68 -- config.toml --
69 baseURL = 'http://example.com/'
70 disableKinds = ["section", "home", "rss", "taxonomy", "term", "rss"]
71 -- content/p1.md --
72 ---
73 title: "P1"
74 ---
75 -- layouts/partials/counter.html --
76 {{ if .Scratch.Get "counter" }}{{ .Scratch.Add "counter" 1 }}{{ else }}{{ .Scratch.Set "counter" 1 }}{{ end }}{{ return true }}
77 -- layouts/_default/single.html --
78 continue:{{ range seq 5 }}{{ if eq . 2 }}{{continue}}{{ end }}{{ . }}{{ end }}:END:
79 break:{{ range seq 5 }}{{ if eq . 2 }}{{break}}{{ end }}{{ . }}{{ end }}:END:
80 continue2:{{ range seq 5 }}{{ if eq . 2 }}{{ continue }}{{ end }}{{ . }}{{ end }}:END:
81 break2:{{ range seq 5 }}{{ if eq . 2 }}{{ break }}{{ end }}{{ . }}{{ end }}:END:
82
83 counter1: {{ partial "counter.html" . }}/{{ .Scratch.Get "counter" }}
84 and1: {{ if (and false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
85 or1: {{ if (or true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
86 and2: {{ if (and true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
87 or2: {{ if (or false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
88
89
90 counter2: {{ .Scratch.Get "counter" }}
91
92
93 `
94
95 b := hugolib.NewIntegrationTestBuilder(
96 hugolib.IntegrationTestConfig{
97 T: t,
98 TxtarString: files,
99 NeedsOsFS: true,
100 },
101 )
102 b.Build()
103
104 b.AssertFileContent("public/p1/index.html", `
105 continue:1345:END:
106 break:1:END:
107 continue2:1345:END:
108 break2:1:END:
109 counter1: true/1
110 and1: false
111 or1: true
112 and2: true
113 or2: true
114 counter2: 3
115 `)
116
117 }