From 5056eac15993187eeb44dd5c9c11658b9bff6333 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Sat, 27 Jan 2024 16:06:08 -0800 Subject: [PATCH] internal/test: allow images to be +/- 1 RGB value per pixel This provides robustness when the rendering engine behaves (ever so) slightly differently. Fixes #4573 (I think, I can't be entirely sure because my tests still fail because of #4572) While we're here, let gofumpt add an octal marker to 0o755. --- internal/test/util.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/test/util.go b/internal/test/util.go index 2f0631356d..0ec6e35870 100644 --- a/internal/test/util.go +++ b/internal/test/util.go @@ -10,7 +10,6 @@ import ( "path/filepath" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -44,13 +43,38 @@ func AssertImageMatches(t *testing.T, masterFilename string, img image.Image, ms if len(msgAndArgs) > 0 { msg = fmt.Sprintf(msgAndArgs[0].(string)+"\n", msgAndArgs[1:]...) } - if !assert.Equal(t, masterPix, capturePix, "%sImage did not match master. Actual image written to file://%s.", msg, failedPath) { + if !pixCloseEnough(masterPix, capturePix) { + t.Logf("%sImage did not match master. Actual image written to file://%s.", msg, failedPath) require.NoError(t, writeImage(failedPath, img)) return false } return true } +// pixCloseEnough reports whether a and b vary by at most 1 at any given offset. +func pixCloseEnough(a, b []uint8) bool { + if len(a) != len(b) { + return false + } + + for i, v := range a { + w := b[i] + if v == w { + continue + } + // Allow a delta of 1 for rendering variation. + delta := int(v) - int(w) // use int to avoid overflow + if delta < 0 { + delta *= -1 + } + if delta > 1 { + return false + } + } + + return true +} + // NewCheckedImage returns a new black/white checked image with the specified size // and the specified amount of horizontal and vertical tiles. func NewCheckedImage(w, h, hTiles, vTiles int) image.Image { @@ -83,7 +107,7 @@ func pixelsForImage(t *testing.T, img image.Image) []uint8 { } func writeImage(path string, img image.Image) error { - if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return err } f, err := os.Create(path)