Debian Codegraph @

internal/warpc/webp_integration_test.go

raw ◀ ▶
1 Debian patch touch this file
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 warpc_test
15
16 import (
17 "path/filepath"
18 "runtime"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/hugolib"
•23 "github.com/gohugoio/hugo/resources/images/imagetesting"
24 )
25
26 func TestWebPMisc(t *testing.T) {
•27 if imagetesting.SkipGoldenTests {
28 t.Skip("Skip golden test on this architecture")
29 }
30
31 files := `
32 -- assets/sunrise.webp --
33 sourcefilename: ../../resources/testdata/sunrise.webp
34 -- layouts/home.html --
35 {{ $image := resources.Get "sunrise.webp" }}
36 {{ $resized := $image.Resize "123x456" }}
37 Original Width/Height: {{ $image.Width }}/{{ $image.Height }}|
38 Resized Width:/Height {{ $resized.Width }}/{{ $resized.Height }}|
39 Resized RelPermalink: {{ $resized.RelPermalink }}|
40 {{ $ic := images.Config "/assets/sunrise.webp" }}
41 ImageConfig: {{ printf "%d/%d" $ic.Width $ic.Height }}|
42 `
43
44 b := hugolib.Test(t, files)
45
46 b.ImageHelper("public/sunrise_hu_8dd1706a77fb35ce.webp").AssertFormat("webp").AssertIsAnimated(false)
47
48 b.AssertFileContent("public/index.html",
49 "Original Width/Height: 1024/640|",
50 "Resized Width:/Height 123/456|",
51 "Resized RelPermalink: /sunrise_hu_8dd1706a77fb35ce.webp|",
52 "ImageConfig: 1024/640|",
53 )
54 }
55
56 func TestWebPEncodeGrayscale(t *testing.T) {
•57 if imagetesting.SkipGoldenTests {
58 t.Skip("Skip golden test on this architecture")
59 }
60
61 files := `
62 -- assets/gopher.png --
63 sourcefilename: ../../resources/testdata/bw-gopher.png
64 -- layouts/home.html --
65 {{ $image := resources.Get "gopher.png" }}
66 {{ $resized := $image.Resize "123x456 webp" }}
67 Resized RelPermalink: {{ $resized.RelPermalink }}|
68 `
69
70 b := hugolib.Test(t, files)
71
72 b.ImageHelper("public/gopher_hu_cc98ebaf742cba8e.webp").AssertFormat("webp")
73 }
74
75 func TestWebPInvalid(t *testing.T) {
76 files := `
77 -- assets/invalid.webp --
78 sourcefilename: ../../resources/testdata/webp/invalid.webp
79 -- layouts/home.html --
80 {{ $image := resources.Get "invalid.webp" }}
81 {{ $resized := $image.Resize "123x456 webp" }}
82 Resized RelPermalink: {{ $resized.RelPermalink }}|
83 `
84 tempDir := t.TempDir()
85
86 b, err := hugolib.TestE(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
87 cfg.NeedsOsFS = true
88 cfg.WorkingDir = tempDir
89 }))
90 b.Assert(err, qt.IsNotNil)
91
92 if runtime.GOOS != "windows" {
93 // Make sure the full image filename is in the error message.
94 filename := filepath.Join(tempDir, "assets/invalid.webp")
95 b.Assert(err.Error(), qt.Contains, filename)
96 }
97 }
98
99 // This test isn't great, but we have golden tests to verify the output itself.
100 func TestWebPAnimation(t *testing.T) {
•101 if imagetesting.SkipGoldenTests {
102 t.Skip("Skip golden test on this architecture")
103 }
104
105 files := `
106 -- hugo.toml --
107 disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "robotsTXT", "404"]
108 -- assets/anim.webp --
109 sourcefilename: ../../resources/testdata/webp/anim.webp
110 -- assets/giphy.gif --
111 sourcefilename: ../../resources/testdata/giphy.gif
112 -- layouts/home.html --
113 {{ $webpAnim := resources.Get "anim.webp" }}
114 {{ $gifAnim := resources.Get "giphy.gif" }}
115 {{ ($webpAnim.Resize "100x100 webp").Publish }}
116 {{ ($webpAnim.Resize "100x100 gif").Publish }}
117 {{ ($gifAnim.Resize "100x100 gif").Publish }}
118 {{ ($gifAnim.Resize "100x100 webp").Publish }}
119
120 `
121
122 b := hugolib.Test(t, files)
123
124 // Source animated gif:
125 // Frame durations in ms.
126 giphyFrameDurations := []int{200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200}
127
128 b.ImageHelper("public/giphy_hu_bb052284cc220165.webp").AssertFormat("webp").AssertIsAnimated(true).AssertLoopCount(0).AssertFrameDurations(giphyFrameDurations)
129 b.ImageHelper("public/giphy_hu_c6b8060edf0363b1.gif").AssertFormat("gif").AssertIsAnimated(true).AssertLoopCount(0).AssertFrameDurations(giphyFrameDurations)
130
131 // Source animated webp:
132 animFrameDurations := []int{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80}
133 b.ImageHelper("public/anim_hu_edc2f24aaad2cee6.webp").AssertFormat("webp").AssertIsAnimated(true).AssertLoopCount(0).AssertFrameDurations(animFrameDurations)
134 b.ImageHelper("public/anim_hu_58eb49733894e7ce.gif").AssertFormat("gif").AssertIsAnimated(true).AssertLoopCount(0).AssertFrameDurations(animFrameDurations)
135 }
136
137 // See issue 14985.
138 func TestWebPEncodeOutOfMemory(t *testing.T) {
139 files := `
140 -- assets/gopher.png --
141 sourcefilename: ../../resources/testdata/bw-gopher.png
142 -- layouts/home.html --
143 {{ $img := resources.Get "gopher.png" }}
144 {{ $r := try ($img.Resize "3000x3000 webp") }}
145 {{ with $r.Err }}BigErr: {{ . }}|{{ else }}BigOK|{{ end }}
146 {{ $small := $img.Resize "32x32 webp" }}
147 SmallAfter: {{ $small.RelPermalink }}|
148 `
149
150 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
151 c.WarpcMemory = 8
152 }))
153
154 // The big resize must fail gracefully (caught by try) ...
155 b.AssertFileContent("public/index.html",
156 "BigErr:",
157 "out of memory allocating",
158 "for blob data",
159 // ... and the dispatcher must still process images afterwards.
160 "SmallAfter: /gopher_",
161 )
162 }
163
164 func BenchmarkWebp(b *testing.B) {
165 files := `
166 -- content/p1/sunrise.webp --
167 sourcefilename: ../../resources/testdata/sunrise.webp
168 -- content/p1/index.md --
169 -- content/p2/sunrise.webp --
170 sourcefilename: ../../resources/testdata/sunrise.webp
171 -- content/p2/index.md --
172 -- content/p3/sunrise.webp --
173 sourcefilename: ../../resources/testdata/sunrise.webp
174 -- content/p3/index.md --
175 -- content/p4/sunrise.webp --
176 sourcefilename: ../../resources/testdata/sunrise.webp
177 -- content/p4/index.md --
178 -- content/p5/sunrise.webp --
179 sourcefilename: ../../resources/testdata/sunrise.webp
180 -- content/p5/index.md --
181 -- content/p6/sunrise.webp --
182 sourcefilename: ../../resources/testdata/sunrise.webp
183 -- content/p6/index.md --
184 -- content/p7/sunrise.webp --
185 sourcefilename: ../../resources/testdata/sunrise.webp
186 -- content/p7/index.md --
187 -- content/p8/sunrise.webp --
188 sourcefilename: ../../resources/testdata/sunrise.webp
189 -- content/p8/index.md --
190 -- content/p9/sunrise.webp --
191 sourcefilename: ../../resources/testdata/sunrise.webp
192 -- content/p9/index.md --
193 -- content/p10/sunrise.webp --
194 sourcefilename: ../../resources/testdata/sunrise.webp
195 -- content/p10/index.md --
196 -- layouts/home.html --
197 Home.
198 -- layouts/page.html --
199 {{ $image := .Resources.Get "sunrise.webp" }}
200 {{ $resized := $image.Fit "400x300 webp" }}
201 Resized RelPermalink: {{ $resized.RelPermalink }}|
202 `
203
204 cfg := hugolib.IntegrationTestConfig{
205 T: b,
206 TxtarString: files,
207 }
208
209 for b.Loop() {
210 b.StopTimer()
211 builder := hugolib.NewIntegrationTestBuilder(cfg).Init()
212 b.StartTimer()
213 builder.Build()
214 }
215 }