github.com/cosmos/cosmos-sdk@v0.50.10/x/genutil/collect_test.go (about) 1 package genutil_test 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/cosmos/gogoproto/proto" 10 11 "github.com/cosmos/cosmos-sdk/codec" 12 addresscodec "github.com/cosmos/cosmos-sdk/codec/address" 13 cdctypes "github.com/cosmos/cosmos-sdk/codec/types" 14 "github.com/cosmos/cosmos-sdk/server" 15 sdk "github.com/cosmos/cosmos-sdk/types" 16 bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported" 17 "github.com/cosmos/cosmos-sdk/x/genutil" 18 "github.com/cosmos/cosmos-sdk/x/genutil/types" 19 ) 20 21 type doNothingUnmarshalJSON struct { 22 codec.JSONCodec 23 } 24 25 func (dnj *doNothingUnmarshalJSON) UnmarshalJSON(_ []byte, _ proto.Message) error { 26 return nil 27 } 28 29 type doNothingIterator struct { 30 types.GenesisBalancesIterator 31 } 32 33 func (dni *doNothingIterator) IterateGenesisBalances(_ codec.JSONCodec, _ map[string]json.RawMessage, _ func(bankexported.GenesisBalance) bool) { 34 } 35 36 // Ensures that CollectTx correctly traverses directories and won't error out on encountering 37 // a directory during traversal of the first level. See issue https://github.com/cosmos/cosmos-sdk/issues/6788. 38 func TestCollectTxsHandlesDirectories(t *testing.T) { 39 testDir, err := os.MkdirTemp(os.TempDir(), "testCollectTxs") 40 if err != nil { 41 t.Fatal(err) 42 } 43 defer os.RemoveAll(testDir) 44 45 // 1. We'll insert a directory as the first element before JSON file. 46 subDirPath := filepath.Join(testDir, "_adir") 47 if err := os.MkdirAll(subDirPath, 0o755); err != nil { 48 t.Fatal(err) 49 } 50 51 txDecoder := sdk.TxDecoder(func(txBytes []byte) (sdk.Tx, error) { 52 return nil, nil 53 }) 54 55 // 2. Ensure that we don't encounter any error traversing the directory. 56 srvCtx := server.NewDefaultContext() 57 _ = srvCtx 58 cdc := codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) 59 genesis := &types.AppGenesis{AppState: []byte("{}")} 60 balItr := new(doNothingIterator) 61 62 dnc := &doNothingUnmarshalJSON{cdc} 63 if _, _, err := genutil.CollectTxs(dnc, txDecoder, "foo", testDir, genesis, balItr, types.DefaultMessageValidator, 64 addresscodec.NewBech32Codec("cosmosvaloper")); err != nil { 65 t.Fatal(err) 66 } 67 }