Skip to content

Commit

Permalink
fix: Add better error handling in test utils (#944)
Browse files Browse the repository at this point in the history
* fix: add better error handling

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: address PR comments

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

* chore: remove unused code

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>

---------

Signed-off-by: Ivan Ivanov <ivanivanov.ii726@gmail.com>
  • Loading branch information
0xivanov authored May 30, 2024
1 parent 4b48262 commit ea7b49a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions utilities_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package hedera
*/

import (
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -84,12 +85,11 @@ func NewIntegrationTestEnv(t *testing.T) IntegrationTestEnv {
env.Client = ClientForTestnet()
} else if os.Getenv("CONFIG_FILE") != "" {
env.Client, err = ClientFromConfigFile(os.Getenv("CONFIG_FILE"))
if err != nil {
panic(err)
}
} else {
panic("Failed to construct client from environment variables")
err = fmt.Errorf("Failed to construct client from environment variables")
}
require.NoError(t, err)
assert.NotNil(t, env.Client)

configOperatorID := os.Getenv("OPERATOR_ID")
configOperatorKey := os.Getenv("OPERATOR_KEY")
Expand Down Expand Up @@ -142,7 +142,8 @@ func NewIntegrationTestEnv(t *testing.T) IntegrationTestEnv {
_ = env.Client.SetNetwork(network)

if len(network) == 0 {
panic("failed to construct network; each node returned an error")
t.Log("failed to construct network; each node returned an error")
t.FailNow()
}

env.OriginalOperatorID = env.Client.GetOperatorAccountID()
Expand All @@ -154,17 +155,12 @@ func NewIntegrationTestEnv(t *testing.T) IntegrationTestEnv {
SetInitialBalance(NewHbar(150)).
SetAutoRenewPeriod(time.Hour*24*81 + time.Minute*26 + time.Second*39).
Execute(env.Client)
if err != nil {
panic(err)
}

require.NoError(t, err)

receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
if err != nil {
panic(err)
}
require.NoError(t, err)

env.OriginalOperatorID = env.Client.GetOperatorAccountID()
env.OriginalOperatorKey = env.Client.GetOperatorPublicKey()
env.OperatorID = *receipt.AccountID
env.OperatorKey = newKey
env.NodeAccountIDs = []AccountID{resp.NodeID}
Expand Down Expand Up @@ -198,6 +194,11 @@ func CloseIntegrationTestEnv(env IntegrationTestEnv, token *TokenID) error {
return err
}

// Check if env.Client.operator is nil
if env.Client.operator == nil {
return fmt.Errorf("client operator is nil")
}

// This is needed, because we can't delete the account while still having tokens.
// This works only, because the token is deleted, otherwise the acount would need to have 0 balance of it before dissociating.
dissociateTx, err := NewTokenDissociateTransaction().
Expand Down

0 comments on commit ea7b49a

Please sign in to comment.