Skip to content

Commit

Permalink
test: add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Dec 17, 2024
1 parent 14b98f0 commit 5be4db8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
7 changes: 6 additions & 1 deletion app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,17 @@ func init() {
// Setup sets up basic environment for suite (App, Ctx, and test accounts)
// preserves the caching enabled/disabled state.
func (s *KeeperTestHelper) Setup() {
s.T().Log("Setting up KeeperTestHelper")
dir, err := os.MkdirTemp("", "osmosisd-test-home")
if err != nil {
panic(fmt.Sprintf("failed creating temporary directory: %v", err))
}
s.T().Cleanup(func() { os.RemoveAll(dir); s.withCaching = false })
s.App = app.SetupWithCustomHome(false, dir)
if app.IsDebugLogEnabled() {
s.App = app.SetupWithCustomHome(false, dir, s.T())
} else {
s.App = app.SetupWithCustomHome(false, dir)
}
s.setupGeneral()

// Manually set validator signing info, otherwise we panic
Expand Down
53 changes: 49 additions & 4 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"encoding/json"
"os"
"testing"
"time"

"cosmossdk.io/log"
Expand All @@ -17,6 +18,8 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cosmoserver "github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sims "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -117,13 +120,55 @@ func GenesisStateWithValSet(app *OsmosisApp) GenesisState {
var defaultGenesisStatebytes = []byte{}

// SetupWithCustomHome initializes a new OsmosisApp with a custom home directory
func SetupWithCustomHome(isCheckTx bool, dir string) *OsmosisApp {
return SetupWithCustomHomeAndChainId(isCheckTx, dir, "osmosis-1")
func SetupWithCustomHome(isCheckTx bool, dir string, t ...*testing.T) *OsmosisApp {
return SetupWithCustomHomeAndChainId(isCheckTx, dir, "osmosis-1", t...)
}

func SetupWithCustomHomeAndChainId(isCheckTx bool, dir, chainId string) *OsmosisApp {
// DebugAppOptions is a stub implementing AppOptions
type DebugAppOptions struct{}

// Get implements AppOptions
func (ao DebugAppOptions) Get(o string) interface{} {
if o == cosmoserver.FlagTrace {
return true
}
return nil
}

func IsDebugLogEnabled() bool {
return os.Getenv("OSMO_KEEPER_DEBUG") != ""
}

func SetupWithCustomHomeAndChainId(isCheckTx bool, dir, chainId string, t ...*testing.T) *OsmosisApp {
db := cosmosdb.NewMemDB()
app := NewOsmosisApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, dir, 0, sims.EmptyAppOptions{}, EmptyWasmOpts, baseapp.SetChainID(chainId))
var (
l log.Logger
appOpts servertypes.AppOptions
)
if IsDebugLogEnabled() {
appOpts = DebugAppOptions{}
} else {
appOpts = sims.EmptyAppOptions{}
}
if len(t) > 0 {
testEnv := t[0]
testEnv.Log("Using test environment logger")
l = log.NewTestLogger(testEnv)
} else {
l = log.NewNopLogger()
}
app := NewOsmosisApp(
l,
db,
nil,
true,
map[int64]bool{},
dir,
0,
appOpts,
EmptyWasmOpts,
baseapp.SetChainID(chainId),
)
if !isCheckTx {
if len(defaultGenesisStatebytes) == 0 {
var err error
Expand Down

0 comments on commit 5be4db8

Please sign in to comment.