test: add cmdtest package (#15251)
## Description This PR introduces the `cmdtest` package, offering a lightweight wrapper around cobra commands to simplify testing CLI utilities. I backfilled tests for the `version` command, which was an example of a very simple test setup; and for the `export` command, which was more involved due to the server and client context requirements. I did notice that there are some existing tests for some utilities, but the `cmdtest` package follows a simple pattern that has been easy to use successfully in [the relayer](https://github.com/cosmos/relayer/blob/main/internal/relayertest/system.go) and in other projects outside the Cosmos ecosystem. While filling in these tests, I started removing uses of `cmd.Print`, as that is the root cause of issues like #8498, #7964, #15167, and possibly others. Internal to cobra, the print family of methods write to `cmd.OutOrStderr()` -- meaning that if the authors call `cmd.SetOutput()` before executing the command, the output will be written to stdout as expected; otherwise it will go to stderr. I don't understand why that would be the default behavior, but it is probably too late to change from cobra's side. Instead of `cmd.Print`, we prefer to `fmt.Fprint(cmd.OutOrStdout())` or `fmt.Fprint(cmd.ErrOrStderr())` as appropriate, giving an unambiguous destination for output. And the new tests collect those outputs in plain `bytes.Buffer` values so that we can assert their content appropriately. In the longer term, I would like to deprecate and eventually remove the `testutil` package's `ApplyMockIO` method and its `BufferWriter` and `BufferReader` types, as they are unnecessary indirection when a simpler solution exists. But that can wait until `cmdtest` has propagated through the codebase more. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] ~~provided a link to the relevant issue or specification~~ - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] confirmed that this PR does not change production code
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// Package cmdtest contains a framework for testing cobra Commands within Go unit tests.
|
||||
package cmdtest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// System is a system under test.
|
||||
type System struct {
|
||||
commands []*cobra.Command
|
||||
}
|
||||
|
||||
// NewSystem returns a new System.
|
||||
func NewSystem() *System {
|
||||
// We aren't doing any special initialization yet,
|
||||
// but let's encourage a constructor to make it simpler
|
||||
// to update later, if needed.
|
||||
return new(System)
|
||||
}
|
||||
|
||||
// AddCommands sets commands to be available to the Run family of methods on s.
|
||||
func (s *System) AddCommands(cmds ...*cobra.Command) {
|
||||
s.commands = append(s.commands, cmds...)
|
||||
}
|
||||
|
||||
// RunResult is the stdout and stderr resulting from a call to a System's Run family of methods,
|
||||
// and any error that was returned.
|
||||
type RunResult struct {
|
||||
Stdout, Stderr bytes.Buffer
|
||||
|
||||
Err error
|
||||
}
|
||||
|
||||
// Run calls s.RunC with context.Background().
|
||||
func (s *System) Run(args ...string) RunResult {
|
||||
return s.RunC(context.Background(), args...)
|
||||
}
|
||||
|
||||
// RunC calls s.RunWithInput with an empty stdin.
|
||||
func (s *System) RunC(ctx context.Context, args ...string) RunResult {
|
||||
return s.RunWithInputC(ctx, bytes.NewReader(nil), args...)
|
||||
}
|
||||
|
||||
// RunWithInput calls s.RunWithInputC with context.Background().
|
||||
func (s *System) RunWithInput(in io.Reader, args ...string) RunResult {
|
||||
return s.RunWithInputC(context.Background(), in, args...)
|
||||
}
|
||||
|
||||
// RunWithInputC executes a new root command with subcommands
|
||||
// that were set in s.AddCommands().
|
||||
// The command's stdin is set to the in argument.
|
||||
// RunWithInputC returns a RunResult wrapping stdout, stderr, and any returned error.
|
||||
func (s *System) RunWithInputC(ctx context.Context, in io.Reader, args ...string) RunResult {
|
||||
rootCmd := &cobra.Command{}
|
||||
rootCmd.AddCommand(s.commands...)
|
||||
|
||||
rootCmd.SetIn(in)
|
||||
|
||||
var res RunResult
|
||||
rootCmd.SetOutput(&res.Stdout)
|
||||
rootCmd.SetErr(&res.Stderr)
|
||||
|
||||
rootCmd.SetArgs(args)
|
||||
|
||||
res.Err = rootCmd.ExecuteContext(ctx)
|
||||
return res
|
||||
}
|
||||
|
||||
// MustRun calls s.Run, but also calls t.FailNow if RunResult.Err is not nil.
|
||||
func (s *System) MustRun(t TestingT, args ...string) RunResult {
|
||||
t.Helper()
|
||||
|
||||
return s.MustRunC(t, context.Background(), args...)
|
||||
}
|
||||
|
||||
// MustRunC calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.
|
||||
func (s *System) MustRunC(t TestingT, ctx context.Context, args ...string) RunResult { //nolint:revive // As a variation of MustRun, t is more important than ctx.
|
||||
t.Helper()
|
||||
|
||||
return s.MustRunWithInputC(t, ctx, bytes.NewReader(nil), args...)
|
||||
}
|
||||
|
||||
// MustRunWithInput calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.
|
||||
func (s *System) MustRunWithInput(t TestingT, in io.Reader, args ...string) RunResult {
|
||||
t.Helper()
|
||||
|
||||
return s.MustRunWithInputC(t, context.Background(), in, args...)
|
||||
}
|
||||
|
||||
// MustRunWithInputC calls s.RunWithInputC, but also calls t.FailNow if RunResult.Err is not nil.
|
||||
func (s *System) MustRunWithInputC(t TestingT, ctx context.Context, in io.Reader, args ...string) RunResult { //nolint:revive // As a variation of MustRun, t is more important than ctx.
|
||||
t.Helper()
|
||||
|
||||
res := s.RunWithInputC(ctx, in, args...)
|
||||
if res.Err != nil {
|
||||
t.Logf("Error executing %v: %v", args, res.Err)
|
||||
t.Logf("Stdout: %q", res.Stdout.String())
|
||||
t.Logf("Stderr: %q", res.Stderr.String())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// TestingT is a subset of testing.TB,
|
||||
// containing only what the (*System).Must methods use.
|
||||
//
|
||||
// This simplifies using other testing wrappers,
|
||||
// such as testify suite, etc.
|
||||
type TestingT interface {
|
||||
Helper()
|
||||
|
||||
Logf(format string, args ...any)
|
||||
|
||||
FailNow()
|
||||
}
|
||||
Reference in New Issue
Block a user