Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
huof6829 committed Jun 22, 2022
1 parent 9a75119 commit 3f2bb7a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
33 changes: 25 additions & 8 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
_configDir = os.Getenv("HOME") + "/.config/ioctl/default"
)

// info contains the information of config file
Expand All @@ -49,17 +50,21 @@ type info struct {

// InitConfig load config data from default config file
func InitConfig() (config.Config, string, error) {
info := &info{}
configDir := os.Getenv("HOME") + "/.config/ioctl/default"
info := &info{
readConfig: config.Config{
Aliases: make(map[string]string),
},
}

// Create path to config directory
err := os.MkdirAll(configDir, 0700)
err := os.MkdirAll(_configDir, 0700)
if err != nil {
return info.readConfig, info.defaultConfigFile, err
}
info.defaultConfigFile = filepath.Join(configDir, _defaultConfigFileName)
info.defaultConfigFile = filepath.Join(_configDir, _defaultConfigFileName)

// Load or reset config file
info.readConfig, err = config.LoadConfig()
err = info.loadConfig()
if os.IsNotExist(err) {
err = info.reset() // Config file doesn't exist
} else if err != nil {
Expand All @@ -69,7 +74,7 @@ func InitConfig() (config.Config, string, error) {
// Check completeness of config file
completeness := true
if info.readConfig.Wallet == "" {
info.readConfig.Wallet = configDir
info.readConfig.Wallet = _configDir
completeness = false
}
if info.readConfig.Language == "" {
Expand Down Expand Up @@ -109,8 +114,8 @@ func (c *info) reset() error {
c.readConfig.Endpoint = ""
c.readConfig.SecureConnect = true
c.readConfig.DefaultAccount = *new(config.Context)
c.readConfig.Explorer = "iotexscan"
c.readConfig.Language = "English"
c.readConfig.Explorer = _validExpl[0]
c.readConfig.Language = _supportedLanguage[0]
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint

err := c.writeConfig()
Expand Down Expand Up @@ -146,3 +151,15 @@ func (c *info) writeConfig() error {
}
return nil
}

// loadConfig loads config file in yaml format
func (c *info) loadConfig() error {
in, err := os.ReadFile(c.defaultConfigFile)
if err != nil {
return err
}
if err = yaml.Unmarshal(in, &c.readConfig); err != nil {
return errors.Wrap(err, "failed to unmarshal config")
}
return nil
}
33 changes: 33 additions & 0 deletions ioctl/newcmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/testutil"
)

func TestInitConfig(t *testing.T) {
require := require.New(t)
testPath, err := os.MkdirTemp(os.TempDir(), "testCfg")
require.NoError(err)
defer func() {
testutil.CleanupPath(testPath)
}()
_configDir = testPath
cfg, cfgFilePath, err := InitConfig()
require.NoError(err)
require.Equal(testPath, cfg.Wallet)
require.Equal(_validExpl[0], cfg.Explorer)
require.Equal(_supportedLanguage[0], cfg.Language)
require.Equal(filepath.Join(testPath, _defaultConfigFileName), cfgFilePath)
}
40 changes: 40 additions & 0 deletions ioctl/newcmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package newcmd

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
)

func TestNew(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mock_ioctlclient.NewMockClient(ctrl)
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {}).AnyTimes()
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {}).AnyTimes()

cmds := []*cobra.Command{
NewIoctl(client),
NewXctl(client),
}
for _, cmd := range cmds {
result, err := util.ExecuteCmd(cmd)
require.NoError(err)
require.Contains(result, "Available Commands")
require.Contains(result, "Flags")
}
}

0 comments on commit 3f2bb7a

Please sign in to comment.