Skip to content

Commit

Permalink
gno test support for external pkgs
Browse files Browse the repository at this point in the history
Look for downloaded packages in GNOHOME
  • Loading branch information
harry-hov committed May 30, 2024
1 parent 9c60a23 commit 380d80c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.RootDir()
if cfg.rootDir == "" {
return errors.New("GNOROOT not set")
}
}

paths, err := targetsFromPatterns(args)
Expand Down
12 changes: 8 additions & 4 deletions gnovm/cmd/gno/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/transpiler"
)

Expand Down Expand Up @@ -209,7 +209,7 @@ func getPathsFromImportSpec(importSpec []*ast.ImportSpec) (importPaths []importP
// e.g
// Output Dir: Temp/gno-transpile
// Pkg Path: ../example/gno.land/p/pkg
// Returns -> Temp/gno-transpile/example/gno.land/p/pkg
// Returns -> Temp/gno-precompile/pkg
func ResolvePath(output string, path importPath) (string, error) {
absOutput, err := filepath.Abs(output)
if err != nil {
Expand All @@ -219,9 +219,13 @@ func ResolvePath(output string, path importPath) (string, error) {
if err != nil {
return "", err
}
pkgPath := strings.TrimPrefix(absPkgPath, gnoenv.RootDir())
pkgRootPath, err := gnomod.FindRootDir(absPkgPath)
if err != nil {
return "", err
}
stagingPath := strings.TrimPrefix(absPkgPath, filepath.Dir(pkgRootPath))

return filepath.Join(absOutput, pkgPath), nil
return filepath.Join(absOutput, stagingPath), nil
}

// WriteDirFile write file to the path and also create
Expand Down
24 changes: 23 additions & 1 deletion gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"time"
"unicode/utf8"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
teststdlibs "github.com/gnolang/gno/gnovm/tests/stdlibs"
teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std"
Expand Down Expand Up @@ -381,7 +382,27 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
}
}

// if examples package...
// look for downloaded packages in `GNOHOME`
homeDir := gnoenv.HomeDir()
if homeDir != "" {
examplePath := filepath.Join(homeDir, "pkg", "mod", pkgPath)
if osm.DirExists(examplePath) {
memPkg := gno.ReadMemPackage(examplePath, pkgPath)
if memPkg.IsEmpty() {
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}

m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
})
pn, pv = m2.RunMemPackage(memPkg, true)
return
}
}

// if not found in `GNOHOME`, look in `/examples` dir.
examplePath := filepath.Join(rootDir, "examples", pkgPath)
if osm.DirExists(examplePath) {
memPkg := gno.ReadMemPackage(examplePath, pkgPath)
Expand All @@ -400,6 +421,7 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
pn, pv = m2.RunMemPackage(memPkg, true)
return
}

return nil, nil
}
// NOTE: store is also used in closure above.
Expand Down

0 comments on commit 380d80c

Please sign in to comment.