cosmos-sdk/testutil
Mark Rushakoff e7097b1468
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
2023-03-06 19:40:24 +00:00
..
cli refactor: rename to CometBFT (#14914) 2023-02-06 18:11:14 +00:00
cmdtest test: add cmdtest package (#15251) 2023-03-06 19:40:24 +00:00
configurator chore: loosen assertions in SetOrderInitGenesis and SetOrderExportGenesis (#14457) 2023-01-02 10:05:09 +01:00
mock feat!: use cosmossdk.io/log logger (#15011) 2023-02-27 21:36:22 +00:00
network refactor(log): require destination in calls to NewLogger (#15262) 2023-03-06 19:05:49 +00:00
sims refactor(log): require destination in calls to NewLogger (#15262) 2023-03-06 19:05:49 +00:00
testdata chore: make proto-gen due to dep update (#15268) 2023-03-06 10:54:50 +01:00
account.go refactor(crisis): CLI tests using Tendermint mock (#12874) 2022-08-10 13:54:31 +00:00
assert_helpers.go fix: correct path required proto testdata (#14991) 2023-02-11 16:59:16 +00:00
context.go chore: rename log pkg and add zerolog (#14955) 2023-02-08 11:32:22 +00:00
ioutil_test.go ci: improve error checking (errcheck linter) (#11195) 2022-04-13 06:46:51 +00:00
ioutil.go refactor!: abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests (#11496) 2022-04-21 15:30:36 -04:00
key_test.go refactor(testutil): remove dependency on simapp (#12624) 2022-07-20 07:34:58 +00:00
key.go chore: move server.GenerateCoinKey and server.GenerateSaveCoinKey to … (#10957) 2022-01-18 15:24:16 +00:00
list.go refactor: remove weird casting for staking.Keeper in slashing and distribution (#12973) 2022-08-20 21:11:44 +00:00
rest.go refactor!: extract simulation helpers out of simapp (#13402) 2022-09-27 21:19:44 +02:00