github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/alias/alias_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package alias
     7  
     8  import (
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  	"gopkg.in/yaml.v2"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/validator"
    17  )
    18  
    19  func TestAlias(t *testing.T) {
    20  	require := require.New(t)
    21  
    22  	_, err := testInit(t)
    23  	require.NoError(err)
    24  
    25  	raullen := "raullen"
    26  	qevan := "qevan"
    27  	jing := "jing"
    28  	frankonly := "ifyouraliasistoolongthealiassetcommandmayfailwhenrunningmycode"
    29  	aliasTestCase := [][]string{
    30  		{qevan, "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx"},
    31  		{frankonly, "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx"},
    32  		{qevan, "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1"},
    33  		{raullen, "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx"},
    34  		{jing, "io188fptstp82y53l3x0eadfhxg6qmywgny24mgfp"},
    35  	}
    36  	expected := []string{
    37  		"",
    38  		validator.ErrLongAlias.Error(),
    39  		validator.ErrInvalidAddr.Error(),
    40  		"",
    41  		"",
    42  	}
    43  	for i, testCase := range aliasTestCase {
    44  		err := set(testCase)
    45  		if err != nil {
    46  			require.Contains(err.Error(), expected[i])
    47  		}
    48  	}
    49  	require.Equal("io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx", config.ReadConfig.Aliases[raullen])
    50  	require.Equal("", config.ReadConfig.Aliases[qevan])
    51  	require.Equal("io188fptstp82y53l3x0eadfhxg6qmywgny24mgfp", config.ReadConfig.Aliases[jing])
    52  	aliases := GetAliasMap()
    53  	require.Equal(raullen, aliases["io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx"])
    54  	require.Equal(jing, aliases["io188fptstp82y53l3x0eadfhxg6qmywgny24mgfp"])
    55  	require.Equal("", aliases["io1uwnr55vqmhf3xeg5phgurlyl702af6eju542s1"])
    56  	require.NoError(remove(raullen))
    57  	require.Equal("", config.ReadConfig.Aliases[raullen])
    58  	require.NoError(set([]string{jing, "io1kmpejl35lys5pxcpk74g8am0kwmzwwuvsvqrp8"}))
    59  	require.Equal("io1kmpejl35lys5pxcpk74g8am0kwmzwwuvsvqrp8", config.ReadConfig.Aliases[jing])
    60  	aliases = GetAliasMap()
    61  	require.Equal("", aliases["io188fptstp82y53l3x0eadfhxg6qmywgny24mgfp"])
    62  	require.Equal(jing, aliases["io1kmpejl35lys5pxcpk74g8am0kwmzwwuvsvqrp8"])
    63  }
    64  
    65  func testInit(t *testing.T) (string, error) {
    66  	var err error
    67  	testPathd := t.TempDir()
    68  	config.ConfigDir = testPathd
    69  	config.DefaultConfigFile = config.ConfigDir + "/config.default"
    70  	config.ReadConfig, err = config.LoadConfig()
    71  	if err != nil && !os.IsNotExist(err) {
    72  		return testPathd, err
    73  	}
    74  	config.ReadConfig.Wallet = config.ConfigDir
    75  	out, err := yaml.Marshal(&config.ReadConfig)
    76  	if err != nil {
    77  		return testPathd, err
    78  	}
    79  	if err := os.WriteFile(config.DefaultConfigFile, out, 0600); err != nil {
    80  		return testPathd, err
    81  	}
    82  	return testPathd, nil
    83  }