From 8ce87b8603dd9f247b51e379400e7d978f1507bb Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Fri, 9 Feb 2024 13:06:37 +0600 Subject: [PATCH] :technologist: Show diff between test output and golden file --- go.mod | 2 ++ go.sum | 2 ++ testing/testing.go | 7 ++++--- 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 72b1b81..1aea0cb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module source.toby3d.me/toby3d/hacks go 1.22.0 + +require github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5a8d551 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/testing/testing.go b/testing/testing.go index 10b9796..8e30c3d 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -2,13 +2,14 @@ package testing import ( - "bytes" "errors" "flag" "io" "os" "path/filepath" "testing" + + "github.com/google/go-cmp/cmp" ) //nolint:gochecknoglobals // сompiler global flags cannot be read from within tests @@ -67,7 +68,7 @@ func GoldenEqual(tb testing.TB, output io.Reader) { tb.Fatal("cannot read golden file data:", err) } - if !bytes.Equal(actual, expect) { - tb.Errorf("the test output does not match the contents of %s.golden file", tb.Name()) + if diff := cmp.Diff(actual, expect); diff != "" { + tb.Error(diff) } }