Skip to content

Commit a3d48e0

Browse files
authored
Prompts unit tests (#2841)
* more prompter to its own file. remove test for standard slices.Contains. * unify some names * refining the tests * a bit more coverage * almost full coverage on prompts.go * start adding validations tests * reorganize code so coverage for prompts.go tests are included * more tests * coverage of validations much improved * comparator test * nice * even more coverage * more ov * nit * split tests among two files * almost there * 99.4 coverage on prompts * address codeQL report
1 parent 3284ed0 commit a3d48e0

File tree

19 files changed

+10715
-995
lines changed

19 files changed

+10715
-995
lines changed

cmd/blockchaincmd/deploy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ava-labs/avalanche-cli/pkg/models"
2727
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
2828
"github.com/ava-labs/avalanche-cli/pkg/prompts"
29+
"github.com/ava-labs/avalanche-cli/pkg/prompts/comparator"
2930
"github.com/ava-labs/avalanche-cli/pkg/subnet"
3031
"github.com/ava-labs/avalanche-cli/pkg/txutils"
3132
"github.com/ava-labs/avalanche-cli/pkg/utils"
@@ -334,15 +335,15 @@ func getSubnetEVMMainnetChainID(sc *models.Sidecar, blockchainName string) error
334335
ux.Logger.PrintToUser("Enter your blockchain's ChainID. It can be any positive integer != %d.", originalChainID)
335336
newChainID, err := app.Prompt.CapturePositiveInt(
336337
"ChainID",
337-
[]prompts.Comparator{
338+
[]comparator.Comparator{
338339
{
339340
Label: "Zero",
340-
Type: prompts.MoreThan,
341+
Type: comparator.MoreThan,
341342
Value: 0,
342343
},
343344
{
344345
Label: "Original Chain ID",
345-
Type: prompts.NotEq,
346+
Type: comparator.NotEq,
346347
Value: originalChainID,
347348
},
348349
},

cmd/blockchaincmd/publish_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/ava-labs/avalanche-cli/pkg/config"
1717
"github.com/ava-labs/avalanche-cli/pkg/constants"
1818
"github.com/ava-labs/avalanche-cli/pkg/models"
19+
promptsmocks "github.com/ava-labs/avalanche-cli/pkg/prompts/mocks"
1920
"github.com/ava-labs/avalanche-cli/pkg/subnet"
2021
"github.com/ava-labs/avalanche-cli/pkg/ux"
2122
"github.com/ava-labs/avalanchego/ids"
@@ -393,7 +394,7 @@ func TestPublishing(t *testing.T) {
393394
mockPrompt.ExpectedCalls = nil
394395
}
395396

396-
func configureMockPrompt(mockPrompt *mocks.Prompter) {
397+
func configureMockPrompt(mockPrompt *promptsmocks.Prompter) {
397398
mockPrompt.On("CaptureList", mock.Anything, mock.Anything).Return("Add", nil).Once()
398399
mockPrompt.On("CaptureEmail", mock.Anything).Return("someone@somewhere.com", nil)
399400
mockPrompt.On("CaptureList", mock.Anything, mock.Anything).Return("Done", nil).Once()
@@ -407,14 +408,14 @@ func configureMockPrompt(mockPrompt *mocks.Prompter) {
407408
mockPrompt.On("CaptureVersion", mock.Anything).Return("v0.9.99", nil)
408409
}
409410

410-
func setupTestEnv(t *testing.T) (*require.Assertions, *mocks.Prompter) {
411+
func setupTestEnv(t *testing.T) (*require.Assertions, *promptsmocks.Prompter) {
411412
require := require.New(t)
412413
testDir := t.TempDir()
413414
err := os.Mkdir(filepath.Join(testDir, "repos"), 0o755)
414415
require.NoError(err)
415416
ux.NewUserLog(logging.NoLog{}, io.Discard)
416417
app = &application.Avalanche{}
417-
mockPrompt := mocks.NewPrompter(t)
418+
mockPrompt := promptsmocks.NewPrompter(t)
418419
app.Setup(testDir, logging.NoLog{}, config.New(), "", mockPrompt, application.NewDownloader(), nil)
419420

420421
return require, mockPrompt

cmd/blockchaincmd/upgradecmd/generate.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package upgradecmd
55
import (
66
"encoding/json"
77
"fmt"
8+
"math"
89
"os"
910
"time"
1011

@@ -26,7 +27,7 @@ import (
2627
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
2728
subnetevmutils "github.com/ava-labs/subnet-evm/utils"
2829
"github.com/ethereum/go-ethereum/common"
29-
"github.com/ethereum/go-ethereum/common/math"
30+
goethereummath "github.com/ethereum/go-ethereum/common/math"
3031
"github.com/spf13/cobra"
3132
)
3233

@@ -249,7 +250,7 @@ func promptNativeMintParams(
249250
precompiles *[]params.PrecompileUpgrade,
250251
date time.Time,
251252
) (bool, error) {
252-
initialMint := map[common.Address]*math.HexOrDecimal256{}
253+
initialMint := map[common.Address]*goethereummath.HexOrDecimal256{}
253254
adminAddrs, managerAddrs, enabledAddrs, cancelled, err := promptAdminManagerAndEnabledAddresses(sc, "mint native tokens")
254255
if cancelled || err != nil {
255256
return cancelled, err
@@ -271,7 +272,11 @@ func promptNativeMintParams(
271272
if err != nil {
272273
return "", err
273274
}
274-
initialMint[addr] = math.NewHexOrDecimal256(int64(amount))
275+
// Check for overflow when converting uint64 to int64
276+
if amount > math.MaxInt64 {
277+
return "", fmt.Errorf("amount %d exceeds maximum allowed value of %d", amount, math.MaxInt64)
278+
}
279+
initialMint[addr] = goethereummath.NewHexOrDecimal256(int64(amount))
275280
return fmt.Sprintf("%s-%d", addr.Hex(), amount), nil
276281
},
277282
"Add an address to amount pair",

cmd/flags/blockchain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"fmt"
77
"testing"
88

9-
"github.com/ava-labs/avalanche-cli/internal/mocks"
109
"github.com/ava-labs/avalanche-cli/pkg/application"
10+
"github.com/ava-labs/avalanche-cli/pkg/prompts/mocks"
1111
"github.com/spf13/cobra"
1212
"github.com/stretchr/testify/require"
1313
)

cmd/nodecmd/local.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
2222
"github.com/ava-labs/avalanche-cli/pkg/node"
2323
"github.com/ava-labs/avalanche-cli/pkg/prompts"
24+
"github.com/ava-labs/avalanche-cli/pkg/prompts/comparator"
2425
"github.com/ava-labs/avalanche-cli/pkg/signatureaggregator"
2526
"github.com/ava-labs/avalanche-cli/pkg/subnet"
2627
"github.com/ava-labs/avalanche-cli/pkg/utils"
@@ -436,10 +437,10 @@ func localValidate(_ *cobra.Command, args []string) error {
436437
if stakeAmount == 0 {
437438
stakeAmount, err = app.Prompt.CaptureUint64Compare(
438439
"Enter the amount of token to stake for each validator",
439-
[]prompts.Comparator{
440+
[]comparator.Comparator{
440441
{
441442
Label: "Positive",
442-
Type: prompts.MoreThan,
443+
Type: comparator.MoreThan,
443444
Value: 0,
444445
},
445446
},

cmd/primarycmd/add_validator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/ava-labs/avalanche-cli/pkg/models"
1919
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
2020
"github.com/ava-labs/avalanche-cli/pkg/prompts"
21+
"github.com/ava-labs/avalanche-cli/pkg/prompts/comparator"
2122
"github.com/ava-labs/avalanche-cli/pkg/subnet"
2223
"github.com/ava-labs/avalanche-cli/pkg/ux"
2324
"github.com/ava-labs/avalanchego/ids"
@@ -233,10 +234,10 @@ func getDelegationFeeOption(app *application.Avalanche, network models.Network)
233234
ux.Logger.PrintToUser("Note that 20 000 is equivalent to 2%%")
234235
delegationFee, err := app.Prompt.CapturePositiveInt(
235236
delegationFeePrompt,
236-
[]prompts.Comparator{
237+
[]comparator.Comparator{
237238
{
238239
Label: "Min Delegation Fee",
239-
Type: prompts.MoreThanEq,
240+
Type: comparator.MoreThanEq,
240241
Value: uint64(defaultFee),
241242
},
242243
},

0 commit comments

Comments
 (0)