Debian Codegraph @

internal/warpc/webp_integration_test.go

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