From ebada00cf1e97b6429c34dba4686e73f8e162105 Mon Sep 17 00:00:00 2001 From: Vlad Klokun Date: Wed, 11 Jan 2023 18:44:06 +0200 Subject: [PATCH] tests: show diffs when comparing autofixes This change refactors the TestApplyFixKeepsFormatting test to use assert.Equalf so it will display a convenient diff between the expected and actual fixing result. --- core/pkg/fixhandler/fixhandler_test.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 872def5c..4b263ef1 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -9,6 +9,7 @@ import ( metav1 "github.com/kubescape/kubescape/v2/core/meta/datastructures/v1" reporthandlingv2 "github.com/kubescape/opa-utils/reporthandling/v2" "github.com/mikefarah/yq/v4/pkg/yqlib" + "github.com/stretchr/testify/assert" "gopkg.in/op/go-logging.v1" ) @@ -149,7 +150,7 @@ func getTestCases() []indentationTestCase { return indentationTestCases } -func TestApplyFixKeepsIndentation(t *testing.T) { +func TestApplyFixKeepsFormatting(t *testing.T) { testCases := getTestCases() for _, tc := range testCases { @@ -161,24 +162,22 @@ func TestApplyFixKeepsIndentation(t *testing.T) { } input, _ := os.ReadFile(getTestDataPath(tc.inputFile)) - want, _ := os.ReadFile(getTestDataPath(tc.expectedFile)) + wantRaw, _ := os.ReadFile(getTestDataPath(tc.expectedFile)) + want := string(wantRaw) expression := tc.yamlExpression h, _ := NewFixHandlerMock() got, _ := h.ApplyFixToContent(string(input), expression) - if got != string(want) { - t.Errorf( - "Contents of the fixed file don't match the expectation.\n"+ - "FilePath: %s\n\n"+ - "Got:\n<%s>\n\n"+ - "Want:\n<%s>", - tc.inputFile, - got, - want, - ) - } + assert.Equalf( + t, want, got, + "Contents of the fixed file don't match the expectation.\n"+ + "Input file: %s\n\n"+ + "Got: <%s>\n\n"+ + "Want: <%s>", + tc.inputFile, got, want, + ) }, )