Skip to content

Commit

Permalink
chore(client): use command's configured output (backport #22334) (#22337
Browse files Browse the repository at this point in the history
)

Co-authored-by: Mark Rushakoff <mark.rushakoff@gmail.com>
  • Loading branch information
mergify[bot] and mark-rushakoff authored Oct 22, 2024
1 parent 7df17f4 commit d6028b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func GetClientQueryContext(cmd *cobra.Command) (Context, error) {
// - client.Context field pre-populated & flag not set: uses pre-populated value
// - client.Context field pre-populated & flag set: uses set flag value
func GetClientTxContext(cmd *cobra.Command) (Context, error) {
ctx := GetClientContextFromCmd(cmd)
ctx := GetClientContextFromCmd(cmd).WithOutput(cmd.OutOrStdout())
return readTxCommandFlags(ctx, cmd.Flags())
}

Expand Down
26 changes: 26 additions & 0 deletions client/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client_test

import (
"bytes"
"context"
"fmt"
"testing"
Expand Down Expand Up @@ -137,3 +138,28 @@ func TestSetCmdClientContextHandler(t *testing.T) {
})
}
}

func TestContext_usesCobraCommandOutput(t *testing.T) {
var initCtx client.Context

cmd := &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
return client.SetCmdClientContextHandler(initCtx, cmd)
},
RunE: func(cmd *cobra.Command, _ []string) error {
cctx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

return cctx.PrintString("hello")
},
}

var outBuf bytes.Buffer
cmd.SetOutput(&outBuf)

require.NoError(t, cmd.Execute())

require.Equal(t, "hello", outBuf.String())
}

0 comments on commit d6028b1

Please sign in to comment.