From d8f2b47432aa25e6e0eeba319096e6d24a7cef9e Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 5 Aug 2019 18:59:35 -0700 Subject: [PATCH] TestDisplayJSONMessagesStreamInvalidJSON: fix error check As pointed out by govet, > pkg/jsonmessage/jsonmessage_test.go:231:94: nilness: nil dereference in dynamic method call (govet) > if err := DisplayJSONMessagesStream(reader, data, inFd, false, nil); err == nil && err.Error()[:17] != "invalid character" { > ^ The nil deref never happened as err was always non-nil, and so the check for error message text was not performed. Fix this, and while at it, refactor the code a bit. Signed-off-by: Kir Kolyshkin --- pkg/jsonmessage/jsonmessage_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/jsonmessage/jsonmessage_test.go b/pkg/jsonmessage/jsonmessage_test.go index 3d9667a153..a7100daa73 100644 --- a/pkg/jsonmessage/jsonmessage_test.go +++ b/pkg/jsonmessage/jsonmessage_test.go @@ -228,8 +228,9 @@ func TestDisplayJSONMessagesStreamInvalidJSON(t *testing.T) { reader := strings.NewReader("This is not a 'valid' JSON []") inFd, _ = term.GetFdInfo(reader) - if err := DisplayJSONMessagesStream(reader, data, inFd, false, nil); err == nil && err.Error()[:17] != "invalid character" { - t.Fatalf("Should have thrown an error (invalid character in ..), got %q", err) + exp := "invalid character " + if err := DisplayJSONMessagesStream(reader, data, inFd, false, nil); err == nil || !strings.HasPrefix(err.Error(), exp) { + t.Fatalf("Expected error (%s...), got %q", exp, err) } }