integration_test.go (6078B)
1 // Copyright 2022 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package partials_test
15
16 import (
17 "bytes"
18 "fmt"
19 "regexp"
20 "sort"
21 "strings"
22 "testing"
23
24 "github.com/gohugoio/hugo/htesting/hqt"
25 "github.com/gohugoio/hugo/hugolib"
26 )
27
28 func TestInclude(t *testing.T) {
29 t.Parallel()
30
31 files := `
32 -- config.toml --
33 baseURL = 'http://example.com/'
34 -- layouts/index.html --
35 partial: {{ partials.Include "foo.html" . }}
36 -- layouts/partials/foo.html --
37 foo
38 `
39
40 b := hugolib.NewIntegrationTestBuilder(
41 hugolib.IntegrationTestConfig{
42 T: t,
43 TxtarString: files,
44 },
45 ).Build()
46
47 b.AssertFileContent("public/index.html", `
48 partial: foo
49 `)
50 }
51
52 func TestIncludeCached(t *testing.T) {
53 t.Parallel()
54
55 files := `
56 -- config.toml --
57 baseURL = 'http://example.com/'
58 -- layouts/index.html --
59 partialCached: {{ partials.IncludeCached "foo.html" . }}
60 partialCached: {{ partials.IncludeCached "foo.html" . }}
61 -- layouts/partials/foo.html --
62 foo
63 `
64
65 b := hugolib.NewIntegrationTestBuilder(
66 hugolib.IntegrationTestConfig{
67 T: t,
68 TxtarString: files,
69 },
70 ).Build()
71
72 b.AssertFileContent("public/index.html", `
73 partialCached: foo
74 partialCached: foo
75 `)
76 }
77
78 // Issue 9519
79 func TestIncludeCachedRecursion(t *testing.T) {
80 t.Parallel()
81
82 files := `
83 -- config.toml --
84 baseURL = 'http://example.com/'
85 -- layouts/index.html --
86 {{ partials.IncludeCached "p1.html" . }}
87 -- layouts/partials/p1.html --
88 {{ partials.IncludeCached "p2.html" . }}
89 -- layouts/partials/p2.html --
90 P2
91
92 `
93
94 b := hugolib.NewIntegrationTestBuilder(
95 hugolib.IntegrationTestConfig{
96 T: t,
97 TxtarString: files,
98 },
99 ).Build()
100
101 b.AssertFileContent("public/index.html", `
102 P2
103 `)
104 }
105
106 // Issue #588
107 func TestIncludeCachedRecursionShortcode(t *testing.T) {
108 t.Parallel()
109
110 files := `
111 -- config.toml --
112 baseURL = 'http://example.com/'
113 -- content/_index.md --
114 ---
115 title: "Index"
116 ---
117 {{< short >}}
118 -- layouts/index.html --
119 {{ partials.IncludeCached "p1.html" . }}
120 -- layouts/partials/p1.html --
121 {{ .Content }}
122 {{ partials.IncludeCached "p2.html" . }}
123 -- layouts/partials/p2.html --
124 -- layouts/shortcodes/short.html --
125 SHORT
126 {{ partials.IncludeCached "p2.html" . }}
127 P2
128
129 `
130
131 b := hugolib.NewIntegrationTestBuilder(
132 hugolib.IntegrationTestConfig{
133 T: t,
134 TxtarString: files,
135 },
136 ).Build()
137
138 b.AssertFileContent("public/index.html", `
139 SHORT
140 P2
141 `)
142 }
143
144 func TestIncludeCacheHints(t *testing.T) {
145 t.Parallel()
146
147 files := `
148 -- config.toml --
149 baseURL = 'http://example.com/'
150 templateMetrics=true
151 templateMetricsHints=true
152 disableKinds = ["page", "section", "taxonomy", "term", "sitemap"]
153 [outputs]
154 home = ["HTML"]
155 -- layouts/index.html --
156 {{ partials.IncludeCached "static1.html" . }}
157 {{ partials.IncludeCached "static1.html" . }}
158 {{ partials.Include "static2.html" . }}
159
160 D1I: {{ partials.Include "dynamic1.html" . }}
161 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
162 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
163 D1C: {{ partials.IncludeCached "dynamic1.html" . }}
164 H1I: {{ partials.Include "halfdynamic1.html" . }}
165 H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
166 H1C: {{ partials.IncludeCached "halfdynamic1.html" . }}
167
168 -- layouts/partials/static1.html --
169 P1
170 -- layouts/partials/static2.html --
171 P2
172 -- layouts/partials/dynamic1.html --
173 {{ math.Counter }}
174 -- layouts/partials/halfdynamic1.html --
175 D1
176 {{ math.Counter }}
177
178
179 `
180
181 b := hugolib.NewIntegrationTestBuilder(
182 hugolib.IntegrationTestConfig{
183 T: t,
184 TxtarString: files,
185 },
186 ).Build()
187
188 // fmt.Println(b.FileContent("public/index.html"))
189
190 var buf bytes.Buffer
191 b.H.Metrics.WriteMetrics(&buf)
192
193 got := buf.String()
194
195 // Get rid of all the durations, they are never the same.
196 durationRe := regexp.MustCompile(`\b[\.\d]*(ms|µs|s)\b`)
197
198 normalize := func(s string) string {
199 s = durationRe.ReplaceAllString(s, "")
200 linesIn := strings.Split(s, "\n")[3:]
201 var lines []string
202 for _, l := range linesIn {
203 l = strings.TrimSpace(l)
204 if l == "" {
205 continue
206 }
207 lines = append(lines, l)
208 }
209
210 sort.Strings(lines)
211
212 return strings.Join(lines, "\n")
213 }
214
215 got = normalize(got)
216
217 expect := `
218 0 0 0 1 index.html
219 100 0 0 1 partials/static2.html
220 100 50 1 2 partials/static1.html
221 25 50 2 4 partials/dynamic1.html
222 66 33 1 3 partials/halfdynamic1.html
223 `
224
225 b.Assert(got, hqt.IsSameString, expect)
226 }
227
228 // gobench --package ./tpl/partials
229 func BenchmarkIncludeCached(b *testing.B) {
230 files := `
231 -- config.toml --
232 baseURL = 'http://example.com/'
233 -- layouts/index.html --
234 -- layouts/_default/single.html --
235 {{ partialCached "heavy.html" "foo" }}
236 {{ partialCached "easy1.html" "bar" }}
237 {{ partialCached "easy1.html" "baz" }}
238 {{ partialCached "easy2.html" "baz" }}
239 -- layouts/partials/easy1.html --
240 ABCD
241 -- layouts/partials/easy2.html --
242 ABCDE
243 -- layouts/partials/heavy.html --
244 {{ $result := slice }}
245 {{ range site.RegularPages }}
246 {{ $result = $result | append (dict "title" .Title "link" .RelPermalink "readingTime" .ReadingTime) }}
247 {{ end }}
248 {{ range $result }}
249 * {{ .title }} {{ .link }} {{ .readingTime }}
250 {{ end }}
251
252
253 `
254
255 for i := 1; i < 100; i++ {
256 files += fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i)
257 }
258
259 cfg := hugolib.IntegrationTestConfig{
260 T: b,
261 TxtarString: files,
262 }
263 builders := make([]*hugolib.IntegrationTestBuilder, b.N)
264
265 for i, _ := range builders {
266 builders[i] = hugolib.NewIntegrationTestBuilder(cfg)
267 }
268
269 b.ResetTimer()
270
271 for i := 0; i < b.N; i++ {
272 builders[i].Build()
273 }
274 }