Skip to content

Commit

Permalink
chore: lint tests (#4156)
Browse files Browse the repository at this point in the history
* Update .golangci.yml

* lint tests, cleanup linter config

* reenable test linting

* finish linting tests

* tweak gci

* bump linter version

* changelog

* fix exportToFlagSet

* revert changes

* lint

* errcheck

* check errors in interface_flag.go

* set value in tests

* fix test

* always provide a value when testing flags

* add a comment to trigger ci

* changelog

* check an additional error

* Update changelog.md

Co-authored-by: Danilo Pantani <[email protected]>

* Update ignite/cmd/chain_faucet.go

Co-authored-by: Danilo Pantani <[email protected]>

* Update ignite/cmd/node_query.go

Co-authored-by: Danilo Pantani <[email protected]>

* Update ignite/cmd/chain_simulate.go

Co-authored-by: Danilo Pantani <[email protected]>

* Update ignite/pkg/protoanalysis/protoutil/cursor_test.go

Co-authored-by: Danilo Pantani <[email protected]>

* fix imports

* remove dup

* Update changelog.md

---------

Co-authored-by: Danilo Pantani <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored Jul 10, 2024
1 parent 0d47fd9 commit 4135601
Show file tree
Hide file tree
Showing 30 changed files with 142 additions and 69 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

### Changes


- [#4157](https://github.com/ignite/cli/pull/4157) Upgrade golang to 1.22
- [#4094](https://github.com/ignite/cli/pull/4094) Scaffolding a multi-index map using `ignite s map foo bar baz --index foobar,foobaz` is no longer supported. Use one index instead of use `collections.IndexedMap`.
- [#4058](https://github.com/ignite/cli/pull/4058) Simplify scaffolded modules by including `ValidateBasic()` logic in message handler.
Expand Down
21 changes: 21 additions & 0 deletions ignite/cmd/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func buildRootCmd(ctx context.Context) *cobra.Command {
}

func assertFlags(t *testing.T, expectedFlags []*plugin.Flag, execCmd *plugin.ExecutedCommand) {
t.Helper()
var (
have []string
expected []string
Expand Down Expand Up @@ -116,6 +117,7 @@ func TestLinkPluginCmds(t *testing.T) {
{
name: "ok: link foo at root",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
cmd := &plugin.Command{
Use: "foo",
}
Expand All @@ -135,6 +137,7 @@ ignite
{
name: "ok: link foo at subcommand",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
cmd := &plugin.Command{
Use: "foo",
PlaceCommandUnder: "ignite scaffold",
Expand All @@ -155,6 +158,7 @@ ignite
{
name: "ok: link foo at subcommand with incomplete PlaceCommandUnder",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
cmd := &plugin.Command{
Use: "foo",
PlaceCommandUnder: "scaffold",
Expand All @@ -175,6 +179,7 @@ ignite
{
name: "fail: link to runnable command",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -193,6 +198,7 @@ ignite
{
name: "fail: link to unknown command",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -211,6 +217,7 @@ ignite
{
name: "fail: plugin name exists in legacy commands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -228,6 +235,7 @@ ignite
{
name: "fail: plugin name with args exists in legacy commands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -245,6 +253,7 @@ ignite
{
name: "fail: plugin name exists in legacy sub commands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -263,6 +272,7 @@ ignite
{
name: "ok: link multiple at root",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
fooCmd := &plugin.Command{
Use: "foo",
}
Expand Down Expand Up @@ -293,6 +303,7 @@ ignite
{
name: "ok: link with subcommands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
cmd := &plugin.Command{
Use: "foo",
Commands: []*plugin.Command{
Expand Down Expand Up @@ -324,6 +335,7 @@ ignite
{
name: "ok: link with multiple subcommands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
cmd := &plugin.Command{
Use: "foo",
Commands: []*plugin.Command{
Expand Down Expand Up @@ -413,6 +425,7 @@ func TestLinkPluginHooks(t *testing.T) {
// helper to assert pluginInterface.ExecuteHook*() calls in expected order
// (pre, then post, then cleanup)
expectExecuteHook = func(t *testing.T, p *mocks.PluginInterface, expectedFlags []*plugin.Flag, hooks ...*plugin.Hook) {
t.Helper()
matcher := func(hook *plugin.Hook) any {
return mock.MatchedBy(func(execHook *plugin.ExecutedHook) bool {
return hook.Name == execHook.Hook.Name &&
Expand Down Expand Up @@ -463,6 +476,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "fail: command not runnable",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -481,6 +495,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "fail: command doesn't exists",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
p.EXPECT().
Manifest(ctx).
Return(&plugin.Manifest{
Expand All @@ -499,6 +514,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "ok: single hook",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
hook := &plugin.Hook{
Name: "test-hook",
PlaceHookOn: "scaffold chain",
Expand All @@ -512,6 +528,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "ok: multiple hooks on same command",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
hook1 := &plugin.Hook{
Name: "test-hook-1",
PlaceHookOn: "scaffold chain",
Expand All @@ -529,6 +546,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "ok: multiple hooks on different commands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
hookChain1 := &plugin.Hook{
Name: "test-hook-1",
PlaceHookOn: "scaffold chain",
Expand All @@ -551,6 +569,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "ok: duplicate hook names on same command",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
hooks := []*plugin.Hook{
{
Name: "test-hook",
Expand All @@ -570,6 +589,7 @@ func TestLinkPluginHooks(t *testing.T) {
{
name: "ok: duplicate hook names on different commands",
setup: func(t *testing.T, ctx context.Context, p *mocks.PluginInterface) {
t.Helper()
hookChain := &plugin.Hook{
Name: "test-hook",
PlaceHookOn: "ignite scaffold chain",
Expand Down Expand Up @@ -619,6 +639,7 @@ func TestLinkPluginHooks(t *testing.T) {

// execCmd executes all the runnable commands contained in c.
func execCmd(t *testing.T, c *cobra.Command, args []string) {
t.Helper()
if c.Runnable() {
os.Args = strings.Fields(c.CommandPath())
os.Args = append(os.Args, args...)
Expand Down
3 changes: 3 additions & 0 deletions ignite/config/plugins/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,15 @@ func TestConfigSave(t *testing.T) {
{
name: "fail: config path is empty",
buildConfig: func(t *testing.T) *pluginsconfig.Config {
t.Helper()
return &pluginsconfig.Config{}
},
expectedError: "plugin config save: empty path",
},
{
name: "ok: config path is a file that doesn't exist",
buildConfig: func(t *testing.T) *pluginsconfig.Config {
t.Helper()
cfg, err := pluginsconfig.ParseDir(t.TempDir())
require.NoError(t, err)
return cfg
Expand All @@ -275,6 +277,7 @@ func TestConfigSave(t *testing.T) {
{
name: "ok: config path is an existing file",
buildConfig: func(t *testing.T) *pluginsconfig.Config {
t.Helper()
// copy testdata/igniteapps.yml to tmp because it will be modified
dir := t.TempDir()
bz, err := os.ReadFile("testdata/igniteapps.yml")
Expand Down
3 changes: 3 additions & 0 deletions ignite/internal/plugin/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestConsumerPlugin(t *testing.T) {
name: "fail: writeFenesis w/o genesis.json",
args: []string{"writeGenesis"},
setup: func(t *testing.T, path string) {
t.Helper()
// Add priv_validator_key.json to path
bz, err := os.ReadFile("testdata/consumer/config/priv_validator_key.json")
require.NoError(t, err)
Expand All @@ -51,6 +52,7 @@ func TestConsumerPlugin(t *testing.T) {
name: "ok: writeGenesis",
args: []string{"writeGenesis"},
setup: func(t *testing.T, path string) {
t.Helper()
// Add priv_validator_key.json to path
bz, err := os.ReadFile("testdata/consumer/config/priv_validator_key.json")
require.NoError(t, err)
Expand All @@ -73,6 +75,7 @@ func TestConsumerPlugin(t *testing.T) {
name: "ok: isInitialized returns true",
args: []string{"isInitialized"},
setup: func(t *testing.T, path string) {
t.Helper()
// isInitialized returns true if there's a consumer genesis with an
// InitialValSet length != 0
// Add priv_validator_key.json to path
Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/cosmosclient/cosmosclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type suite struct {
}

func newClient(t *testing.T, setup func(suite), opts ...cosmosclient.Option) cosmosclient.Client {
t.Helper()
s := suite{
rpcClient: mocks.NewRPCClient(t),
accountRetriever: mocks.NewAccountRetriever(t),
Expand Down
5 changes: 4 additions & 1 deletion ignite/pkg/cosmosfaucet/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func TestServeHTTPCORS(t *testing.T) {
f.ServeHTTP(res, req)

// Assert
require.Equal(t, http.StatusNoContent, res.Result().StatusCode)
result := res.Result()
defer result.Body.Close() // Ensure the response body is closed

require.Equal(t, http.StatusNoContent, result.StatusCode)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestSave(t *testing.T) {
)
attrStmt.
ExpectExec().
WithArgs(evtID, string(evtAttr.Key), jsonEvtAttrValue).
WithArgs(evtID, evtAttr.Key, jsonEvtAttrValue).
WillReturnResult(insertResult)

mock.ExpectCommit()
Expand Down Expand Up @@ -540,6 +540,7 @@ func TestEventQueryWithEventAttrFilters(t *testing.T) {
}

func createMatchEqualSQLMock(t *testing.T) (*sql.DB, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New(
sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual),
)
Expand Down
8 changes: 4 additions & 4 deletions ignite/pkg/cosmostxcollector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCollector(t *testing.T) {
fromHeight,
mock.AnythingOfType("chan<- []cosmosclient.TX"),
).
Run(func(ctx context.Context, fromHeight int64, tc chan<- []cosmosclient.TX) {
Run(func(_ context.Context, _ int64, tc chan<- []cosmosclient.TX) {
defer close(tc)

// Send the collected block transactions
Expand All @@ -46,7 +46,7 @@ func TestCollector(t *testing.T) {
mock.Anything,
mock.AnythingOfType("[]cosmosclient.TX"),
).
Run(func(ctx context.Context, txs []cosmosclient.TX) {
Run(func(_ context.Context, txs []cosmosclient.TX) {
// Save the transactions
savedTXs = append(savedTXs, txs)
}).
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestCollectorWithCollectError(t *testing.T) {
mock.AnythingOfType("int64"),
mock.AnythingOfType("chan<- []cosmosclient.TX"),
).
Run(func(ctx context.Context, fromHeight int64, tc chan<- []cosmosclient.TX) {
Run(func(_ context.Context, _ int64, tc chan<- []cosmosclient.TX) {
close(tc)
}).
Return(wantErr).
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestCollectorWithSaveError(t *testing.T) {
mock.AnythingOfType("int64"),
mock.AnythingOfType("chan<- []cosmosclient.TX"),
).
Run(func(ctx context.Context, fromHeight int64, tc chan<- []cosmosclient.TX) {
Run(func(_ context.Context, _ int64, tc chan<- []cosmosclient.TX) {
defer close(tc)

// Send the collected block transactions
Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/dirchange/dirchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
)

func randomBytes(t *testing.T, n int) []byte {
t.Helper()
bytes := make([]byte, n)
_, err := rand.Read(bytes)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/httpstatuschecker/httpstatuschecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestCheckStatus(t *testing.T) {
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tt.returnedStatus)
}))
defer ts.Close()
Expand Down
2 changes: 1 addition & 1 deletion ignite/pkg/jsonfile/jsonfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestFromURL(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
url := tt.args.url
if url == "" {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
file, err := os.ReadFile(tt.args.filepath)
require.NoError(t, err)
_, err = w.Write(file)
Expand Down
17 changes: 9 additions & 8 deletions ignite/pkg/protoanalysis/protoutil/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestCreatePackage(t *testing.T) {
}
}

// Options
// Options.
func TestCreateOption(t *testing.T) {
cases := []struct {
name, constant, setField string
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestCreateService(t *testing.T) {
}
}

// Fields
// Fields.
func TestCreateField(t *testing.T) {
cases := []struct {
name, typeName string
Expand Down Expand Up @@ -332,15 +332,16 @@ func TestCreateMessage(t *testing.T) {
// options added first, then fields and then enums.
lenOpts, lenFields, lenEnums := len(test.options), len(test.fields), len(test.enums)
for i, field := range message.Elements {
if i < lenOpts {
switch {
case i < lenOpts:
opt, ok := field.(*proto.Option)
require.True(t, ok, "expected option, got %T", field)
require.Equal(t, test.options[i], opt, "expected %v, got %v", test.options[i], opt)
} else if i < lenOpts+lenFields {
case i < lenOpts+lenFields:
field, ok := field.(*proto.NormalField)
require.True(t, ok, "expected field, got %T", field)
require.Equal(t, test.fields[i-lenOpts], field, "expected %v, got %v", test.fields[i-lenOpts], field)
} else {
default:
enum, ok := field.(*proto.Enum)
require.True(t, ok, "expected enum, got %T", field)
require.Equal(t, test.enums[i-lenOpts-lenFields], enum, "expected %v, got %v", test.enums[i-lenOpts-lenFields], enum)
Expand Down Expand Up @@ -387,7 +388,7 @@ func TestCreateEnumField(t *testing.T) {
}
}

// Enums:
// Enums:.
func TestCreateEnum(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -435,7 +436,7 @@ func TestCreateEnum(t *testing.T) {
}
}

// OneOf fields:
// OneOf fields:.
func TestCreateOneofField(t *testing.T) {
cases := []struct {
name, typeName string
Expand Down Expand Up @@ -473,7 +474,7 @@ func TestCreateOneofField(t *testing.T) {
}
}

// Oneof:
// Oneof:.
func TestCreateOneof(t *testing.T) {
cases := []struct {
name string
Expand Down
Loading

0 comments on commit 4135601

Please sign in to comment.