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 baseURL = "/"
172 disableKinds = ['home','rss','section','taxonomy','term']
173 [sitemap]
174 disable = true
175 -- content/p1.md --
176 ---
177 title: p1
178 sitemap:
179 p1_disable: foo
180 ---
181 -- content/p2.md --
182 ---
183 title: p2
184
185 ---
186 -- layouts/single.html --
187 {{ .Title }}
188 `
189
190 // Test A: Exclude all pages via project config.
191 b := hugolib.Test(t, files)
192 b.AssertFileContentExact(
193 "public/sitemap.xml",
194 "<?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",
195 )
196
197 // Test B: Include all pages via project config.
198 files_b := strings.ReplaceAll(files, "disable = true", "disable = false")
199 b = hugolib.Test(t, files_b)
200 b.AssertFileContentExact(
201 "public/sitemap.xml",
202 "<?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",
203 )
204
205 // Test C: Exclude all pages via project config, but include p1 via front matter.
206 files_c := strings.ReplaceAll(files, "p1_disable: foo", "disable: false")
207 b = hugolib.Test(t, files_c)
208 b.AssertFileContentExact(
209 "public/sitemap.xml",
210 "<?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",
211 )
212
213 // Test D: Include all pages via project config, but exclude p1 via front matter.
214 files_d := strings.ReplaceAll(files_b, "p1_disable: foo", "disable: true")
215 b = hugolib.Test(t, files_d)
216 b.AssertFileContentExact(
217 "public/sitemap.xml",
218 "<?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",
219 )
220 }
221
222 // Issue 12963
223 func TestEditBaseofParseAfterExecute(t *testing.T) {
224 files := `
225 -- hugo.toml --
226 baseURL = "https://example.com"
227 disableLiveReload = true
228 disableKinds = ["taxonomy", "term", "rss", "404", "sitemap"]
229 [internal]
230 fastRenderMode = true
231 -- layouts/baseof.html --
232 Baseof!
233 {{ block "main" . }}default{{ end }}
234 {{ with (templates.Defer (dict "key" "global")) }}
235 Now. {{ now }}
236 {{ end }}
237 -- layouts/single.html --
238 {{ define "main" }}
239 Single.
240 {{ end }}
241 -- layouts/list.html --
242 {{ define "main" }}
243 List.
244 {{ .Content }}
245 {{ range .Pages }}{{ .Title }}{{ end }}|
246 {{ end }}
247 -- content/mybundle1/index.md --
248 ---
249 title: "My Bundle 1"
250 ---
251 -- content/mybundle2/index.md --
252 ---
253 title: "My Bundle 2"
254 ---
255 -- content/_index.md --
256 ---
257 title: "Home"
258 ---
259 Home!
260 `
261
262 b := hugolib.TestRunning(t, files)
263 b.AssertFileContent("public/index.html", "Home!")
264 b.EditFileReplaceAll("layouts/baseof.html", "baseof", "Baseof!").Build()
265 b.BuildPartial("/")
266 b.AssertFileContent("public/index.html", "Baseof!")
267 b.BuildPartial("/mybundle1/")
268 b.AssertFileContent("public/mybundle1/index.html", "Baseof!")
269 }