Debian Codegraph @

hugolib/resource_chain_test.go

raw
1 // Copyright 2025 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 hugolib
15
16 import (
17 "encoding/base64"
18 "io"
19 "net/http"
20 "net/http/httptest"
21 "os"
22 "strings"
23 "testing"
24
25 "github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
26 )
27
28 func TestResourceChainBasic(t *testing.T) {
29 failIfHandler := func(h http.Handler) http.Handler {
30 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31 if r.URL.Path == "/fail.jpg" {
32 http.Error(w, "{ msg: failed }", http.StatusNotImplemented)
33 return
34 }
35 h.ServeHTTP(w, r)
36 })
37 }
38 ts := httptest.NewServer(
39 failIfHandler(http.FileServer(http.Dir("testdata/"))),
40 )
41 t.Cleanup(func() {
42 ts.Close()
43 })
44
45 files := `
46 -- hugo.toml --
47 baseURL = "http://example.com/"
48 [security.http]
49 urls = ['.*']
50 -- assets/images/sunset.jpg --
51 ` + getTestSunset(t) + `
52 -- layouts/home.html --
53 {{ $hello := "<h1> Hello World! </h1>" | resources.FromString "hello.html" | fingerprint "sha512" | minify | fingerprint }}
54 {{ $cssFingerprinted1 := "body { background-color: lightblue; }" | resources.FromString "styles.css" | minify | fingerprint }}
55 {{ $cssFingerprinted2 := "body { background-color: orange; }" | resources.FromString "styles2.css" | minify | fingerprint }}
56
57
58 HELLO: {{ $hello.Name }}|{{ $hello.RelPermalink }}|{{ $hello.Content | safeHTML }}
59
60 {{ $img := resources.Get "images/sunset.jpg" }}
61 {{ $fit := $img.Fit "200x200" }}
62 {{ $fit2 := $fit.Fit "100x200" }}
63 {{ $img = $img | fingerprint }}
64 SUNSET: {{ $img.Name }}|{{ $img.RelPermalink }}|{{ $img.Width }}|{{ len $img.Content }}
65 FIT: {{ $fit.Name }}|{{ $fit.RelPermalink }}|{{ $fit.Width }}
66 CSS integrity Data first: {{ $cssFingerprinted1.Data.Integrity }} {{ $cssFingerprinted1.RelPermalink }}
67 CSS integrity Data last: {{ $cssFingerprinted2.RelPermalink }} {{ $cssFingerprinted2.Data.Integrity }}
68
69 {{ $failedImg := try (resources.GetRemote "HTTPTEST_SERVER_URL/fail.jpg") }}
70 {{ $rimg := resources.GetRemote "HTTPTEST_SERVER_URL/sunset.jpg" }}
71 {{ $remotenotfound := resources.GetRemote "HTTPTEST_SERVER_URL/notfound.jpg" }}
72 {{ $localnotfound := resources.Get "images/notfound.jpg" }}
73 {{ $gopherprotocol := try (resources.GetRemote "gopher://example.org") }}
74 {{ $rfit := $rimg.Fit "200x200" }}
75 {{ $rfit2 := $rfit.Fit "100x200" }}
76 {{ $rimg = $rimg | fingerprint }}
77 SUNSET REMOTE: {{ $rimg.Name }}|{{ $rimg.RelPermalink }}|{{ $rimg.Width }}|{{ len $rimg.Content }}
78 FIT REMOTE: {{ $rfit.Name }}|{{ $rfit.RelPermalink }}|{{ $rfit.Width }}
79 REMOTE NOT FOUND: {{ if $remotenotfound }}FAILED{{ else}}OK{{ end }}
80 LOCAL NOT FOUND: {{ if $localnotfound }}FAILED{{ else}}OK{{ end }}
81 PRINT PROTOCOL ERROR1: {{ with $gopherprotocol }}{{ .Value | safeHTML }}{{ end }}
82 PRINT PROTOCOL ERROR2: {{ with $gopherprotocol }}{{ .Err | safeHTML }}{{ end }}
83 PRINT PROTOCOL ERROR DETAILS: {{ with $gopherprotocol }}{{ with .Err }}Err: {{ . | safeHTML }}{{ with .Cause }}|{{ with .Data }}Body: {{ .Body }}|StatusCode: {{ .StatusCode }}{{ end }}|{{ end }}{{ end }}{{ end }}
84 FAILED REMOTE ERROR DETAILS CONTENT: {{ with $failedImg }}{{ with .Err }}{{ with .Cause }}{{ . }}|{{ with .Data }}Body: {{ .Body }}|StatusCode: {{ .StatusCode }}|ContentLength: {{ .ContentLength }}|ContentType: {{ .ContentType }}{{ end }}{{ end }}{{ end }}{{ end }}|
85 `
86 files = strings.ReplaceAll(files, "HTTPTEST_SERVER_URL", ts.URL)
87
88 b := Test(t, files)
89
90 b.AssertFileContent("public/index.html", "HELLO: /hello.html")
91 b.AssertFileContent("public/index.html", "SUNSET: /images/sunset.jpg")
92 b.AssertFileContent("public/index.html", "FIT: /images/sunset.jpg")
93 b.AssertFileContent("public/index.html", "CSS integrity Data first:")
94 b.AssertFileContent("public/index.html", "CSS integrity Data last:")
95 b.AssertFileContent("public/index.html", "SUNSET REMOTE:")
96 b.AssertFileContent("public/index.html", "FIT REMOTE:")
97 b.AssertFileContent("public/index.html", "REMOTE NOT FOUND: OK")
98 b.AssertFileContent("public/index.html", "LOCAL NOT FOUND: OK")
99 b.AssertFileContent("public/index.html", "PRINT PROTOCOL ERROR DETAILS:")
100 b.AssertFileContent("public/index.html", "FAILED REMOTE ERROR DETAILS CONTENT:")
101
102 b.AssertFileContent("public/styles.min.a1df58687c3c9cc38bf26532f7b4b2f2c2b0315dcde212376959995c04f11fef.css", "body{background-color:#add8e6}")
103 b.AssertFileContent("public//styles2.min.1cfc52986836405d37f9998a63fd6dd8608e8c410e5e3db1daaa30f78bc273ba.css", "body{background-color:orange}")
104 }
105
106 // getTestSunset reads the sunset.jpg file from testdata and returns its content as a string.
107 // This is used to embed the image content directly into the txtar string.
108 func getTestSunset(t testing.TB) string {
109 t.Helper()
110 b, err := os.ReadFile("testdata/sunset.jpg")
111 if err != nil {
112 t.Fatal(err)
113 }
114 return base64.StdEncoding.EncodeToString(b)
115 }
116
117 func TestResourceChainPostProcess(t *testing.T) {
118 t.Parallel()
119
120 files := `
121 -- hugo.toml --
122 disableLiveReload = true
123 [minify]
124 minifyOutput = true
125 [minify.tdewolff]
126 [minify.tdewolff.html]
127 keepQuotes = false
128 keepWhitespace = false
129 -- content/page1.md --
130 ---
131 title: Page1
132 ---
133 -- content/page2.md --
134 ---
135 title: Page2
136 ---
137 -- layouts/single.html --
138 {{ $hello := "<h1> Hello World! </h1>" | resources.FromString "hello.html" | minify | fingerprint "md5" | resources.PostProcess }}
139 HELLO: {{ $hello.RelPermalink }}
140 -- layouts/home.html --
141 Start.
142 {{ $hello := "<h1> Hello World! </h1>" | resources.FromString "hello.html" | minify | fingerprint "md5" | resources.PostProcess }}
143
144 HELLO: {{ $hello.RelPermalink }}|Integrity: {{ $hello.Data.Integrity }}|MediaType: {{ $hello.MediaType.Type }}
145 HELLO2: Name: {{ $hello.Name }}|Content: {{ $hello.Content }}|Title: {{ $hello.Title }}|ResourceType: {{ $hello.ResourceType }}
146
147 // Issue #10269
148 {{ $m := dict "relPermalink" $hello.RelPermalink "integrity" $hello.Data.Integrity "mediaType" $hello.MediaType.Type }}
149 {{ $json := jsonify (dict "indent" " ") $m | resources.FromString "hello.json" -}}
150 JSON: {{ $json.RelPermalink }}
151
152 // Issue #8884
153 <a href="hugo.rocks">foo</a>
154 <a href="{{ $hello.RelPermalink }}" integrity="{{ $hello.Data.Integrity}}">Hello</a>
155 a b a b a b
156
157
158 End.
159 `
160
161 b := Test(t, files)
162
163 b.AssertFileContent("public/index.html",
164 `Start.
165 HELLO: /hello.min.a2d1cb24f24b322a7dad520414c523e9.html|Integrity: md5-otHLJPJLMip9rVIEFMUj6Q==|MediaType: text/html
166 HELLO2: Name: /hello.html|Content: <h1>Hello World!</h1>|Title: /hello.html|ResourceType: text
167 <a href=hugo.rocks>foo</a>
168 <a href="/hello.min.a2d1cb24f24b322a7dad520414c523e9.html" integrity="md5-otHLJPJLMip9rVIEFMUj6Q==">Hello</a>
169 a b a b a b
170
171
172 End.`)
173
174 b.AssertFileContent("public/page1/index.html", `HELLO: /hello.min.a2d1cb24f24b322a7dad520414c523e9.html`)
175 b.AssertFileContent("public/page2/index.html", `HELLO: /hello.min.a2d1cb24f24b322a7dad520414c523e9.html`)
176 b.AssertFileContent("public/hello.json", `
177 integrity": "md5-otHLJPJLMip9rVIEFMUj6Q==
178 mediaType": "text/html
179 relPermalink": "/hello.min.a2d1cb24f24b322a7dad520414c523e9.html"
180 `)
181 }
182
183 func TestResourceChains(t *testing.T) {
184 t.Parallel()
185
186 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
187 switch r.URL.Path {
188 case "/css/styles1.css":
189 w.Header().Set("Content-Type", "text/css")
190 w.Write([]byte(`h1 {
191 font-style: bold;
192 }`))
193 return
194
195 case "/js/script1.js":
196 w.Write([]byte(`var x; x = 5, document.getElementById("demo").innerHTML = x * 10`))
197 return
198
199 case "/mydata/json1.json":
200 w.Write([]byte(`{
201 "employees": [
202 {
203 "firstName": "John",
204 "lastName": "Doe"
205 },
206 {
207 "firstName": "Anna",
208 "lastName": "Smith"
209 },
210 {
211 "firstName": "Peter",
212 "lastName": "Jones"
213 }
214 ]
215 }`))
216 return
217
218 case "/mydata/xml1.xml":
219 w.Write([]byte(`
220 <hello>
221 <world>Hugo Rocks!</<world>
222 </hello>`))
223 return
224
225 case "/mydata/svg1.svg":
226 w.Header().Set("Content-Disposition", `attachment; filename="image.svg"`)
227 w.Write([]byte(`
228 <svg height="100" width="100">
229 <path d="M1e2 1e2H3e2 2e2z"/>
230 </svg>`))
231 return
232
233 case "/mydata/html1.html":
234 w.Write([]byte(`
235 <html>
236 <a href=#>Cool</a>
237 </html>`))
238 return
239
240 case "/authenticated/":
241 w.Header().Set("Content-Type", "text/plain")
242 if r.Header.Get("Authorization") == "Bearer abcd" {
243 w.Write([]byte(`Welcome`))
244 return
245 }
246 http.Error(w, "Forbidden", http.StatusForbidden)
247 return
248
249 case "/post":
250 w.Header().Set("Content-Type", "text/plain")
251 if r.Method == http.MethodPost {
252 body, err := io.ReadAll(r.Body)
253 if err != nil {
254 http.Error(w, "Internal server error", http.StatusInternalServerError)
255 return
256 }
257 w.Write(body)
258 return
259 }
260 http.Error(w, "Bad request", http.StatusBadRequest)
261 return
262 }
263
264 http.Error(w, "Not found", http.StatusNotFound)
265 }))
266 t.Cleanup(func() {
267 ts.Close()
268 })
269
270 tests := []struct {
271 name string
272 shouldRun func() bool
273 files string
274 assert func(b *IntegrationTestBuilder)
275 }{
276 {"tocss", func() bool { return scss.Supports() }, `
277 -- hugo.toml --
278 baseURL = "http://example.com/"
279 -- content/_index.md --
280 ---
281 title: Home
282 ---
283 Home.
284 -- content/page1.md --
285 ---
286 title: Hello1
287 ---
288 Hello1
289 -- content/page2.md --
290 ---
291 title: Hello2
292 ---
293 Hello2
294 -- content/t1.txt --
295 t1t|
296 -- content/t2.txt --
297 t2t|
298 -- assets/css/styles1.css --
299 h1 {
300 font-style: bold;
301 }
302 -- assets/js/script1.js --
303 var x;
304 x = 5;
305 document.getElementById("demo").innerHTML = x * 10;
306 -- assets/mydata/json1.json --
307 {
308 "employees":[
309 {"firstName":"John", "lastName":"Doe"},
310 {"firstName":"Anna", "lastName":"Smith"},
311 {"firstName":"Peter", "lastName":"Jones"}
312 ]
313 }
314 -- assets/mydata/svg1.svg --
315 <svg height="100" width="100">
316 <path d="M 100 100 L 300 100 L 200 100 z"/>
317 </svg>
318 -- assets/mydata/xml1.xml --
319 <hello>
320 <world>Hugo Rocks!</<world>
321 </hello>
322 -- assets/mydata/html1.html --
323 <html>
324 <a href="#">
325 Cool
326 </a >
327 </html>
328 -- assets/scss/styles2.scss --
329 $color: #333;
330
331 body {
332 color: $color;
333 }
334 -- assets/sass/styles3.sass --
335 $color: #333;
336
337 .content-navigation
338 border-color: $color
339 -- layouts/home.html --
340 {{ $scss := resources.Get "scss/styles2.scss" | toCSS }}
341 {{ $sass := resources.Get "sass/styles3.sass" | toCSS }}
342 {{ $scssCustomTarget := resources.Get "scss/styles2.scss" | toCSS (dict "targetPath" "styles/main.css") }}
343 {{ $scssCustomTargetString := resources.Get "scss/styles2.scss" | toCSS "styles/main.css" }}
344 {{ $scssMin := resources.Get "scss/styles2.scss" | toCSS | minify }}
345 {{ $scssFromTempl := ".{{ .Kind }} { color: blue; }" | resources.FromString "kindofblue.templ" | resources.ExecuteAsTemplate "kindofblue.scss" . | toCSS (dict "targetPath" "styles/templ.css") | minify }}
346 {{ $bundle1 := slice $scssFromTempl $scssMin | resources.Concat "styles/bundle1.css" }}
347 T1: Len Content: {{ len $scss.Content }}|RelPermalink: {{ $scss.RelPermalink }}|Permalink: {{ $scss.Permalink }}|MediaType: {{ $scss.MediaType.Type }}
348 T2: Content: {{ $scssMin.Content }}|RelPermalink: {{ $scssMin.RelPermalink }}
349 T3: Content: {{ len $scssCustomTarget.Content }}|RelPermalink: {{ $scssCustomTarget.RelPermalink }}|MediaType: {{ $scssCustomTarget.MediaType.Type }}
350 T4: Content: {{ len $scssCustomTargetString.Content }}|RelPermalink: {{ $scssCustomTargetString.RelPermalink }}|MediaType: {{ $scssCustomTargetString.MediaType.Type }}
351 T5: Content: {{ $sass.Content }}|T5 RelPermalink: {{ $sass.RelPermalink }}|
352 T6: {{ $bundle1.Permalink }}
353 `, func(b *IntegrationTestBuilder) {
354 b.AssertFileContent("public/index.html", `T1: Len Content: 24|RelPermalink: /scss/styles2.css|Permalink: http://example.com/scss/styles2.css|MediaType: text/css`)
355 b.AssertFileContent("public/index.html", `T2: Content: body{color:#333}|RelPermalink: /scss/styles2.min.css`)
356 b.AssertFileContent("public/index.html", `T3: Content: 24|RelPermalink: /styles/main.css|MediaType: text/css`)
357 b.AssertFileContent("public/index.html", `T4: Content: 24|RelPermalink: /styles/main.css|MediaType: text/css`)
358 b.AssertFileContent("public/index.html", `T5: Content: .content-navigation {`)
359 b.AssertFileContent("public/index.html", `T5 RelPermalink: /sass/styles3.css|`)
360 b.AssertFileContent("public/index.html", `T6: http://example.com/styles/bundle1.css`)
361
362 b.AssertFileExists("public/styles/templ.min.css", false)
363 b.AssertFileContent("public/styles/bundle1.css", `.home{color:blue}body{color:#333}`)
364 }},
365
366 {"minify", func() bool { return true }, `
367 -- hugo.toml --
368 baseURL = "http://example.com/"
369 [minify]
370 [minify.tdewolff]
371 [minify.tdewolff.html]
372 keepWhitespace = false
373 -- content/_index.md --
374 ---
375 title: Home
376 ---
377 Home.
378 -- content/page1.md --
379 ---
380 title: Hello1
381 ---
382 Hello1
383 -- content/page2.md --
384 ---
385 title: Hello2
386 ---
387 Hello2
388 -- content/t1.txt --
389 t1t|
390 -- content/t2.txt --
391 t2t|
392 -- assets/css/styles1.css --
393 h1 {
394 font-style: bold;
395 }
396 -- assets/js/script1.js --
397 var x;
398 x = 5;
399 document.getElementById("demo").innerHTML = x * 10;
400 -- assets/mydata/json1.json --
401 {
402 "employees":[
403 {"firstName":"John", "lastName":"Doe"},
404 {"firstName":"Anna", "lastName":"Smith"},
405 {"firstName":"Peter", "lastName":"Jones"}
406 ]
407 }
408 -- assets/mydata/svg1.svg --
409 <svg height="100" width="100">
410 <path d="M 100 100 L 300 100 L 200 100 z"/>
411 </svg>
412 -- assets/mydata/xml1.xml --
413 <hello>
414 <world>Hugo Rocks!</<world>
415 </hello>
416 -- assets/mydata/html1.html --
417 <html>
418 <a href="#">
419 Cool
420 </a >
421 </html>
422 -- assets/scss/styles2.scss --
423 $color: #333;
424
425 body {
426 color: $color;
427 }
428 -- assets/sass/styles3.sass --
429 $color: #333;
430
431 .content-navigation
432 border-color: $color
433 -- layouts/home.html --
434 Min CSS: {{ ( resources.Get "css/styles1.css" | minify ).Content }}
435 Min CSS Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/css/styles1.css" | minify ).Content }}
436 Min JS: {{ ( resources.Get "js/script1.js" | resources.Minify ).Content | safeJS }}
437 Min JS Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/js/script1.js" | minify ).Content }}
438 Min JSON: {{ ( resources.Get "mydata/json1.json" | resources.Minify ).Content | safeHTML }}
439 Min JSON Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/mydata/json1.json" | resources.Minify ).Content | safeHTML }}
440 Min XML: {{ ( resources.Get "mydata/xml1.xml" | resources.Minify ).Content | safeHTML }}
441 Min XML Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/mydata/xml1.xml" | resources.Minify ).Content | safeHTML }}
442 Min SVG: {{ ( resources.Get "mydata/svg1.svg" | resources.Minify ).Content | safeHTML }}
443 Min SVG Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/mydata/svg1.svg" | resources.Minify ).Content | safeHTML }}
444 Min SVG again: {{ ( resources.Get "mydata/svg1.svg" | resources.Minify ).Content | safeHTML }}
445 Min HTML: {{ ( resources.Get "mydata/html1.html" | resources.Minify ).Content | safeHTML }}
446 Min HTML Remote: {{ ( resources.GetRemote "HTTPTEST_SERVER_URL/mydata/html1.html" | resources.Minify ).Content | safeHTML }}
447 `, func(b *IntegrationTestBuilder) {
448 b.AssertFileContent("public/index.html", `Min CSS: h1{font-style:bold}`)
449 b.AssertFileContent("public/index.html", `Min CSS Remote: h1{font-style:bold}`)
450 b.AssertFileContent("public/index.html", `Min JS: var x=5;document.getElementById(&#34;demo&#34;).innerHTML=x*10`)
451 b.AssertFileContent("public/index.html", `Min JS Remote: var x=5;document.getElementById(&#34;demo&#34;).innerHTML=x*10`)
452 b.AssertFileContent("public/index.html", `Min JSON: {"employees":[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]}`)
453 b.AssertFileContent("public/index.html", `Min JSON Remote: {"employees":[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]}`)
454 b.AssertFileContent("public/index.html", `Min XML: <hello><world>Hugo Rocks!</<world></hello>`)
455 b.AssertFileContent("public/index.html", `Min XML Remote: <hello><world>Hugo Rocks!</<world></hello>`)
456 b.AssertFileContent("public/index.html", `Min SVG: <svg height="100" width="100"><path d="M1e2 1e2H3e2 2e2z"/></svg>`)
457 b.AssertFileContent("public/index.html", `Min SVG Remote: <svg height="100" width="100"><path d="M1e2 1e2H3e2 2e2z"/></svg>`)
458 b.AssertFileContent("public/index.html", `Min SVG again: <svg height="100" width="100"><path d="M1e2 1e2H3e2 2e2z"/></svg>`)
459 b.AssertFileContent("public/index.html", `Min HTML: <html><a href=#>Cool</a></html>`)
460 b.AssertFileContent("public/index.html", `Min HTML Remote: <html><a href=#>Cool</a></html>`)
461 }},
462
463 {"remote", func() bool { return true }, `
464 -- hugo.toml --
465 baseURL = "http://example.com/"
466 -- content/_index.md --
467 ---
468 title: Home
469 ---
470 Home.
471 -- content/page1.md --
472 ---
473 title: Hello1
474 ---
475 Hello1
476 -- content/page2.md --
477 ---
478 title: Hello2
479 ---
480 Hello2
481 -- content/t1.txt --
482 t1t|
483 -- content/t2.txt --
484 t2t|
485 -- assets/css/styles1.css --
486 h1 {
487 font-style: bold;
488 }
489 -- assets/js/script1.js --
490 var x;
491 x = 5;
492 document.getElementById("demo").innerHTML = x * 10;
493 -- assets/mydata/json1.json --
494 {
495 "employees":[
496 {"firstName":"John", "lastName":"Doe"},
497 {"firstName":"Anna", "lastName":"Smith"},
498 {"firstName":"Peter", "lastName":"Jones"}
499 ]
500 }
501 -- assets/mydata/svg1.svg --
502 <svg height="100" width="100">
503 <path d="M 100 100 L 300 100 L 200 100 z"/>
504 </svg>
505 -- assets/mydata/xml1.xml --
506 <hello>
507 <world>Hugo Rocks!</<world>
508 </hello>
509 -- assets/mydata/html1.html --
510 <html>
511 <a href="#">
512 Cool
513 </a >
514 </html>
515 -- assets/scss/styles2.scss --
516 $color: #333;
517
518 body {
519 color: $color;
520 }
521 -- assets/sass/styles3.sass --
522 $color: #333;
523
524 .content-navigation
525 border-color: $color
526 -- layouts/home.html --
527 {{$js := resources.GetRemote "HTTPTEST_SERVER_URL/js/script1.js" }}
528 Remote Filename: {{ $js.RelPermalink }}
529 {{$svg := resources.GetRemote "HTTPTEST_SERVER_URL/mydata/svg1.svg" }}
530 Remote Content-Disposition: {{ $svg.RelPermalink }}
531 {{$auth := resources.GetRemote "HTTPTEST_SERVER_URL/authenticated/" (dict "headers" (dict "Authorization" "Bearer abcd")) }}
532 Remote Authorization: {{ $auth.Content }}
533 {{$post := resources.GetRemote "HTTPTEST_SERVER_URL/post" (dict "method" "post" "body" "Request body") }}
534 Remote POST: {{ $post.Content }}
535 `, func(b *IntegrationTestBuilder) {
536 b.AssertFileContent("public/index.html", `Remote Filename: /script1_`)
537 b.AssertFileContent("public/index.html", `Remote Content-Disposition: /image_`)
538 b.AssertFileContent("public/index.html", `Remote Authorization: Welcome`)
539 b.AssertFileContent("public/index.html", `Remote POST: Request body`)
540 }},
541
542 {"concat", func() bool { return true }, `
543 -- hugo.toml --
544 baseURL = "http://example.com/"
545 -- content/_index.md --
546 ---
547 title: Home
548 ---
549 Home.
550 -- content/page1.md --
551 ---
552 title: Hello1
553 ---
554 Hello1
555 -- content/page2.md --
556 ---
557 title: Hello2
558 ---
559 Hello2
560 -- content/t1.txt --
561 t1t|
562 -- content/t2.txt --
563 t2t|
564 -- assets/css/styles1.css --
565 h1 {
566 font-style: bold;
567 }
568 -- assets/js/script1.js --
569 var x;
570 x = 5;
571 document.getElementById("demo").innerHTML = x * 10;
572 -- assets/mydata/json1.json --
573 {
574 "employees":[
575 {"firstName":"John", "lastName":"Doe"},
576 {"firstName":"Anna", "lastName":"Smith"},
577 {"firstName":"Peter", "lastName":"Jones"}
578 ]
579 }
580 -- assets/mydata/svg1.svg --
581 <svg height="100" width="100">
582 <path d="M 100 100 L 300 100 L 200 100 z"/>
583 </svg>
584 -- assets/mydata/xml1.xml --
585 <hello>
586 <world>Hugo Rocks!</<world>
587 </hello>
588 -- assets/mydata/html1.html --
589 <html>
590 <a href="#">
591 Cool
592 </a >
593 </html>
594 -- assets/scss/styles2.scss --
595 $color: #333;
596
597 body {
598 color: $color;
599 }
600 -- assets/sass/styles3.sass --
601 $color: #333;
602
603 .content-navigation
604 border-color: $color
605 -- layouts/home.html --
606 {{ $a := "A" | resources.FromString "a.txt"}}
607 {{ $b := "B" | resources.FromString "b.txt"}}
608 {{ $c := "C" | resources.FromString "c.txt"}}
609 {{ $textResources := .Resources.Match "*.txt" }}
610 {{ $combined := slice $a $b $c | resources.Concat "bundle/concat.txt" }}
611 T1: Content: {{ $combined.Content }}|RelPermalink: {{ $combined.RelPermalink }}|Permalink: {{ $combined.Permalink }}|MediaType: {{ $combined.MediaType.Type }}
612 {{ with $textResources }}
613 {{ $combinedText := . | resources.Concat "bundle/concattxt.txt" }}
614 T2: Content: {{ $combinedText.Content }}|{{ $combinedText.RelPermalink }}
615 {{ end }}
616 {{/* https://github.com/gohugoio/hugo/issues/5269 */}}
617 {{ $css := "body { color: blue; }" | resources.FromString "styles.css" }}
618 {{ $minified := resources.Get "css/styles1.css" | minify }}
619 {{ slice $css $minified | resources.Concat "bundle/mixed.css" }}
620 {{/* https://github.com/gohugoio/hugo/issues/5403 */}}
621 {{ $d := "function D {} // A comment" | resources.FromString "d.js"}}
622 {{ $e := "(function E {})" | resources.FromString "e.js"}}
623 {{ $f := "(function F {})()" | resources.FromString "f.js"}}
624 {{ $jsResources := .Resources.Match "*.js" }}
625 {{ $combinedJs := slice $d $e $f | resources.Concat "bundle/concatjs.js" }}
626 T3: Content: {{ $combinedJs.Content }}|{{ $combinedJs.RelPermalink }}
627 `, func(b *IntegrationTestBuilder) {
628 b.AssertFileContent("public/index.html", `T1: Content: ABC|RelPermalink: /bundle/concat.txt|Permalink: http://example.com/bundle/concat.txt|MediaType: text/plain`)
629 b.AssertFileContent("public/bundle/concat.txt", "ABC")
630
631 b.AssertFileContent("public/index.html", `T2: Content: t1t|t2t|`)
632 b.AssertFileContent("public/bundle/concattxt.txt", "t1t|t2t|")
633
634 b.AssertFileContent("public/index.html", `T3: Content: function D {} // A comment
635 ;
636 (function E {})
637 ;
638 (function F {})()|`)
639 b.AssertFileContent("public/bundle/concatjs.js", `function D {} // A comment
640 ;
641 (function E {})
642 ;
643 (function F {})()`)
644 }},
645
646 {
647 "concat and fingerprint", func() bool { return true }, `
648 -- hugo.toml --
649 baseURL = "http://example.com/"
650 -- content/_index.md --
651 ---
652 title: Home
653 ---
654 Home.
655 -- content/page1.md --
656 ---
657 title: Hello1
658 ---
659 Hello1
660 -- content/page2.md --
661 ---
662 title: Hello2
663 ---
664 Hello2
665 -- content/t1.txt --
666 t1t|
667 -- content/t2.txt --
668 t2t|
669 -- assets/css/styles1.css --
670 h1 {
671 font-style: bold;
672 }
673 -- assets/js/script1.js --
674 var x;
675 x = 5;
676 document.getElementById("demo").innerHTML = x * 10;
677 -- assets/mydata/json1.json --
678 {
679 "employees":[
680 {"firstName":"John", "lastName":"Doe"},
681 {"firstName":"Anna", "lastName":"Smith"},
682 {"firstName":"Peter", "lastName":"Jones"}
683 ]
684 }
685 -- assets/mydata/svg1.svg --
686 <svg height="100" width="100">
687 <path d="M 100 100 L 300 100 L 200 100 z"/>
688 </svg>
689 -- assets/mydata/xml1.xml --
690 <hello>
691 <world>Hugo Rocks!</<world>
692 </hello>
693 -- assets/mydata/html1.html --
694 <html>
695 <a href="#">
696 Cool
697 </a >
698 </html>
699 -- assets/scss/styles2.scss --
700 $color: #333;
701
702 body {
703 color: $color;
704 }
705 -- assets/sass/styles3.sass --
706 $color: #333;
707
708 .content-navigation
709 border-color: $color
710 -- layouts/home.html --
711 {{ $a := "A" | resources.FromString "a.txt"}}
712 {{ $b := "B" | resources.FromString "b.txt"}}
713 {{ $c := "C" | resources.FromString "c.txt"}}
714 {{ $combined := slice $a $b $c | resources.Concat "bundle/concat.txt" }}
715 {{ $fingerprinted := $combined | fingerprint }}
716 Fingerprinted: {{ $fingerprinted.RelPermalink }}
717 `,
718 func(b *IntegrationTestBuilder) {
719 b.AssertFileContent("public/index.html", "Fingerprinted: /bundle/concat.b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78.txt")
720 b.AssertFileContent("public/bundle/concat.b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78.txt", "ABC")
721 },
722 },
723 {"fromstring", func() bool { return true }, `
724 -- hugo.toml --
725 baseURL = "http://example.com/"
726 -- content/_index.md --
727 ---
728 title: Home
729 ---
730 Home.
731 -- content/page1.md --
732 ---
733 title: Hello1
734 ---
735 Hello1
736 -- content/page2.md --
737 ---
738 title: Hello2
739 ---
740 Hello2
741 -- content/t1.txt --
742 t1t|
743 -- content/t2.txt --
744 t2t|
745 -- assets/css/styles1.css --
746 h1 {
747 font-style: bold;
748 }
749 -- assets/js/script1.js --
750 var x;
751 x = 5;
752 document.getElementById("demo").innerHTML = x * 10;
753 -- assets/mydata/json1.json --
754 {
755 "employees":[
756 {"firstName":"John", "lastName":"Doe"},
757 {"firstName":"Anna", "lastName":"Smith"},
758 {"firstName":"Peter", "lastName":"Jones"}
759 ]
760 }
761 -- assets/mydata/svg1.svg --
762 <svg height="100" width="100">
763 <path d="M 100 100 L 300 100 L 200 100 z"/>
764 </svg>
765 -- assets/mydata/xml1.xml --
766 <hello>
767 <world>Hugo Rocks!</<world>
768 </hello>
769 -- assets/mydata/html1.html --
770 <html>
771 <a href="#">
772 Cool
773 </a >
774 </html>
775 -- assets/scss/styles2.scss --
776 $color: #333;
777
778 body {
779 color: $color;
780 }
781 -- assets/sass/styles3.sass --
782 $color: #333;
783
784 .content-navigation
785 border-color: $color
786 -- layouts/home.html --
787 {{ $r := "Hugo Rocks!" | resources.FromString "rocks/hugo.txt" }}
788 {{ $r.Content }}|{{ $r.RelPermalink }}|{{ $r.Permalink }}|{{ $r.MediaType.Type }}
789 `, func(b *IntegrationTestBuilder) {
790 b.AssertFileContent("public/index.html", `Hugo Rocks!|/rocks/hugo.txt|http://example.com/rocks/hugo.txt|text/plain`)
791 b.AssertFileContent("public/rocks/hugo.txt", "Hugo Rocks!")
792 }},
793 {"execute-as-template", func() bool {
794 return true
795 }, `
796 -- hugo.toml --
797 baseURL = "http://example.com/"
798 -- content/_index.md --
799 ---
800 title: Home
801 ---
802 Home.
803 -- content/page1.md --
804 ---
805 title: Hello1
806 ---
807 Hello1
808 -- content/page2.md --
809 ---
810 title: Hello2
811 ---
812 Hello2
813 -- content/t1.txt --
814 t1t|
815 -- content/t2.txt --
816 t2t|
817 -- assets/css/styles1.css --
818 h1 {
819 font-style: bold;
820 }
821 -- assets/js/script1.js --
822 var x;
823 x = 5;
824 document.getElementById("demo").innerHTML = x * 10;
825 -- assets/mydata/json1.json --
826 {
827 "employees":[
828 {"firstName":"John", "lastName":"Doe"},
829 {"firstName":"Anna", "lastName":"Smith"},
830 {"firstName":"Peter", "lastName":"Jones"}
831 ]
832 }
833 -- assets/mydata/svg1.svg --
834 <svg height="100" width="100">
835 <path d="M 100 100 L 300 100 L 200 100 z"/>
836 </svg>
837 -- assets/mydata/xml1.xml --
838 <hello>
839 <world>Hugo Rocks!</<world>
840 </hello>
841 -- assets/mydata/html1.html --
842 <html>
843 <a href="#">
844 Cool
845 </a >
846 </html>
847 -- assets/scss/styles2.scss --
848 $color: #333;
849
850 body {
851 color: $color;
852 }
853 -- assets/sass/styles3.sass --
854 $color: #333;
855
856 .content-navigation
857 border-color: $color
858 -- layouts/home.html --
859 {{ $var := "Hugo Page" }}
860 {{ if .IsHome }}
861 {{ $var = "Hugo Home" }}
862 {{ end }}
863 T1: {{ $var }}
864 {{ $result := "{{ .Kind | upper }}" | resources.FromString "mytpl.txt" | resources.ExecuteAsTemplate "result.txt" . }}
865 T2: {{ $result.Content }}|{{ $result.RelPermalink}}|{{$result.MediaType.Type }}
866 `, func(b *IntegrationTestBuilder) {
867 b.AssertFileContent("public/index.html", `T2: HOME|/result.txt|text/plain`, `T1: Hugo Home`)
868 }},
869 {"fingerprint", func() bool { return true }, `
870 -- hugo.toml --
871 baseURL = "http://example.com/"
872 -- content/_index.md --
873 ---
874 title: Home
875 ---
876 Home.
877 -- content/page1.md --
878 ---
879 title: Hello1
880 ---
881 Hello1
882 -- content/page2.md --
883 ---
884 title: Hello2
885 ---
886 Hello2
887 -- content/t1.txt --
888 t1t|
889 -- content/t2.txt --
890 t2t|
891 -- assets/css/styles1.css --
892 h1 {
893 font-style: bold;
894 }
895 -- assets/js/script1.js --
896 var x;
897 x = 5;
898 document.getElementById("demo").innerHTML = x * 10;
899 -- assets/mydata/json1.json --
900 {
901 "employees":[
902 {"firstName":"John", "lastName":"Doe"},
903 {"firstName":"Anna", "lastName":"Smith"},
904 {"firstName":"Peter", "lastName":"Jones"}
905 ]
906 }
907 -- assets/mydata/svg1.svg --
908 <svg height="100" width="100">
909 <path d="M 100 100 L 300 100 L 200 100 z"/>
910 </svg>
911 -- assets/mydata/xml1.xml --
912 <hello>
913 <world>Hugo Rocks!</<world>
914 </hello>
915 -- assets/mydata/html1.html --
916 <html>
917 <a href="#">
918 Cool
919 </a >
920 </html>
921 -- assets/scss/styles2.scss --
922 $color: #333;
923
924 body {
925 color: $color;
926 }
927 -- assets/sass/styles3.sass --
928 $color: #333;
929
930 .content-navigation
931 border-color: $color
932 -- layouts/home.html --
933 {{ $r := "ab" | resources.FromString "rocks/hugo.txt" }}
934 {{ $result := $r | fingerprint }}
935 {{ $result512 := $r | fingerprint "sha512" }}
936 {{ $resultMD5 := $r | fingerprint "md5" }}
937 T1: {{ $result.Content }}|{{ $result.RelPermalink}}|{{$result.MediaType.Type }}|{{ $result.Data.Integrity }}|
938 T2: {{ $result512.Content }}|{{ $result512.RelPermalink}}|{{$result512.MediaType.Type }}|{{ $result512.Data.Integrity }}|
939 T3: {{ $resultMD5.Content }}|{{ $resultMD5.RelPermalink}}|{{$resultMD5.MediaType.Type }}|{{ $resultMD5.Data.Integrity }}|
940 {{ $r2 := "bc" | resources.FromString "rocks/hugo2.txt" | fingerprint }}
941 {{/* https://github.com/gohugoio/hugo/issues/5296 */}}
942 T4: {{ $r2.Data.Integrity }}|
943 `, func(b *IntegrationTestBuilder) {
944 b.AssertFileContent("public/index.html", `T1: ab|/rocks/hugo.fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603.txt|text/plain|sha256-&#43;44g/C5MPySMYMOb1lLzwTRymLuXe4tNWQO4UFViBgM=|`)
945 b.AssertFileContent("public/index.html", `T2: ab|/rocks/hugo.2d408a0717ec188158278a796c689044361dc6fdde28d6f04973b80896e1823975cdbf12eb63f9e0591328ee235d80e9b5bf1aa6a44f4617ff3caf6400eb172d.txt|text/plain|sha512-LUCKBxfsGIFYJ4p5bGiQRDYdxv3eKNbwSXO4CJbhgjl1zb8S62P54FkTKO4jXYDptb8apqRPRhf/PK9kAOsXLQ==|`)
946 b.AssertFileContent("public/index.html", `T3: ab|/rocks/hugo.187ef4436122d1cc2f40dc2b92f0eba0.txt|text/plain|md5-GH70Q2Ei0cwvQNwrkvDroA==|`)
947 b.AssertFileContent("public/index.html", `T4: sha256-Hgu9bGhroFC46wP/7txk/cnYCUf86CGrvl1tyNJSxaw=|`)
948 }},
949 // https://github.com/gohugoio/hugo/issues/5226
950 {"baseurl-path", func() bool { return true }, `
951 -- hugo.toml --
952 baseURL = "https://example.com/hugo/"
953 -- content/_index.md --
954 ---
955 title: Home
956 ---
957 Home.
958 -- content/page1.md --
959 ---
960 title: Hello1
961 ---
962 Hello1
963 -- content/page2.md --
964 ---
965 title: Hello2
966 ---
967 Hello2
968 -- content/t1.txt --
969 t1t|
970 -- content/t2.txt --
971 t2t|
972 -- assets/css/styles1.css --
973 h1 {
974 font-style: bold;
975 }
976 -- assets/js/script1.js --
977 var x;
978 x = 5;
979 document.getElementById("demo").innerHTML = x * 10;
980 -- assets/mydata/json1.json --
981 {
982 "employees":[
983 {"firstName":"John", "lastName":"Doe"},
984 {"firstName":"Anna", "lastName":"Smith"},
985 {"firstName":"Peter", "lastName":"Jones"}
986 ]
987 }
988 -- assets/mydata/svg1.svg --
989 <svg height="100" width="100">
990 <path d="M 100 100 L 300 100 L 200 100 z"/>
991 </svg>
992 -- assets/mydata/xml1.xml --
993 <hello>
994 <world>Hugo Rocks!</<world>
995 </hello>
996 -- assets/mydata/html1.html --
997 <html>
998 <a href="#">
999 Cool
1000 </a >
1001 </html>
1002 -- assets/scss/styles2.scss --
1003 $color: #333;
1004
1005 body {
1006 color: $color;
1007 }
1008 -- assets/sass/styles3.sass --
1009 $color: #333;
1010
1011 .content-navigation
1012 border-color: $color
1013 -- layouts/home.html --
1014 {{ $r1 := "ab" | resources.FromString "rocks/hugo.txt" }}
1015 T1: {{ $r1.Permalink }}|{{ $r1.RelPermalink }}
1016 `, func(b *IntegrationTestBuilder) {
1017 b.AssertFileContent("public/index.html", `T1: https://example.com/hugo/rocks/hugo.txt|/hugo/rocks/hugo.txt`)
1018 }},
1019
1020 // https://github.com/gohugoio/hugo/issues/4944
1021 {"Prevent resource publish on .Content only", func() bool { return true }, `
1022 -- hugo.toml --
1023 baseURL = "http://example.com/"
1024 -- content/_index.md --
1025 ---
1026 title: Home
1027 ---
1028 Home.
1029 -- content/page1.md --
1030 ---
1031 title: Hello1
1032 ---
1033 Hello1
1034 -- content/page2.md --
1035 ---
1036 title: Hello2
1037 ---
1038 Hello2
1039 -- content/t1.txt --
1040 t1t|
1041 -- content/t2.txt --
1042 t2t|
1043 -- assets/css/styles1.css --
1044 h1 {
1045 font-style: bold;
1046 }
1047 -- assets/js/script1.js --
1048 var x;
1049 x = 5;
1050 document.getElementById("demo").innerHTML = x * 10;
1051 -- assets/mydata/json1.json --
1052 {
1053 "employees":[
1054 {"firstName":"John", "lastName":"Doe"},
1055 {"firstName":"Anna", "lastName":"Smith"},
1056 {"firstName":"Peter", "lastName":"Jones"}
1057 ]
1058 }
1059 -- assets/mydata/svg1.svg --
1060 <svg height="100" width="100">
1061 <path d="M 100 100 L 300 100 L 200 100 z"/>
1062 </svg>
1063 -- assets/mydata/xml1.xml --
1064 <hello>
1065 <world>Hugo Rocks!</<world>
1066 </hello>
1067 -- assets/mydata/html1.html --
1068 <html>
1069 <a href="#">
1070 Cool
1071 </a >
1072 </html>
1073 -- assets/scss/styles2.scss --
1074 $color: #333;
1075
1076 body {
1077 color: $color;
1078 }
1079 -- assets/sass/styles3.sass --
1080 $color: #333;
1081
1082 .content-navigation
1083 border-color: $color
1084 -- layouts/home.html --
1085 {{ $cssInline := "body { color: green; }" | resources.FromString "inline.css" | minify }}
1086 {{ $cssPublish1 := "body { color: blue; }" | resources.FromString "external1.css" | minify }}
1087 {{ $cssPublish2 := "body { color: orange; }" | resources.FromString "external2.css" | minify }}
1088
1089 Inline: {{ $cssInline.Content }}
1090 Publish 1: {{ $cssPublish1.Content }} {{ $cssPublish1.RelPermalink }}
1091 Publish 2: {{ $cssPublish2.Permalink }}
1092 `, func(b *IntegrationTestBuilder) {
1093 b.AssertFileContent("public/index.html",
1094 `Inline: body{color:green}`,
1095 "Publish 1: body{color:blue} /external1.min.css",
1096 "Publish 2: http://example.com/external2.min.css",
1097 )
1098 b.AssertFileExists("public/external2.css", false)
1099 b.AssertFileExists("public/external1.css", false)
1100 b.AssertFileExists("public/external2.min.css", true)
1101 b.AssertFileExists("public/external1.min.css", true)
1102 b.AssertFileExists("public/inline.min.css", false)
1103 }},
1104
1105 {"unmarshal", func() bool { return true }, `
1106 -- hugo.toml --
1107 baseURL = "http://example.com/"
1108 -- content/_index.md --
1109 ---
1110 title: Home
1111 ---
1112 Home.
1113 -- content/page1.md --
1114 ---
1115 title: Hello1
1116 ---
1117 Hello1
1118 -- content/page2.md --
1119 ---
1120 title: Hello2
1121 ---
1122 Hello2
1123 -- content/t1.txt --
1124 t1t|
1125 -- content/t2.txt --
1126 t2t|
1127 -- assets/css/styles1.css --
1128 h1 {
1129 font-style: bold;
1130 }
1131 -- assets/js/script1.js --
1132 var x;
1133 x = 5;
1134 document.getElementById("demo").innerHTML = x * 10;
1135 -- assets/mydata/json1.json --
1136 {
1137 "employees":[
1138 {"firstName":"John", "lastName":"Doe"},
1139 {"firstName":"Anna", "lastName":"Smith"},
1140 {"firstName":"Peter", "lastName":"Jones"}
1141 ]
1142 }
1143 -- assets/mydata/svg1.svg --
1144 <svg height="100" width="100">
1145 <path d="M 100 100 L 300 100 L 200 100 z"/>
1146 </svg>
1147 -- assets/mydata/xml1.xml --
1148 <hello>
1149 <world>Hugo Rocks!</<world>
1150 </hello>
1151 -- assets/mydata/html1.html --
1152 <html>
1153 <a href="#">
1154 Cool
1155 </a >
1156 </html>
1157 -- assets/scss/styles2.scss --
1158 $color: #333;
1159
1160 body {
1161 color: $color;
1162 }
1163 -- assets/sass/styles3.sass --
1164 $color: #333;
1165
1166 .content-navigation
1167 border-color: $color
1168 -- layouts/home.html --
1169 {{ $toml := "slogan = \"Hugo Rocks!\"" | resources.FromString "slogan.toml" | transform.Unmarshal }}
1170 {{ $csv1 := "\"Hugo Rocks\",\"Hugo is Fast!\"" | resources.FromString "slogans.csv" | transform.Unmarshal }}
1171 {{ $csv2 := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }}
1172 {{ $xml := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>You</to><from>Me</from><heading>Reminder</heading><body>Do not forget XML</body></note>" | transform.Unmarshal }}
1173
1174 Slogan: {{ $toml.slogan }}
1175 CSV1: {{ $csv1 }} {{ len (index $csv1 0) }}
1176 CSV2: {{ $csv2 }}
1177 XML: {{ $xml.body }}
1178 `, func(b *IntegrationTestBuilder) {
1179 b.AssertFileContent("public/index.html",
1180 `Slogan: Hugo Rocks!`,
1181 `[[Hugo Rocks Hugo is Fast!]] 2`,
1182 `CSV2: [[a b c]]`,
1183 `XML: Do not forget XML`,
1184 )
1185 }},
1186 {"resources.Get", func() bool { return true }, `
1187 -- hugo.toml --
1188 baseURL = "http://example.com/"
1189 -- content/_index.md --
1190 ---
1191 title: Home
1192 ---
1193 Home.
1194 -- content/page1.md --
1195 ---
1196 title: Hello1
1197 ---
1198 Hello1
1199 -- content/page2.md --
1200 ---
1201 title: Hello2
1202 ---
1203 Hello2
1204 -- content/t1.txt --
1205 t1t|
1206 -- content/t2.txt --
1207 t2t|
1208 -- assets/css/styles1.css --
1209 h1 {
1210 font-style: bold;
1211 }
1212 -- assets/js/script1.js --
1213 var x;
1214 x = 5;
1215 document.getElementById("demo").innerHTML = x * 10;
1216 -- assets/mydata/json1.json --
1217 {
1218 "employees":[
1219 {"firstName":"John", "lastName":"Doe"},
1220 {"firstName":"Anna", "lastName":"Smith"},
1221 {"firstName":"Peter", "lastName":"Jones"}
1222 ]
1223 }
1224 -- assets/mydata/svg1.svg --
1225 <svg height="100" width="100">
1226 <path d="M 100 100 L 300 100 L 200 100 z"/>
1227 </svg>
1228 -- assets/mydata/xml1.xml --
1229 <hello>
1230 <world>Hugo Rocks!</<world>
1231 </hello>
1232 -- assets/mydata/html1.html --
1233 <html>
1234 <a href="#">
1235 Cool
1236 </a >
1237 </html>
1238 -- assets/scss/styles2.scss --
1239 $color: #333;
1240
1241 body {
1242 color: $color;
1243 }
1244 -- assets/sass/styles3.sass --
1245 $color: #333;
1246
1247 .content-navigation
1248 border-color: $color
1249 -- layouts/home.html --
1250 NOT FOUND: {{ if (resources.Get "this-does-not-exist") }}FAILED{{ else }}OK{{ end }}
1251 `, func(b *IntegrationTestBuilder) {
1252 b.AssertFileContent("public/index.html", "NOT FOUND: OK")
1253 }},
1254
1255 {"template", func() bool { return true }, `
1256 -- hugo.toml --
1257 baseURL = "http://example.com/"
1258 -- content/_index.md --
1259 ---
1260 title: Home
1261 ---
1262 Home.
1263 -- content/page1.md --
1264 ---
1265 title: Hello1
1266 ---
1267 Hello1
1268 -- content/page2.md --
1269 ---
1270 title: Hello2
1271 ---
1272 Hello2
1273 -- content/t1.txt --
1274 t1t|
1275 -- content/t2.txt --
1276 t2t|
1277 -- assets/css/styles1.css --
1278 h1 {
1279 font-style: bold;
1280 }
1281 -- assets/js/script1.js --
1282 var x;
1283 x = 5;
1284 document.getElementById("demo").innerHTML = x * 10;
1285 -- assets/mydata/json1.json --
1286 {
1287 "employees":[
1288 {"firstName":"John", "lastName":"Doe"},
1289 {"firstName":"Anna", "lastName":"Smith"},
1290 {"firstName":"Peter", "lastName":"Jones"}
1291 ]
1292 }
1293 -- assets/mydata/svg1.svg --
1294 <svg height="100" width="100">
1295 <path d="M 100 100 L 300 100 L 200 100 z"/>
1296 </svg>
1297 -- assets/mydata/xml1.xml --
1298 <hello>
1299 <world>Hugo Rocks!</<world>
1300 </hello>
1301 -- assets/mydata/html1.html --
1302 <html>
1303 <a href="#">
1304 Cool
1305 </a >
1306 </html>
1307 -- assets/scss/styles2.scss --
1308 $color: #333;
1309
1310 body {
1311 color: $color;
1312 }
1313 -- assets/sass/styles3.sass --
1314 $color: #333;
1315
1316 .content-navigation
1317 border-color: $color
1318 -- layouts/home.html --
1319 Template test.
1320 `, func(b *IntegrationTestBuilder) {
1321 b.AssertFileContent("public/index.html", "Template test.")
1322 }},
1323 }
1324
1325 for _, test := range tests {
1326 t.Run(test.name, func(t *testing.T) {
1327 if !test.shouldRun() {
1328 t.Skip()
1329 }
1330 t.Parallel()
1331
1332 files := test.files
1333 files = strings.ReplaceAll(files, "HTTPTEST_SERVER_URL", ts.URL)
1334 files = strings.Replace(files, `baseURL = "http://example.com/"`,
1335 "baseURL = \"http://example.com/\"\n[security.http]\nurls = ['.*']", 1)
1336
1337 b := Test(t, files)
1338
1339 test.assert(b)
1340 })
1341 }
1342 }
1343
1344 func TestResourcesMatch(t *testing.T) {
1345 t.Parallel()
1346
1347 files := `
1348 -- hugo.toml --
1349 baseURL = "http://example.com/"
1350 -- assets/images/img1.png --
1351 -- assets/images/img2.jpg --
1352 -- assets/jsons/data1.json --
1353 json1 content
1354 -- assets/jsons/data2.json --
1355 json2 content
1356 -- assets/jsons/data3.xml --
1357 xml content
1358 -- content/page.md --
1359 -- layouts/home.html --
1360 {{ $jsons := (resources.Match "jsons/*.json") }}
1361 {{ $json := (resources.GetMatch "jsons/*.json") }}
1362 {{ printf "jsonsMatch: %d" (len $jsons) }}
1363 {{ printf "imagesByType: %d" (len (resources.ByType "image") ) }}
1364 {{ printf "applicationByType: %d" (len (resources.ByType "application") ) }}
1365 JSON: {{ $json.RelPermalink }}: {{ $json.Content }}
1366 {{ range $jsons }}
1367 {{- .RelPermalink }}: {{ .Content }}
1368 {{ end }}
1369 `
1370 b := Test(t, files)
1371
1372 b.AssertFileContent("public/index.html",
1373 "JSON: /jsons/data1.json: json1 content",
1374 "jsonsMatch: 2",
1375 "imagesByType: 2",
1376 "applicationByType: 3",
1377 "/jsons/data1.json: json1 content")
1378 }
1379
1380 func TestResourceMinifyDisabled(t *testing.T) {
1381 t.Parallel()
1382
1383 files := `
1384 -- hugo.toml --
1385 baseURL = "https://example.org"
1386 [minify]
1387 disableXML=true
1388 -- assets/xml/data.xml --
1389 <root> <foo> asdfasdf </foo> </root>
1390 -- content/page.md --
1391 -- layouts/home.html --
1392 {{ $xml := resources.Get "xml/data.xml" | minify | fingerprint }}
1393 XML: {{ $xml.Content | safeHTML }}|{{ $xml.RelPermalink }}
1394 `
1395 b := Test(t, files)
1396
1397 b.AssertFileContent("public/index.html", `
1398 XML: <root> <foo> asdfasdf </foo> </root>|/xml/data.min.3be4fddd19aaebb18c48dd6645215b822df74701957d6d36e59f203f9c30fd9f.xml
1399 `)
1400 }