Skip to content

Commit cda42a8

Browse files
committed
add cligen test
1 parent fbf66a0 commit cda42a8

File tree

10 files changed

+270
-23
lines changed

10 files changed

+270
-23
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ integration-test:
7676
e2e-test:
7777
$(MAKE) -C ./tests e2e-test
7878

79+
cligen-test:
80+
$(MAKE) -C ./tests cligen-test
81+
7982
build-image:
8083
docker build . -t bluele/hypermint:${VERSION}
8184

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ require (
2828
github.com/perlin-network/life v0.0.0-20181106205055-98065d82a6ee
2929
github.com/pkg/errors v0.8.1
3030
github.com/rjeczalik/notify v0.9.2 // indirect
31-
github.com/spf13/cobra v0.0.1
32-
github.com/spf13/pflag v1.0.3
33-
github.com/spf13/viper v1.0.0
31+
github.com/spf13/cobra v0.0.5
32+
github.com/spf13/pflag v1.0.5
33+
github.com/spf13/viper v1.4.0
3434
github.com/stretchr/testify v1.3.0
3535
github.com/tendermint/go-amino v0.14.1
3636
github.com/tendermint/iavl v0.12.4

go.sum

Lines changed: 86 additions & 0 deletions
Large diffs are not rendered by default.

pkg/account/cli/bind/template.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
build/
1+
build/
2+
cligen/contract_test*

tests/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GO_BIN?=go
22
GO_TEST_FLAGS?=-v -count=1
33
GO_TEST_CMD=$(GO_BIN) test $(GO_TEST_FLAGS)
44
VERSION ?= v0.2.0
5+
TOP_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/..
56

67
build-image:
78
docker build . -t hypermint/go-rust:$(VERSION)
@@ -19,3 +20,9 @@ integration-test: contract_test.wasm
1920

2021
e2e-test: contract_test.wasm external_contract_test.wasm
2122
@$(GO_TEST_CMD) ./e2e/...
23+
24+
cligen-test: contract_test.wasm
25+
make -C .. abigen cligen
26+
(cd $(TOP_DIR)/hmcdk/genabi && cargo run $(TOP_DIR)/tests/contracts/contract_test/src/lib.rs) > cligen/contract_test.json
27+
$(TOP_DIR)/build/cligen --outdir cligen --name contract_test --package cligen --abi cligen/contract_test.json
28+
@$(GO_TEST_CMD) ./cligen/...

tests/cligen/cligen_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cligen
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
ecommon "github.com/bluele/hypermint/tests/e2e/common"
8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/spf13/cobra"
10+
"github.com/stretchr/testify/suite"
11+
"path/filepath"
12+
"regexp"
13+
"testing"
14+
"time"
15+
16+
cmn "github.com/tendermint/tendermint/libs/common"
17+
)
18+
19+
const (
20+
testContractPath = "../build/contract_test.wasm"
21+
)
22+
23+
func TestCligenTestSuite(t *testing.T) {
24+
suite.Run(t, new(CligenTestSuite))
25+
}
26+
27+
type CligenTestSuite struct {
28+
ecommon.NodeTestSuite
29+
}
30+
31+
func (ts *CligenTestSuite) SetupTest() {
32+
pjRoot, err := filepath.Abs(filepath.Join("..", ".."))
33+
if err != nil {
34+
ts.FailNow("failed to call Abs()", err.Error())
35+
}
36+
// TODO these values should be configurable?
37+
ts.Setup(
38+
filepath.Join(pjRoot, "build"),
39+
filepath.Join(filepath.Join(pjRoot, ".hm"), cmn.RandStr(8)),
40+
)
41+
}
42+
43+
func (ts *CligenTestSuite) TearDownTest() {
44+
ts.TearDown()
45+
}
46+
47+
func (ts *CligenTestSuite) TestCLI() {
48+
ctx := context.Background()
49+
c, err := ts.DeployContract(ctx, ts.Account(1), testContractPath)
50+
if !ts.NoError(err) {
51+
return
52+
}
53+
54+
caller := ts.Account(0)
55+
56+
var rootCmd = &cobra.Command{
57+
Use: "test",
58+
Short: "Test CLI",
59+
}
60+
61+
for _, tc := range []struct{
62+
suc bool
63+
args []string
64+
outregex string
65+
}{
66+
{ true, []string{"help"}, `.*$`},
67+
{ true, []string{"test-get-sender"}, caller.Hex()+`\n$`},
68+
{ true, []string{"test-get-contract-address"}, c.Hex()+`\n$`},
69+
} {
70+
args := append([]string{"contract_test", "--passphrase", "password", "--ksdir", ts.GetCLIHomeDir()}, tc.args...)
71+
buf := new(bytes.Buffer)
72+
rootCmd.SetOut(buf)
73+
rootCmd.SetArgs(args)
74+
rootCmd.AddCommand(ContractTestCmd(c.Hex(), caller.Hex()))
75+
if err := rootCmd.Execute(); tc.suc != (err == nil) {
76+
if err != nil {
77+
ts.T().Error(err)
78+
} else {
79+
ts.T().Fail()
80+
}
81+
}
82+
output := buf.String()
83+
r := regexp.MustCompile(tc.outregex)
84+
if !r.MatchString(output) {
85+
ts.T().Error(output, "does not match with the regexp", "'"+tc.outregex+"'", args)
86+
}
87+
}
88+
}
89+
90+
func (ts *CligenTestSuite) DeployContract(ctx context.Context, from common.Address, path string) (common.Address, error) {
91+
cmd := fmt.Sprintf("contract deploy --address=%v --path=%v --gas=1 --password=password", from.Hex(), path)
92+
address, err := ts.sendTxCMD(ctx, cmd)
93+
if err != nil {
94+
return common.Address{}, err
95+
}
96+
return common.HexToAddress(string(address)), nil
97+
}
98+
99+
func (ts *CligenTestSuite) sendTxCMD(ctx context.Context, cmd string) ([]byte, error) {
100+
if out, e, err := ts.ExecCLICommand(ctx, cmd); err != nil {
101+
return nil, fmt.Errorf("%v:%v:%v", string(out), string(e), err)
102+
} else {
103+
time.Sleep(2 * ts.TimeoutCommit)
104+
return out, nil
105+
}
106+
}

