| 1 |
// Copyright 2024 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 |
qt "github.com/frankban/quicktest" |
| 25 |
|
| 26 |
"github.com/gohugoio/hugo/htesting" |
| 27 |
"github.com/gohugoio/hugo/htesting/hqt" |
| 28 |
"github.com/gohugoio/hugo/hugolib" |
| 29 |
) |
| 30 |
|
| 31 |
func TestInclude(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
files := ` |
| 35 |
-- hugo.toml -- |
| 36 |
baseURL = 'http://example.com/' |
| 37 |
-- layouts/home.html -- |
| 38 |
partial: {{ partials.Include "foo.html" . }} |
| 39 |
-- layouts/_partials/foo.html -- |
| 40 |
foo |
| 41 |
` |
| 42 |
|
| 43 |
b := hugolib.Test(t, files) |
| 44 |
|
| 45 |
b.AssertFileContent("public/index.html", ` |
| 46 |
partial: foo |
| 47 |
`) |
| 48 |
} |
| 49 |
|
| 50 |
func TestIncludeCached(t *testing.T) { |
| 51 |
t.Parallel() |
| 52 |
|
| 53 |
files := ` |
| 54 |
-- hugo.toml -- |
| 55 |
baseURL = 'http://example.com/' |
| 56 |
-- layouts/home.html -- |
| 57 |
partialCached: {{ partials.IncludeCached "foo.html" . }} |
| 58 |
partialCached: {{ partials.IncludeCached "foo.html" . }} |
| 59 |
-- layouts/_partials/foo.html -- |
| 60 |
foo |
| 61 |
` |
| 62 |
|
| 63 |
b := hugolib.Test(t, files) |
| 64 |
|
| 65 |
b.AssertFileContent("public/index.html", ` |
| 66 |
partialCached: foo |
| 67 |
partialCached: foo |
| 68 |
`) |
| 69 |
} |
| 70 |
|
| 71 |
// Issue 9519 |
| 72 |
func TestIncludeCachedRecursion(t *testing.T) { |
| 73 |
t.Parallel() |
| 74 |
|
| 75 |
files := ` |
| 76 |
-- hugo.toml -- |
| 77 |
baseURL = 'http://example.com/' |
| 78 |
-- layouts/home.html -- |
| 79 |
{{ partials.IncludeCached "p1.html" . }} |
| 80 |
-- layouts/_partials/p1.html -- |
| 81 |
{{ partials.IncludeCached "p2.html" . }} |
| 82 |
-- layouts/_partials/p2.html -- |
| 83 |
P2 |
| 84 |
|
| 85 |
` |
| 86 |
|
| 87 |
b := hugolib.Test(t, files) |
| 88 |
|
| 89 |
b.AssertFileContent("public/index.html", ` |
| 90 |
P2 |
| 91 |
`) |
| 92 |
} |
| 93 |
|
| 94 |
// Issue #588 |
| 95 |
func TestIncludeCachedRecursionShortcode(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
|
| 98 |
files := ` |
| 99 |
-- hugo.toml -- |
| 100 |
baseURL = 'http://example.com/' |
| 101 |
-- content/_index.md -- |
| 102 |
--- |
| 103 |
title: "Index" |
| 104 |
--- |
| 105 |
{{< short >}} |
| 106 |
-- layouts/home.html -- |
| 107 |
{{ partials.IncludeCached "p1.html" . }} |
| 108 |
-- layouts/_partials/p1.html -- |
| 109 |
{{ .Content }} |
| 110 |
{{ partials.IncludeCached "p2.html" . }} |
| 111 |
-- layouts/_partials/p2.html -- |
| 112 |
-- layouts/_shortcodes/short.html -- |
| 113 |
SHORT |
| 114 |
{{ partials.IncludeCached "p2.html" . }} |
| 115 |
P2 |
| 116 |
|
| 117 |
` |
| 118 |
|
| 119 |
b := hugolib.Test(t, files) |
| 120 |
|
| 121 |
b.AssertFileContent("public/index.html", ` |
| 122 |
SHORT |
| 123 |
P2 |
| 124 |
`) |
| 125 |
} |
| 126 |
|
| 127 |
func TestIncludeCacheHints(t *testing.T) { |
| 128 |
t.Parallel() |
| 129 |
|
| 130 |
files := ` |
| 131 |
-- hugo.toml -- |
| 132 |
baseURL = 'http://example.com/' |
| 133 |
templateMetrics=true |
| 134 |
templateMetricsHints=true |
| 135 |
disableKinds = ["page", "section", "taxonomy", "term", "sitemap"] |
| 136 |
[outputs] |
| 137 |
home = ["HTML"] |
| 138 |
-- layouts/home.html -- |
| 139 |
{{ partials.IncludeCached "static1.html" . }} |
| 140 |
{{ partials.IncludeCached "static1.html" . }} |
| 141 |
{{ partials.Include "static2.html" . }} |
| 142 |
|
| 143 |
D1I: {{ partials.Include "dynamic1.html" . }} |
| 144 |
D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 145 |
D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 146 |
D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 147 |
H1I: {{ partials.Include "halfdynamic1.html" . }} |
| 148 |
H1C: {{ partials.IncludeCached "halfdynamic1.html" . }} |
| 149 |
H1C: {{ partials.IncludeCached "halfdynamic1.html" . }} |
| 150 |
|
| 151 |
-- layouts/_partials/static1.html -- |
| 152 |
P1 |
| 153 |
-- layouts/_partials/static2.html -- |
| 154 |
P2 |
| 155 |
-- layouts/_partials/dynamic1.html -- |
| 156 |
{{ math.Counter }} |
| 157 |
-- layouts/_partials/halfdynamic1.html -- |
| 158 |
D1 |
| 159 |
{{ math.Counter }} |
| 160 |
|
| 161 |
|
| 162 |
` |
| 163 |
|
| 164 |
b := hugolib.Test(t, files) |
| 165 |
|
| 166 |
// fmt.Println(b.FileContent("public/index.html")) |
| 167 |
|
| 168 |
var buf bytes.Buffer |
| 169 |
b.H.Metrics.WriteMetrics(&buf) |
| 170 |
|
| 171 |
got := buf.String() |
| 172 |
|
| 173 |
// Get rid of all the durations, they are never the same. |
| 174 |
durationRe := regexp.MustCompile(`\b[\.\d]*(ms|ns|µs|s)\b`) |
| 175 |
|
| 176 |
normalize := func(s string) string { |
| 177 |
s = durationRe.ReplaceAllString(s, "") |
| 178 |
linesIn := strings.Split(s, "\n")[3:] |
| 179 |
var lines []string |
| 180 |
for _, l := range linesIn { |
| 181 |
l = strings.TrimSpace(l) |
| 182 |
if l == "" { |
| 183 |
continue |
| 184 |
} |
| 185 |
lines = append(lines, l) |
| 186 |
} |
| 187 |
|
| 188 |
sort.Strings(lines) |
| 189 |
|
| 190 |
return strings.Join(lines, "\n") |
| 191 |
} |
| 192 |
|
| 193 |
got = normalize(got) |
| 194 |
|
| 195 |
expect := ` |
| 196 |
0 0 0 1 home.html |
| 197 |
100 0 0 1 _partials/static2.html |
| 198 |
100 50 1 2 _partials/static1.html |
| 199 |
25 50 2 4 _partials/dynamic1.html |
| 200 |
66 33 1 3 _partials/halfdynamic1.html |
| 201 |
` |
| 202 |
|
| 203 |
b.Assert(got, hqt.IsSameString, expect) |
| 204 |
} |
| 205 |
|
| 206 |
// gobench --package ./tpl/partials |
| 207 |
func BenchmarkIncludeCached(b *testing.B) { |
| 208 |
var files strings.Builder |
| 209 |
files.WriteString(` |
| 210 |
-- hugo.toml -- |
| 211 |
baseURL = 'http://example.com/' |
| 212 |
-- layouts/home.html -- |
| 213 |
-- layouts/single.html -- |
| 214 |
{{ partialCached "heavy.html" "foo" }} |
| 215 |
{{ partialCached "easy1.html" "bar" }} |
| 216 |
{{ partialCached "easy1.html" "baz" }} |
| 217 |
{{ partialCached "easy2.html" "baz" }} |
| 218 |
-- layouts/_partials/easy1.html -- |
| 219 |
ABCD |
| 220 |
-- layouts/_partials/easy2.html -- |
| 221 |
ABCDE |
| 222 |
-- layouts/_partials/heavy.html -- |
| 223 |
{{ $result := slice }} |
| 224 |
{{ range site.RegularPages }} |
| 225 |
{{ $result = $result | append (dict "title" .Title "link" .RelPermalink "readingTime" .ReadingTime) }} |
| 226 |
{{ end }} |
| 227 |
{{ range $result }} |
| 228 |
* {{ .title }} {{ .link }} {{ .readingTime }} |
| 229 |
{{ end }} |
| 230 |
|
| 231 |
|
| 232 |
`) |
| 233 |
|
| 234 |
for i := 1; i < 100; i++ { |
| 235 |
files.WriteString(fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i)) |
| 236 |
} |
| 237 |
|
| 238 |
cfg := hugolib.IntegrationTestConfig{ |
| 239 |
T: b, |
| 240 |
TxtarString: files.String(), |
| 241 |
} |
| 242 |
|
| 243 |
for b.Loop() { |
| 244 |
b.StopTimer() |
| 245 |
bb := hugolib.NewIntegrationTestBuilder(cfg) |
| 246 |
b.StartTimer() |
| 247 |
bb.Build() |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestIncludeTimeout(t *testing.T) { |
| 252 |
htesting.SkipSlowTestUnlessCI(t) |
| 253 |
t.Parallel() |
| 254 |
|
| 255 |
files := ` |
| 256 |
-- hugo.toml -- |
| 257 |
baseURL = 'http://example.com/' |
| 258 |
-- layouts/home.html -- |
| 259 |
{{ partials.Include "foo.html" . }} |
| 260 |
-- layouts/_partials/foo.html -- |
| 261 |
{{ partial "foo.html" . }} |
| 262 |
` |
| 263 |
|
| 264 |
b, err := hugolib.TestE(t, files) |
| 265 |
|
| 266 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 267 |
b.Assert(err.Error(), qt.Contains, "maximum template call stack size exceeded") |
| 268 |
} |
| 269 |
|
| 270 |
func TestIncludeCachedTimeout(t *testing.T) { |
| 271 |
t.Parallel() |
| 272 |
|
| 273 |
files := ` |
| 274 |
-- hugo.toml -- |
| 275 |
baseURL = 'http://example.com/' |
| 276 |
timeout = '200ms' |
| 277 |
-- layouts/home.html -- |
| 278 |
{{ partials.IncludeCached "foo.html" . }} |
| 279 |
-- layouts/_partials/foo.html -- |
| 280 |
{{ partialCached "bar.html" . }} |
| 281 |
-- layouts/_partials/bar.html -- |
| 282 |
{{ partialCached "foo.html" . }} |
| 283 |
` |
| 284 |
|
| 285 |
b, err := hugolib.TestE(t, files) |
| 286 |
|
| 287 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 288 |
b.Assert(err.Error(), qt.Contains, `error calling partialCached: circular call stack detected in partial`) |
| 289 |
} |
| 290 |
|
| 291 |
// See Issue #13889 |
| 292 |
func TestIncludeCachedDifferentKey(t *testing.T) { |
| 293 |
t.Parallel() |
| 294 |
|
| 295 |
files := ` |
| 296 |
-- hugo.toml -- |
| 297 |
baseURL = 'http://example.com/' |
| 298 |
timeout = '200ms' |
| 299 |
-- layouts/home.html -- |
| 300 |
{{ partialCached "foo.html" "a" "a" }} |
| 301 |
-- layouts/_partials/foo.html -- |
| 302 |
{{ if eq . "a" }} |
| 303 |
{{ partialCached "bar.html" . }} |
| 304 |
{{ else }} |
| 305 |
DONE |
| 306 |
{{ end }} |
| 307 |
-- layouts/_partials/bar.html -- |
| 308 |
{{ partialCached "foo.html" "b" "b" }} |
| 309 |
` |
| 310 |
b := hugolib.Test(t, files) |
| 311 |
|
| 312 |
b.AssertFileContent("public/index.html", ` |
| 313 |
DONE |
| 314 |
`) |
| 315 |
} |
| 316 |
|
| 317 |
// See Issue #10789 |
| 318 |
func TestReturnExecuteFromTemplateInPartial(t *testing.T) { |
| 319 |
t.Parallel() |
| 320 |
|
| 321 |
files := ` |
| 322 |
-- hugo.toml -- |
| 323 |
baseURL = 'http://example.com/' |
| 324 |
-- layouts/home.html -- |
| 325 |
{{ $r := partial "foo" }} |
| 326 |
FOO:{{ $r.Content }} |
| 327 |
-- layouts/_partials/foo.html -- |
| 328 |
{{ $r := §§{{ partial "bar" }}§§ | resources.FromString "bar.html" | resources.ExecuteAsTemplate "bar.html" . }} |
| 329 |
{{ return $r }} |
| 330 |
-- layouts/_partials/bar.html -- |
| 331 |
BAR |
| 332 |
` |
| 333 |
|
| 334 |
b := hugolib.Test(t, files) |
| 335 |
|
| 336 |
b.AssertFileContent("public/index.html", "OO:BAR") |
| 337 |
} |