Skip to content

Commit

Permalink
feat: add integration tests for IBC chains (#3820)
Browse files Browse the repository at this point in the history
* create a method to fill other methods

* add chain methods

* simplify te logic

* spin up both chains

* add reminder

* add relayers

* create configs into the code and add a helper to setup and run chains

* fix chain config

* fix rpc ports

* fix wrong urls and missing relayer accounts

* reset relayer config and avoid port conflicts

* test relayer

* fix config port address

* draft check realyer

* use hermes realyer instead ts relayer

* fix the default config values

* fix the config flag

* add missing tx.go file by default and enable cli if autocli not exist

* add changelog

* fix wrong error pkg

* add missing imports

* only scaffold `cli/tx.go` if is a ibc module

* move tx.go.plush to right place

* add comment to cobra send packet command

* add missing ibc interfaces to chain client

* set port range

* Revert "refactor(templates): add all ibc commands (#3858)"

This reverts commit 3dda9b0.

* fix changelog

* fix ibc.go app

* query channels

* check balances

* check ibc balance

* improve test cleanup

* fix chain home and config paths

* fix log typo

* cerate the chain path before use

* remove unused const

* decrease cleanup time

* setup the remote hermes app url

* use parser.ParseExpr instead parser.ParseExpr

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani authored Jan 31, 2024
1 parent 86ebb55 commit a74fb46
Show file tree
Hide file tree
Showing 9 changed files with 798 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [#3839](https://github.com/ignite/cli/pull/3839) New structure for app scaffolding
- [#3835](https://github.com/ignite/cli/pull/3835) Add `--minimal` flag to `scaffold chain` to scaffold a chain with the least amount of sdk modules
- [#3820](https://github.com/ignite/cli/pull/3820) Add integration tests for IBC chains

### Changes

Expand Down
59 changes: 59 additions & 0 deletions ignite/pkg/goanalysis/goanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,62 @@ func createUnderscoreImport(imp string) *ast.ImportSpec {
},
}
}

// ReplaceCode replace a function implementation into a package path. The method will find
// the method signature and re-write the method implementation based in the new function.
func ReplaceCode(pkgPath, oldFunctionName, newFunction string) (err error) {
absPath, err := filepath.Abs(pkgPath)
if err != nil {
return err
}

fileSet := token.NewFileSet()
all, err := parser.ParseDir(fileSet, absPath, func(os.FileInfo) bool { return true }, parser.ParseComments)
if err != nil {
return err
}

for _, pkg := range all {
for _, f := range pkg.Files {
found := false
ast.Inspect(f, func(n ast.Node) bool {
if funcDecl, ok := n.(*ast.FuncDecl); ok {
// Check if the function has the name you want to replace.
if funcDecl.Name.Name == oldFunctionName {
// Replace the function body with the replacement code.
replacementExpr, err := parser.ParseExpr(newFunction)
if err != nil {
return false
}
funcDecl.Body = &ast.BlockStmt{List: []ast.Stmt{
&ast.ExprStmt{X: replacementExpr},
}}
found = true
return false
}
}
return true
})
if err != nil {
return err
}
if !found {
continue
}
filePath := fileSet.Position(f.Package).Filename
outFile, err := os.Create(filePath)
if err != nil {
return err
}

// Format and write the modified AST to the output file.
if err := format.Node(outFile, fileSet, f); err != nil {
return err
}
if err := outFile.Close(); err != nil {
return err
}
}
}
return nil
}
74 changes: 74 additions & 0 deletions ignite/pkg/goanalysis/goanalysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,77 @@ func TestUpdateInitImports(t *testing.T) {
})
}
}

func TestReplaceCode(t *testing.T) {
var (
newFunction = `package test
func NewMethod1() {
n := "test new method"
bla := fmt.Sprintf("test new - %s", n)
fmt.Println(bla)
}`
rollback = `package test
func NewMethod1() {
foo := 100
bar := fmt.Sprintf("test - %d", foo)
fmt.Println(bar)
}`
)

type args struct {
path string
oldFunctionName string
newFunction string
}
tests := []struct {
name string
args args
err error
}{
{
name: "function fooTest",
args: args{
path: "testdata",
oldFunctionName: "fooTest",
newFunction: newFunction,
},
},
{
name: "function BazTest",
args: args{
path: "testdata",
oldFunctionName: "BazTest",
newFunction: newFunction,
},
},
{
name: "function invalidFunction",
args: args{
path: "testdata",
oldFunctionName: "invalidFunction",
newFunction: newFunction,
},
},
{
name: "invalid path",
args: args{
path: "invalid_path",
oldFunctionName: "invalidPath",
newFunction: newFunction,
},
err: os.ErrNotExist,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := goanalysis.ReplaceCode(tt.args.path, tt.args.oldFunctionName, tt.args.newFunction)
if tt.err != nil {
require.Error(t, err)
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
require.NoError(t, goanalysis.ReplaceCode(tt.args.path, tt.args.oldFunctionName, rollback))
})
}
}
16 changes: 16 additions & 0 deletions ignite/pkg/goanalysis/testdata/replace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package testdata

import "fmt"

func fooTest() {
n := "test new method"
bla := fmt.Sprintf("test new - %s", n)
fmt.
Println(bla)
}

func BazTest() {
foo := 100
bar := fmt.Sprintf("test - %d", foo)
fmt.Println(bar)
}
5 changes: 5 additions & 0 deletions ignite/pkg/goenv/goenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ func GoModCache() string {
}
return filepath.Join(build.Default.GOPATH, modDir)
}

// GoPath returns the go path.
func GoPath() string {
return os.Getenv(GOPATH)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
ibccmd "github.com/cosmos/ibc-go/v8/modules/core/client/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -87,6 +88,7 @@ func queryCommand() *cobra.Command {
server.QueryBlocksCmd(),
authcmd.QueryTxCmd(),
server.QueryBlockResultsCmd(),
ibccmd.GetQueryCmd(),
)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

Expand All @@ -113,6 +115,7 @@ func txCommand() *cobra.Command {
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
authcmd.GetSimulateCmd(),
ibccmd.GetTxCmd(),
)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var _ = strconv.Itoa(0)

// CmdSend<%= packetName.UpperCamel %>() returns the <%= packetName.UpperCamel %> send packet command.
// This command does not use AutoCLI because it gives a better UX to do not.
func CmdSend<%= packetName.UpperCamel %>() *cobra.Command {
flagPacketTimeoutTimestamp := "packet-timeout-timestamp"
Expand Down
4 changes: 4 additions & 0 deletions integration/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (a App) SourcePath() string {
return a.path
}

func (a *App) SetHomePath(homePath string) {
a.homePath = homePath
}

func (a *App) SetConfigPath(path string) {
a.configPath = path
}
Expand Down
Loading

0 comments on commit a74fb46

Please sign in to comment.