Skip to content

Commit

Permalink
Merge pull request #130 from unicornultrafoundation/test/prepare-inte…
Browse files Browse the repository at this point in the history
…gration-tests

Implement some basic integration tests
  • Loading branch information
trinhdn97 authored Dec 17, 2024
2 parents 1493910 + b261e11 commit bb0aa9e
Show file tree
Hide file tree
Showing 15 changed files with 782 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cmd/u2u/launcher/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package launcher

import (
"os"
)

func Run() error {
initApp()
initAppHelp()
return app.Run(os.Args)
}
6 changes: 5 additions & 1 deletion cmd/u2u/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func initFlags() {
}

// init the CLI app.
func init() {
func initApp() {
discfilter.Enable()
overrideFlags()
overrideParams()
Expand All @@ -189,6 +189,8 @@ func init() {

// App.

app = cli.NewApp()
app.Name = "u2u"
app.Action = heliosMain
app.Version = params.VersionWithCommit(gitCommit, gitDate)
app.HideVersion = true // we have a command to print the version
Expand Down Expand Up @@ -248,6 +250,8 @@ func init() {
}

func Launch(args []string) error {
initApp()
initAppHelp()
return app.Run(args)
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/u2u/launcher/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (tt *testcli) readConfig() {
func init() {
// Run the app if we've been exec'd as "u2u-test" in exec().
reexec.Register("u2u-test", func() {
initApp()
initAppHelp()
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/u2u/launcher/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func calcAppHelpFlagGroups() []flags.FlagGroup {
}
}

func init() {
func initAppHelp() {
// Override the default app help template
cli.AppHelpTemplate = flags.AppHelpTemplate

Expand Down
2 changes: 1 addition & 1 deletion cmd/u2u/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
if err := launcher.Launch(os.Args); err != nil {
if err := launcher.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down
20 changes: 20 additions & 0 deletions debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "net/http/pprof"
"os"
"runtime"
"sync"

"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
Expand Down Expand Up @@ -117,6 +118,25 @@ func init() {
// Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program.
func Setup(ctx *cli.Context) error {
setupMutex.Lock()
defer setupMutex.Unlock()
if setupDone {
return nil
}
err := setup(ctx)
if err != nil {
return err
}
setupDone = true
return nil
}

var (
setupMutex sync.Mutex
setupDone = false
)

func setup(ctx *cli.Context) error {
var ostream log.Handler
output := io.Writer(os.Stderr)
if ctx.GlobalBool(logjsonFlag.Name) {
Expand Down
22 changes: 22 additions & 0 deletions integrationtests/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package integrationtests

import (
"crypto/ecdsa"

"github.com/unicornultrafoundation/go-u2u/common"
"github.com/unicornultrafoundation/go-u2u/crypto"
)

type Account struct {
PrivateKey *ecdsa.PrivateKey
}

func NewAccount() *Account {
key, _ := crypto.GenerateKey()
return &Account{
PrivateKey: key,
}
}
func (a *Account) Address() common.Address {
return crypto.PubkeyToAddress(a.PrivateKey.PublicKey)
}
1 change: 1 addition & 0 deletions integrationtests/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
14 changes: 14 additions & 0 deletions integrationtests/contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Test Contracts
This directory contains a set of smart contracts used by integration tests.
Contracts are grouped in applications, which may each consist of multiple
contracts. Each application is retained in its own directory.
Within each application directory, the following files should be present:
- *.sol ... the Solidity code for the application
- gen.go ... a go file with a generator rule producing Go bindings for the app
- *.go ... the generated Go binding files (checked in in the code repo)
For an example application, see the `counter` directory.
## Code Generation Tools
For compiling Solidity code and generating the Go bindings the following tools
need to be installed on your system:
- the `solc` compiler; on Ubuntu you an install it using `sudo snap install solc --edge`
- the `abigen` tool; this can be installed using `go install github.com/unicornultrafoundation/go-u2u/cmd/abigen@latest`
216 changes: 216 additions & 0 deletions integrationtests/contracts/counter/counter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions integrationtests/contracts/counter/counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Counter {
int private count = 0;
function incrementCounter() public {
count += 1;
}
function getCount() public view returns (int) {
return count;
}
}
4 changes: 4 additions & 0 deletions integrationtests/contracts/counter/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package counter

//go:generate solc --bin counter.sol --abi counter.sol -o build --overwrite
//go:generate abigen --bin=build/Counter.bin --abi=build/Counter.abi --pkg=counter --out=counter.go
Loading

0 comments on commit bb0aa9e

Please sign in to comment.