Debian Codegraph @

tpl/images/images_integration_test.go

raw ◀ ▶
1 Debian patch touch this file
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 images_test
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 "github.com/gohugoio/hugo/resources/images/imagetesting"
23 )
24
25 func TestImageConfigFromModule(t *testing.T) {
26 t.Parallel()
27
28 files := `
29 -- hugo.toml --
30 baseURL = 'http://example.com/'
31 theme = ["mytheme"]
32 -- static/images/pixel1.png --
33 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
34 -- themes/mytheme/static/images/pixel2.png --
35 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
36 -- layouts/home.html --
37 {{ $path := "static/images/pixel1.png" }}
38 fileExists OK: {{ fileExists $path }}|
39 imageConfig OK: {{ (imageConfig $path).Width }}|
40 {{ $path2 := "static/images/pixel2.png" }}
41 fileExists2 OK: {{ fileExists $path2 }}|
42 imageConfig2 OK: {{ (imageConfig $path2).Width }}|
43
44 `
45
46 b := hugolib.Test(t, files)
47
48 b.AssertFileContent("public/index.html", `
49 fileExists OK: true|
50 imageConfig OK: 1|
51 fileExists2 OK: true|
52 imageConfig2 OK: 1|
53 `)
54 }
55
56 func TestQR(t *testing.T) {
57 t.Parallel()
58
59 files := `
60 -- hugo.toml --
61 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
62 -- layouts/home.html --
63 {{- $text := "https://gohugo.io" }}
64 {{- $optionMaps := slice
65 (dict)
66 (dict "level" "medium")
67 (dict "level" "medium" "scale" 4)
68 (dict "level" "low" "scale" 2)
69 (dict "level" "medium" "scale" 3)
70 (dict "level" "quartile" "scale" 5)
71 (dict "level" "high" "scale" 6)
72 (dict "level" "high" "scale" 6 "targetDir" "foo/bar")
73 }}
74 {{- range $k, $opts := $optionMaps }}
75 {{- with images.QR $text $opts }}
76 <img data-id="{{ $k }}" data-img-hash="{{ .Content | hash.XxHash }}" data-level="{{ $opts.level }}" data-scale="{{ $opts.scale }}" data-targetDir="{{ $opts.targetDir }}" src="{{ .RelPermalink }}">
77 {{- end }}
78 {{- end }}
79 `
80
81 b := hugolib.Test(t, files)
82 b.AssertFileContent(
83 "public/index.html",
84 `<img data-id="0" data-img-hash="6ccacf8056c41475" data-level="" data-scale="" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
85 `<img data-id="1" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
86 `<img data-id="2" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="4" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
87 `<img data-id="3" data-img-hash="c29338c3d105b156" data-level="low" data-scale="2" data-targetDir="" src="/qr_9bf1ce25c5f2c058.png">`,
88 `<img data-id="4" data-img-hash="8f7a639cea917b0e" data-level="medium" data-scale="3" data-targetDir="" src="/qr_7af14b329dd10af7.png">`,
89 `<img data-id="5" data-img-hash="2d15d6dcb861b5da" data-level="quartile" data-scale="5" data-targetDir="" src="/qr_9600ecb2010c2185.png">`,
90 `<img data-id="6" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="" src="/qr_bdc74ee7f5c11cc6.png">`,
91 `<img data-id="7" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="foo/bar" src="/foo/bar/qr_14162f02f2b83fff.png">`,
92 )
93
94 files = strings.ReplaceAll(files, "low", "foo")
95
96 b, err := hugolib.TestE(t, files)
97 b.Assert(err.Error(), qt.Contains, "error correction level must be one of low, medium, quartile, or high")
98
99 files = strings.ReplaceAll(files, "foo", "low")
100 files = strings.ReplaceAll(files, "https://gohugo.io", "")
101
102 b, err = hugolib.TestE(t, files)
103 b.Assert(err.Error(), qt.Contains, "cannot encode an empty string")
104 }
105
106 func TestImagesGoldenFuncs(t *testing.T) {
107 t.Parallel()
108
109 if imagetesting.SkipGoldenTests {
110 t.Skip("Skip golden test on this architecture")
111 }
112
113 // Will be used as the base folder for generated images.
114 name := "funcs"
115
116 files := `
117 -- hugo.toml --
118 -- assets/sunset.jpg --
119 sourcefilename: ../../resources/testdata/sunset.jpg
120
121 -- layouts/home.html --
122 Home.
123
124 {{ template "copy" (dict "name" "qr-default.png" "img" (images.QR "https://gohugo.io")) }}
125 {{ template "copy" (dict "name" "qr-level-high_scale-6.png" "img" (images.QR "https://gohugo.io" (dict "level" "high" "scale" 6))) }}
126
127 {{ define "copy"}}
128 {{ if lt (len (path.Ext .name)) 4 }}
129 {{ errorf "No extension in %q" .name }}
130 {{ end }}
131 {{ $img := .img }}
132 {{ $name := printf "images/%s" .name }}
133 {{ with $img | resources.Copy $name }}
134 {{ .Publish }}
135 {{ end }}
136 {{ end }}
137 `
138
139 opts := imagetesting.DefaultGoldenOpts
140 opts.T = t
141 opts.Name = name
142 opts.Files = files
143
144 imagetesting.RunGolden(opts)
145 }
146
147 func TestPNGIssue14288(t *testing.T) {
•148 if imagetesting.SkipGoldenTests {
149 t.Skip("Skip golden test on this architecture")
150 }
151
152 t.Parallel()
153
154 files := `
155 -- assets/qr.png --
156 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
157 -- layouts/home.html --
158 {{ $img := resources.Get "qr.png" }}
159 images.Config: {{ (images.Config "assets/qr.png").Width }}
160 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
161 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
162 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
163 `
164
165 b := hugolib.Test(t, files)
166 b.AssertFileContent("public/index.html", `
167 images.Config: 222
168 Resize to 100x100: 100
169 Brightnes method: /qr_hu_e82a6802e23c740e.png
170 Brightnes func: /qr_hu_e82a6802e23c740e.png
171 `)
172 }
173
174 func TestPNGNamedWebpIssue14288(t *testing.T) {
•175 if imagetesting.SkipGoldenTests {
176 t.Skip("Skip golden test on this architecture")
177 }
178
179 t.Parallel()
180
181 files := `
182 -- assets/qr.webp --
183 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
184 -- layouts/home.html --
185 {{ $img := resources.Get "qr.webp" }}
186 images.Config: {{ (images.Config "assets/qr.webp").Width }}
187 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
188 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
189 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
190 `
191
192 b := hugolib.Test(t, files)
193 b.AssertFileContent("public/index.html", `
194 images.Config: 222
195 Resize to 100x100: 100
196 Brightnes method: /qr_hu_61e637d0f762ec78.webp
197 Brightnes func: /qr_hu_61e637d0f762ec78.webp
198 `)
199 }
200
201 // Note that this test doesn't really reproduce the original issue,
202 // but keep it as a regression test for WebP decoding in general.
203 // TODO(bep) I fixed the failing site, but I don't really understand why it failed. But now it's Christmas.
204 func TestPNGIssue14295(t *testing.T) {
205 t.Parallel()
206
207 files := `
208 -- hugo.toml --
209 disableLiveReload = true
210 -- assets/qr.png --
211 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
212 -- layouts/home.html --
213 {{ $img := resources.Get "qr.png" }}
214 {{ $img = $img.Resize "200x webp" | images.Filter (images.GaussianBlur 5) }}
215 Image: {{ $img.RelPermalink }}
216
217 `
218
219 tempDir := t.TempDir()
220
221 for range 2 {
222 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
223 cfg.NeedsOsFS = true
224 cfg.WorkingDir = tempDir
225 }))
226 b.AssertFileContent("public/index.html", `Image: /qr_hu_7178b50f0d0a9def.webp`)
227
228 }
229 }