From 37645965de4f17b500b4dbcc5359a2afb04b1669 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 4 Nov 2026 12:00:21 +0100 Subject: [PATCH] streamformatter: TestFormatError: rewrite to a table-test Signed-off-by: Sebastiaan van Stijn --- .../streamformatter/streamformatter_test.go | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/daemon/internal/streamformatter/streamformatter_test.go b/daemon/internal/streamformatter/streamformatter_test.go index c13ee79521..f6c5a1b0cf 100644 --- a/daemon/internal/streamformatter/streamformatter_test.go +++ b/daemon/internal/streamformatter/streamformatter_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "net/http" "testing" "github.com/google/go-cmp/cmp" @@ -19,16 +20,29 @@ func TestFormatStatus(t *testing.T) { } func TestFormatError(t *testing.T) { - res := FormatError(errors.New("Error for formatter")) - expected := `{"error":"Error for formatter","errorDetail":{"message":"Error for formatter"}}` + "\r\n" - assert.Check(t, is.Equal(expected, string(res))) -} + tests := []struct { + name string + err error + expected string + }{ + { + name: "regular error", + err: errors.New("something bad happened"), + expected: `{"error":"something bad happened","errorDetail":{"message":"something bad happened"}}`, + }, + { + name: "JSON error", + err: &jsonstream.Error{Code: http.StatusBadRequest, Message: "JSON error"}, + expected: `{"error":"JSON error","errorDetail":{"code":400,"message":"JSON error"}}`, + }, + } -func TestFormatJSONError(t *testing.T) { - err := &jsonstream.Error{Code: 50, Message: "Json error"} - res := FormatError(err) - expected := `{"error":"Json error","errorDetail":{"code":50,"message":"Json error"}}` + streamNewline - assert.Check(t, is.Equal(expected, string(res))) + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + res := FormatError(tc.err) + assert.Check(t, is.Equal(tc.expected+streamNewline, string(res))) + }) + } } func TestJsonProgressFormatterFormatProgress(t *testing.T) {