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("public/index.html",
83 `<img data-id="0" data-img-hash="6ccacf8056c41475" data-level="" data-scale="" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
84 `<img data-id="1" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
85 `<img data-id="2" data-img-hash="6ccacf8056c41475" data-level="medium" data-scale="4" data-targetDir="" src="/qr_924bf7d80a564b23.png">`,
86 `<img data-id="3" data-img-hash="c29338c3d105b156" data-level="low" data-scale="2" data-targetDir="" src="/qr_9bf1ce25c5f2c058.png">`,
87 `<img data-id="4" data-img-hash="8f7a639cea917b0e" data-level="medium" data-scale="3" data-targetDir="" src="/qr_7af14b329dd10af7.png">`,
88 `<img data-id="5" data-img-hash="2d15d6dcb861b5da" data-level="quartile" data-scale="5" data-targetDir="" src="/qr_9600ecb2010c2185.png">`,
89 `<img data-id="6" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="" src="/qr_bdc74ee7f5c11cc6.png">`,
90 `<img data-id="7" data-img-hash="113c45f2c091bc4d" data-level="high" data-scale="6" data-targetDir="foo/bar" src="/foo/bar/qr_14162f02f2b83fff.png">`,
91 )
92
93 files = strings.ReplaceAll(files, "low", "foo")
94
95 b, err := hugolib.TestE(t, files)
96 b.Assert(err.Error(), qt.Contains, "error correction level must be one of low, medium, quartile, or high")
97
98 files = strings.ReplaceAll(files, "foo", "low")
99 files = strings.ReplaceAll(files, "https://gohugo.io", "")
100
101 b, err = hugolib.TestE(t, files)
102 b.Assert(err.Error(), qt.Contains, "cannot encode an empty string")
103 }
104
105 func TestImagesGoldenFuncs(t *testing.T) {
106 t.Parallel()
107
108 if imagetesting.SkipGoldenTests {
109 t.Skip("Skip golden test on this architecture")
110 }
111
112 // Will be used as the base folder for generated images.
113 name := "funcs"
114
115 files := `
116 -- hugo.toml --
117 -- assets/sunset.jpg --
118 sourcefilename: ../../resources/testdata/sunset.jpg
119
120 -- layouts/home.html --
121 Home.
122
123 {{ template "copy" (dict "name" "qr-default.png" "img" (images.QR "https://gohugo.io")) }}
124 {{ template "copy" (dict "name" "qr-level-high_scale-6.png" "img" (images.QR "https://gohugo.io" (dict "level" "high" "scale" 6))) }}
125
126 {{ define "copy"}}
127 {{ if lt (len (path.Ext .name)) 4 }}
128 {{ errorf "No extension in %q" .name }}
129 {{ end }}
130 {{ $img := .img }}
131 {{ $name := printf "images/%s" .name }}
132 {{ with $img | resources.Copy $name }}
133 {{ .Publish }}
134 {{ end }}
135 {{ end }}
136 `
137
138 opts := imagetesting.DefaultGoldenOpts
139 opts.T = t
140 opts.Name = name
141 opts.Files = files
142
143 imagetesting.RunGolden(opts)
144 }
145
146 func TestPNGIssue14288(t *testing.T) {
•147 if imagetesting.SkipGoldenTests {
148 t.Skip("Skip golden test on this architecture")
149 }
150
151 t.Parallel()
152
153 files := `
154 -- assets/qr.png --
155 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
156 -- layouts/home.html --
157 {{ $img := resources.Get "qr.png" }}
158 images.Config: {{ (images.Config "assets/qr.png").Width }}
159 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
160 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
161 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
162 `
163
164 b := hugolib.Test(t, files)
165 b.AssertFileContent("public/index.html", `
166 images.Config: 222
167 Resize to 100x100: 100
168 Brightnes method: /qr_hu_d5f06fd7594d0594.png
169 Brightnes func: /qr_hu_d5f06fd7594d0594.png
170 `)
171 }
172
173 func TestPNGNamedWebpIssue14288(t *testing.T) {
•174 if imagetesting.SkipGoldenTests {
175 t.Skip("Skip golden test on this architecture")
176 }
177
178 t.Parallel()
179
180 files := `
181 -- assets/qr.webp --
182 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
183 -- layouts/home.html --
184 {{ $img := resources.Get "qr.webp" }}
185 images.Config: {{ (images.Config "assets/qr.webp").Width }}
186 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
187 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
188 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
189 `
190
191 b := hugolib.Test(t, files)
192 b.AssertFileContent("public/index.html", `
193 images.Config: 222
194 Resize to 100x100: 100
195 Brightnes method: /qr_hu_d5f06fd7594d0594.webp
196 Brightnes func: /qr_hu_d5f06fd7594d0594.webp
197 `)
198 }
199
200 // Note that this test doesn't really reproduce the original issue,
201 // but keep it as a regression test for WebP decoding in general.
202 // TODO(bep) I fixed the failing site, but I don't really understand why it failed. But now it's Christmas.
203 func TestPNGIssue14295(t *testing.T) {
204 t.Parallel()
205
206 files := `
207 -- hugo.toml --
208 disableLiveReload = true
209 -- assets/qr.png --
210 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
211 -- layouts/home.html --
212 {{ $img := resources.Get "qr.png" }}
213 {{ $img = $img.Resize "200x webp" | images.Filter (images.GaussianBlur 5) }}
214 Image: {{ $img.RelPermalink }}
215
216 `
217
218 tempDir := t.TempDir()
219
220 for range 2 {
221 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
222 cfg.NeedsOsFS = true
223 cfg.WorkingDir = tempDir
224 }))
225 b.AssertFileContent("public/index.html", `Image: /qr_hu_6b5f59be9c4d3b3a.webp`)
226
227 }
228 }