Skip to content

Commit

Permalink
improve control flow in test helper assertGoldenMatch
Browse files Browse the repository at this point in the history
includes replacement of deprecated functions
  • Loading branch information
toaster committed Mar 4, 2024
1 parent 9db3deb commit bdf57ba
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
Expand Down Expand Up @@ -1077,20 +1078,21 @@ func assertGoldenMatch(t *testing.T, file string, buf []byte, format ImageType)
prefix := file[:i] + "." + name + "-" + getEnvironment()
ext := format.FileExt()
goldenFile := prefix + ".golden" + ext

golden, _ := ioutil.ReadFile(goldenFile)
if assert.NotNil(t, golden, "golden file %s is missing", goldenFile) {
sameAsGolden := assert.True(t, bytes.Equal(buf, golden), "Actual image (size=%d) didn't match expected golden file=%s (size=%d)", len(buf), goldenFile, len(golden))
if !sameAsGolden {
failed := prefix + ".failed" + ext
require.NoError(t, ioutil.WriteFile(failed, buf, 0666))
}
if !assert.FileExists(t, goldenFile) {
t.Log("writing golden file: " + goldenFile)
err := os.WriteFile(goldenFile, buf, 0644)
assert.NoError(t, err)
return
}

t.Log("writing golden file: " + goldenFile)
err := ioutil.WriteFile(goldenFile, buf, 0644)
assert.NoError(t, err)
golden, err := os.ReadFile(goldenFile)
require.NoError(t, err)

sameAsGolden := assert.True(t, bytes.Equal(buf, golden), "Actual image (size=%d) didn't match expected golden file=%s (size=%d)", len(buf), goldenFile, len(golden))
if !sameAsGolden {
failed := prefix + ".failed" + ext
require.NoError(t, os.WriteFile(failed, buf, 0666))
}
}

func goldenAnimatedTest(
Expand Down

0 comments on commit bdf57ba

Please sign in to comment.