11package cli_test
22
33import (
4+ "encoding/json"
5+ "fmt"
6+ "io"
47 "os"
58 "testing"
69
710 "github.com/stretchr/testify/require"
811
912 "github.com/cosmos/cosmos-sdk/client"
13+ "github.com/cosmos/cosmos-sdk/codec"
1014 "github.com/cosmos/cosmos-sdk/testutil"
1115 clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
16+ "github.com/cosmos/cosmos-sdk/types/module"
1217 "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
1318)
1419
@@ -33,14 +38,33 @@ var v037Exported = `{
3338
3439func TestValidateGenesis (t * testing.T ) {
3540 testCases := []struct {
36- name string
37- genesis string
38- expErr bool
41+ name string
42+ genesis string
43+ expErrStr string
44+ basicManager module.BasicManager
3945 }{
46+ {
47+ "invalid json" ,
48+ `{"app_state": {x,}}` ,
49+ "error at offset 16: invalid character" ,
50+ module .NewBasicManager (),
51+ },
52+ {
53+ "invalid: missing module config in app_state" ,
54+ func () string {
55+ bz , err := os .ReadFile ("../../types/testdata/app_genesis.json" )
56+ require .NoError (t , err )
57+
58+ return string (bz )
59+ }(),
60+ "section is missing in the app_state" ,
61+ module .NewBasicManager (mockModule {}),
62+ },
4063 {
4164 "exported 0.37 genesis file" ,
4265 v037Exported ,
43- true ,
66+ "make sure that you have correctly migrated all CometBFT consensus params" ,
67+ module .NewBasicManager (),
4468 },
4569 {
4670 "valid 0.50 genesis file" ,
@@ -50,7 +74,8 @@ func TestValidateGenesis(t *testing.T) {
5074
5175 return string (bz )
5276 }(),
53- false ,
77+ "" ,
78+ module .NewBasicManager (),
5479 },
5580 }
5681
@@ -59,12 +84,30 @@ func TestValidateGenesis(t *testing.T) {
5984
6085 t .Run (tc .name , func (t * testing.T ) {
6186 genesisFile := testutil .WriteToNewTempFile (t , tc .genesis )
62- _ , err := clitestutil .ExecTestCLICmd (client.Context {}, cli .ValidateGenesisCmd (nil ), []string {genesisFile .Name ()})
63- if tc .expErr {
64- require .Contains (t , err .Error (), "make sure that you have correctly migrated all CometBFT consensus params" )
87+ _ , err := clitestutil .ExecTestCLICmd (client.Context {}, cli .ValidateGenesisCmd (tc . basicManager ), []string {genesisFile .Name ()})
88+ if tc .expErrStr != "" {
89+ require .Contains (t , err .Error (), tc . expErrStr )
6590 } else {
6691 require .NoError (t , err )
6792 }
6893 })
6994 }
7095}
96+
97+ var _ module.HasGenesisBasics = mockModule {}
98+
99+ type mockModule struct {
100+ module.AppModuleBasic
101+ }
102+
103+ func (m mockModule ) Name () string {
104+ return "mock"
105+ }
106+
107+ func (m mockModule ) DefaultGenesis (codec.JSONCodec ) json.RawMessage {
108+ return json .RawMessage (`{"foo": "bar"}` )
109+ }
110+
111+ func (m mockModule ) ValidateGenesis (codec.JSONCodec , client.TxEncodingConfig , json.RawMessage ) error {
112+ return fmt .Errorf ("mock section is missing: %w" , io .EOF )
113+ }
0 commit comments