Skip to content

Commit 80ea90c

Browse files
committed
images: Add some more PNG tests
To try to figure out #14288.
1 parent f740d7c commit 80ea90c

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

‎tpl/images/images_integration_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@ Home.
142142

143143
imagetesting.RunGolden(opts)
144144
}
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+
}

‎tpl/images/images_test.go‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,32 @@ import (
1717
"bytes"
1818
"image"
1919
"image/color"
20-
"image/png"
2120
"path/filepath"
2221
"testing"
2322

2423
qt "github.com/frankban/quicktest"
2524
"github.com/gohugoio/hugo/config"
2625
"github.com/gohugoio/hugo/config/testconfig"
26+
"github.com/gohugoio/hugo/resources/images"
2727
"github.com/spf13/afero"
2828
"github.com/spf13/cast"
2929
)
3030

3131
type tstNoStringer struct{}
3232

33+
type widthHeight struct {
34+
Width int
35+
Height int
36+
}
37+
3338
var configTests = []struct {
3439
path any
35-
input []byte
40+
input widthHeight
3641
expect any
3742
}{
3843
{
3944
path: "a.png",
40-
input: blankImage(10, 10),
45+
input: widthHeight{10, 10},
4146
expect: image.Config{
4247
Width: 10,
4348
Height: 10,
@@ -46,7 +51,7 @@ var configTests = []struct {
4651
},
4752
{
4853
path: "a.png",
49-
input: blankImage(10, 10),
54+
input: widthHeight{10, 10},
5055
expect: image.Config{
5156
Width: 10,
5257
Height: 10,
@@ -55,7 +60,7 @@ var configTests = []struct {
5560
},
5661
{
5762
path: "b.png",
58-
input: blankImage(20, 15),
63+
input: widthHeight{20, 15},
5964
expect: image.Config{
6065
Width: 20,
6166
Height: 15,
@@ -64,7 +69,7 @@ var configTests = []struct {
6469
},
6570
{
6671
path: "a.png",
67-
input: blankImage(20, 15),
72+
input: widthHeight{20, 15},
6873
expect: image.Config{
6974
Width: 10,
7075
Height: 10,
@@ -101,7 +106,8 @@ func TestNSConfig(t *testing.T) {
101106
// cast path to string for afero.WriteFile
102107
sp, err := cast.ToStringE(test.path)
103108
c.Assert(err, qt.IsNil)
104-
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), test.input, 0o755)
109+
img := blankImage(d.ResourceSpec.Imaging.Codec, test.input.Width, test.input.Height)
110+
afero.WriteFile(ns.deps.Fs.Source, filepath.Join(bcfg.WorkingDir(), sp), img, 0o755)
105111

106112
result, err := ns.Config(test.path)
107113

@@ -111,10 +117,13 @@ func TestNSConfig(t *testing.T) {
111117
}
112118
}
113119

114-
func blankImage(width, height int) []byte {
120+
func blankImage(codec *images.Codec, width, height int) []byte {
115121
var buf bytes.Buffer
116122
img := image.NewRGBA(image.Rect(0, 0, width, height))
117-
if err := png.Encode(&buf, img); err != nil {
123+
cfg := images.ImageConfig{
124+
TargetFormat: images.PNG,
125+
}
126+
if err := codec.EncodeTo(cfg, &buf, img); err != nil {
118127
panic(err)
119128
}
120129
return buf.Bytes()

0 commit comments

Comments
 (0)