Skip to content

Commit

Permalink
style: use thelper (cosmos#16777)
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat authored Jul 3, 2023
1 parent e4e7922 commit f0aec3f
Show file tree
Hide file tree
Showing 114 changed files with 359 additions and 149 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ linters:
- revive
- stylecheck
- typecheck
- thelper
- unconvert
- unused

Expand Down Expand Up @@ -59,6 +60,9 @@ issues:
- text: 'SA1019: legacybech32.UnmarshalPubKey' # TODO remove once ready to remove from the sdk
linters:
- staticcheck
- text: 'SA1019: params.SendEnabled is deprecated' # TODO remove once ready to remove from the sdk
linters:
- staticcheck
- text: 'leading space'
linters:
- nolintlint
Expand Down
4 changes: 4 additions & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type (
)

func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite {
t.Helper()
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())

Expand Down Expand Up @@ -86,6 +87,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
}

func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...func(*baseapp.BaseApp)) *BaseAppSuite {
t.Helper()
snapshotTimeout := 1 * time.Minute
snapshotStore, err := snapshots.NewStore(dbm.NewMemDB(), testutil.GetTempDir(t))
require.NoError(t, err)
Expand Down Expand Up @@ -232,6 +234,7 @@ func TestSetLoader(t *testing.T) {
}

initStore := func(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
t.Helper()
rs := rootmulti.NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
rs.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))

Expand All @@ -252,6 +255,7 @@ func TestSetLoader(t *testing.T) {
}

checkStore := func(t *testing.T, db dbm.DB, ver int64, storeKey string, k, v []byte) {
t.Helper()
rs := rootmulti.NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
rs.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningDefault))

Expand Down
1 change: 1 addition & 0 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestQueryDataRaces_uniqueConnectionsToSameHandler(t *testing.T) {
}

