Debian Codegraph @

resources/images/imagetesting/testing.go

raw
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 imagetesting
15
16 import (
17 "image"
18 "io/fs"
19 "os"
20 "path/filepath"
21 "runtime"
22 "testing"
23
24 qt "github.com/frankban/quicktest"
25 "github.com/google/go-cmp/cmp"
26
27 "github.com/gohugoio/gift"
28 "github.com/gohugoio/hugo/common/hashing"
29 "github.com/gohugoio/hugo/common/himage"
30 "github.com/gohugoio/hugo/common/hugio"
31 "github.com/gohugoio/hugo/htesting"
32 "github.com/gohugoio/hugo/hugofs"
33 "github.com/gohugoio/hugo/hugolib"
34 )
35
36 var eq = qt.CmpEquals(
37 cmp.Comparer(func(p1, p2 os.FileInfo) bool {
38 return p1.Name() == p2.Name() && p1.Size() == p2.Size() && p1.IsDir() == p2.IsDir()
39 }),
40 cmp.Comparer(func(d1, d2 fs.DirEntry) bool {
41 p1, err1 := d1.Info()
42 p2, err2 := d2.Info()
43 if err1 != nil || err2 != nil {
44 return false
45 }
46 return p1.Name() == p2.Name() && p1.Size() == p2.Size() && p1.IsDir() == p2.IsDir()
47 }),
48 )
49
50 // GoldenImageTestOpts provides options for a golden image test.
51 type GoldenImageTestOpts struct {
52 // The test.
53 T testing.TB
54
55 // Name of the test. Will be used as the base folder for generated images.
56 // Slashes allowed and encouraged.
57 Name string
58
59 // The test site's files in txttar format.
60 Files string
61
62 // Set to true to write golden files to disk.
63 WriteFiles bool
64
65 // If not set, a temporary directory will be created.
66 WorkingDir string
67
68 // Set to true to print the temp dir used and keep it after the test.
69 PrintAndKeepTempDir bool
70
71 // Set to true to skip any assertions. Useful when adding new golden variants to a test.
72 DevMode bool
73
74 // Set to skip any assertions.
75 SkipAssertions bool
76
77 // Whether this represents a rebuild of the same site.
78 // Setting this to true will keep the previous golden image set.
79 Rebuild bool
80 }
81
82 // To rebuild all Golden image tests, toggle WriteFiles=true and run:
83 // GOARCH=amd64 go test -count 1 -timeout 30s -run "^TestImagesGolden" ./...
84 // TODO(bep) see if we can do this via flags.
85 var DefaultGoldenOpts = GoldenImageTestOpts{
86 WriteFiles: false,
87 DevMode: false,
88 }
89
90 func RunGolden(opts GoldenImageTestOpts) *hugolib.IntegrationTestBuilder {
91 opts.T.Helper()
92
93 c := hugolib.Test(opts.T, opts.Files, hugolib.TestOptWithConfig(func(conf *hugolib.IntegrationTestConfig) {
94 conf.NeedsOsFS = true
95 conf.WorkingDir = opts.WorkingDir
96 conf.PrintAndKeepTempDir = opts.PrintAndKeepTempDir
97 }))
98
99 codec := c.H.ResourceSpec.Imaging.Codec
100
101 c.AssertFileContent("public/index.html", "Home.")
102
103 outputDir := filepath.Join(c.H.Conf.WorkingDir(), "public", "images")
104 goldenBaseDir := filepath.Join("testdata", "images_golden")
105 goldenDir := filepath.Join(goldenBaseDir, filepath.FromSlash(opts.Name))
106 if opts.WriteFiles {
107 c.Assert(htesting.IsRealCI(), qt.IsFalse)
108 if !opts.Rebuild {
109 c.Assert(os.MkdirAll(goldenBaseDir, 0o777), qt.IsNil)
110 c.Assert(os.RemoveAll(goldenDir), qt.IsNil)
111 }
112 c.Assert(hugio.CopyDir(hugofs.Os, outputDir, goldenDir, nil), qt.IsNil)
113 return c
114 }
115
116 if opts.SkipAssertions {
117 return c
118 }
119
120 if opts.DevMode {
121 c.Assert(htesting.IsRealCI(), qt.IsFalse)
122 return c
123 }
124
125 shouldSkip := func(d fs.DirEntry) bool {
126 if runtime.GOARCH == "arm64" {
127 // TODO(bep) figure out why this fails on arm64. I have inspected the images, and they look identical.
128 if d.Name() == "giphy_hu_bb052284cc220165.webp" {
129 c.Logf("skipping %s on %s", d.Name(), runtime.GOARCH)
130 return true
131 }
132 }
133 return false
134 }
135
136 decodeAll := func(f *os.File) []image.Image {
137 c.Helper()
138 var images []image.Image
139 v, err := codec.Decode(f)
140 c.Assert(err, qt.IsNil, qt.Commentf(f.Name()))
141
142 if anim, ok := v.(himage.AnimatedImage); ok {
143 images = anim.GetFrames()
144 } else {
145 images = append(images, v)
146 }
147 return images
148 }
149
150 entries1, err := os.ReadDir(outputDir)
151 c.Assert(err, qt.IsNil)
152 entries2, err := os.ReadDir(goldenDir)
153 c.Assert(err, qt.IsNil)
154 c.Assert(len(entries1), qt.Equals, len(entries2))
155 for i, e1 := range entries1 {
156 if shouldSkip(e1) {
157 continue
158 }
159 c.Assert(filepath.Ext(e1.Name()), qt.Not(qt.Equals), "")
160 func() {
161 e2 := entries2[i]
162
163 f1, err := os.Open(filepath.Join(outputDir, e1.Name()))
164 c.Assert(err, qt.IsNil)
165 defer f1.Close()
166
167 f2, err := os.Open(filepath.Join(goldenDir, e2.Name()))
168 c.Assert(err, qt.IsNil)
169 defer f2.Close()
170
171 imgs2 := decodeAll(f2)
172 imgs1 := decodeAll(f1)
173 c.Assert(len(imgs1), qt.Equals, len(imgs2))
174
175 if !UsesFMA {
176 c.Assert(e1, eq, e2)
177 _, err = f1.Seek(0, 0)
178 c.Assert(err, qt.IsNil)
179 _, err = f2.Seek(0, 0)
180 c.Assert(err, qt.IsNil)
181
182 hash1, _, err := hashing.XXHashFromReader(f1)
183 c.Assert(err, qt.IsNil)
184 hash2, _, err := hashing.XXHashFromReader(f2)
185 c.Assert(err, qt.IsNil)
186
187 c.Assert(hash1, qt.Equals, hash2)
188 }
189
190 for i, img1 := range imgs1 {
191 img2 := imgs2[i]
192 nrgba1 := image.NewNRGBA(img1.Bounds())
193 gift.New().Draw(nrgba1, img1)
194 nrgba2 := image.NewNRGBA(img2.Bounds())
195 gift.New().Draw(nrgba2, img2)
196 c.Assert(goldenEqual(nrgba1, nrgba2), qt.Equals, true, qt.Commentf(e1.Name()))
197 }
198 }()
199 }
200 return c
201 }
202
203 // goldenEqual compares two NRGBA images. It is used in golden tests only.
204 // A small tolerance is allowed on architectures using "fused multiply and add"
205 // (FMA) instruction to accommodate for floating-point rounding differences
206 // with control golden images that were generated on amd64 architecture.
207 // See https://golang.org/ref/spec#Floating_point_operators
208 // and https://github.com/gohugoio/hugo/issues/6387 for more information.
209 //
210 // Based on https://github.com/disintegration/gift/blob/a999ff8d5226e5ab14b64a94fca07c4ac3f357cf/gift_test.go#L598-L625
211 // Copyright (c) 2014-2019 Grigory Dryapak
212 // Licensed under the MIT License.
213 func goldenEqual(img1, img2 *image.NRGBA) bool {
214 maxDiff := 0
215 if runtime.GOARCH != "amd64" {
216 // The golden files are created using the AMD64 architecture.
217 // Be lenient on other platforms due to floaging point and dithering differences.
218 maxDiff = 15
219 }
220 if !img1.Rect.Eq(img2.Rect) {
221 return false
222 }
223 if len(img1.Pix) != len(img2.Pix) {
224 return false
225 }
226 for i := range img1.Pix {
227 diff := int(img1.Pix[i]) - int(img2.Pix[i])
228 if diff < 0 {
229 diff = -diff
230 }
231 if diff > maxDiff {
232 return false
233 }
234 }
235 return true
236 }
237
238 // We don't have a CI test environment for these, and there are known dithering issues that makes these time consuming to maintain.
239 // Go changed their JPEG implementation in Go 1.26, so we cannot run the golden tests on earlier versions. See https://go.dev/doc/go1.26#imagejpegpkgimagejpeg
240 var SkipGoldenTests = htesting.GoMinorVersion() < 26 || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "s390x"
241
242 // UsesFMA indicates whether "fused multiply and add" (FMA) instruction is
243 // used. The command "grep FMADD go/test/codegen/floats.go" can help keep
244 // the FMA-using architecture list updated.
245 var UsesFMA = runtime.GOARCH == "s390x" ||
246 runtime.GOARCH == "ppc64" ||
247 runtime.GOARCH == "ppc64le" ||
248 runtime.GOARCH == "arm64" ||
249 runtime.GOARCH == "riscv64" ||
250 runtime.GOARCH == "loong64"