Skip to content

Commit

Permalink
fix: Images compare (#227)
Browse files Browse the repository at this point in the history
* fix: Images compare

* fix: Diff image filename

* ci: Add macos to matrix

* ci: Remove macos from matrix

* fix: Skip add borders on ci

* chore: Update deps

* style: Fix imports order
  • Loading branch information
obalunenko authored Jan 4, 2023
1 parent 8b68b22 commit 1445f04
Show file tree
Hide file tree
Showing 19 changed files with 933 additions and 7 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ require (
github.com/briandowns/spinner v1.20.0
github.com/disintegration/imaging v1.6.2
github.com/hashicorp/go-multierror v1.1.1
github.com/obalunenko/getenv v1.1.0
github.com/obalunenko/logger v0.5.1
github.com/obalunenko/version v1.1.0
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6
github.com/ory/dockertest/v3 v3.9.1
github.com/schollz/progressbar/v3 v3.12.2
github.com/spf13/viper v1.14.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,14 @@ github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXy
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
github.com/obalunenko/getenv v1.1.0 h1:vdaHNGjM0eahS+pXbld6enRNA74HEg+eYyiqgXja5GQ=
github.com/obalunenko/getenv v1.1.0/go.mod h1:rQSU30CHN52CamPdfTjUF4dk0oaoTo03kjxw2Yx3s2Y=
github.com/obalunenko/logger v0.5.1 h1:49DIEwy4sBvRzM/OtD/m7zITi5L3S5DtSYZ/WoUrnno=
github.com/obalunenko/logger v0.5.1/go.mod h1:/GnmNcmEV7tAL9StHrs3nDkXAK+TEvChoNY+YYRZww4=
github.com/obalunenko/version v1.1.0 h1:yVua7OHnK3+MJpendeMmAlfzVmq7R1h8MO3Ufz7HEec=
github.com/obalunenko/version v1.1.0/go.mod h1:Or267aCQxNcAtgOeWA7yOe/RqJS4XDaMfcFwk3ohbOg=
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6 h1:a/kynVgbdXJQDq3WWTgwL0bHyg4hu4/oIK9UB+Ugvfo=
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6/go.mod h1:OgMVaRcJ1TgmPHB/MF2YaHOzRxmw6vVG/DquoMhkCiY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
Expand Down
48 changes: 41 additions & 7 deletions internal/media/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package media

import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/obalunenko/getenv"
diff "github.com/olegfedoseev/image-diff"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -17,6 +20,10 @@ type file struct {
}

func Test_addBorders(t *testing.T) {
if getenv.BoolOrDefault("CI", false) {
t.Skip("Doesn't work on CI")
}

type file struct {
path string
}
Expand Down Expand Up @@ -119,7 +126,7 @@ func Test_addBorders(t *testing.T) {

want := getReaderFromPath(t, tt.want.path)

diff(t, want, got)
diffImageReaders(t, want, got)
})
}
}
Expand All @@ -133,18 +140,45 @@ func getReaderFromPath(tb testing.TB, path string) io.Reader {
return bytes.NewReader(content)
}

func diff(tb testing.TB, want, actual io.Reader) {
func diffImageReaders(tb testing.TB, want, actual io.Reader) {
tb.Helper()

h1, h2 := sha256.New(), sha256.New()
wantimg, err := decode(want)
require.NoError(tb, err)

_, err := io.Copy(h1, want)
actimg, err := decode(actual)
require.NoError(tb, err)

_, err = io.Copy(h2, actual)
var eq bool

d, percent, err := diff.CompareImages(wantimg, actimg)
require.NoError(tb, err)

assert.True(tb, bytes.Equal(h1.Sum(nil), h2.Sum(nil)))
if percent > 0.0 {
name := strings.ReplaceAll(fmt.Sprintf("%s_diff.jpg", tb.Name()), "/", "_")

f, err := os.Create(filepath.Join("testdata", name))
require.NoError(tb, err)

tb.Cleanup(func() {
require.NoError(tb, f.Close())
})

r, err := encode(d)
require.NoError(tb, err)

buf := new(bytes.Buffer)

_, err = buf.ReadFrom(r)
require.NoError(tb, err)

_, err = f.Write(buf.Bytes())
require.NoError(tb, err)
} else {
eq = true
}

assert.True(tb, eq)
}

func Test_getFileContentType(t *testing.T) {
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions vendor/github.com/obalunenko/getenv/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/obalunenko/getenv/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 126 additions & 0 deletions vendor/github.com/obalunenko/getenv/.golangci.pipe.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1445f04

Please sign in to comment.