Skip to content

Commit 9a4ca45

Browse files
pieternclaude
andauthored
Add cmdio test helpers for output capture (#4334)
## Changes Adds two new test helper functions to reduce boilerplate: - NewTestContextWithStdout: Captures stdout, discards stderr - NewTestContextWithStderr: Captures stderr, discards stdout Migrates existing tests in bundle/render and cmd/pipelines to use the new helpers. ## Why Reduces number of lines required for test setup. --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 90ecacc commit 9a4ca45

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

bundle/render/render_text_output_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/databricks/cli/libs/cmdio"
1414
"github.com/databricks/cli/libs/diag"
1515
"github.com/databricks/cli/libs/dyn"
16-
"github.com/databricks/cli/libs/flags"
1716
"github.com/databricks/cli/libs/logdiag"
1817
"github.com/databricks/databricks-sdk-go/service/catalog"
1918
"github.com/databricks/databricks-sdk-go/service/iam"
@@ -277,15 +276,12 @@ func TestRenderDiagnostics(t *testing.T) {
277276

278277
for _, tc := range testCases {
279278
t.Run(tc.name, func(t *testing.T) {
280-
writer := &bytes.Buffer{}
281-
ctx := context.Background()
282-
cmdIO := cmdio.NewIO(ctx, flags.OutputText, nil, io.Discard, writer, "", "")
283-
ctx = cmdio.InContext(ctx, cmdIO)
279+
ctx, stderr := cmdio.NewTestContextWithStderr(context.Background())
284280

285281
err := cmdio.RenderDiagnostics(ctx, tc.diags)
286282
require.NoError(t, err)
287283

288-
assert.Equal(t, tc.expected, writer.String())
284+
assert.Equal(t, tc.expected, stderr.String())
289285
})
290286
}
291287
}

cmd/pipelines/run_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package pipelines
22

33
import (
4-
"bytes"
54
"context"
65
"testing"
76
"time"
87

98
"github.com/databricks/cli/libs/cmdio"
10-
"github.com/databricks/cli/libs/flags"
119
"github.com/databricks/databricks-sdk-go/service/pipelines"
1210
"github.com/stretchr/testify/assert"
1311
)
@@ -146,15 +144,11 @@ Pipeline configurations for this update:
146144

147145
for _, tt := range tests {
148146
t.Run(tt.name, func(t *testing.T) {
149-
var buf bytes.Buffer
150-
151-
ctx := context.Background()
152-
cmdIO := cmdio.NewIO(ctx, flags.OutputText, nil, &buf, &buf, "", "")
153-
ctx = cmdio.InContext(ctx, cmdIO)
147+
ctx, stdout := cmdio.NewTestContextWithStdout(context.Background())
154148

155149
err := displayPipelineUpdate(ctx, tt.update, tt.pipelineID, tt.events)
156150
assert.NoError(t, err)
157-
assert.Equal(t, tt.expected, buf.String())
151+
assert.Equal(t, tt.expected, stdout.String())
158152
})
159153
}
160154
}
@@ -376,15 +370,11 @@ RUNNING 750ms
376370

377371
for _, tt := range tests {
378372
t.Run(tt.name, func(t *testing.T) {
379-
var buf bytes.Buffer
380-
381-
ctx := context.Background()
382-
cmdIO := cmdio.NewIO(ctx, flags.OutputText, nil, &buf, &buf, "", "")
383-
ctx = cmdio.InContext(ctx, cmdIO)
373+
ctx, stdout := cmdio.NewTestContextWithStdout(context.Background())
384374

385375
err := displayProgressEventsDurations(ctx, tt.events)
386376
assert.NoError(t, err)
387-
assert.Equal(t, tt.expected, buf.String())
377+
assert.Equal(t, tt.expected, stdout.String())
388378
})
389379
}
390380
}

libs/cmdio/testing.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package cmdio
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
"io"
8+
9+
"github.com/databricks/cli/libs/flags"
710
)
811

912
type Test struct {
@@ -62,3 +65,19 @@ func SetupTest(ctx context.Context, opts TestOptions) (context.Context, *Test) {
6265
Stderr: bufio.NewReader(rerr),
6366
}
6467
}
68+
69+
// NewTestContextWithStdout creates a cmdio context that captures stdout output.
70+
// Stderr is discarded. Use this for testing data output and results.
71+
func NewTestContextWithStdout(ctx context.Context) (context.Context, *bytes.Buffer) {
72+
stdout := &bytes.Buffer{}
73+
cmdIO := NewIO(ctx, flags.OutputText, nil, stdout, io.Discard, "", "")
74+
return InContext(ctx, cmdIO), stdout
75+
}
76+
77+
// NewTestContextWithStderr creates a cmdio context that captures stderr output.
78+
// Stdout is discarded. Use this for testing diagnostics, logs, and error messages.
79+
func NewTestContextWithStderr(ctx context.Context) (context.Context, *bytes.Buffer) {
80+
stderr := &bytes.Buffer{}
81+
cmdIO := NewIO(ctx, flags.OutputText, nil, io.Discard, stderr, "", "")
82+
return InContext(ctx, cmdIO), stderr
83+
}

libs/cmdio/testing_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmdio_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/databricks/cli/libs/cmdio"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNewTestContextWithStdout(t *testing.T) {
13+
ctx, stdout := cmdio.NewTestContextWithStdout(context.Background())
14+
15+
// Render writes to stdout
16+
data := map[string]string{"message": "test output"}
17+
err := cmdio.Render(ctx, data)
18+
require.NoError(t, err)
19+
20+
assert.Contains(t, stdout.String(), "test output")
21+
}
22+
23+
func TestNewTestContextWithStderr(t *testing.T) {
24+
ctx, stderr := cmdio.NewTestContextWithStderr(context.Background())
25+
26+
require.NotPanics(t, func() {
27+
cmdio.LogString(ctx, "test message")
28+
})
29+
30+
assert.Contains(t, stderr.String(), "test message")
31+
}

0 commit comments

Comments
 (0)