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