ci: improve error checking (errcheck linter) (#11195)

## Description

Implements part of #7258

Check some of currently unchecked errors.

- [x] baseapp
- [x] client
- [x] codec
- [x] crypto
- [x] server
- [x] simapp
- [ ] snapshots
- [ ] store
- [x] testutil
- [ ] types
- [ ] modules

---

### 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...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] 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 `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Julien Robert
2022-04-13 06:46:51 +00:00
committed by GitHub
parent fed09c593a
commit 1fe59eb22a
39 changed files with 240 additions and 162 deletions
+14 -5
View File
@@ -292,14 +292,23 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo
switch outputFormat {
case OutputFormatText:
cmd.PrintErrln()
printKeyringRecord(cmd.OutOrStdout(), k, keyring.MkAccKeyOutput, outputFormat)
if err := printKeyringRecord(cmd.OutOrStdout(), k, keyring.MkAccKeyOutput, outputFormat); err != nil {
return err
}
// print mnemonic unless requested not to.
if showMnemonic {
fmt.Fprintln(cmd.ErrOrStderr(), "\n**Important** write this mnemonic phrase in a safe place.")
fmt.Fprintln(cmd.ErrOrStderr(), "It is the only way to recover your account if you ever forget your password.")
fmt.Fprintln(cmd.ErrOrStderr(), "")
fmt.Fprintln(cmd.ErrOrStderr(), mnemonic)
if _, err := fmt.Fprintf(
cmd.ErrOrStderr(),
`
*Important** write this mnemonic phrase in a safe place.
It is the only way to recover your account if you ever forget your password.
%s
`, mnemonic); err != nil {
return fmt.Errorf("failed to print mnemonic: %v", err)
}
}
case OutputFormatJSON:
out, err := keyring.MkAccKeyOutput(k)
+1 -3
View File
@@ -89,9 +89,7 @@ func Test_runExportCmd(t *testing.T) {
// create a key
kb, err := keyring.New(sdk.KeyringServiceName(), tc.keyringBackend, kbHome, bufio.NewReader(mockInBuf), cdc)
require.NoError(t, err)
t.Cleanup(func() {
kb.Delete("keyname1") // nolint:errcheck
})
t.Cleanup(cleanupKeys(t, kb, "keyname1"))
path := sdk.GetConfig().GetFullBIP44Path()
_, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1)
+1 -4
View File
@@ -91,12 +91,9 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
WithCodec(cdc)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
t.Cleanup(func() {
kb.Delete("keyname1") // nolint:errcheck
})
t.Cleanup(cleanupKeys(t, kb, "keyname1"))
keyfile := filepath.Join(kbHome, "key.asc")
require.NoError(t, os.WriteFile(keyfile, []byte(armoredKey), 0644))
defer func() {
+1 -2
View File
@@ -34,8 +34,7 @@ func runListCmd(cmd *cobra.Command, _ []string) error {
}
if ok, _ := cmd.Flags().GetBool(flagListNames); !ok {
printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
return nil
return printKeyringRecords(cmd.OutOrStdout(), records, clientCtx.OutputFormat)
}
for _, k := range records {
+11 -9
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client"
@@ -18,6 +17,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
func cleanupKeys(t *testing.T, kb keyring.Keyring, keys ...string) func() {
return func() {
for _, k := range keys {
if err := kb.Delete(k); err != nil {
t.Log("can't delete KB key ", k, err)
}
}
}
}
func Test_runListCmd(t *testing.T) {
cmd := ListKeysCmd()
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
@@ -37,14 +46,7 @@ func Test_runListCmd(t *testing.T) {
_, err = kb.NewAccount("something", testdata.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)
t.Cleanup(func() {
kb.Delete("something") // nolint:errcheck
})
type args struct {
cmd *cobra.Command
args []string
}
t.Cleanup(cleanupKeys(t, kb, "something"))
testData := []struct {
name string
+7 -2
View File
@@ -128,9 +128,14 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
if isShowPubKey {
out = ko.PubKey
}
fmt.Fprintln(cmd.OutOrStdout(), out)
if _, err := fmt.Fprintln(cmd.OutOrStdout(), out); err != nil {
return err
}
default:
printKeyringRecord(cmd.OutOrStdout(), k, bechKeyOut, outputFormat)
if err := printKeyringRecord(cmd.OutOrStdout(), k, bechKeyOut, outputFormat); err != nil {
return err
}
}
if isShowDevice {
+2 -2
View File
@@ -72,8 +72,8 @@ func Test_runShowCmd(t *testing.T) {
fakeKeyName2 := "runShowCmd_Key2"
t.Cleanup(func() {
kb.Delete("runShowCmd_Key1")
kb.Delete("runShowCmd_Key2")
cleanupKeys(t, kb, "runShowCmd_Key1")
cleanupKeys(t, kb, "runShowCmd_Key2")
})
path := hd.NewFundraiserParams(1, sdk.CoinType, 0).String()
+29 -13
View File
@@ -17,53 +17,69 @@ const (
type bechKeyOutFn func(k *cryptokeyring.Record) (cryptokeyring.KeyOutput, error)
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) {
func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKeyOutFn, output string) error {
ko, err := bechKeyOut(k)
if err != nil {
panic(err)
return err
}
switch output {
case OutputFormatText:
printTextRecords(w, []cryptokeyring.KeyOutput{ko})
if err := printTextRecords(w, []cryptokeyring.KeyOutput{ko}); err != nil {
return err
}
case OutputFormatJSON:
out, err := KeysCdc.MarshalJSON(ko)
if err != nil {
panic(err)
return err
}
fmt.Fprintln(w, string(out))
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}
}
return nil
}
func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) {
func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output string) error {
kos, err := cryptokeyring.MkAccKeysOutput(records)
if err != nil {
panic(err)
return err
}
switch output {
case OutputFormatText:
printTextRecords(w, kos)
if err := printTextRecords(w, kos); err != nil {
return err
}
case OutputFormatJSON:
// TODO https://github.com/cosmos/cosmos-sdk/issues/8046
// Replace AminoCdc with Proto JSON
out, err := KeysCdc.MarshalJSON(kos)
if err != nil {
panic(err)
return err
}
fmt.Fprintf(w, "%s", out)
if _, err := fmt.Fprintf(w, "%s", out); err != nil {
return err
}
}
return nil
}
func printTextRecords(w io.Writer, kos []cryptokeyring.KeyOutput) {
func printTextRecords(w io.Writer, kos []cryptokeyring.KeyOutput) error {
out, err := yaml.Marshal(&kos)
if err != nil {
panic(err)
return err
}
fmt.Fprintln(w, string(out))
if _, err := fmt.Fprintln(w, string(out)); err != nil {
return err
}
return nil
}