Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1445f04

Browse files
authored
fix: Images compare (#227)
* 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
1 parent 8b68b22 commit 1445f04

19 files changed

Lines changed: 933 additions & 7 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ require (
77
github.com/briandowns/spinner v1.20.0
88
github.com/disintegration/imaging v1.6.2
99
github.com/hashicorp/go-multierror v1.1.1
10+
github.com/obalunenko/getenv v1.1.0
1011
github.com/obalunenko/logger v0.5.1
1112
github.com/obalunenko/version v1.1.0
13+
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6
1214
github.com/ory/dockertest/v3 v3.9.1
1315
github.com/schollz/progressbar/v3 v3.12.2
1416
github.com/spf13/viper v1.14.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,14 @@ github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXy
238238
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
239239
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
240240
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
241+
github.com/obalunenko/getenv v1.1.0 h1:vdaHNGjM0eahS+pXbld6enRNA74HEg+eYyiqgXja5GQ=
242+
github.com/obalunenko/getenv v1.1.0/go.mod h1:rQSU30CHN52CamPdfTjUF4dk0oaoTo03kjxw2Yx3s2Y=
241243
github.com/obalunenko/logger v0.5.1 h1:49DIEwy4sBvRzM/OtD/m7zITi5L3S5DtSYZ/WoUrnno=
242244
github.com/obalunenko/logger v0.5.1/go.mod h1:/GnmNcmEV7tAL9StHrs3nDkXAK+TEvChoNY+YYRZww4=
243245
github.com/obalunenko/version v1.1.0 h1:yVua7OHnK3+MJpendeMmAlfzVmq7R1h8MO3Ufz7HEec=
244246
github.com/obalunenko/version v1.1.0/go.mod h1:Or267aCQxNcAtgOeWA7yOe/RqJS4XDaMfcFwk3ohbOg=
247+
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6 h1:a/kynVgbdXJQDq3WWTgwL0bHyg4hu4/oIK9UB+Ugvfo=
248+
github.com/olegfedoseev/image-diff v0.0.0-20171116094004-897a4e73dfd6/go.mod h1:OgMVaRcJ1TgmPHB/MF2YaHOzRxmw6vVG/DquoMhkCiY=
245249
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
246250
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
247251
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=

internal/media/ops_test.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package media
22

33
import (
44
"bytes"
5-
"crypto/sha256"
5+
"fmt"
66
"io"
77
"os"
88
"path/filepath"
9+
"strings"
910
"testing"
1011

12+
"github.com/obalunenko/getenv"
13+
diff "github.com/olegfedoseev/image-diff"
1114
"github.com/stretchr/testify/assert"
1215
"github.com/stretchr/testify/require"
1316
)
@@ -17,6 +20,10 @@ type file struct {
1720
}
1821

1922
func Test_addBorders(t *testing.T) {
23+
if getenv.BoolOrDefault("CI", false) {
24+
t.Skip("Doesn't work on CI")
25+
}
26+
2027
type file struct {
2128
path string
2229
}
@@ -119,7 +126,7 @@ func Test_addBorders(t *testing.T) {
119126

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

122-
diff(t, want, got)
129+
diffImageReaders(t, want, got)
123130
})
124131
}
125132
}
@@ -133,18 +140,45 @@ func getReaderFromPath(tb testing.TB, path string) io.Reader {
133140
return bytes.NewReader(content)
134141
}
135142

136-
func diff(tb testing.TB, want, actual io.Reader) {
143+
func diffImageReaders(tb testing.TB, want, actual io.Reader) {
137144
tb.Helper()
138145

139-
h1, h2 := sha256.New(), sha256.New()
146+
wantimg, err := decode(want)
147+
require.NoError(tb, err)
140148

141-
_, err := io.Copy(h1, want)
149+
actimg, err := decode(actual)
142150
require.NoError(tb, err)
143151

144-
_, err = io.Copy(h2, actual)
152+
var eq bool
153+
154+
d, percent, err := diff.CompareImages(wantimg, actimg)
145155
require.NoError(tb, err)
146156

147-
assert.True(tb, bytes.Equal(h1.Sum(nil), h2.Sum(nil)))
157+
if percent > 0.0 {
158+
name := strings.ReplaceAll(fmt.Sprintf("%s_diff.jpg", tb.Name()), "/", "_")
159+
160+
f, err := os.Create(filepath.Join("testdata", name))
161+
require.NoError(tb, err)
162+
163+
tb.Cleanup(func() {
164+
require.NoError(tb, f.Close())
165+
})
166+
167+
r, err := encode(d)
168+
require.NoError(tb, err)
169+
170+
buf := new(bytes.Buffer)
171+
172+
_, err = buf.ReadFrom(r)
173+
require.NoError(tb, err)
174+
175+
_, err = f.Write(buf.Bytes())
176+
require.NoError(tb, err)
177+
} else {
178+
eq = true
179+
}
180+
181+
assert.True(tb, eq)
148182
}
149183

150184
func Test_getFileContentType(t *testing.T) {

internal/media/testdata/1440x960_heic.jpg

Whitespace-only changes.

vendor/github.com/obalunenko/getenv/.gitattributes

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/obalunenko/getenv/.gitignore

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/obalunenko/getenv/.golangci.pipe.yml

Lines changed: 126 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)