Browse code

client/pkg/streamformatter: fix faulty TestNewJSONProgressOutput

This test was added in 956c5b61cea9e476847b9d2231ac66d3ec85f018, but
wasn't really testing anything (other than testing stdlib).

Ironically there was a review comment on the PR, but about the naming
of the test, and not about the test itself;

> but it isn't really testing NewJSONProgressOutput. NewJSONProgressOutput
> just returns a struct. IMHO it's testing WriteProgress.

Let's make the test test something.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2026/02/19 09:03:41
Showing 1 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"testing"
9 9
 
10 10
 	"github.com/moby/moby/api/types/jsonstream"
11
+	"github.com/moby/moby/client/pkg/progress"
11 12
 	"gotest.tools/v3/assert"
12 13
 	is "gotest.tools/v3/assert/cmp"
13 14
 )
... ...
@@ -80,9 +81,50 @@ func TestJsonProgressFormatterFormatStatus(t *testing.T) {
80 80
 	assert.Check(t, is.Equal(`{"status":"a1","id":"ID"}`+streamNewline, string(res)))
81 81
 }
82 82
 
83
-func TestNewJSONProgressOutput(t *testing.T) {
84
-	b := bytes.Buffer{}
85
-	b.Write(formatStatus("id", "Downloading"))
86
-	_ = NewJSONProgressOutput(&b, false)
87
-	assert.Check(t, is.Equal(`{"status":"Downloading","id":"id"}`+streamNewline, b.String()))
83
+func TestJSONProgressOutputWriteProgress(t *testing.T) {
84
+	tests := []struct {
85
+		doc        string
86
+		newlines   bool
87
+		lastUpdate bool
88
+		expected   string
89
+	}{
90
+		{
91
+			doc:      "no newlines",
92
+			expected: `{"status":"Downloading","id":"id"}` + streamNewline,
93
+		},
94
+		{
95
+			doc:        "no newlines last update",
96
+			lastUpdate: true,
97
+			expected:   `{"status":"Downloading","id":"id"}` + streamNewline,
98
+		},
99
+		{
100
+			doc:        "newlines",
101
+			newlines:   true,
102
+			lastUpdate: false,
103
+			expected:   `{"status":"Downloading","id":"id"}` + streamNewline,
104
+		},
105
+		{
106
+			// Should print an extra (empty) message to add newlines after last message
107
+			// (LastUpdate=true); see https://github.com/moby/moby/pull/1425
108
+			doc:        "newlines last update",
109
+			newlines:   true,
110
+			lastUpdate: true,
111
+			expected:   `{"status":"Downloading","id":"id"}` + streamNewline + `{}` + streamNewline,
112
+		},
113
+	}
114
+
115
+	for _, tc := range tests {
116
+		t.Run(tc.doc, func(t *testing.T) {
117
+			var b bytes.Buffer
118
+			po := NewJSONProgressOutput(&b, tc.newlines)
119
+
120
+			err := po.WriteProgress(progress.Progress{
121
+				ID:         "id",
122
+				Message:    "Downloading",
123
+				LastUpdate: tc.lastUpdate,
124
+			})
125
+			assert.NilError(t, err)
126
+			assert.Check(t, is.Equal(b.String(), tc.expected))
127
+		})
128
+	}
88 129
 }