func testQueryDataRacesSameHandler(t *testing.T, makeClientConn func(*baseapp.GRPCQueryRouter) *baseapp.QueryServiceTestHelper) {
t.Helper()
t.Parallel()

qr := baseapp.NewGRPCQueryRouter()
Expand Down
9 changes: 9 additions & 0 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func incrementCounter(ctx context.Context,
deliverKey []byte,
msg sdk.Msg,
) (*baseapptestutil.MsgCreateCounterResponse, error) {
t.Helper()
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(capKey)

Expand Down Expand Up @@ -202,6 +203,7 @@ func counterEvent(evType string, msgCount int64) sdk.Events {
}

func anteHandlerTxTest(t *testing.T, capKey storetypes.StoreKey, storeKey []byte) sdk.AnteHandler {
t.Helper()
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
store := ctx.KVStore(capKey)
counter, failOnAnte := parseTxMemo(t, tx)
Expand All @@ -225,6 +227,7 @@ func anteHandlerTxTest(t *testing.T, capKey storetypes.StoreKey, storeKey []byte
}

func incrementingCounter(t *testing.T, store storetypes.KVStore, counterKey []byte, counter int64) (*sdk.Result, error) {
t.Helper()
storedCounter := getIntFromStore(t, store, counterKey)
require.Equal(t, storedCounter, counter)
setIntOnStore(store, counterKey, counter+1)
Expand Down Expand Up @@ -275,6 +278,7 @@ func (ps paramStore) Get(_ context.Context) (cmtproto.ConsensusParams, error) {
}

func setTxSignature(t *testing.T, builder client.TxBuilder, nonce uint64) {
t.Helper()
privKey := secp256k1.GenPrivKeyFromSecret([]byte("test"))
pubKey := privKey.PubKey()
err := builder.SetSignatures(
Expand All @@ -288,6 +292,7 @@ func setTxSignature(t *testing.T, builder client.TxBuilder, nonce uint64) {
}

func testLoadVersionHelper(t *testing.T, app *baseapp.BaseApp, expectedHeight int64, expectedID storetypes.CommitID) {
t.Helper()
lastHeight := app.LastBlockHeight()
lastID := app.LastCommitID()
require.Equal(t, expectedHeight, lastHeight)
Expand All @@ -309,6 +314,7 @@ func getFinalizeBlockStateCtx(app *baseapp.BaseApp) sdk.Context {
}

func parseTxMemo(t *testing.T, tx sdk.Tx) (counter int64, failOnAnte bool) {
t.Helper()
txWithMemo, ok := tx.(sdk.TxWithMemo)
require.True(t, ok)

Expand All @@ -324,6 +330,7 @@ func parseTxMemo(t *testing.T, tx sdk.Tx) (counter int64, failOnAnte bool) {
}

func newTxCounter(t *testing.T, cfg client.TxConfig, counter int64, msgCounters ...int64) signing.Tx {
t.Helper()
_, _, addr := testdata.KeyTestPubAddr()
msgs := make([]sdk.Msg, 0, len(msgCounters))
for _, c := range msgCounters {
Expand All @@ -340,6 +347,7 @@ func newTxCounter(t *testing.T, cfg client.TxConfig, counter int64, msgCounters
}

func getIntFromStore(t *testing.T, store storetypes.KVStore, key []byte) int64 {
t.Helper()
bz := store.Get(key)
if len(bz) == 0 {
return 0
Expand All @@ -352,6 +360,7 @@ func getIntFromStore(t *testing.T, store storetypes.KVStore, key []byte) int64 {
}

func setFailOnAnte(t *testing.T, cfg client.TxConfig, tx signing.Tx, failOnAnte bool) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMsgs(tx.GetMsgs()...)

Expand Down
1 change: 1 addition & 0 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (

// initClientContext initiates client Context for tests
func initClientContext(t *testing.T, envVar string) (client.Context, func()) {
t.Helper()
home := t.TempDir()
chainID := "test-chain"
clientCtx := client.Context{}.
Expand Down
1 change: 1 addition & 0 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func cleanupKeys(t *testing.T, kb keyring.Keyring, keys ...string) func() {
t.Helper()
return func() {
for _, k := range keys {
if err := kb.Delete(k); err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/tx/aux_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func TestAuxTxBuilder(t *testing.T) {

// checkCorrectData that the auxSignerData's content matches the inputs we gave.
func checkCorrectData(t *testing.T, cdc codec.Codec, auxSignerData typestx.AuxSignerData, signMode signing.SignMode) {
t.Helper()
pkAny, err := codectypes.NewAnyWithValue(pub1)
require.NoError(t, err)
msgAny, err := codectypes.NewAnyWithValue(msg1)
Expand Down
2 changes: 2 additions & 0 deletions client/v2/autocli/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

func testExecCommon(t *testing.T, buildModuleCommand func(string, *Builder) (*cobra.Command, error), args ...string) *testClientConn {
t.Helper()
server := grpc.NewServer()
testpb.RegisterQueryServer(server, &testEchoServer{})
reflectionv2alpha1.RegisterReflectionServiceServer(server, &testReflectionServer{})
Expand Down Expand Up @@ -70,6 +71,7 @@ func testExecCommon(t *testing.T, buildModuleCommand func(string, *Builder) (*co
}

func testExecCommonWithErr(t *testing.T, expectedErr string, buildModuleCommand func(string, *Builder) (*cobra.Command, error), args ...string) {
t.Helper()
server := grpc.NewServer()
testpb.RegisterQueryServer(server, &testEchoServer{})
reflectionv2alpha1.RegisterReflectionServiceServer(server, &testReflectionServer{})
Expand Down
5 changes: 3 additions & 2 deletions client/v2/internal/strcase/kebab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"cosmossdk.io/client/v2/internal/strcase"
)

func toKebab(t testing.TB) {
func toKebab(tb testing.TB) {
tb.Helper()
cases := [][]string{
{"testCase", "test-case"},
{"TestCase", "test-case"},
Expand All @@ -28,7 +29,7 @@ func toKebab(t testing.TB) {
in := i[0]
out := i[1]
result := strcase.ToKebab(in)
assert.Equal(t, out, result, "ToKebab(%s) = %s, want %s", in, result, out)
assert.Equal(tb, out, result, "ToKebab(%s) = %s, want %s", in, result, out)
}
}

Expand Down
1 change: 1 addition & 0 deletions codec/codec_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func testMarshaling(t *testing.T, cdc interface {
codec.JSONCodec
},
) {
t.Helper()
any, err := types.NewAnyWithValue(&testdata.Dog{Name: "rufus"})
require.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions codec/unknownproto/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func BenchmarkRejectUnknownFields_parallel(b *testing.B) {
}

func benchmarkRejectUnknownFields(b *testing.B, parallel bool) {
b.Helper()
b.ReportAllocs()

if !parallel {
Expand Down Expand Up @@ -89,6 +90,7 @@ func BenchmarkProtoUnmarshal_parallel(b *testing.B) {
}

func benchmarkProtoUnmarshal(b *testing.B, parallel bool) {
b.Helper()
b.ReportAllocs()

if !parallel {
Expand Down
3 changes: 3 additions & 0 deletions collections/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type testFixture struct {
}

func initFixture(t *testing.T) *testFixture {
t.Helper()
sk, ctx := deps()
schemaBuilder := NewSchemaBuilder(sk)
m := NewMap(schemaBuilder, NewPrefix(1), "map", StringKey, Uint64Value)
Expand All @@ -110,6 +111,7 @@ func initFixture(t *testing.T) *testFixture {
}

func createTestGenesisSource(t *testing.T) appmodule.GenesisSource {
t.Helper()
expectedOrder := []string{"item", "key_set", "map", "sequence"}
currentIndex := 0
return func(field string) (io.ReadCloser, error) {
Expand Down Expand Up @@ -149,6 +151,7 @@ func (b *bufCloser) Close() error {
}

func newBufCloser(t *testing.T, str string) *bufCloser {
t.Helper()
b := &bufCloser{
Buffer: bytes.NewBufferString(str),
closed: false,
Expand Down
1 change: 1 addition & 0 deletions core/genesis/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestSource(t *testing.T) {
}

func expectJSON(t *testing.T, source appmodule.GenesisSource, field, contents string) {
t.Helper()
r, err := source(field)
require.NoError(t, err)
bz, err := io.ReadAll(r)
Expand Down
1 change: 1 addition & 0 deletions crypto/hd/fundraiser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestFullFundraiserPath(t *testing.T) {
}

func initFundraiserTestVectors(t *testing.T) []addrData {
t.Helper()
// NOTE: atom fundraiser address
// var hdPath string = "m/44'/118'/0'/0/0"
var hdToAddrTable []addrData
Expand Down
4 changes: 4 additions & 0 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,7 @@ func TestChangeBcrypt(t *testing.T) {
}

func requireEqualRenamedKey(t *testing.T, key, mnemonic *Record, nameMatch bool) {
t.Helper()
if nameMatch {
require.Equal(t, key.Name, mnemonic.Name)
}
Expand All @@ -1997,19 +1998,22 @@ func requireEqualRenamedKey(t *testing.T, key, mnemonic *Record, nameMatch bool)
}

func newKeyring(t *testing.T, name string) Keyring {
t.Helper()
cdc := getCodec()
kr, err := New(name, "test", t.TempDir(), nil, cdc)
require.NoError(t, err)
return kr
}

func newKeyRecord(t *testing.T, kr Keyring, name string) *Record {
t.Helper()
k, _, err := kr.NewMnemonic(name, English, sdk.FullFundraiserPath, DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(t, err)
return k
}

func assertKeysExist(t *testing.T, kr Keyring, names ...string) {
t.Helper()
for _, n := range names {
_, err := kr.Key(n)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions crypto/keys/secp256k1/internal/secp256k1/secp256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func randSig() []byte {
// tests for malleability
// highest bit of signature ECDSA s value must be 0, in the 33th byte
func compactSigCheck(t *testing.T, sig []byte) {
t.Helper()
b := int(sig[32])
if b < 0 {
t.Errorf("highest bit is negative: %d", b)
Expand Down Expand Up @@ -148,6 +149,7 @@ func TestRandomMessagesWithRandomKeys(t *testing.T) {
}

func signAndRecoverWithRandomMessages(t *testing.T, keys func() ([]byte, []byte)) {
t.Helper()
for i := 0; i < TestCount; i++ {
pubkey1, seckey := keys()
msg := csprngEntropy(32)
Expand Down
1 change: 1 addition & 0 deletions crypto/ledger/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func checkAminoJSON(t *testing.T, src, dst interface{}, isNil bool) {
t.Helper()
// Marshal to JSON bytes.
js, err := cdc.MarshalJSON(src)
require.Nil(t, err, "%+v", err)
Expand Down
2 changes: 2 additions & 0 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,10 +909,12 @@ func LegacyMaxDec(d1, d2 LegacyDec) LegacyDec {

// intended to be used with require/assert: require.True(DecEq(...))
func LegacyDecEq(t *testing.T, exp, got LegacyDec) (*testing.T, bool, string, string, string) {
t.Helper()
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
}

func LegacyDecApproxEq(t *testing.T, d1, d2, tol LegacyDec) (*testing.T, bool, string, string, string) {
t.Helper()
diff := d1.Sub(d2).Abs()
return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String()
}
Expand Down
1 change: 1 addition & 0 deletions math/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ func (i *Int) UnmarshalAmino(bz []byte) error { return i.Unmarshal(bz) }

// intended to be used with require/assert: require.True(IntEq(...))
func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) {
t.Helper()
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
}

Expand Down
2 changes: 2 additions & 0 deletions orm/encoding/ormfield/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestCodec(t *testing.T) {
}

func testCodec(t *testing.T, spec testutil.TestFieldSpec) {
t.Helper()
t.Run(fmt.Sprintf("%s %v", spec.FieldName, false), func(t *testing.T) {
testCodecNT(t, spec.FieldName, spec.Gen, false)
})
Expand All @@ -30,6 +31,7 @@ func testCodec(t *testing.T, spec testutil.TestFieldSpec) {
}

func testCodecNT(t *testing.T, fname protoreflect.Name, generator *rapid.Generator[any], nonTerminal bool) {
t.Helper()
cdc, err := testutil.MakeTestCodec(fname, nonTerminal)
assert.NilError(t, err)
rapid.Check(t, func(t *rapid.T) {
Expand Down
1 change: 1 addition & 0 deletions orm/encoding/ormfield/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func TestDurationCompare(t *testing.T) {
}

func encodeValue(t *testing.T, cdc ormfield.Codec, val protoreflect.Value) []byte {
t.Helper()
buf := &bytes.Buffer{}
assert.NilError(t, cdc.Encode(val, buf))
return buf.Bytes()
Expand Down
7 changes: 4 additions & 3 deletions orm/internal/testkv/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"cosmossdk.io/orm/model/ormtable"
)

func NewGoLevelDBBackend(t testing.TB) ormtable.Backend {
db, err := dbm.NewGoLevelDB("test", t.TempDir(), nil)
assert.NilError(t, err)
func NewGoLevelDBBackend(tb testing.TB) ormtable.Backend {
tb.Helper()
db, err := dbm.NewGoLevelDB("test", tb.TempDir(), nil)
assert.NilError(tb, err)
return ormtable.NewBackend(ormtable.BackendOptions{
CommitmentStore: db,
})
Expand Down
1 change: 1 addition & 0 deletions orm/model/ormdb/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func TestModuleDB(t *testing.T) {
}

func runSimpleBankTests(t *testing.T, k Keeper, ctx context.Context) {
t.Helper()
// mint coins
denom := "foo"
acct1 := "bob"
Expand Down
1 change: 1 addition & 0 deletions orm/model/ormtable/auto_increment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestAutoIncrementScenario(t *testing.T) {
}

func runAutoIncrementScenario(t *testing.T, table ormtable.AutoIncrementTable, ctx context.Context) {
t.Helper()
store, err := testpb.NewExampleAutoIncrementTableTable(table)
assert.NilError(t, err)

Expand Down
Loading

0 comments on commit f0aec3f

Please sign in to comment.