From bf685cf8326dc6896cb3573fb18e12a6dadb79d1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 22 Jun 2023 18:32:49 +0200 Subject: [PATCH] Cleanup: improve test setup and check for the right error string Use the assert package to check expectations; also, check for the exact error message instead of just whether any error occurred. --- pkg/utils/yaml_utils/yaml_utils_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/utils/yaml_utils/yaml_utils_test.go b/pkg/utils/yaml_utils/yaml_utils_test.go index 4bdfeb432..518907cb1 100644 --- a/pkg/utils/yaml_utils/yaml_utils_test.go +++ b/pkg/utils/yaml_utils/yaml_utils_test.go @@ -1,6 +1,10 @@ package yaml_utils -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) func TestUpdateYaml(t *testing.T) { tests := []struct { @@ -49,16 +53,14 @@ func TestUpdateYaml(t *testing.T) { for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { - out, err := UpdateYaml([]byte(test.in), test.path, test.value) - if test.expectedErr != "" { - if err == nil { - t.Errorf("expected error %q but got none", test.expectedErr) - } - } else if err != nil { - t.Errorf("unexpected error: %v", err) - } else if string(out) != test.expectedOut { - t.Errorf("expected %q but got %q", test.expectedOut, string(out)) + out, actualErr := UpdateYaml([]byte(test.in), test.path, test.value) + if test.expectedErr == "" { + assert.NoError(t, actualErr) + } else { + assert.EqualError(t, actualErr, test.expectedErr) } + + assert.Equal(t, test.expectedOut, string(out)) }) } }