Skip to content

Commit

Permalink
fix: txtar tests load full config and execute serially (#1342)
Browse files Browse the repository at this point in the history
This fixes an issue where executing txtar tests would fail because the
example packages weren't loaded into memory.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Signed-off-by: gfanton <[email protected]>
Co-authored-by: gfanton <[email protected]>
  • Loading branch information
deelawn and gfanton authored Nov 8, 2023
1 parent 041d0a8 commit c33c9f2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 28 deletions.
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 @@ func NewInMemoryNode(logger log.Logger, cfg *InMemoryNodeConfig) (*node.Node, er
)

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

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

// generate p2p node identity
// XXX: do we need to configur
Expand All @@ -141,7 +141,7 @@ func NewInMemoryNode(logger log.Logger, cfg *InMemoryNodeConfig) (*node.Node, er
cfg.PrivValidator, nodekey,
appClientCreator,
genProvider,
node.DefaultDBProvider,
dbProvider,
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

0 comments on commit c33c9f2

Please sign in to comment.