@@ -87,85 +87,91 @@ func must[T any](a T, e error) T {
8787 return a
8888}
8989
90- var testCases = []testCase {
91- {
92- name : "Workspace with header and template" ,
93- v : dummyWorkspace1 ,
94- outputFormat : flags .OutputText ,
95- headerTemplate : "id\t name" ,
96- template : "{{.WorkspaceId}}\t {{.WorkspaceName}}" ,
97- expected : `id name
90+ // makeTestCases builds the table fresh on every call. The Workspace_Iterator
91+ // rows wrap a stateful *dummyIterator that's consumed by Next; sharing one
92+ // across iterations of TestRender (which `go test -count=N` does) makes the
93+ // second run see an empty iterator and the test fails.
94+ func makeTestCases () []testCase {
95+ return []testCase {
96+ {
97+ name : "Workspace with header and template" ,
98+ v : dummyWorkspace1 ,
99+ outputFormat : flags .OutputText ,
100+ headerTemplate : "id\t name" ,
101+ template : "{{.WorkspaceId}}\t {{.WorkspaceName}}" ,
102+ expected : `id name
98103123 abc` ,
99- },
100- {
101- name : "Workspace with no header and template" ,
102- v : dummyWorkspace1 ,
103- outputFormat : flags .OutputText ,
104- template : "{{.WorkspaceId}}\t {{.WorkspaceName}}" ,
105- expected : `123 abc` ,
106- },
107- {
108- name : "Workspace with no header and no template" ,
109- v : dummyWorkspace1 ,
110- outputFormat : flags .OutputText ,
111- expected : `{
104+ },
105+ {
106+ name : "Workspace with no header and template" ,
107+ v : dummyWorkspace1 ,
108+ outputFormat : flags .OutputText ,
109+ template : "{{.WorkspaceId}}\t {{.WorkspaceName}}" ,
110+ expected : `123 abc` ,
111+ },
112+ {
113+ name : "Workspace with no header and no template" ,
114+ v : dummyWorkspace1 ,
115+ outputFormat : flags .OutputText ,
116+ expected : `{
112117 "workspace_id": 123,
113118 "workspace_name": "abc"
114119}
115120` ,
116- },
117- {
118- name : "Workspace Iterator with header and template" ,
119- v : makeIterator (2 ),
120- outputFormat : flags .OutputText ,
121- headerTemplate : "id\t name" ,
122- template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
123- expected : `id name
121+ },
122+ {
123+ name : "Workspace Iterator with header and template" ,
124+ v : makeIterator (2 ),
125+ outputFormat : flags .OutputText ,
126+ headerTemplate : "id\t name" ,
127+ template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
128+ expected : `id name
124129123 abc
125130456 def
126131` ,
127- },
128- {
129- name : "Workspace Iterator with no header and template" ,
130- v : makeIterator (2 ),
131- outputFormat : flags .OutputText ,
132- template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
133- expected : `123 abc
132+ },
133+ {
134+ name : "Workspace Iterator with no header and template" ,
135+ v : makeIterator (2 ),
136+ outputFormat : flags .OutputText ,
137+ template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
138+ expected : `123 abc
134139456 def
135140` ,
136- },
137- {
138- name : "Workspace Iterator with no header and no template" ,
139- v : makeIterator (2 ),
140- outputFormat : flags .OutputText ,
141- expected : string (must (json .MarshalIndent (makeWorkspaces (2 ), "" , " " ))) + "\n " ,
142- },
143- {
144- name : "Big Workspace Iterator with template" ,
145- v : makeIterator (234 ),
146- outputFormat : flags .OutputText ,
147- headerTemplate : "id\t name" ,
148- template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
149- expected : "id name\n " + makeBigOutput (234 ),
150- },
151- {
152- name : "Big Workspace Iterator with no template" ,
153- v : makeIterator (234 ),
154- outputFormat : flags .OutputText ,
155- expected : string (must (json .MarshalIndent (makeWorkspaces (234 ), "" , " " ))) + "\n " ,
156- },
157- {
158- name : "io.Reader" ,
159- v : strings .NewReader ("a test" ),
160- outputFormat : flags .OutputText ,
161- expected : "a test" ,
162- },
163- {
164- name : "io.Reader" ,
165- v : strings .NewReader ("a test" ),
166- outputFormat : flags .OutputJSON ,
167- errMessage : "json output not supported" ,
168- },
141+ },
142+ {
143+ name : "Workspace Iterator with no header and no template" ,
144+ v : makeIterator (2 ),
145+ outputFormat : flags .OutputText ,
146+ expected : string (must (json .MarshalIndent (makeWorkspaces (2 ), "" , " " ))) + "\n " ,
147+ },
148+ {
149+ name : "Big Workspace Iterator with template" ,
150+ v : makeIterator (234 ),
151+ outputFormat : flags .OutputText ,
152+ headerTemplate : "id\t name" ,
153+ template : "{{range .}}{{.WorkspaceId}}\t {{.WorkspaceName}}\n {{end}}" ,
154+ expected : "id name\n " + makeBigOutput (234 ),
155+ },
156+ {
157+ name : "Big Workspace Iterator with no template" ,
158+ v : makeIterator (234 ),
159+ outputFormat : flags .OutputText ,
160+ expected : string (must (json .MarshalIndent (makeWorkspaces (234 ), "" , " " ))) + "\n " ,
161+ },
162+ {
163+ name : "io.Reader" ,
164+ v : strings .NewReader ("a test" ),
165+ outputFormat : flags .OutputText ,
166+ expected : "a test" ,
167+ },
168+ {
169+ name : "io.Reader" ,
170+ v : strings .NewReader ("a test" ),
171+ outputFormat : flags .OutputJSON ,
172+ errMessage : "json output not supported" ,
173+ },
174+ }
169175}
170176
171177// TestRenderJSONColorGate verifies defaultRenderer.renderJson honors the
@@ -209,7 +215,7 @@ func TestRenderJSONColorGate(t *testing.T) {
209215}
210216
211217func TestRender (t * testing.T ) {
212- for _ , c := range testCases {
218+ for _ , c := range makeTestCases () {
213219 t .Run (c .name , func (t * testing.T ) {
214220 output := & bytes.Buffer {}
215221 ctx := t .Context ()
0 commit comments