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