github.com/Finschia/finschia-sdk@v0.49.1/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 octypes "github.com/Finschia/ostracon/types" 10 "github.com/gogo/protobuf/proto" 11 12 "github.com/Finschia/finschia-sdk/codec" 13 cdctypes "github.com/Finschia/finschia-sdk/codec/types" 14 "github.com/Finschia/finschia-sdk/server" 15 "github.com/Finschia/finschia-sdk/types" 16 bankexported "github.com/Finschia/finschia-sdk/x/bank/exported" 17 "github.com/Finschia/finschia-sdk/x/genutil" 18 gtypes "github.com/Finschia/finschia-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 gtypes.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 := types.TxDecoder(func(txBytes []byte) (types.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 gdoc := octypes.GenesisDoc{AppState: []byte("{}")} 60 balItr := new(doNothingIterator) 61 62 dnc := &doNothingUnmarshalJSON{cdc} 63 if _, _, err := genutil.CollectTxs(dnc, txDecoder, "foo", testDir, gdoc, balItr); err != nil { 64 t.Fatal(err) 65 } 66 }