Debian Codegraph @

resources/resources_integration_test.go

raw
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 resources_test
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 // Issue 8931
25 func TestImageCache(t *testing.T) {
26 t.Parallel()
27
28 files := `
29 -- hugo.toml --
30 disableLiveReload = true
31 baseURL = "https://example.org"
32 -- content/mybundle/index.md --
33 ---
34 title: "My Bundle"
35 ---
36 -- content/mybundle/pixel.png --
37 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
38 -- content/mybundle/giphy.gif --
39 sourcefilename: testdata/giphy.gif
40 -- layouts/foo.html --
41 -- layouts/home.html --
42 {{ $p := site.GetPage "mybundle"}}
43 {{ $img := $p.Resources.Get "pixel.png" }}
44 {{ $giphy := $p.Resources.Get "giphy.gif" }}
45 {{ $gif := $img.Resize "1x2 gif" }}
46 {{ $bmp := $img.Resize "2x3 bmp" }}
47 {{ $anigif := $giphy.Resize "4x5" }}
48
49
50 gif: {{ $gif.RelPermalink }}|}|{{ $gif.Width }}|{{ $gif.Height }}|{{ $gif.MediaType }}|
51 bmp: {{ $bmp.RelPermalink }}|}|{{ $bmp.Width }}|{{ $bmp.Height }}|{{ $bmp.MediaType }}|
52 anigif: {{ $anigif.RelPermalink }}|{{ $anigif.Width }}|{{ $anigif.Height }}|{{ $anigif.MediaType }}|
53 `
54
55 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptRunning())
56
57 assertImages := func() {
58 b.AssertFileContent("public/index.html", `
59 gif: /mybundle/pixel_hu_d6bad5e71f783c98.gif|}|1|2|image/gif|
60 bmp: /mybundle/pixel_hu_a8812c9bf8812b53.bmp|}|2|3|image/bmp|
61 anigif: /mybundle/giphy_hu_7f64f85f904209d4.gif|4|5|image/gif|
62 `)
63 }
64
65 assertImages()
66
67 b.EditFileReplaceFunc("content/mybundle/index.md", func(s string) string { return strings.ReplaceAll(s, "Bundle", "BUNDLE") })
68 b.Build()
69
70 assertImages()
71 }
72
73 func TestSVGError(t *testing.T) {
74 t.Parallel()
75
76 files := `
77 -- hugo.toml --
78 -- assets/circle.svg --
79 <svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>
80 -- layouts/home.html --
81 {{ $svg := resources.Get "circle.svg" }}
82 Width: {{ $svg.Width }}
83 `
84
85 b, err := hugolib.TestE(t, files, hugolib.TestOptOsFs(), hugolib.TestOptRunning())
86
87 b.Assert(err, qt.IsNotNil)
88 b.Assert(err.Error(), qt.Contains, `error calling Width: resource "/circle.svg" of media type "image/svg+xml" does not support this method: use reflect.IsImageResource, reflect.IsImageResourceProcessable, or reflect.IsImageResourceWithMeta to check if the resource supports this method before calling it`)
89 }
90
91 // Issue 10255.
92 func TestNoPublishOfUnusedProcessedImage(t *testing.T) {
93 t.Parallel()
94
95 workingDir := t.TempDir()
96
97 files := `
98 -- assets/images/pixel.png --
99 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
100 -- layouts/home.html --
101 {{ $image := resources.Get "images/pixel.png" }}
102 {{ $image = $image.Resize "400x" }}
103 {{ $image = $image.Resize "300x" }}
104 {{ $image = $image.Resize "200x" }}
105 {{ $image = $image.Resize "100x" }}
106 {{ $image = $image.Crop "50x50" }}
107 {{ $image = $image.Filter (images.GaussianBlur 6) }}
108 {{ ($image | fingerprint).Permalink }}
109
110
111 `
112
113 for range 3 {
114
115 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
116 cfg.WorkingDir = workingDir
117 }))
118
119 b.AssertFileCount("resources/_gen/images", 6)
120 b.AssertFileCount("public/images", 1)
121 }
122 }
123
124 func TestProcessFilter(t *testing.T) {
125 t.Parallel()
126
127 files := `
128 -- assets/images/pixel.png --
129 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
130 -- layouts/home.html --
131 {{ $pixel := resources.Get "images/pixel.png" }}
132 {{ $filters := slice (images.GaussianBlur 6) (images.Pixelate 8) (images.Process "jpg") }}
133 {{ $image := $pixel.Filter $filters }}
134 jpg|RelPermalink: {{ $image.RelPermalink }}|MediaType: {{ $image.MediaType }}|Width: {{ $image.Width }}|Height: {{ $image.Height }}|
135 {{ $filters := slice (images.GaussianBlur 6) (images.Pixelate 8) (images.Process "jpg resize 20x30") }}
136 {{ $image := $pixel.Filter $filters }}
137 resize 1|RelPermalink: {{ $image.RelPermalink }}|MediaType: {{ $image.MediaType }}|Width: {{ $image.Width }}|Height: {{ $image.Height }}|
138 {{ $image := $pixel.Filter $filters }}
139 resize 2|RelPermalink: {{ $image.RelPermalink }}|MediaType: {{ $image.MediaType }}|Width: {{ $image.Width }}|Height: {{ $image.Height }}|
140
141 `
142
143 b := hugolib.Test(t, files)
144
145 b.AssertFileContent("public/index.html",
146 "jpg|RelPermalink: /images/pixel_hu_38c3f257174fc757.jpg|MediaType: image/jpeg|Width: 1|Height: 1|",
147 "resize 1|RelPermalink: /images/pixel_hu_b5c2a3d88991f65a.jpg|MediaType: image/jpeg|Width: 20|Height: 30|",
148 "resize 2|RelPermalink: /images/pixel_hu_b5c2a3d88991f65a.jpg|MediaType: image/jpeg|Width: 20|Height: 30|",
149 )
150 }
151
152 // Issue #11563
153 func TestGroupByParamDate(t *testing.T) {
154 t.Parallel()
155
156 files := `
157 -- hugo.toml --
158 disableKinds = ['section','rss','sitemap','taxonomy','term']
159 -- layouts/home.html --
160 {{- range site.RegularPages.GroupByParamDate "eventDate" "2006-01" }}
161 {{- .Key }}|{{ range .Pages }}{{ .Title }}|{{ end }}
162 {{- end }}
163 -- content/p1.md --
164 +++
165 title = 'p1'
166 eventDate = 2023-09-01
167 +++
168 -- content/p2.md --
169 +++
170 title = 'p2'
171 eventDate = '2023-09-01'
172 +++
173 -- content/p3.md --
174 ---
175 title: p3
176 eventDate: 2023-09-01
177 ---
178 -- content/p4.md --
179 +++
180 title = 'p4'
181 eventDate = 2023-10-01T08:00:00
182 +++
183 -- content/p5.md --
184 +++
185 title = 'p5'
186 eventDate = '2023-10-01T08:00:00'
187 +++
188 -- content/p6.md --
189 ---
190 title: p6
191 eventDate: 2023-10-01T08:00:00
192 ---
193 -- content/p7.md --
194 +++
195 title = 'p7'
196 eventDate = 2023-11-01T07:00:00-08:00
197 +++
198 -- content/p8.md --
199 +++
200 title = 'p8'
201 eventDate = '2023-11-01T07:00:00-08:00'
202 +++
203 -- content/p9.md --
204 ---
205 title: p9
206 eventDate: 2023-11-01T07:00:00-08:00
207 ---
208 `
209
210 b := hugolib.Test(t, files)
211
212 b.AssertFileContent("public/index.html", "2023-11|p9|p8|p7|2023-10|p6|p5|p4|2023-09|p3|p2|p1|")
213 }
214
215 // Issue 10412
216 func TestImageTransformThenCopy(t *testing.T) {
217 t.Parallel()
218
219 files := `
220 -- hugo.toml --
221 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
222 -- assets/pixel.png --
223 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
224 -- layouts/home.html --
225 {{- with resources.Get "pixel.png" }}
226 {{- with .Resize "200x" | resources.Copy "pixel.png" }}
227 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">|{{ .Key }}
228 {{- end }}
229 {{- end }}
230 `
231
232 b := hugolib.Test(t, files)
233
234 b.AssertFileExists("public/pixel.png", true)
235 b.AssertFileContent("public/index.html",
236 `<img src="/pixel.png" width="200" height="200">|/pixel.png`,
237 )
238 }
239
240 // Issue 12310
241 func TestUseDifferentCacheKeyForResourceCopy(t *testing.T) {
242 t.Parallel()
243
244 files := `
245 -- hugo.toml --
246 disableKinds = ['page','section','rss','sitemap','taxonomy','term']
247 -- assets/a.txt --
248 This was assets/a.txt
249 -- layouts/home.html --
250 {{ $nilResource := resources.Get "/p1/b.txt" }}
251 {{ $r := resources.Get "a.txt" }}
252 {{ $r = resources.Copy "/p1/b.txt" $r }}
253 {{ $r.RelPermalink }}
254 `
255
256 b, err := hugolib.TestE(t, files)
257
258 b.Assert(err, qt.IsNil)
259 b.AssertFileContent("public/p1/b.txt", "This was assets/a.txt")
260 }