github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/update/update_test.go (about) 1 // Copyright (c) 2022 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 update 7 8 import ( 9 "testing" 10 11 "github.com/golang/mock/gomock" 12 "github.com/pkg/errors" 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/util" 17 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 18 ) 19 20 func TestNewUpdateCmd(t *testing.T) { 21 require := require.New(t) 22 ctrl := gomock.NewController(t) 23 client := mock_ioctlclient.NewMockClient(ctrl) 24 25 expectedValue := "ioctl is up-to-date now." 26 client.EXPECT().SelectTranslation(gomock.Any()).Return(expectedValue, 27 config.English).Times(18) 28 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(2) 29 client.EXPECT().Execute(gomock.Any()).Return(nil).Times(2) 30 31 t.Run("update cli with stable", func(t *testing.T) { 32 cmd := NewUpdateCmd(client) 33 result, err := util.ExecuteCmd(cmd) 34 require.NoError(err) 35 require.Contains(result, expectedValue) 36 }) 37 38 t.Run("update cli with unstable", func(t *testing.T) { 39 cmd := NewUpdateCmd(client) 40 result, err := util.ExecuteCmd(cmd, "-t", "unstable") 41 require.NoError(err) 42 require.Contains(result, expectedValue) 43 }) 44 45 t.Run("failed to execute bash command", func(t *testing.T) { 46 expectedError := errors.New("failed to execute bash command") 47 client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationResult", 48 config.English).Times(9) 49 client.EXPECT().AskToConfirm(gomock.Any()).Return(true, nil).Times(1) 50 client.EXPECT().Execute(gomock.Any()).Return(expectedError).Times(1) 51 52 cmd := NewUpdateCmd(client) 53 _, err := util.ExecuteCmd(cmd) 54 require.Equal("mockTranslationResult: "+expectedError.Error(), err.Error()) 55 }) 56 57 t.Run("invalid version type", func(t *testing.T) { 58 expectedError := errors.New("invalid version-type flag:pre-release") 59 client.EXPECT().SelectTranslation(gomock.Any()).Return("invalid version-type flag:%s", 60 config.English).Times(9) 61 client.EXPECT().Execute(gomock.Any()).Return(expectedError).AnyTimes() 62 63 cmd := NewUpdateCmd(client) 64 _, err := util.ExecuteCmd(cmd, "-t", "pre-release") 65 require.Error(err) 66 require.Equal(expectedError.Error(), err.Error()) 67 }) 68 }