From 8cb7d39941d0ed684aebb9b393df655dfe125524 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:19:53 +0100 Subject: [PATCH 01/21] feat(txtar): add `use` command Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/gnoland/genesis.go | 46 ++++--- gno.land/pkg/integration/doc.go | 12 ++ .../pkg/integration/testdata/use_both.txtar | 23 ++++ .../integration/testdata/use_example.txtar | 28 +++++ .../pkg/integration/testdata/use_work.txtar | 27 +++++ .../pkg/integration/testing_integration.go | 113 ++++++++++++++---- 6 files changed, 211 insertions(+), 38 deletions(-) create mode 100644 gno.land/pkg/integration/testdata/use_both.txtar create mode 100644 gno.land/pkg/integration/testdata/use_example.txtar create mode 100644 gno.land/pkg/integration/testdata/use_work.txtar diff --git a/gno.land/pkg/gnoland/genesis.go b/gno.land/pkg/gnoland/genesis.go index 8f2fa0debf6..fb761914587 100644 --- a/gno.land/pkg/gnoland/genesis.go +++ b/gno.land/pkg/gnoland/genesis.go @@ -93,6 +93,7 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee, deposit s return nil, fmt.Errorf("listing gno packages: %w", err) } + pkgs.Sort() // Sort packages by dependencies. sortedPkgs, err := pkgs.Sort() if err != nil { @@ -103,27 +104,38 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee, deposit s nonDraftPkgs := sortedPkgs.GetNonDraftPkgs() txs := []std.Tx{} for _, pkg := range nonDraftPkgs { - // Open files in directory as MemPackage. - memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name) - if err := memPkg.Validate(); err != nil { - return nil, fmt.Errorf("invalid package: %w", err) - } - - // Create transaction - tx := std.Tx{ - Fee: fee, - Msgs: []std.Msg{ - vmm.MsgAddPackage{ - Creator: creator, - Package: memPkg, - Deposit: deposit, - }, - }, + tx, err := LoadPackage(pkg, creator, fee, deposit) + if err != nil { + return nil, fmt.Errorf("unable to load package %q: %w", pkg.Dir, err) } - tx.Signatures = make([]std.Signature, len(tx.GetSigners())) txs = append(txs, tx) } return txs, nil } + +// LoadPackage load a single package into a tx +func LoadPackage(pkg gnomod.Pkg, creator bft.Address, fee std.Fee, deposit std.Coins) (std.Tx, error) { + var tx std.Tx + + // Open files in directory as MemPackage. + memPkg := gno.ReadMemPackage(pkg.Dir, pkg.Name) + err := memPkg.Validate() + if err != nil { + return tx, fmt.Errorf("invalid package: %w", err) + } + + // Create transaction + tx.Fee = fee + tx.Msgs = []std.Msg{ + vmm.MsgAddPackage{ + Creator: creator, + Package: memPkg, + Deposit: deposit, + }, + } + tx.Signatures = make([]std.Signature, len(tx.GetSigners())) + + return tx, nil +} diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index fb0b0bdf7a0..a87a922da56 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -21,6 +21,18 @@ // - Creates a new user in the default keybase directory. // - Must be run before `gnoland start`. // +// 4. `use`: +// - Loads a specific package from the example folder or from the working ($WORK) directory. +// - Can be used to load a single package or all packages within a directory. +// - The command takes either one or two arguments. The first argument is the path to the package(s), +// and the second (optional) argument is the name of the package. +// Examples: +// - # Load a package from the example packages directory: +// use gno.land/p/demo/ufmt +// - # Load a package from the current working directory with the name `gno.land/r/foobar/bar`: +// use $WORK/bar gno.land/r/foobar/bar +// - If the path is not prefixed with the working directory, it is assumed to be relative to the examples directory. + // Logging: // // Gnoland logs aren't forwarded to stdout to avoid overwhelming the tests with too much diff --git a/gno.land/pkg/integration/testdata/use_both.txtar b/gno.land/pkg/integration/testdata/use_both.txtar new file mode 100644 index 00000000000..19a779319d7 --- /dev/null +++ b/gno.land/pkg/integration/testdata/use_both.txtar @@ -0,0 +1,23 @@ +# load a package from the example packages directory. +use gno.land/p/demo/ufmt +use $WORK gno.land/r/importtest + +## start a new node +gnoland start + +## 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! + +-- import.gno -- +package importtest + +import ( + "gno.land/p/demo/ufmt" +) + +func Render(_ string) string { + return ufmt.Sprintf("%d", 92054) +} + diff --git a/gno.land/pkg/integration/testdata/use_example.txtar b/gno.land/pkg/integration/testdata/use_example.txtar new file mode 100644 index 00000000000..87c7360af61 --- /dev/null +++ b/gno.land/pkg/integration/testdata/use_example.txtar @@ -0,0 +1,28 @@ +# load a package from the example packages directory. +use gno.land/p/demo/ufmt + +## 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) +} + diff --git a/gno.land/pkg/integration/testdata/use_work.txtar b/gno.land/pkg/integration/testdata/use_work.txtar new file mode 100644 index 00000000000..0a537372b7f --- /dev/null +++ b/gno.land/pkg/integration/testdata/use_work.txtar @@ -0,0 +1,27 @@ +use $WORK/bar gno.land/r/foobar/bar + +## start a new node +gnoland start + +## execute Render +gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script/script.gno + +## compare render +stdout 'main: --- hello from foo ---' +stdout 'OK!' +stdout 'GAS WANTED: 200000' +stdout 'GAS USED: ' + +-- bar/bar.gno -- +package bar + +func Render(path string) string { + return "hello from foo" +} + +-- script/script.gno -- +package main +import "gno.land/r/foobar/bar" +func main() { + println("main: ---", bar.Render(""), "---") +} diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index b0dcc5737e6..f284f1edd77 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -17,8 +17,10 @@ import ( "github.com/gnolang/gno/gno.land/pkg/keyscli" "github.com/gnolang/gno/gno.land/pkg/log" "github.com/gnolang/gno/gnovm/pkg/gnoenv" + "github.com/gnolang/gno/gnovm/pkg/gnomod" "github.com/gnolang/gno/tm2/pkg/bft/node" "github.com/gnolang/gno/tm2/pkg/commands" + "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/crypto/bip39" "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/tm2/pkg/crypto/keys/client" @@ -28,6 +30,11 @@ import ( "go.uber.org/zap/zapcore" ) +const ( + envKeyGenesis int = iota + envKeyLogger +) + const numTestAccounts int = 4 type tSeqShim struct{ *testing.T } @@ -82,9 +89,10 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // Testscripts run concurrently by default, so we need to be prepared for that. nodes := map[string]*testNode{} - // Track new user balances added via the `adduser` command. These are added to the genesis - // state when the node is started. - var newUserBalances []gnoland.Balance + // type packagesTxs struct { + // []gnoland.Balance + // [] + // } updateScripts, _ := strconv.ParseBool(os.Getenv("UPDATE_SCRIPTS")) persistWorkDir, _ := strconv.ParseBool(os.Getenv("TESTWORK")) @@ -119,7 +127,15 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { } } - env.Values["_logger"] = logger + env.Values[envKeyLogger] = logger + } + + // Track new user balances added via the `adduser` + // command and packages added with the `use` commnand. + // This genesis will be use when node is started. + genesis := &gnoland.GnoGenesisState{ + Balances: LoadDefaultGenesisBalanceFile(t, gnoRootDir), + Txs: []std.Tx{}, } // test1 must be created outside of the loop below because it is already included in genesis so @@ -138,9 +154,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { return fmt.Errorf("error creating account %s: %w", accountName, err) } - newUserBalances = append(newUserBalances, balance) + genesis.Balances = append(genesis.Balances, balance) } - + env.Values[envKeyGenesis] = genesis env.Setenv("GNOROOT", gnoRootDir) env.Setenv("GNOHOME", gnoHomeDir) @@ -153,8 +169,8 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { return } - logger := ts.Value("_logger").(*slog.Logger) // grab logger - sid := getNodeSID(ts) // grab session id + logger := ts.Value(envKeyLogger).(*slog.Logger) // grab logger + sid := getNodeSID(ts) // grab session id var cmd string cmd, args = args[0], args[1:] @@ -167,20 +183,17 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { break } + // get pacakges + // Warp up `ts` so we can pass it to other testing method t := TSTestingT(ts) // Generate config and node - cfg, _ := TestingNodeConfig(t, gnoRootDir) + cfg := TestingMinimalNodeConfig(t, gnoRootDir) + genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) - // Add balances for users added via the `adduser` command. - genesis := cfg.Genesis - genesisState := gnoland.GnoGenesisState{ - Balances: genesis.AppState.(gnoland.GnoGenesisState).Balances, - Txs: genesis.AppState.(gnoland.GnoGenesisState).Txs, - } - genesisState.Balances = append(genesisState.Balances, newUserBalances...) - cfg.Genesis.AppState = genesisState + // setup genesis state + cfg.Genesis.AppState = *genesis n, remoteAddr := TestingInMemoryNode(t, logger, cfg) @@ -197,7 +210,6 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { err = fmt.Errorf("node not started cannot be stopped") break } - if err = n.Stop(); err == nil { delete(nodes, sid) @@ -212,8 +224,8 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { tsValidateError(ts, "gnoland "+cmd, neg, err) }, "gnokey": func(ts *testscript.TestScript, neg bool, args []string) { - logger := ts.Value("_logger").(*slog.Logger) // grab logger - sid := ts.Getenv("SID") // grab session id + logger := ts.Value(envKeyLogger).(*slog.Logger) // grab logger + sid := ts.Getenv("SID") // grab session id // Setup IO command io := commands.NewTestIO() @@ -270,7 +282,66 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { ts.Fatalf("error creating account %s: %s", args[0], err) } - newUserBalances = append(newUserBalances, balance) + // Add balance to genesis + genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) + genesis.Balances = append(genesis.Balances, balance) + }, + // use load a specific packagae from the example folder or from the working directory + "use": func(ts *testscript.TestScript, neg bool, args []string) { + creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 + defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) + + // special dirs + workDir := ts.Getenv("WORK") + examplesDir := filepath.Join(gnoRootDir, "examples") + + var txs []std.Tx + switch len(args) { + case 1: + var path string + if path == "all" { + // if `all` is specified fully load example folder + path = examplesDir + } else { + path = filepath.Clean(args[0]) + } + + if !strings.HasPrefix(path, workDir) { + path = filepath.Join(examplesDir, path) + } + + var err error + txs, err = gnoland.LoadPackagesFromDir(path, creator, defaultFee, nil) + if err != nil { + ts.Fatalf("`use` unable to load package(s) %s: %s", path, err) + } + case 2: + path := filepath.Clean(args[0]) + if !strings.HasPrefix(path, workDir) { + path = filepath.Join(examplesDir, path) + } + + pkg := gnomod.Pkg{ + Dir: path, + Name: filepath.Clean(args[1]), + Draft: false, + Requires: []string{}, + } + tx, err := gnoland.LoadPackage(pkg, creator, defaultFee, nil) + if err != nil { + ts.Fatalf("`use` unable to load package(s) %s: %s", path, err) + } + txs = []std.Tx{tx} + case 0: + ts.Fatalf("`use`: no arguements specified") + default: + ts.Fatalf("`use`: too many arguements specified") + } + + genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) + // Add packages txs to genesis + genesis.Txs = append(genesis.Txs, txs...) + ts.Logf("%s package was added to genesis", args[0]) }, }, } From 7d15c1c3422a391bebf626006455bf9571d301d0 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:32:52 +0100 Subject: [PATCH 02/21] fix: fixup existing txtar Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/cmd/gnoland/testdata/append.txtar | 2 ++ gno.land/cmd/gnoland/testdata/import.txtar | 1 + gno.land/cmd/gnoland/testdata/issue-1167.txtar | 1 + gno.land/cmd/gnoland/testdata/wugnot.txtar | 2 ++ gno.land/pkg/integration/testing_integration.go | 9 ++++----- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/append.txtar b/gno.land/cmd/gnoland/testdata/append.txtar index 792d71882e5..0669319075f 100644 --- a/gno.land/cmd/gnoland/testdata/append.txtar +++ b/gno.land/cmd/gnoland/testdata/append.txtar @@ -1,3 +1,5 @@ +use gno.land/p/demo/ufmt + # start a new node gnoland start diff --git a/gno.land/cmd/gnoland/testdata/import.txtar b/gno.land/cmd/gnoland/testdata/import.txtar index 3ad070654cf..641409355d3 100644 --- a/gno.land/cmd/gnoland/testdata/import.txtar +++ b/gno.land/cmd/gnoland/testdata/import.txtar @@ -1,4 +1,5 @@ # test that the example packages directory is loaded and usable. +use gno.land/p/demo/ufmt ## start a new node gnoland start diff --git a/gno.land/cmd/gnoland/testdata/issue-1167.txtar b/gno.land/cmd/gnoland/testdata/issue-1167.txtar index a24cca5dce9..8a53c65f1fe 100644 --- a/gno.land/cmd/gnoland/testdata/issue-1167.txtar +++ b/gno.land/cmd/gnoland/testdata/issue-1167.txtar @@ -1,4 +1,5 @@ # Reproducible Test for https://github.com/gnolang/gno/issues/1167 +use gno.land/p/demo/avl gnoland start diff --git a/gno.land/cmd/gnoland/testdata/wugnot.txtar b/gno.land/cmd/gnoland/testdata/wugnot.txtar index 5c2d7d3cb90..0f1b2cf71df 100644 --- a/gno.land/cmd/gnoland/testdata/wugnot.txtar +++ b/gno.land/cmd/gnoland/testdata/wugnot.txtar @@ -1,3 +1,5 @@ +use all + gnoland start gnokey maketx call -pkgpath gno.land/r/demo/wugnot -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index f284f1edd77..3983b44877c 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -299,15 +299,14 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { switch len(args) { case 1: var path string - if path == "all" { + if args[0] == "all" { // if `all` is specified fully load example folder path = examplesDir } else { path = filepath.Clean(args[0]) - } - - if !strings.HasPrefix(path, workDir) { - path = filepath.Join(examplesDir, path) + if !strings.HasPrefix(path, workDir) { + path = filepath.Join(examplesDir, path) + } } var err error From ea904c7c6dd577524b4be8a9877ac0078843765c Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:46:11 +0100 Subject: [PATCH 03/21] chore: lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 3983b44877c..a75baff1a6e 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -332,9 +332,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { } txs = []std.Tx{tx} case 0: - ts.Fatalf("`use`: no arguements specified") + ts.Fatalf("`use`: no arguments specified") default: - ts.Fatalf("`use`: too many arguements specified") + ts.Fatalf("`use`: too many arguments specified") } genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) From bac62c4ea2b023c953340051e4fff1be393f58c2 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:11:55 +0100 Subject: [PATCH 04/21] fix: correctly load subdependency Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/cmd/gnoland/testdata/addpkg.txtar | 2 +- .../cmd/gnoland/testdata/grc20-registry.txtar | 5 +- gno.land/cmd/gnoland/testdata/run.txtar | 5 +- gno.land/cmd/gnoland/testdata/wugnot.txtar | 2 +- gno.land/pkg/gnoland/genesis.go | 1 - .../integration}/testdata/import.txtar | 4 +- .../pkg/integration/testing_integration.go | 206 +++++++++++++----- 7 files changed, 154 insertions(+), 71 deletions(-) rename gno.land/{cmd/gnoland => pkg/integration}/testdata/import.txtar (76%) diff --git a/gno.land/cmd/gnoland/testdata/addpkg.txtar b/gno.land/cmd/gnoland/testdata/addpkg.txtar index 7130fe54dab..e7437552b50 100644 --- a/gno.land/cmd/gnoland/testdata/addpkg.txtar +++ b/gno.land/cmd/gnoland/testdata/addpkg.txtar @@ -20,4 +20,4 @@ package bar func Render(path string) string { return "hello from foo" -} \ No newline at end of file +} diff --git a/gno.land/cmd/gnoland/testdata/grc20-registry.txtar b/gno.land/cmd/gnoland/testdata/grc20-registry.txtar index ad1b6b9b823..226c8907872 100644 --- a/gno.land/cmd/gnoland/testdata/grc20-registry.txtar +++ b/gno.land/cmd/gnoland/testdata/grc20-registry.txtar @@ -1,11 +1,10 @@ # example for contract-contract interaction with ownership +# add registry +use gno.land/r/registry $WORK/registry ## start a new node gnoland start -# add registry -gnokey maketx addpkg -pkgdir $WORK/registry -pkgpath gno.land/r/registry -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 - # we call Transfer with foo20, before it's registered gnokey maketx call -pkgpath gno.land/r/registry -func TransferByName -args 'foo20' -args 'g123456789' -args '42' -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 stdout 'not found' diff --git a/gno.land/cmd/gnoland/testdata/run.txtar b/gno.land/cmd/gnoland/testdata/run.txtar index 94b32de041e..f3218b52703 100644 --- a/gno.land/cmd/gnoland/testdata/run.txtar +++ b/gno.land/cmd/gnoland/testdata/run.txtar @@ -1,9 +1,8 @@ +use gno.land/r/foobar/bar $WORK/bar + ## start a new node gnoland start -## add bar.gno package located in $WORK directory as gno.land/r/foobar/bar -gnokey maketx addpkg -pkgdir $WORK/bar -pkgpath gno.land/r/foobar/bar -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 - ## execute Render gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script/script.gno diff --git a/gno.land/cmd/gnoland/testdata/wugnot.txtar b/gno.land/cmd/gnoland/testdata/wugnot.txtar index 0f1b2cf71df..5fcf46c0f03 100644 --- a/gno.land/cmd/gnoland/testdata/wugnot.txtar +++ b/gno.land/cmd/gnoland/testdata/wugnot.txtar @@ -1,4 +1,4 @@ -use all +use gno.land/r/demo/wugnot gnoland start diff --git a/gno.land/pkg/gnoland/genesis.go b/gno.land/pkg/gnoland/genesis.go index fb761914587..cb103a1bff8 100644 --- a/gno.land/pkg/gnoland/genesis.go +++ b/gno.land/pkg/gnoland/genesis.go @@ -93,7 +93,6 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee, deposit s return nil, fmt.Errorf("listing gno packages: %w", err) } - pkgs.Sort() // Sort packages by dependencies. sortedPkgs, err := pkgs.Sort() if err != nil { diff --git a/gno.land/cmd/gnoland/testdata/import.txtar b/gno.land/pkg/integration/testdata/import.txtar similarity index 76% rename from gno.land/cmd/gnoland/testdata/import.txtar rename to gno.land/pkg/integration/testdata/import.txtar index 641409355d3..0c7d0e02828 100644 --- a/gno.land/cmd/gnoland/testdata/import.txtar +++ b/gno.land/pkg/integration/testdata/import.txtar @@ -1,12 +1,10 @@ # test that the example packages directory is loaded and usable. use gno.land/p/demo/ufmt +use $WORK ## 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)' diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index a75baff1a6e..370c627c9a8 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -19,6 +19,7 @@ import ( "github.com/gnolang/gno/gnovm/pkg/gnoenv" "github.com/gnolang/gno/gnovm/pkg/gnomod" "github.com/gnolang/gno/tm2/pkg/bft/node" + bft "github.com/gnolang/gno/tm2/pkg/bft/types" "github.com/gnolang/gno/tm2/pkg/commands" "github.com/gnolang/gno/tm2/pkg/crypto" "github.com/gnolang/gno/tm2/pkg/crypto/bip39" @@ -33,6 +34,7 @@ import ( const ( envKeyGenesis int = iota envKeyLogger + envKeyPkgs ) const numTestAccounts int = 4 @@ -89,11 +91,6 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // Testscripts run concurrently by default, so we need to be prepared for that. nodes := map[string]*testNode{} - // type packagesTxs struct { - // []gnoland.Balance - // [] - // } - updateScripts, _ := strconv.ParseBool(os.Getenv("UPDATE_SCRIPTS")) persistWorkDir, _ := strconv.ParseBool(os.Getenv("TESTWORK")) return testscript.Params{ @@ -145,18 +142,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { env.Setenv("USER_SEED_"+DefaultAccount_Name, DefaultAccount_Seed) env.Setenv("USER_ADDR_"+DefaultAccount_Name, DefaultAccount_Address) - // Create test accounts starting from test2. - for i := 1; i < numTestAccounts; i++ { - accountName := "test" + strconv.Itoa(i+1) - - balance, err := createAccount(env, kb, accountName) - if err != nil { - return fmt.Errorf("error creating account %s: %w", accountName, err) - } - - genesis.Balances = append(genesis.Balances, balance) - } env.Values[envKeyGenesis] = genesis + env.Values[envKeyPkgs] = pkgsLoader{} + env.Setenv("GNOROOT", gnoRootDir) env.Setenv("GNOHOME", gnoHomeDir) @@ -184,6 +172,13 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { } // get pacakges + pkgs := ts.Value(envKeyPkgs).(pkgsLoader) // grab logger + creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 + defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) + pkgsTxs, err := pkgs.Txs(creator, defaultFee, nil) + if err != nil { + ts.Fatalf("unable to load packages txs: %s", err) + } // Warp up `ts` so we can pass it to other testing method t := TSTestingT(ts) @@ -191,6 +186,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // Generate config and node cfg := TestingMinimalNodeConfig(t, gnoRootDir) genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) + genesis.Txs = append(pkgsTxs, genesis.Txs...) // setup genesis state cfg.Genesis.AppState = *genesis @@ -286,61 +282,50 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) genesis.Balances = append(genesis.Balances, balance) }, - // use load a specific packagae from the example folder or from the working directory + // use load a specific package from the example folder or from the working directory "use": func(ts *testscript.TestScript, neg bool, args []string) { - creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 - defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) - // special dirs workDir := ts.Getenv("WORK") examplesDir := filepath.Join(gnoRootDir, "examples") - var txs []std.Tx - switch len(args) { - case 1: - var path string - if args[0] == "all" { - // if `all` is specified fully load example folder - path = examplesDir - } else { - path = filepath.Clean(args[0]) - if !strings.HasPrefix(path, workDir) { - path = filepath.Join(examplesDir, path) - } - } + pkgs := ts.Value(envKeyPkgs).(pkgsLoader) - var err error - txs, err = gnoland.LoadPackagesFromDir(path, creator, defaultFee, nil) - if err != nil { - ts.Fatalf("`use` unable to load package(s) %s: %s", path, err) - } + var path, name string + switch len(args) { case 2: - path := filepath.Clean(args[0]) - if !strings.HasPrefix(path, workDir) { - path = filepath.Join(examplesDir, path) - } - - pkg := gnomod.Pkg{ - Dir: path, - Name: filepath.Clean(args[1]), - Draft: false, - Requires: []string{}, - } - tx, err := gnoland.LoadPackage(pkg, creator, defaultFee, nil) - if err != nil { - ts.Fatalf("`use` unable to load package(s) %s: %s", path, err) - } - txs = []std.Tx{tx} + name = args[0] + path = filepath.Clean(args[1]) + case 1: + path = filepath.Clean(args[0]) case 0: ts.Fatalf("`use`: no arguments specified") default: ts.Fatalf("`use`: too many arguments specified") } - genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) - // Add packages txs to genesis - genesis.Txs = append(genesis.Txs, txs...) - ts.Logf("%s package was added to genesis", args[0]) + // if `all` is specified fully load example folder + // NOTE: in 99% of cases, this is not needed and + // packages should be load individually + if path == "all" { + ts.Logf("warning: loading all packages") + err := pkgs.loadPackagesFromDir(examplesDir) + if err != nil { + ts.Fatalf("unable to load packages from %q: %s", examplesDir, err) + } + + return + } + + if !strings.HasPrefix(path, workDir) { + path = filepath.Join(examplesDir, path) + } + + err := pkgs.loadPackages(examplesDir, path, name) + if err != nil { + ts.Fatalf("`use` unable to load package(s) from %q: %s", args[0], err) + } + + ts.Logf("%q package was added to genesis", args[0]) }, }, } @@ -439,6 +424,109 @@ func createAccount(env envSetter, kb keys.Keybase, accountName string) (gnoland. return gnoland.Balance{ Address: address, - Amount: std.Coins{std.NewCoin("ugnot", 1000000000000000000)}, + Amount: std.Coins{std.NewCoin("ugnot", 10e6)}, }, nil } + +type pkgsLoader map[string]gnomod.Pkg + +func (pkgs pkgsLoader) list() gnomod.PkgList { + list := make([]gnomod.Pkg, 0, len(pkgs)) + for _, pkg := range pkgs { + list = append(list, pkg) + } + return list +} + +func (pkgs pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { + pkgslist, err := pkgs.list().Sort() + if err != nil { + return nil, fmt.Errorf("uanble to sort packages: %w", err) + } + + txs := make([]std.Tx, len(pkgslist)) + for i, pkg := range pkgslist { + tx, err := gnoland.LoadPackage(pkg, creator, fee, deposit) + if err != nil { + return nil, fmt.Errorf("unable to load pkg %q: %w", pkg.Name, err) + } + txs[i] = tx + } + + return txs, nil +} + +func (pkgs pkgsLoader) loadPackagesFromDir(path string) error { + // list all packages from target path + pkgslist, err := gnomod.ListPkgs(path) + if err != nil { + return fmt.Errorf("listing gno packages: %w", err) + } + + for _, pkg := range pkgslist { + if _, ok := pkgs[pkg.Name]; !ok { + pkgs[pkg.Name] = pkg + } + } + + return nil +} + +func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { + if path == "" { + return fmt.Errorf("no path specified for package") + } + + pkg := gnomod.Pkg{ + Dir: path, + Name: name, + } + + if pkg.Name == "" { + // try to load `gno.mod` informations + gnoModPath := filepath.Join(pkg.Dir, "gno.mod") + data, err := os.ReadFile(gnoModPath) + if os.IsNotExist(err) { + return fmt.Errorf("no name specified /gnomod doesn't exist (%q)", gnoModPath) + } + + if err != nil { + return fmt.Errorf("unable to load gno.mod: %w", err) + } + + gnoMod, err := gnomod.Parse(gnoModPath, data) + if err != nil { + return fmt.Errorf("%q parse gnomod error: %w", gnoModPath, err) + } + + gnoMod.Sanitize() + if err := gnoMod.Validate(); err != nil { + return fmt.Errorf("validate %q error: %w", gnoModPath, err) + } + + // override pkg info with mod infos + pkg.Name = gnoMod.Module.Mod.Path + pkg.Draft = gnoMod.Draft + for _, req := range gnoMod.Require { + pkg.Requires = append(pkg.Requires, req.Mod.Path) + } + } + + if pkg.Draft { + return nil // skip draft package + } + + if _, ok := pkgs[pkg.Name]; ok { + return nil // we already know this pkg + } + pkgs[pkg.Name] = pkg + + for _, pkgpath := range pkg.Requires { + pkgpath := filepath.Join(modroot, pkgpath) + if err := pkgs.loadPackages(modroot, pkgpath, ""); err != nil { + return fmt.Errorf("require %q: %w", pkgpath, err) + } + } + + return nil +} From 5a9f10573b6608df2fdd753187c77d61e29c8d16 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:13:12 +0100 Subject: [PATCH 05/21] chore: remove default test2+ accounts Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 370c627c9a8..cb35d7ee8f9 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -37,8 +37,6 @@ const ( envKeyPkgs ) -const numTestAccounts int = 4 - type tSeqShim struct{ *testing.T } // noop Parallel method allow us to run test sequentially From 7d678ef1f3d711a3dc9a35344846a2622b09b1d7 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:14:51 +0100 Subject: [PATCH 06/21] chore: rename vars Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index cb35d7ee8f9..a87fde51044 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -34,7 +34,7 @@ import ( const ( envKeyGenesis int = iota envKeyLogger - envKeyPkgs + envKeyPkgsLoader ) type tSeqShim struct{ *testing.T } @@ -141,7 +141,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { env.Setenv("USER_ADDR_"+DefaultAccount_Name, DefaultAccount_Address) env.Values[envKeyGenesis] = genesis - env.Values[envKeyPkgs] = pkgsLoader{} + env.Values[envKeyPkgsLoader] = pkgsLoader{} env.Setenv("GNOROOT", gnoRootDir) env.Setenv("GNOHOME", gnoHomeDir) @@ -170,7 +170,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { } // get pacakges - pkgs := ts.Value(envKeyPkgs).(pkgsLoader) // grab logger + pkgs := ts.Value(envKeyPkgsLoader).(pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) pkgsTxs, err := pkgs.Txs(creator, defaultFee, nil) @@ -286,7 +286,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { workDir := ts.Getenv("WORK") examplesDir := filepath.Join(gnoRootDir, "examples") - pkgs := ts.Value(envKeyPkgs).(pkgsLoader) + pkgs := ts.Value(envKeyPkgsLoader).(pkgsLoader) var path, name string switch len(args) { From a2a3d12b976f2de7acfa0f46fee2af3b9fef6b9f Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:25:08 +0100 Subject: [PATCH 07/21] fix: update `use` doc Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/doc.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index a87a922da56..21833ccd979 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -24,15 +24,16 @@ // 4. `use`: // - Loads a specific package from the example folder or from the working ($WORK) directory. // - Can be used to load a single package or all packages within a directory. -// - The command takes either one or two arguments. The first argument is the path to the package(s), -// and the second (optional) argument is the name of the package. +// - The command takes either one or two arguments. The first argument is the name of the package(s), +// and the second (optional) argument is the path to the package(s). // Examples: -// - # Load a package from the example packages directory: -// use gno.land/p/demo/ufmt -// - # Load a package from the current working directory with the name `gno.land/r/foobar/bar`: -// use $WORK/bar gno.land/r/foobar/bar +// -- # Load a package from the example packages directory: +// -- use gno.land/p/demo/ufmt +// -- # Load a package `./bar` from the current testscript's working directory with the name `gno.land/r/foobar/bar`: +// -- use gno.land/r/foobar/bar $WORK/bar // - If the path is not prefixed with the working directory, it is assumed to be relative to the examples directory. - +// - The command should always be executed before `gnoland start` to ensure loading +// // Logging: // // Gnoland logs aren't forwarded to stdout to avoid overwhelming the tests with too much From 8d1848f67067aab5f5e0f3efb278e450d0ecb32a Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:36:06 +0100 Subject: [PATCH 08/21] chore: fix & lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- .../testdata/{use_both.txtar => use_example_and_work.txtar} | 2 +- gno.land/pkg/integration/testdata/use_work.txtar | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename gno.land/pkg/integration/testdata/{use_both.txtar => use_example_and_work.txtar} (93%) diff --git a/gno.land/pkg/integration/testdata/use_both.txtar b/gno.land/pkg/integration/testdata/use_example_and_work.txtar similarity index 93% rename from gno.land/pkg/integration/testdata/use_both.txtar rename to gno.land/pkg/integration/testdata/use_example_and_work.txtar index 19a779319d7..41e84609650 100644 --- a/gno.land/pkg/integration/testdata/use_both.txtar +++ b/gno.land/pkg/integration/testdata/use_example_and_work.txtar @@ -1,6 +1,6 @@ # load a package from the example packages directory. use gno.land/p/demo/ufmt -use $WORK gno.land/r/importtest +use gno.land/r/importtest $WORK ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testdata/use_work.txtar b/gno.land/pkg/integration/testdata/use_work.txtar index 0a537372b7f..c58d8d1727a 100644 --- a/gno.land/pkg/integration/testdata/use_work.txtar +++ b/gno.land/pkg/integration/testdata/use_work.txtar @@ -1,4 +1,4 @@ -use $WORK/bar gno.land/r/foobar/bar +use gno.land/r/foobar/bar $WORK/bar ## start a new node gnoland start From 07025a7ec11998fd5c30f93164462f707ad195e3 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:58:23 +0100 Subject: [PATCH 09/21] fix: keep order if outside example Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/doc.go | 4 +++- gno.land/pkg/integration/testing_integration.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index 21833ccd979..41d107c72b1 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -32,7 +32,9 @@ // -- # Load a package `./bar` from the current testscript's working directory with the name `gno.land/r/foobar/bar`: // -- use gno.land/r/foobar/bar $WORK/bar // - If the path is not prefixed with the working directory, it is assumed to be relative to the examples directory. -// - The command should always be executed before `gnoland start` to ensure loading +// - It's important to note that the load order is significant when using +// multiple use command; packages should be loaded in the order they are +// dependent upon. // // Logging: // diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index a87fde51044..fa7863f1cc3 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -508,12 +508,22 @@ func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { for _, req := range gnoMod.Require { pkg.Requires = append(pkg.Requires, req.Mod.Path) } + } if pkg.Draft { return nil // skip draft package } + // if no require pkgs are declared, use previous one to keep load order + // XXX: find a better way to achieve this + if !strings.HasPrefix(pkg.Dir, modroot) && len(pkg.Requires) == 0 { + pkg.Requires = make([]string, 0, len(pkgs)) + for name := range pkgs { + pkg.Requires = append(pkg.Requires, name) + } + } + if _, ok := pkgs[pkg.Name]; ok { return nil // we already know this pkg } From 42f603c7e65eb765fdbe7043ef95cba121fc15ee Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:31:16 +0100 Subject: [PATCH 10/21] chore: lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index fa7863f1cc3..e3d7cec0d42 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -508,7 +508,6 @@ func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { for _, req := range gnoMod.Require { pkg.Requires = append(pkg.Requires, req.Mod.Path) } - } if pkg.Draft { From 4866eb70ad56e1c765ddc5bac4e33dd880251b08 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:23:07 +0100 Subject: [PATCH 11/21] chore: lint & fixup Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/gnoland/genesis.go | 2 +- .../pkg/integration/testdata/use_work.txtar | 2 + .../pkg/integration/testing_integration.go | 37 ++++++------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/gno.land/pkg/gnoland/genesis.go b/gno.land/pkg/gnoland/genesis.go index cb103a1bff8..d99dfe7e5ed 100644 --- a/gno.land/pkg/gnoland/genesis.go +++ b/gno.land/pkg/gnoland/genesis.go @@ -114,7 +114,7 @@ func LoadPackagesFromDir(dir string, creator bft.Address, fee std.Fee, deposit s return txs, nil } -// LoadPackage load a single package into a tx +// LoadPackage loads a single package into a `std.Tx` func LoadPackage(pkg gnomod.Pkg, creator bft.Address, fee std.Fee, deposit std.Coins) (std.Tx, error) { var tx std.Tx diff --git a/gno.land/pkg/integration/testdata/use_work.txtar b/gno.land/pkg/integration/testdata/use_work.txtar index c58d8d1727a..e0ddd37af28 100644 --- a/gno.land/pkg/integration/testdata/use_work.txtar +++ b/gno.land/pkg/integration/testdata/use_work.txtar @@ -21,7 +21,9 @@ func Render(path string) string { -- script/script.gno -- package main + import "gno.land/r/foobar/bar" + func main() { println("main: ---", bar.Render(""), "---") } diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index e3d7cec0d42..b6a7e8bf045 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -280,7 +280,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) genesis.Balances = append(genesis.Balances, balance) }, - // use load a specific package from the example folder or from the working directory + // `use` load a specific package from the example folder or from the working directory "use": func(ts *testscript.TestScript, neg bool, args []string) { // special dirs workDir := ts.Getenv("WORK") @@ -301,9 +301,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { ts.Fatalf("`use`: too many arguments specified") } - // if `all` is specified fully load example folder - // NOTE: in 99% of cases, this is not needed and - // packages should be load individually + // If `all` is specified, fully load example folder. + // NOTE: In 99% of cases, this is not needed, and + // packages should be loaded individually. if path == "all" { ts.Logf("warning: loading all packages") err := pkgs.loadPackagesFromDir(examplesDir) @@ -318,8 +318,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { path = filepath.Join(examplesDir, path) } - err := pkgs.loadPackages(examplesDir, path, name) - if err != nil { + if err := pkgs.loadPackages(examplesDir, path, name); err != nil { ts.Fatalf("`use` unable to load package(s) from %q: %s", args[0], err) } @@ -439,7 +438,7 @@ func (pkgs pkgsLoader) list() gnomod.PkgList { func (pkgs pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { pkgslist, err := pkgs.list().Sort() if err != nil { - return nil, fmt.Errorf("uanble to sort packages: %w", err) + return nil, fmt.Errorf("unable to sort packages: %w", err) } txs := make([]std.Tx, len(pkgslist)) @@ -483,29 +482,17 @@ func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { if pkg.Name == "" { // try to load `gno.mod` informations gnoModPath := filepath.Join(pkg.Dir, "gno.mod") - data, err := os.ReadFile(gnoModPath) - if os.IsNotExist(err) { - return fmt.Errorf("no name specified /gnomod doesn't exist (%q)", gnoModPath) - } - + gm, err := gnomod.ParseGnoMod(gnoModPath) if err != nil { - return fmt.Errorf("unable to load gno.mod: %w", err) + return fmt.Errorf("unable to load %q: %w", gnoModPath, err) } - gnoMod, err := gnomod.Parse(gnoModPath, data) - if err != nil { - return fmt.Errorf("%q parse gnomod error: %w", gnoModPath, err) - } - - gnoMod.Sanitize() - if err := gnoMod.Validate(); err != nil { - return fmt.Errorf("validate %q error: %w", gnoModPath, err) - } + gm.Sanitize() // override pkg info with mod infos - pkg.Name = gnoMod.Module.Mod.Path - pkg.Draft = gnoMod.Draft - for _, req := range gnoMod.Require { + pkg.Name = gm.Module.Mod.Path + pkg.Draft = gm.Draft + for _, req := range gm.Require { pkg.Requires = append(pkg.Requires, req.Mod.Path) } } From 4ea469406e1d2a8c9a9e81169f99e5152f8743c5 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:28:39 +0100 Subject: [PATCH 12/21] fix: lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index b6a7e8bf045..f0a74558423 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -306,8 +306,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // packages should be loaded individually. if path == "all" { ts.Logf("warning: loading all packages") - err := pkgs.loadPackagesFromDir(examplesDir) - if err != nil { + if err := pkgs.loadPackagesFromDir(examplesDir); err != nil { ts.Fatalf("unable to load packages from %q: %s", examplesDir, err) } From a8f764d46c9afc7035f76836a95ab98ee9f3f4e1 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:52:09 +0100 Subject: [PATCH 13/21] fix(use): use ordered list for requirements Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- .../pkg/integration/testing_integration.go | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index f0a74558423..2589c05060d 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -141,7 +141,10 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { env.Setenv("USER_ADDR_"+DefaultAccount_Name, DefaultAccount_Address) env.Values[envKeyGenesis] = genesis - env.Values[envKeyPkgsLoader] = pkgsLoader{} + env.Values[envKeyPkgsLoader] = &pkgsLoader{ + pkgs: []gnomod.Pkg{}, + visited: map[string]struct{}{}, + } env.Setenv("GNOROOT", gnoRootDir) env.Setenv("GNOHOME", gnoHomeDir) @@ -170,7 +173,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { } // get pacakges - pkgs := ts.Value(envKeyPkgsLoader).(pkgsLoader) // grab logger + pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) pkgsTxs, err := pkgs.Txs(creator, defaultFee, nil) @@ -286,7 +289,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { workDir := ts.Getenv("WORK") examplesDir := filepath.Join(gnoRootDir, "examples") - pkgs := ts.Value(envKeyPkgsLoader).(pkgsLoader) + pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) var path, name string switch len(args) { @@ -424,17 +427,16 @@ func createAccount(env envSetter, kb keys.Keybase, accountName string) (gnoland. }, nil } -type pkgsLoader map[string]gnomod.Pkg +type pkgsLoader struct { + pkgs []gnomod.Pkg + visited map[string]struct{} +} -func (pkgs pkgsLoader) list() gnomod.PkgList { - list := make([]gnomod.Pkg, 0, len(pkgs)) - for _, pkg := range pkgs { - list = append(list, pkg) - } - return list +func (pl *pkgsLoader) list() gnomod.PkgList { + return pl.pkgs } -func (pkgs pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { +func (pkgs *pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { pkgslist, err := pkgs.list().Sort() if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) @@ -452,7 +454,17 @@ func (pkgs pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) return txs, nil } -func (pkgs pkgsLoader) loadPackagesFromDir(path string) error { +func (pl *pkgsLoader) add(pkg gnomod.Pkg) { + pl.visited[pkg.Name] = struct{}{} + pl.pkgs = append(pl.pkgs, pkg) +} + +func (pl *pkgsLoader) exist(pkg gnomod.Pkg) (ok bool) { + _, ok = pl.visited[pkg.Name] + return +} + +func (pl *pkgsLoader) loadPackagesFromDir(path string) error { // list all packages from target path pkgslist, err := gnomod.ListPkgs(path) if err != nil { @@ -460,15 +472,15 @@ func (pkgs pkgsLoader) loadPackagesFromDir(path string) error { } for _, pkg := range pkgslist { - if _, ok := pkgs[pkg.Name]; !ok { - pkgs[pkg.Name] = pkg + if !pl.exist(pkg) { + pl.add(pkg) } } return nil } -func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { +func (pl *pkgsLoader) loadPackages(modroot string, path, name string) error { if path == "" { return fmt.Errorf("no path specified for package") } @@ -500,23 +512,14 @@ func (pkgs pkgsLoader) loadPackages(modroot string, path, name string) error { return nil // skip draft package } - // if no require pkgs are declared, use previous one to keep load order - // XXX: find a better way to achieve this - if !strings.HasPrefix(pkg.Dir, modroot) && len(pkg.Requires) == 0 { - pkg.Requires = make([]string, 0, len(pkgs)) - for name := range pkgs { - pkg.Requires = append(pkg.Requires, name) - } - } - - if _, ok := pkgs[pkg.Name]; ok { - return nil // we already know this pkg + if pl.exist(pkg) { + return nil } - pkgs[pkg.Name] = pkg + pl.add(pkg) for _, pkgpath := range pkg.Requires { pkgpath := filepath.Join(modroot, pkgpath) - if err := pkgs.loadPackages(modroot, pkgpath, ""); err != nil { + if err := pl.loadPackages(modroot, pkgpath, ""); err != nil { return fmt.Errorf("require %q: %w", pkgpath, err) } } From 7ff85d6fc8be565e2e9923d30055c6bbcadba1f3 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:55:00 +0100 Subject: [PATCH 14/21] chore(use): unexport all pkgsLoader methods Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 2589c05060d..ee96030c964 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -176,7 +176,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) - pkgsTxs, err := pkgs.Txs(creator, defaultFee, nil) + pkgsTxs, err := pkgs.txs(creator, defaultFee, nil) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) } @@ -436,7 +436,7 @@ func (pl *pkgsLoader) list() gnomod.PkgList { return pl.pkgs } -func (pkgs *pkgsLoader) Txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { +func (pkgs *pkgsLoader) txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { pkgslist, err := pkgs.list().Sort() if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) From 0c9efd75c8f55e04d3b2946f3dd353146f2207e5 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:57:12 +0100 Subject: [PATCH 15/21] chore: lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index ee96030c964..04fd26fde23 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -436,8 +436,8 @@ func (pl *pkgsLoader) list() gnomod.PkgList { return pl.pkgs } -func (pkgs *pkgsLoader) txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { - pkgslist, err := pkgs.list().Sort() +func (pl *pkgsLoader) txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { + pkgslist, err := pl.list().Sort() if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) } From 572c3b7500bfc77ccc7caed46c90154881b12214 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Fri, 2 Feb 2024 08:59:36 +0100 Subject: [PATCH 16/21] chore: rename and export methods for clarity Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- .../pkg/integration/testing_integration.go | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 04fd26fde23..894e37d46b5 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -176,7 +176,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) - pkgsTxs, err := pkgs.txs(creator, defaultFee, nil) + pkgsTxs, err := pkgs.loadPackages(creator, defaultFee, nil) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) } @@ -309,7 +309,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // packages should be loaded individually. if path == "all" { ts.Logf("warning: loading all packages") - if err := pkgs.loadPackagesFromDir(examplesDir); err != nil { + if err := pkgs.usePackagesFromDir(examplesDir); err != nil { ts.Fatalf("unable to load packages from %q: %s", examplesDir, err) } @@ -320,7 +320,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { path = filepath.Join(examplesDir, path) } - if err := pkgs.loadPackages(examplesDir, path, name); err != nil { + if err := pkgs.UsePackage(examplesDir, path, name); err != nil { ts.Fatalf("`use` unable to load package(s) from %q: %s", args[0], err) } @@ -432,12 +432,19 @@ type pkgsLoader struct { visited map[string]struct{} } -func (pl *pkgsLoader) list() gnomod.PkgList { +func newPkgsLoader() *pkgsLoader { + return &pkgsLoader{ + pkgs: make([]gnomod.Pkg, 0), + visited: make(map[string]struct{}), + } +} + +func (pl *pkgsLoader) List() gnomod.PkgList { return pl.pkgs } -func (pl *pkgsLoader) txs(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { - pkgslist, err := pl.list().Sort() +func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std.Coins) ([]std.Tx, error) { + pkgslist, err := pl.List().Sort() // sorts packages by their dependencies. if err != nil { return nil, fmt.Errorf("unable to sort packages: %w", err) } @@ -454,17 +461,7 @@ func (pl *pkgsLoader) txs(creator bft.Address, fee std.Fee, deposit std.Coins) ( return txs, nil } -func (pl *pkgsLoader) add(pkg gnomod.Pkg) { - pl.visited[pkg.Name] = struct{}{} - pl.pkgs = append(pl.pkgs, pkg) -} - -func (pl *pkgsLoader) exist(pkg gnomod.Pkg) (ok bool) { - _, ok = pl.visited[pkg.Name] - return -} - -func (pl *pkgsLoader) loadPackagesFromDir(path string) error { +func (pl *pkgsLoader) UseAllPackagesFromDir(path string) error { // list all packages from target path pkgslist, err := gnomod.ListPkgs(path) if err != nil { @@ -480,7 +477,7 @@ func (pl *pkgsLoader) loadPackagesFromDir(path string) error { return nil } -func (pl *pkgsLoader) loadPackages(modroot string, path, name string) error { +func (pl *pkgsLoader) UsePackage(modroot string, path, name string) error { if path == "" { return fmt.Errorf("no path specified for package") } @@ -517,12 +514,23 @@ func (pl *pkgsLoader) loadPackages(modroot string, path, name string) error { } pl.add(pkg) + // `usePackage` recursively on requirements for _, pkgpath := range pkg.Requires { pkgpath := filepath.Join(modroot, pkgpath) - if err := pl.loadPackages(modroot, pkgpath, ""); err != nil { - return fmt.Errorf("require %q: %w", pkgpath, err) + if err := pl.UsePackage(modroot, pkgpath, ""); err != nil { + return fmt.Errorf("require %q: %w", pkg.Name, err) } } return nil } + +func (pl *pkgsLoader) add(pkg gnomod.Pkg) { + pl.visited[pkg.Name] = struct{}{} + pl.pkgs = append(pl.pkgs, pkg) +} + +func (pl *pkgsLoader) exist(pkg gnomod.Pkg) (ok bool) { + _, ok = pl.visited[pkg.Name] + return +} From 5da1dad93b02b8656a08b7e7f09f84ca6583e116 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Fri, 2 Feb 2024 09:42:15 +0100 Subject: [PATCH 17/21] chore: use iterative logic Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- .../pkg/integration/testing_integration.go | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 894e37d46b5..1286ccb93e7 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -176,7 +176,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { pkgs := ts.Value(envKeyPkgsLoader).(*pkgsLoader) // grab logger creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1 defaultFee := std.NewFee(50000, std.MustParseCoin("1000000ugnot")) - pkgsTxs, err := pkgs.loadPackages(creator, defaultFee, nil) + pkgsTxs, err := pkgs.LoadPackages(creator, defaultFee, nil) if err != nil { ts.Fatalf("unable to load packages txs: %s", err) } @@ -309,7 +309,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // packages should be loaded individually. if path == "all" { ts.Logf("warning: loading all packages") - if err := pkgs.usePackagesFromDir(examplesDir); err != nil { + if err := pkgs.UseAllPackagesFromDir(examplesDir); err != nil { ts.Fatalf("unable to load packages from %q: %s", examplesDir, err) } @@ -478,47 +478,48 @@ func (pl *pkgsLoader) UseAllPackagesFromDir(path string) error { } func (pl *pkgsLoader) UsePackage(modroot string, path, name string) error { - if path == "" { - return fmt.Errorf("no path specified for package") - } + // Initialize a queue with the root package + queue := []gnomod.Pkg{{Dir: path, Name: name}} - pkg := gnomod.Pkg{ - Dir: path, - Name: name, - } + for len(queue) > 0 { + // Dequeue the first package + currentPkg := queue[0] + queue = queue[1:] - if pkg.Name == "" { - // try to load `gno.mod` informations - gnoModPath := filepath.Join(pkg.Dir, "gno.mod") - gm, err := gnomod.ParseGnoMod(gnoModPath) - if err != nil { - return fmt.Errorf("unable to load %q: %w", gnoModPath, err) + if currentPkg.Dir == "" { + return fmt.Errorf("no path specified for package") } - gm.Sanitize() + if currentPkg.Name == "" { + // Load `gno.mod` information + gnoModPath := filepath.Join(currentPkg.Dir, "gno.mod") + gm, err := gnomod.ParseGnoMod(gnoModPath) + if err != nil { + return fmt.Errorf("unable to load %q: %w", gnoModPath, err) + } + gm.Sanitize() - // override pkg info with mod infos - pkg.Name = gm.Module.Mod.Path - pkg.Draft = gm.Draft - for _, req := range gm.Require { - pkg.Requires = append(pkg.Requires, req.Mod.Path) + // Override package info with mod infos + currentPkg.Name = gm.Module.Mod.Path + currentPkg.Draft = gm.Draft + for _, req := range gm.Require { + currentPkg.Requires = append(currentPkg.Requires, req.Mod.Path) + } } - } - if pkg.Draft { - return nil // skip draft package - } + if currentPkg.Draft { + continue // Skip draft package + } - if pl.exist(pkg) { - return nil - } - pl.add(pkg) + if pl.exist(currentPkg) { + continue + } + pl.add(currentPkg) - // `usePackage` recursively on requirements - for _, pkgpath := range pkg.Requires { - pkgpath := filepath.Join(modroot, pkgpath) - if err := pl.UsePackage(modroot, pkgpath, ""); err != nil { - return fmt.Errorf("require %q: %w", pkg.Name, err) + // Add requirements to the queue + for _, pkgPath := range currentPkg.Requires { + fullPath := filepath.Join(modroot, pkgPath) + queue = append(queue, gnomod.Pkg{Dir: fullPath}) } } From d65ef9f011ca92a41f593df602257b4617daf92b Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Fri, 2 Feb 2024 09:44:44 +0100 Subject: [PATCH 18/21] chore: lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/testing_integration.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 1286ccb93e7..b43a4c5d066 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -141,10 +141,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { env.Setenv("USER_ADDR_"+DefaultAccount_Name, DefaultAccount_Address) env.Values[envKeyGenesis] = genesis - env.Values[envKeyPkgsLoader] = &pkgsLoader{ - pkgs: []gnomod.Pkg{}, - visited: map[string]struct{}{}, - } + env.Values[envKeyPkgsLoader] = newPkgsLoader() env.Setenv("GNOROOT", gnoRootDir) env.Setenv("GNOHOME", gnoHomeDir) From 784c74b4a9ba7e86d92a9157140fe8d5d606b86a Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 12 Feb 2024 13:24:29 +0100 Subject: [PATCH 19/21] chore: update doc Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/doc.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index 41d107c72b1..34b872dbfda 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -18,12 +18,15 @@ // communicate with the gnoland node. // // 3. `adduser`: -// - Creates a new user in the default keybase directory. // - Must be run before `gnoland start`. +// - Creates a new user in the default keybase directory. // // 4. `use`: +// - Must be run before `gnoland start`. // - Loads a specific package from the example folder or from the working ($WORK) directory. // - Can be used to load a single package or all packages within a directory. +// - If the target package has a `gno.mod`, all its dependencies (and their respective +// dependencies) will also be loaded. // - The command takes either one or two arguments. The first argument is the name of the package(s), // and the second (optional) argument is the path to the package(s). // Examples: @@ -32,9 +35,8 @@ // -- # Load a package `./bar` from the current testscript's working directory with the name `gno.land/r/foobar/bar`: // -- use gno.land/r/foobar/bar $WORK/bar // - If the path is not prefixed with the working directory, it is assumed to be relative to the examples directory. -// - It's important to note that the load order is significant when using -// multiple use command; packages should be loaded in the order they are -// dependent upon. +// - It's important to note that the load order is significant when using multiple use +// command; packages should be loaded in the order they are dependent upon. // // Logging: // From 665bea2c43bb477cebf3f4821d67460aa9767913 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Fri, 23 Feb 2024 09:08:36 +0100 Subject: [PATCH 20/21] chore: rename `use` with `loadpkg` Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/cmd/gnoland/testdata/append.txtar | 2 +- .../cmd/gnoland/testdata/grc20-registry.txtar | 2 +- .../cmd/gnoland/testdata/issue-1167.txtar | 2 +- gno.land/cmd/gnoland/testdata/run.txtar | 2 +- gno.land/cmd/gnoland/testdata/wugnot.txtar | 2 +- .../pkg/integration/testdata/import.txtar | 4 ++-- .../integration/testdata/use_example.txtar | 2 +- .../testdata/use_example_and_work.txtar | 4 ++-- .../pkg/integration/testdata/use_work.txtar | 2 +- .../pkg/integration/testing_integration.go | 24 +++++++++++-------- 10 files changed, 25 insertions(+), 21 deletions(-) diff --git a/gno.land/cmd/gnoland/testdata/append.txtar b/gno.land/cmd/gnoland/testdata/append.txtar index 0669319075f..46b66f9524b 100644 --- a/gno.land/cmd/gnoland/testdata/append.txtar +++ b/gno.land/cmd/gnoland/testdata/append.txtar @@ -1,4 +1,4 @@ -use gno.land/p/demo/ufmt +loadpkg gno.land/p/demo/ufmt # start a new node gnoland start diff --git a/gno.land/cmd/gnoland/testdata/grc20-registry.txtar b/gno.land/cmd/gnoland/testdata/grc20-registry.txtar index 226c8907872..20e78f7ba6e 100644 --- a/gno.land/cmd/gnoland/testdata/grc20-registry.txtar +++ b/gno.land/cmd/gnoland/testdata/grc20-registry.txtar @@ -1,6 +1,6 @@ # example for contract-contract interaction with ownership # add registry -use gno.land/r/registry $WORK/registry +loadpkg gno.land/r/registry $WORK/registry ## start a new node gnoland start diff --git a/gno.land/cmd/gnoland/testdata/issue-1167.txtar b/gno.land/cmd/gnoland/testdata/issue-1167.txtar index 8a53c65f1fe..c43f7a45bd5 100644 --- a/gno.land/cmd/gnoland/testdata/issue-1167.txtar +++ b/gno.land/cmd/gnoland/testdata/issue-1167.txtar @@ -1,5 +1,5 @@ # Reproducible Test for https://github.com/gnolang/gno/issues/1167 -use gno.land/p/demo/avl +loadpkg gno.land/p/demo/avl gnoland start diff --git a/gno.land/cmd/gnoland/testdata/run.txtar b/gno.land/cmd/gnoland/testdata/run.txtar index f3218b52703..7246a10a1a4 100644 --- a/gno.land/cmd/gnoland/testdata/run.txtar +++ b/gno.land/cmd/gnoland/testdata/run.txtar @@ -1,4 +1,4 @@ -use gno.land/r/foobar/bar $WORK/bar +loadpkg gno.land/r/foobar/bar $WORK/bar ## start a new node gnoland start diff --git a/gno.land/cmd/gnoland/testdata/wugnot.txtar b/gno.land/cmd/gnoland/testdata/wugnot.txtar index 5fcf46c0f03..1640909fdb9 100644 --- a/gno.land/cmd/gnoland/testdata/wugnot.txtar +++ b/gno.land/cmd/gnoland/testdata/wugnot.txtar @@ -1,4 +1,4 @@ -use gno.land/r/demo/wugnot +loadpkg gno.land/r/demo/wugnot gnoland start diff --git a/gno.land/pkg/integration/testdata/import.txtar b/gno.land/pkg/integration/testdata/import.txtar index 0c7d0e02828..e9bba043c2c 100644 --- a/gno.land/pkg/integration/testdata/import.txtar +++ b/gno.land/pkg/integration/testdata/import.txtar @@ -1,6 +1,6 @@ # test that the example packages directory is loaded and usable. -use gno.land/p/demo/ufmt -use $WORK +loadpkg gno.land/p/demo/ufmt +loadpkg $WORK ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testdata/use_example.txtar b/gno.land/pkg/integration/testdata/use_example.txtar index 87c7360af61..4f6bd418cf7 100644 --- a/gno.land/pkg/integration/testdata/use_example.txtar +++ b/gno.land/pkg/integration/testdata/use_example.txtar @@ -1,5 +1,5 @@ # load a package from the example packages directory. -use gno.land/p/demo/ufmt +loadpkg gno.land/p/demo/ufmt ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testdata/use_example_and_work.txtar b/gno.land/pkg/integration/testdata/use_example_and_work.txtar index 41e84609650..e38f6a295cd 100644 --- a/gno.land/pkg/integration/testdata/use_example_and_work.txtar +++ b/gno.land/pkg/integration/testdata/use_example_and_work.txtar @@ -1,6 +1,6 @@ # load a package from the example packages directory. -use gno.land/p/demo/ufmt -use gno.land/r/importtest $WORK +loadpkg gno.land/p/demo/ufmt +loadpkg gno.land/r/importtest $WORKDIR ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testdata/use_work.txtar b/gno.land/pkg/integration/testdata/use_work.txtar index e0ddd37af28..c459a7e590d 100644 --- a/gno.land/pkg/integration/testdata/use_work.txtar +++ b/gno.land/pkg/integration/testdata/use_work.txtar @@ -1,4 +1,4 @@ -use gno.land/r/foobar/bar $WORK/bar +loadpkg gno.land/r/foobar/bar $WORK/bar ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index 9498dce9993..f21ffefce75 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -279,8 +279,8 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { genesis := ts.Value(envKeyGenesis).(*gnoland.GnoGenesisState) genesis.Balances = append(genesis.Balances, balance) }, - // `use` load a specific package from the example folder or from the working directory - "use": func(ts *testscript.TestScript, neg bool, args []string) { + // `loadpkg` load a specific package from the example folder or from the working directory + "loadpkg": func(ts *testscript.TestScript, neg bool, args []string) { // special dirs workDir := ts.Getenv("WORK") examplesDir := filepath.Join(gnoRootDir, "examples") @@ -295,9 +295,9 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { case 1: path = filepath.Clean(args[0]) case 0: - ts.Fatalf("`use`: no arguments specified") + ts.Fatalf("`loadpkg`: no arguments specified") default: - ts.Fatalf("`use`: too many arguments specified") + ts.Fatalf("`loadpkg`: too many arguments specified") } // If `all` is specified, fully load example folder. @@ -305,19 +305,23 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { // packages should be loaded individually. if path == "all" { ts.Logf("warning: loading all packages") - if err := pkgs.UseAllPackagesFromDir(examplesDir); err != nil { + if err := pkgs.LoadAllPackagesFromDir(examplesDir); err != nil { ts.Fatalf("unable to load packages from %q: %s", examplesDir, err) } return } - if !strings.HasPrefix(path, workDir) { + if path == "." { + path = workDir + } else if strings.HasPrefix(path, "./") { + path = filepath.Join(workDir, path[2:]) + } else if !strings.HasPrefix(path, workDir) { path = filepath.Join(examplesDir, path) } - if err := pkgs.UsePackage(examplesDir, path, name); err != nil { - ts.Fatalf("`use` unable to load package(s) from %q: %s", args[0], err) + if err := pkgs.LoadPackage(examplesDir, path, name); err != nil { + ts.Fatalf("`loadpkg` unable to load package(s) from %q: %s", args[0], err) } ts.Logf("%q package was added to genesis", args[0]) @@ -457,7 +461,7 @@ func (pl *pkgsLoader) LoadPackages(creator bft.Address, fee std.Fee, deposit std return txs, nil } -func (pl *pkgsLoader) UseAllPackagesFromDir(path string) error { +func (pl *pkgsLoader) LoadAllPackagesFromDir(path string) error { // list all packages from target path pkgslist, err := gnomod.ListPkgs(path) if err != nil { @@ -473,7 +477,7 @@ func (pl *pkgsLoader) UseAllPackagesFromDir(path string) error { return nil } -func (pl *pkgsLoader) UsePackage(modroot string, path, name string) error { +func (pl *pkgsLoader) LoadPackage(modroot string, path, name string) error { // Initialize a queue with the root package queue := []gnomod.Pkg{{Dir: path, Name: name}} From bca5a195078cf8fafafddcd4c26cbd4a9ebadfe0 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Fri, 23 Feb 2024 09:13:56 +0100 Subject: [PATCH 21/21] chore: fixup and lint Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- gno.land/pkg/integration/doc.go | 11 ++++++----- .../{use_example.txtar => loadpkg_example.txtar} | 0 ..._and_work.txtar => loadpkg_example_and_work.txtar} | 2 +- .../testdata/{use_work.txtar => loadpkg_work.txtar} | 0 gno.land/pkg/integration/testing_integration.go | 6 +----- 5 files changed, 8 insertions(+), 11 deletions(-) rename gno.land/pkg/integration/testdata/{use_example.txtar => loadpkg_example.txtar} (100%) rename gno.land/pkg/integration/testdata/{use_example_and_work.txtar => loadpkg_example_and_work.txtar} (92%) rename gno.land/pkg/integration/testdata/{use_work.txtar => loadpkg_work.txtar} (100%) diff --git a/gno.land/pkg/integration/doc.go b/gno.land/pkg/integration/doc.go index 34b872dbfda..4df759a8d66 100644 --- a/gno.land/pkg/integration/doc.go +++ b/gno.land/pkg/integration/doc.go @@ -21,7 +21,7 @@ // - Must be run before `gnoland start`. // - Creates a new user in the default keybase directory. // -// 4. `use`: +// 4. `loadpkg`: // - Must be run before `gnoland start`. // - Loads a specific package from the example folder or from the working ($WORK) directory. // - Can be used to load a single package or all packages within a directory. @@ -31,11 +31,12 @@ // and the second (optional) argument is the path to the package(s). // Examples: // -- # Load a package from the example packages directory: -// -- use gno.land/p/demo/ufmt +// -- loadpkg gno.land/p/demo/ufmt // -- # Load a package `./bar` from the current testscript's working directory with the name `gno.land/r/foobar/bar`: -// -- use gno.land/r/foobar/bar $WORK/bar -// - If the path is not prefixed with the working directory, it is assumed to be relative to the examples directory. -// - It's important to note that the load order is significant when using multiple use +// -- loadpkg gno.land/r/foobar/bar $WORK/bar +// - If the path is not prefixed with the working directory, it is assumed to be relative to the +// examples directory. +// - It's important to note that the load order is significant when using multiple `loadpkg` // command; packages should be loaded in the order they are dependent upon. // // Logging: diff --git a/gno.land/pkg/integration/testdata/use_example.txtar b/gno.land/pkg/integration/testdata/loadpkg_example.txtar similarity index 100% rename from gno.land/pkg/integration/testdata/use_example.txtar rename to gno.land/pkg/integration/testdata/loadpkg_example.txtar diff --git a/gno.land/pkg/integration/testdata/use_example_and_work.txtar b/gno.land/pkg/integration/testdata/loadpkg_example_and_work.txtar similarity index 92% rename from gno.land/pkg/integration/testdata/use_example_and_work.txtar rename to gno.land/pkg/integration/testdata/loadpkg_example_and_work.txtar index e38f6a295cd..15316c0ffd1 100644 --- a/gno.land/pkg/integration/testdata/use_example_and_work.txtar +++ b/gno.land/pkg/integration/testdata/loadpkg_example_and_work.txtar @@ -1,6 +1,6 @@ # load a package from the example packages directory. loadpkg gno.land/p/demo/ufmt -loadpkg gno.land/r/importtest $WORKDIR +loadpkg gno.land/r/importtest $WORK ## start a new node gnoland start diff --git a/gno.land/pkg/integration/testdata/use_work.txtar b/gno.land/pkg/integration/testdata/loadpkg_work.txtar similarity index 100% rename from gno.land/pkg/integration/testdata/use_work.txtar rename to gno.land/pkg/integration/testdata/loadpkg_work.txtar diff --git a/gno.land/pkg/integration/testing_integration.go b/gno.land/pkg/integration/testing_integration.go index f21ffefce75..37b7c696b52 100644 --- a/gno.land/pkg/integration/testing_integration.go +++ b/gno.land/pkg/integration/testing_integration.go @@ -312,11 +312,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params { return } - if path == "." { - path = workDir - } else if strings.HasPrefix(path, "./") { - path = filepath.Join(workDir, path[2:]) - } else if !strings.HasPrefix(path, workDir) { + if !strings.HasPrefix(path, workDir) { path = filepath.Join(examplesDir, path) }