Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: txtar tests load full config and execute serially #1342

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions gno.land/cmd/gnoland/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"testing"

"github.com/gnolang/gno/gno.land/pkg/integration"
"github.com/rogpeppe/go-internal/testscript"
)

func TestTestdata(t *testing.T) {
testscript.Run(t, integration.SetupGnolandTestScript(t, "testdata"))
integration.RunGnolandTestscripts(t, "testdata")
}
27 changes: 27 additions & 0 deletions gno.land/cmd/gnoland/testdata/import.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# test that the example packages directory is loaded and usable.

## start a new node
gnoland start

gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/importtest -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

## execute Render
gnokey maketx call -pkgpath gno.land/r/importtest -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
stdout '("92054" string)'
stdout OK!

-- gno.mod --
module gno.land/r/importtest

-- import.gno --
package importtest

import (
"gno.land/p/demo/ufmt"
)

func Render(_ string) string {
return ufmt.Sprintf("%d", 92054)
}

8 changes: 4 additions & 4 deletions gno.land/pkg/gnoland/node_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@
)

// Create genesis factory
genProvider := func() (*bft.GenesisDoc, error) {
return cfg.Genesis, nil
}
genProvider := func() (*bft.GenesisDoc, error) { return cfg.Genesis, nil }

Check warning on line 131 in gno.land/pkg/gnoland/node_inmemory.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/node_inmemory.go#L131

Added line #L131 was not covered by tests

dbProvider := func(*node.DBContext) (db.DB, error) { return db.NewMemDB(), nil }

Check warning on line 133 in gno.land/pkg/gnoland/node_inmemory.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/node_inmemory.go#L133

Added line #L133 was not covered by tests

// generate p2p node identity
// XXX: do we need to configur
Expand All @@ -141,7 +141,7 @@
cfg.PrivValidator, nodekey,
appClientCreator,
genProvider,
node.DefaultDBProvider,
dbProvider,

Check warning on line 144 in gno.land/pkg/gnoland/node_inmemory.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoland/node_inmemory.go#L144

Added line #L144 was not covered by tests
logger,
)
}
4 changes: 1 addition & 3 deletions gno.land/pkg/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package integration

import (
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func TestTestdata(t *testing.T) {
testscript.Run(t, SetupGnolandTestScript(t, "testdata"))
RunGnolandTestscripts(t, "testdata")
}
53 changes: 34 additions & 19 deletions gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"testing"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
Expand All @@ -20,21 +19,44 @@ import (
"github.com/rogpeppe/go-internal/testscript"
)

type tSeqShim struct{ *testing.T }

// noop Parallel method allow us to run test sequentially
func (tSeqShim) Parallel() {}

func (t tSeqShim) Run(name string, f func(testscript.T)) {
t.T.Run(name, func(t *testing.T) {
f(tSeqShim{t})
})
}

func (t tSeqShim) Verbose() bool {
return testing.Verbose()
}

// RunGnolandTestscripts sets up and runs txtar integration tests for gnoland nodes.
// It prepares an in-memory gnoland node and initializes the necessary environment and custom commands.
// The function adapts the test setup for use with the testscript package, enabling
// the execution of gnoland and gnokey commands within txtar scripts.
//
// Refer to package documentation in doc.go for more information on commands and example txtar scripts.
func RunGnolandTestscripts(t *testing.T, txtarDir string) {
t.Helper()

p := setupGnolandTestScript(t, txtarDir)
if deadline, ok := t.Deadline(); ok && p.Deadline.IsZero() {
p.Deadline = deadline
}

testscript.RunT(tSeqShim{t}, p)
}

type testNode struct {
*node.Node
nGnoKeyExec uint // Counter for execution of gnokey.
}

// SetupGnolandTestScript prepares the test environment to execute txtar tests
// using a partial InMemory gnoland node. It initializes key storage, sets up the gnoland node,
// and provides custom commands like "gnoland" and "gnokey" for txtar script execution.
//
// The function returns testscript.Params which contain the test setup and command
// executions to be used with the testscript package.
//
// For a detailed explanation of the commands and their behaviors, as well as
// example txtar scripts, refer to the package documentation in doc.go.
func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
t.Helper()

tmpdir := t.TempDir()
Expand All @@ -47,7 +69,6 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
gnoHomeDir := filepath.Join(tmpdir, "gno")

// Testscripts run concurrently by default, so we need to be prepared for that.
var muNodes sync.Mutex
nodes := map[string]*testNode{}

updateScripts, _ := strconv.ParseBool(os.Getenv("UPDATE_SCRIPTS"))
Expand Down Expand Up @@ -99,9 +120,6 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"gnoland": func(ts *testscript.TestScript, neg bool, args []string) {
muNodes.Lock()
defer muNodes.Unlock()

if len(args) == 0 {
tsValidateError(ts, "gnoland", neg, fmt.Errorf("syntax: gnoland [start|stop]"))
return
Expand All @@ -125,7 +143,7 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
t := TSTestingT(ts)

// Generate config and node
cfg := TestingMinimalNodeConfig(t, gnoRootDir)
cfg, _ := TestingNodeConfig(t, gnoRootDir)
n, remoteAddr := TestingInMemoryNode(t, logger, cfg)

// Register cleanup
Expand Down Expand Up @@ -156,9 +174,6 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
tsValidateError(ts, "gnoland "+cmd, neg, err)
},
"gnokey": func(ts *testscript.TestScript, neg bool, args []string) {
muNodes.Lock()
defer muNodes.Unlock()

logger := ts.Value("_logger").(log.Logger) // grab logger
sid := ts.Getenv("SID") // grab session id

Expand Down
Loading