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