gen: compare generated code to gold ignoring whitespaces

Reason is that gofmt can change between versions e.g
see https://go-review.googlesource.com/c/go/+/122295/
and this would avoid breaking tests and edit wars

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
Alexander Bezzubov 2019-02-20 15:46:27 +01:00
parent c8e0f75132
commit baefa18475
No known key found for this signature in database
GPG Key ID: 8039F5787EFCD05D

View File

@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -282,6 +283,17 @@ func (s *GeneratorTestSuite) TestGenerationFiles() {
assert.NoError(s.T(), err)
out, err := ioutil.ReadFile(outPath.Name())
assert.NoError(s.T(), err)
assert.Equal(s.T(), string(gold), string(out))
expected := normalizeSpaces(string(gold))
actual := normalizeSpaces(string(out))
assert.Equal(s.T(), expected, actual, "Test %s", test.name)
}
}
// normalizeSpaces returns a copy of str with whitespaces normalizeds.
// We use this to compare generated source as gofmt format may change.
// E.g for changines beteween Go 1.10 and 1.11 see
// https://go-review.googlesource.com/c/go/+/122295/
func normalizeSpaces(str string) string {
return strings.Join(strings.Fields(str), " ")
}