-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ioctl] Build contract prepare command line into new ioctl (#3737)
* [ioctl] build contract prepare command line into new ioctl * build unittest to cover the modification * fix commit * update assert message * update solc version * update solc message version * format * brew install solidity Co-authored-by: huofei <[email protected]>
- Loading branch information
1 parent
35c8528
commit 7371c44
Showing
3 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// 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 contract | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/iotexproject/iotex-core/ioctl" | ||
"github.com/iotexproject/iotex-core/ioctl/config" | ||
"github.com/iotexproject/iotex-core/ioctl/util" | ||
) | ||
|
||
// Multi-language support | ||
var ( | ||
_prepareCmdShorts = map[config.Language]string{ | ||
config.English: "Prepare solidity compiler", | ||
config.Chinese: "准备solidity编译器", | ||
} | ||
) | ||
|
||
// NewContractPrepareCmd represents the contract prepare command | ||
func NewContractPrepareCmd(client ioctl.Client) *cobra.Command { | ||
short, _ := client.SelectTranslation(_prepareCmdShorts) | ||
|
||
return &cobra.Command{ | ||
Use: "prepare", | ||
Short: short, | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
_, err := util.SolidityVersion(_solCompiler) | ||
if err != nil { | ||
cmdString := "curl --silent https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-solc.sh | sh" | ||
installCmd := exec.Command("bash", "-c", cmdString) | ||
cmd.Println("Preparing solidity compiler ...") | ||
|
||
err = installCmd.Run() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to prepare solc") | ||
} | ||
} | ||
solc, err := util.SolidityVersion(_solCompiler) | ||
if err != nil { | ||
return errors.Wrap(err, "solidity compiler not ready") | ||
} | ||
if !checkCompilerVersion(solc) { | ||
return errors.Errorf("unsupported solc version %d.%d.%d, expects solc version 0.8.17\n", | ||
solc.Major, solc.Minor, solc.Patch) | ||
} | ||
|
||
cmd.Println("Solidity compiler is ready now.") | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// 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 contract | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"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 TestNewContractPrepareCmd(t *testing.T) { | ||
skipWithoutSolc(t) | ||
require := require.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
client := mock_ioctlclient.NewMockClient(ctrl) | ||
client.EXPECT().SelectTranslation(gomock.Any()).Return("contract", config.English) | ||
|
||
t.Run("prepare contract", func(t *testing.T) { | ||
cmd := NewContractPrepareCmd(client) | ||
result, err := util.ExecuteCmd(cmd) | ||
require.NoError(err) | ||
require.Equal("Solidity compiler is ready now.\n", result) | ||
}) | ||
} |