Debian Codegraph @

tpl/tplimpl/tplimpl_integration_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Portions Copyright The Go Authors.
4
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package tplimpl_test
17
18 import (
19 "strings"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/hugolib"
24 )
25
26 // Verify that the new keywords in Go 1.18 is available.
27 func TestGo18Constructs(t *testing.T) {
28 t.Parallel()
29
30 files := `
31 -- hugo.toml --
32 baseURL = 'http://example.com/'
33 disableKinds = ["section", "home", "rss", "taxonomy", "term", "rss"]
34 -- content/p1.md --
35 ---
36 title: "P1"
37 ---
38 -- layouts/_partials/counter.html --
39 {{ if .Scratch.Get "counter" }}{{ .Scratch.Add "counter" 1 }}{{ else }}{{ .Scratch.Set "counter" 1 }}{{ end }}{{ return true }}
40 -- layouts/single.html --
41 continue:{{ range seq 5 }}{{ if eq . 2 }}{{continue}}{{ end }}{{ . }}{{ end }}:END:
42 break:{{ range seq 5 }}{{ if eq . 2 }}{{break}}{{ end }}{{ . }}{{ end }}:END:
43 continue2:{{ range seq 5 }}{{ if eq . 2 }}{{ continue }}{{ end }}{{ . }}{{ end }}:END:
44 break2:{{ range seq 5 }}{{ if eq . 2 }}{{ break }}{{ end }}{{ . }}{{ end }}:END:
45
46 counter1: {{ partial "counter.html" . }}/{{ .Scratch.Get "counter" }}
47 and1: {{ if (and false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
48 or1: {{ if (or true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
49 and2: {{ if (and true (partial "counter.html" .)) }}true{{ else }}false{{ end }}
50 or2: {{ if (or false (partial "counter.html" .)) }}true{{ else }}false{{ end }}
51
52
53 counter2: {{ .Scratch.Get "counter" }}
54
55
56 `
57
58 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
59
60 b.AssertFileContent("public/p1/index.html", `
61 continue:1345:END:
62 break:1:END:
63 continue2:1345:END:
64 break2:1:END:
65 counter1: true/1
66 and1: false
67 or1: true
68 and2: true
69 or2: true
70 counter2: 3
71 `)
72 }
73
74 func TestGo23ElseWith(t *testing.T) {
75 t.Parallel()
76
77 files := `
78 -- hugo.toml --
79 title = "Hugo"
80 -- layouts/home.html --
81 {{ with false }}{{ else with .Site }}{{ .Title }}{{ end }}|
82 `
83 b := hugolib.Test(t, files)
84
85 b.AssertFileContent("public/index.html", "Hugo|")
86 }
87
88 // Issue 10495
89 func TestCommentsBeforeBlockDefinition(t *testing.T) {
90 t.Parallel()
91
92 files := `
93 -- hugo.toml --
94 baseURL = 'http://example.com/'
95 -- content/s1/p1.md --
96 ---
97 title: "S1P1"
98 ---
99 -- content/s2/p1.md --
100 ---
101 title: "S2P1"
102 ---
103 -- content/s3/p1.md --
104 ---
105 title: "S3P1"
106 ---
107 -- layouts/baseof.html --
108 {{ block "main" . }}{{ end }}
109 -- layouts/s1/single.html --
110 {{/* foo */}}
111 {{ define "main" }}{{ .Title }}{{ end }}
112 -- layouts/s2/single.html --
113 {{- /* foo */}}
114 {{ define "main" }}{{ .Title }}{{ end }}
115 -- layouts/s3/single.html --
116 {{- /* foo */ -}}
117 {{ define "main" }}{{ .Title }}{{ end }}
118 `
119
120 b := hugolib.Test(t, files)
121
122 b.AssertFileContent("public/s1/p1/index.html", `S1P1`)
123 b.AssertFileContent("public/s2/p1/index.html", `S2P1`)
124 b.AssertFileContent("public/s3/p1/index.html", `S3P1`)
125 }
126
127 func TestGoTemplateBugs(t *testing.T) {
128 t.Run("Issue 11112", func(t *testing.T) {
129 t.Parallel()
130
131 files := `
132 -- hugo.toml --
133 -- layouts/home.html --
134 {{ $m := dict "key" "value" }}
135 {{ $k := "" }}
136 {{ $v := "" }}
137 {{ range $k, $v = $m }}
138 {{ $k }} = {{ $v }}
139 {{ end }}
140 `
141
142 b := hugolib.Test(t, files)
143
144 b.AssertFileContent("public/index.html", `key = value`)
145 })
146 }
147
148 func TestSecurityAllowActionJSTmpl(t *testing.T) {
149 filesTemplate := `
150 -- hugo.toml --
151 SECURITYCONFIG
152 -- layouts/home.html --
153 <script>
154 var a = §§{{.Title }}§§;
155 </script>
156 `
157
158 files := strings.ReplaceAll(filesTemplate, "SECURITYCONFIG", "")
159
160 b, err := hugolib.TestE(t, files)
161
162 // This used to fail, but not in >= Hugo 0.121.0.
163 b.Assert(err, qt.IsNil)
164 }
165
166 func TestSitemap(t *testing.T) {
167 t.Parallel()
168
169 files := `
170 -- hugo.toml --
171 disableKinds = ['home','rss','section','taxonomy','term']
172 [sitemap]
173 disable = true
174 -- content/p1.md --
175 ---
176 title: p1
177 sitemap:
178 p1_disable: foo
179 ---
180 -- content/p2.md --
181 ---
182 title: p2
183
184 ---
185 -- layouts/single.html --
186 {{ .Title }}
187 `
188
189 // Test A: Exclude all pages via project config.
190 b := hugolib.Test(t, files)
191 b.AssertFileContentExact("public/sitemap.xml",
192 "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n \n</urlset>\n",
193 )
194
195 // Test B: Include all pages via project config.
196 files_b := strings.ReplaceAll(files, "disable = true", "disable = false")
197 b = hugolib.Test(t, files_b)
198 b.AssertFileContentExact("public/sitemap.xml",
199 "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p1/</loc>\n </url><url>\n <loc>/p2/</loc>\n </url>\n</urlset>\n",
200 )
201
202 // Test C: Exclude all pages via project config, but include p1 via front matter.
203 files_c := strings.ReplaceAll(files, "p1_disable: foo", "disable: false")
204 b = hugolib.Test(t, files_c)
205 b.AssertFileContentExact("public/sitemap.xml",
206 "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p1/</loc>\n </url>\n</urlset>\n",
207 )
208
209 // Test D: Include all pages via project config, but exclude p1 via front matter.
210 files_d := strings.ReplaceAll(files_b, "p1_disable: foo", "disable: true")
211 b = hugolib.Test(t, files_d)
212 b.AssertFileContentExact("public/sitemap.xml",
213 "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n <url>\n <loc>/p2/</loc>\n </url>\n</urlset>\n",
214 )
215 }
216
217 // Issue 12963
218 func TestEditBaseofParseAfterExecute(t *testing.T) {
219 files := `
220 -- hugo.toml --
221 baseURL = "https://example.com"
222 disableLiveReload = true
223 disableKinds = ["taxonomy", "term", "rss", "404", "sitemap"]
224 [internal]
225 fastRenderMode = true
226 -- layouts/baseof.html --
227 Baseof!
228 {{ block "main" . }}default{{ end }}
229 {{ with (templates.Defer (dict "key" "global")) }}
230 Now. {{ now }}
231 {{ end }}
232 -- layouts/single.html --
233 {{ define "main" }}
234 Single.
235 {{ end }}
236 -- layouts/list.html --
237 {{ define "main" }}
238 List.
239 {{ .Content }}
240 {{ range .Pages }}{{ .Title }}{{ end }}|
241 {{ end }}
242 -- content/mybundle1/index.md --
243 ---
244 title: "My Bundle 1"
245 ---
246 -- content/mybundle2/index.md --
247 ---
248 title: "My Bundle 2"
249 ---
250 -- content/_index.md --
251 ---
252 title: "Home"
253 ---
254 Home!
255 `
256
257 b := hugolib.TestRunning(t, files)
258 b.AssertFileContent("public/index.html", "Home!")
259 b.EditFileReplaceAll("layouts/baseof.html", "baseof", "Baseof!").Build()
260 b.BuildPartial("/")
261 b.AssertFileContent("public/index.html", "Baseof!")
262 b.BuildPartial("/mybundle1/")
263 b.AssertFileContent("public/mybundle1/index.html", "Baseof!")
264 }