gen-test: add readable text diff output

test plan:
 * go test -run '^Test_GeneratorTestSuite$' \
	-testify.m '^(TestGenerationFiles)$' \
	github.com/go-enry/go-enry/v2/internal/code-generator/generator
This commit is contained in:
Alex Bezzubov
2022-12-03 09:49:09 +01:00
parent 86cae02425
commit c79c32f525
2 changed files with 70 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package generator
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
@ -9,6 +10,7 @@ import (
"strings"
"testing"
"github.com/go-enry/go-enry/v2/internal/code-generator/generator/diff"
"github.com/go-enry/go-enry/v2/internal/tokenizer"
"github.com/stretchr/testify/assert"
@ -321,11 +323,17 @@ func (s *GeneratorTestSuite) TestGenerationFiles() {
expected := normalizeSpaces(string(gold))
actual := normalizeSpaces(string(out))
assert.Equal(s.T(), expected, actual, "Test %s", test.name)
// this produces large unreadable output, so we do it 'manually' instead
// assert.Equal(s.T(), expected, actual, "Test %s", test.name)
if expected != actual {
s.T().Logf("%s generated is different from %q", test.name, test.wantOut)
s.T().Logf("Expected %q", expected[:400])
s.T().Logf("Actual %q", actual[:400])
assert.Fail(s.T(), fmt.Sprintf("%s output is different from %q", test.name, test.wantOut))
diff, err := diff.Diff(gold, out)
if err != nil {
s.T().Logf("Failed produce a diff between expected and actual: %s", err.Error())
s.T().Logf("Expected %q", expected[:400])
s.T().Logf("Actual %q", actual[:400])
}
s.T().Logf("\n%s", diff)
}
}