Debian Codegraph @

tpl/images/images_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 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 t.Parallel()
148
149 files := `
150 -- assets/qr.png --
151 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
152 -- layouts/home.html --
153 {{ $img := resources.Get "qr.png" }}
154 images.Config: {{ (images.Config "assets/qr.png").Width }}
155 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
156 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
157 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
158 `
159
160 b := hugolib.Test(t, files)
161 b.AssertFileContent("public/index.html", `
162 images.Config: 222
163 Resize to 100x100: 100
164 Brightnes method: /qr_hu_d5f06fd7594d0594.png
165 Brightnes func: /qr_hu_d5f06fd7594d0594.png
166 `)
167 }
168
169 func TestPNGNamedWebpIssue14288(t *testing.T) {
170 t.Parallel()
171
172 files := `
173 -- assets/qr.webp --
174 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
175 -- layouts/home.html --
176 {{ $img := resources.Get "qr.webp" }}
177 images.Config: {{ (images.Config "assets/qr.webp").Width }}
178 Resize to 100x100: {{ ($img.Resize "100x100" ).Width }}
179 Brightnes method: {{ ($img.Filter (images.Brightness 12) ).RelPermalink }}
180 Brightnes func: {{ ($img | images.Filter (images.Brightness 12) ).RelPermalink }}
181 `
182
183 b := hugolib.Test(t, files)
184 b.AssertFileContent("public/index.html", `
185 images.Config: 222
186 Resize to 100x100: 100
187 Brightnes method: /qr_hu_d5f06fd7594d0594.webp
188 Brightnes func: /qr_hu_d5f06fd7594d0594.webp
189 `)
190 }
191
192 // Note that this test doesn't really reproduce the original issue,
193 // but keep it as a regression test for WebP decoding in general.
194 // TODO(bep) I fixed the failing site, but I don't really understand why it failed. But now it's Christmas.
195 func TestPNGIssue14295(t *testing.T) {
196 t.Parallel()
197
198 files := `
199 -- hugo.toml --
200 disableLiveReload = true
201 -- assets/qr.png --
202 sourcefilename: testdata/images_golden/funcs/qr-level-high_scale-6.png
203 -- layouts/home.html --
204 {{ $img := resources.Get "qr.png" }}
205 {{ $img = $img.Resize "200x webp" | images.Filter (images.GaussianBlur 5) }}
206 Image: {{ $img.RelPermalink }}
207
208 `
209
210 tempDir := t.TempDir()
211
212 for range 2 {
213 b := hugolib.Test(t, files, hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) {
214 cfg.NeedsOsFS = true
215 cfg.WorkingDir = tempDir
216 }))
217 b.AssertFileContent("public/index.html", `Image: /qr_hu_6b5f59be9c4d3b3a.webp`)
218
219 }
220 }