Browse Source

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 <kolyshkin@gmail.com>
Kir Kolyshkin 6 years ago
parent
commit
d8f2b47432
1 changed files with 3 additions and 2 deletions
  1. 3 2
      pkg/jsonmessage/jsonmessage_test.go

+ 3 - 2
pkg/jsonmessage/jsonmessage_test.go

@@ -228,8 +228,9 @@ func TestDisplayJSONMessagesStreamInvalidJSON(t *testing.T) {
 	reader := strings.NewReader("This is not a 'valid' JSON []")
 	reader := strings.NewReader("This is not a 'valid' JSON []")
 	inFd, _ = term.GetFdInfo(reader)
 	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)
 	}
 	}
 }
 }