tests/contracts/contract_test/Cargo.lock

Lines changed: 42 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/contracts/contract_test/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ fn call_check_signature() -> Result<i32, Error> {
1616
}
1717
}
1818

19-
#[contract]
19+
#[contract(readonly)]
2020
pub fn test_get_sender() -> R<Address> {
2121
Ok(Some(get_sender()?))
2222
}
2323

24-
#[contract]
24+
#[contract(readonly)]
2525
pub fn test_get_contract_address() -> R<Address> {
2626
Ok(Some(get_contract_address()?))
2727
}
2828

29-
#[contract]
29+
#[contract(readonly)]
3030
pub fn test_get_arguments() -> R<Vec<u8>> {
3131
let argIdx: i32 = get_arg(0)?;
3232
let arg: Vec<u8> = get_arg(argIdx as usize)?;
3333
Ok(Some(arg))
3434
}
3535

36-
#[contract]
36+
#[contract(readonly)]
3737
pub fn check_signature() -> R<i32> {
3838
Ok(Some(call_check_signature()?))
3939
}
@@ -59,7 +59,7 @@ pub fn test_write_state() -> R<i32> {
5959
Ok(None)
6060
}
6161

62-
#[contract]
62+
#[contract(readonly)]
6363
pub fn test_read_state() -> R<Vec<u8>> {
6464
let key: Vec<u8> = get_arg(0)?;
6565
let value: Vec<u8> = match read_state(&key) {
@@ -113,13 +113,13 @@ pub fn test_write_to_multiple_key() -> R<i32> {
113113
Ok(None)
114114
}
115115

116-
#[contract]
116+
#[contract(readonly)]
117117
pub fn test_keccak256() -> R<Vec<u8>> {
118118
let msg: Vec<u8> = get_arg(0)?;
119119
Ok(Some(keccak256(&msg)?.to_vec()))
120120
}
121121

122-
#[contract]
122+
#[contract(readonly)]
123123
pub fn test_sha256() -> R<Vec<u8>> {
124124
let msg: Vec<u8> = get_arg(0)?;
125125
Ok(Some(sha256(&msg)?.to_vec()))

tests/e2e/common/node.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (ts *NodeTestSuite) RPCClient() rpclient.Client {
163163
return rpclient.NewHTTP(c.RPC.ListenAddress, "/websocket")
164164
}
165165

166+
func (ts *NodeTestSuite) GetCLIHomeDir() string {
167+
return ts.hmcliHomeDir
168+
}
169+
166170
type Env interface {
167171
String() string
168172
}

0 commit comments

Comments
 (0)