github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/server/test_helpers.go (about) 1 package server 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net" 7 "os" 8 "testing" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags" 11 12 "github.com/spf13/viper" 13 "github.com/stretchr/testify/require" 14 ) 15 16 // Get a free address for a test tendermint server 17 // protocol is either tcp, http, etc 18 func FreeTCPAddr() (addr, port string, err error) { 19 l, err := net.Listen("tcp", "localhost:0") 20 if err != nil { 21 return "", "", err 22 } 23 24 closer := func() { 25 err := l.Close() 26 if err != nil { 27 // TODO: Handle with #870 28 panic(err) 29 } 30 } 31 32 defer closer() 33 34 portI := l.Addr().(*net.TCPAddr).Port 35 port = fmt.Sprintf("%d", portI) 36 addr = fmt.Sprintf("tcp://0.0.0.0:%s", port) 37 return 38 } 39 40 // SetupViper creates a homedir to run inside, 41 // and returns a cleanup function to defer 42 func SetupViper(t *testing.T) func() { 43 rootDir, err := ioutil.TempDir("", "mock-sdk-cmd") 44 require.Nil(t, err) 45 viper.Set(flags.FlagHome, rootDir) 46 viper.Set(flags.FlagName, "moniker") 47 return func() { 48 err := os.RemoveAll(rootDir) 49 if err != nil { 50 // TODO: Handle with #870 51 panic(err) 52 } 53 } 54 } 55 56 // DONTCOVER