From 5ff1d0c3e89774b62693bcce2b034c1a5ebb8319 Mon Sep 17 00:00:00 2001 From: Cesare Valitutto Date: Fri, 29 Dec 2023 18:08:06 +0100 Subject: [PATCH] fix(cli): fix output redirection and little refactoring --- cmd/ethkit/balance_test.go | 115 ++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 65 deletions(-) diff --git a/cmd/ethkit/balance_test.go b/cmd/ethkit/balance_test.go index b5c75f72..6ebbc1a1 100644 --- a/cmd/ethkit/balance_test.go +++ b/cmd/ethkit/balance_test.go @@ -2,91 +2,76 @@ package main import ( "bytes" + "fmt" "math" "strconv" - - // "fmt" - // "strconv" + "strings" "testing" "github.com/stretchr/testify/assert" ) -func TestOKExecuteBalanceCmd(t *testing.T) { +func execute(args string) (string, error) { cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia"}) - assert.NoError(t, cmd.Execute()) + actual := new(bytes.Buffer) + cmd.SetOut(actual) + cmd.SetErr(actual) + cmd.SetArgs(strings.Split(args, " ")) + if err := cmd.Execute(); err != nil { + return "", err + } + + return actual.String(), nil } -// TODO: Fix bad output redirection that makes assert to fail -func TestOKBalanceValidWei(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia"}) - assert.NoError(t, cmd.Execute()) - // assert.Equal(t, b.String(), fmt.Sprintln(strconv.Itoa(500_000_000_000_000_000), "wei")) +func Test_BalanceCmd(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia") + assert.Nil(t, err) + assert.NotNil(t, res) } -// TODO: Fix bad output redirection that makes assert to fail -func TestOKBalanceValidEther(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--ether"}) - assert.NoError(t, cmd.Execute()) - // assert.Equal(t, b.String(), fmt.Sprintln(strconv.FormatFloat(0.5, 'f', -1, 64), "ether")) +func Test_BalanceCmd_ValidWei(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia") + assert.Nil(t, err) + assert.Equal(t, res, fmt.Sprintln(strconv.Itoa(500_000_000_000_000_000), "wei")) } -func TestFailInvalidAddress(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x1", "--rpc-url", "https://nodes.sequence.app/sepolia"}) - assert.Error(t, cmd.Execute()) - assert.Contains(t, b.String(), "valid account address") +func Test_BalanceCmd_ValidEther(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --ether") + assert.Nil(t, err) + assert.Equal(t, res, fmt.Sprintln(strconv.FormatFloat(0.5, 'f', -1, 64), "ether")) } -func TestFailInvalidRPC(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.Println(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "nodes.sequence.app/sepolia"}) - assert.Error(t, cmd.Execute()) - assert.Contains(t, b.String(), "valid rpc url") +func Test_BalanceCmd_InvalidAddress(t *testing.T) { + res, err := execute("0x1 --rpc-url https://nodes.sequence.app/sepolia") + assert.NotNil(t, err) + assert.Empty(t, res) + assert.Contains(t, err.Error(), "please provide a valid account address") } -func TestFailNotExistingBlockHeigh(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", strconv.FormatInt(math.MaxInt64, 10)}) - assert.Error(t, cmd.Execute()) - assert.Contains(t, b.String(), "jsonrpc error -32000: header not found") +func Test_BalanceCmd_InvalidRPC(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url nodes.sequence.app/sepolia") + assert.NotNil(t, err) + assert.Empty(t, res) + assert.Contains(t, err.Error(), "please provide a valid rpc url") } -func TestFailNotAValidStringBlockHeigh(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", "something"}) - assert.Error(t, cmd.Execute()) - assert.Contains(t, b.String(), "invalid block height") +func Test_BalanceCmd_NotExistingBlockHeigh(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block " + fmt.Sprint(math.MaxInt64)) + assert.NotNil(t, err) + assert.Empty(t, res) + assert.Contains(t, err.Error(), "jsonrpc error -32000: header not found") } -func TestFailNotAValidNumberBlockHeigh(t *testing.T) { - cmd := NewBalanceCmd() - b := new(bytes.Buffer) - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", "-100"}) - assert.Error(t, cmd.Execute()) +func Test_BalanceCmd_NotAValidStringBlockHeigh(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block something") + assert.NotNil(t, err) + assert.Empty(t, res) + assert.Contains(t, err.Error(), "invalid block height") +} + +func Test_BalanceCmd_NotAValidNumberBlockHeigh(t *testing.T) { + res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block -100") + assert.NotNil(t, err) + assert.Empty(t, res) }