/
test_helpers.go
88 lines (74 loc) · 2.21 KB
/
test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package app
import (
"encoding/json"
"os"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
gitopiaparams "github.com/gitopia/gitopia/v3/app/params"
)
var defaultGenesisBz []byte
func getDefaultGenesisStateBytes(cdc codec.JSONCodec) []byte {
if len(defaultGenesisBz) == 0 {
genesisState := NewDefaultGenesisState(cdc)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
defaultGenesisBz = stateBytes
}
return defaultGenesisBz
}
// Setup initializes a new GitopiaApp.
func Setup(isCheckTx bool) *GitopiaApp {
db := dbm.NewMemDB()
encoding := gitopiaparams.EncodingConfig(MakeEncodingConfig())
app := NewGitopiaApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding, simapp.EmptyAppOptions{})
if !isCheckTx {
stateBytes := getDefaultGenesisStateBytes(app.AppCodec())
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simapp.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
return app
}
// SetupTestingAppWithLevelDb initializes a new GitopiaApp intended for testing,
// with LevelDB as a db.
func SetupTestingAppWithLevelDb(isCheckTx bool) (app *GitopiaApp, cleanupFn func()) {
dir := "osmosis_testing"
db, err := sdk.NewLevelDB("gitopia_leveldb_testing", dir)
if err != nil {
panic(err)
}
encoding := gitopiaparams.EncodingConfig(MakeEncodingConfig())
app = NewGitopiaApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, encoding, simapp.EmptyAppOptions{})
if !isCheckTx {
genesisState := NewDefaultGenesisState(app.AppCodec())
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simapp.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
cleanupFn = func() {
db.Close()
err = os.RemoveAll(dir)
if err != nil {
panic(err)
}
}
return app, cleanupFn
}