Skip to content

Commit

Permalink
feat: improve
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 8, 2025
1 parent c428052 commit 68b2453
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 74 deletions.
18 changes: 9 additions & 9 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ GOIMPORTS_FLAGS ?= $(GOFMT_FLAGS)
GOTEST_FLAGS ?= -v -p 1 -timeout=30m

# Official packages (non-overridable): more reliable and tested modules, distinct from the experimentation area.
OFFICIAL_PACKAGES = ./gno.land/p
OFFICIAL_PACKAGES += ./gno.land/r/demo
OFFICIAL_PACKAGES += ./gno.land/r/gnoland
OFFICIAL_PACKAGES += ./gno.land/r/sys
OFFICIAL_PACKAGES += ./gno.land/r/gov
OFFICIAL_PACKAGES = ./gno.land/p/...
OFFICIAL_PACKAGES += ./gno.land/r/demo/...
OFFICIAL_PACKAGES += ./gno.land/r/gnoland/...
OFFICIAL_PACKAGES += ./gno.land/r/sys/...
OFFICIAL_PACKAGES += ./gno.land/r/gov/...

########################################
# Dev tools
.PHONY: transpile
transpile:
go run ../gnovm/cmd/gno transpile -v .
go run ../gnovm/cmd/gno transpile -v ./...

.PHONY: build
build:
go run ../gnovm/cmd/gno transpile -v --gobuild .
go run ../gnovm/cmd/gno transpile -v --gobuild ./...

.PHONY: test
test:
go run ../gnovm/cmd/gno test -v ./...

.PHONY: lint
lint:
go run ../gnovm/cmd/gno lint -v $(OFFICIAL_PACKAGES)
go run ../gnovm/cmd/gno lint --root-examples -v $(OFFICIAL_PACKAGES)

.PHONY: test.sync
test.sync:
Expand All @@ -58,7 +58,7 @@ clean:
.PHONY: fmt
GNOFMT_FLAGS ?= -w
fmt:
go run ../gnovm/cmd/gno fmt $(GNOFMT_FLAGS) ./...
go run ../gnovm/cmd/gno fmt $(GNOFMT_FLAGS) .

.PHONY: tidy
tidy:
Expand Down
40 changes: 33 additions & 7 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
)

type lintCfg struct {
verbose bool
rootDir string
verbose bool
rootDir string
rootExamples bool
// min_confidence: minimum confidence of a problem to print it (default 0.8)
// auto-fix: apply suggested fixes automatically.
}
Expand All @@ -51,6 +52,7 @@ func (c *lintCfg) RegisterFlags(fs *flag.FlagSet) {

fs.BoolVar(&c.verbose, "v", false, "verbose output when lintning")
fs.StringVar(&c.rootDir, "root-dir", rootdir, "clone location of github.com/gnolang/gno (gno tries to guess it)")
fs.BoolVar(&c.rootExamples, "root-examples", false, "use the examples present in GNOROOT rather than downloading them")
}

type lintCode int
Expand Down Expand Up @@ -91,8 +93,12 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
rootDir = gnoenv.RootDir()
}

conf := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
pkgs, err := packages.Load(conf, args...)
loadCfg := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
if cfg.rootExamples {
loadCfg.GnorootExamples = true
}

pkgs, err := packages.Load(loadCfg, args...)
if err != nil {
return fmt.Errorf("list packages from args: %w", err)
}
Expand All @@ -105,7 +111,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
hasError := false

bs, ts := test.Store(
rootDir, false,
rootDir, pkgsMap, false,
nopReader{}, goio.Discard, goio.Discard,
)

Expand Down Expand Up @@ -140,7 +146,27 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
hasError = true
}

memPkg, err := gno.ReadMemPackage(pkgPath, pkgPath)
// load deps
loadDepsCfg := *loadCfg
loadDepsCfg.Deps = true
loadDepsCfg.Cache = pkgsMap

deps, loadDepsErr := packages.Load(&loadDepsCfg, pkg.Dir)
if loadDepsErr != nil {
io.ErrPrintln(issueFromError(pkgPath, err).String())
hasError = true
continue
}

for _, dep := range deps {
if _, ok := pkgsMap[dep.ImportPath]; ok {
continue
}
pkgsMap[dep.ImportPath] = dep
continue
}

memPkg, err := gno.ReadMemPackage(pkg.Dir, pkgPath)
if err != nil {
io.ErrPrintln(issueFromError(pkgPath, err).String())
hasError = true
Expand All @@ -163,7 +189,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {

var gmFile *gnomod.File
if pkg.ModPath != "" {
gmFile, err = gnomod.ParseGnoMod(filepath.Join(pkg.ModPath, "gno.mod"))
gmFile, err = gnomod.ParseGnoMod(filepath.Join(pkg.Root, "gno.mod"))
if err != nil {
io.ErrPrintln(err)
hasError = true
Expand Down
4 changes: 0 additions & 4 deletions gnovm/cmd/gno/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ func execList(cfg *listCfg, args []string, io commands.IO) error {
os.Exit(1)
}
for _, pkg := range pkgs {
if len(pkg.Match) == 0 && !cfg.deps {
continue
}

pkgBytes, err := json.MarshalIndent(pkg, "", "\t")
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error {

// init store and machine
_, testStore := test.Store(
cfg.rootDir, false,
cfg.rootDir, nil, false,
stdin, stdout, stderr)
if cfg.verbose {
testStore.SetLogStoreOps(true)
Expand Down
60 changes: 40 additions & 20 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/packages"
"github.com/gnolang/gno/gnovm/pkg/test"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/random"
)

type testCfg struct {
Expand Down Expand Up @@ -174,12 +172,20 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
}()
}

pkgsMap := map[string]*packages.Package{}
for _, pkg := range pkgs {
if _, ok := pkgsMap[pkg.ImportPath]; ok {
continue
}
pkgsMap[pkg.ImportPath] = pkg
}

// Set up options to run tests.
stdout := goio.Discard
if cfg.verbose {
stdout = io.Out()
}
opts := test.NewTestOptions(cfg.rootDir, io.In(), stdout, io.Err())
opts := test.NewTestOptions(cfg.rootDir, pkgsMap, io.In(), stdout, io.Err())
opts.RunFlag = cfg.run
opts.Sync = cfg.updateGoldenTests
opts.Verbose = cfg.verbose
Expand All @@ -193,29 +199,43 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
if len(pkg.Match) == 0 {
continue
}
// Determine gnoPkgPath by reading gno.mod
var gnoPkgPath string
modfile, err := gnomod.ParseGnoMod(filepath.Join(pkg.ModPath, "gno.mod"))
if err == nil {
gnoPkgPath = modfile.Module.Mod.Path
} else {
gnoPkgPath = pkgPathFromRootDir(pkg.Dir, cfg.rootDir)
if gnoPkgPath == "" {
// unable to read pkgPath from gno.mod, generate a random realm path
io.ErrPrintfln("--- WARNING: unable to read package path from gno.mod or gno root directory; try creating a gno.mod file")
gnoPkgPath = "gno.land/r/" + strings.ToLower(random.RandStr(8)) // XXX: gno.land hardcoded for convenience.
}

parentName := pkg.ImportPath
if parentName == "" {
parentName = pkg.Dir
}

if len(pkg.Files[packages.FileKindTest]) == 0 && len(pkg.Files[packages.FileKindXTest]) == 0 && len(pkg.Files[packages.FileKindFiletest]) == 0 {
io.ErrPrintfln("? %s \t[no test files]", pkg.ImportPath)
io.ErrPrintfln("? %s \t[no test files]", parentName)
continue
}

Check warning on line 212 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L211-L212

Added lines #L211 - L212 were not covered by tests
depsConf := *conf
depsConf.Deps = true
depsConf.Cache = pkgsMap

deps, loadDepsErr := packages.Load(&depsConf, pkg.Dir)
if loadDepsErr != nil {
io.ErrPrintfln("%s: load deps: %v", pkg.Dir, err)
io.ErrPrintfln("FAIL")
io.ErrPrintfln("FAIL %s", parentName)
io.ErrPrintfln("FAIL")
buildErrCount++
continue
}

for _, dep := range deps {
if _, ok := pkgsMap[dep.ImportPath]; ok {
continue
}
pkgsMap[dep.ImportPath] = dep
continue
}

memPkg := gno.MustReadMemPackage(pkg.Dir, gnoPkgPath)
memPkg := gno.MustReadMemPackage(pkg.Dir, parentName)

startedAt := time.Now()
hasError := catchRuntimeError(gnoPkgPath, io.Err(), func() {
hasError := catchRuntimeError(parentName, io.Err(), func() {
err = test.Test(memPkg, pkg.Dir, opts)
})

Expand All @@ -227,11 +247,11 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
io.ErrPrintfln("%s: test pkg: %v", pkg.Dir, err)
}
io.ErrPrintfln("FAIL")
io.ErrPrintfln("FAIL %s \t%s", pkg.ImportPath, dstr)
io.ErrPrintfln("FAIL %s \t%s", parentName, dstr)
io.ErrPrintfln("FAIL")
testErrCount++
} else {
io.ErrPrintfln("ok %s \t%s", pkg.ImportPath, dstr)
io.ErrPrintfln("ok %s \t%s", parentName, dstr)
}
}
if testErrCount > 0 || buildErrCount > 0 {
Expand Down
9 changes: 2 additions & 7 deletions gnovm/cmd/gno/transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"strings"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/packages"
"github.com/gnolang/gno/gnovm/pkg/transpiler"
"github.com/gnolang/gno/tm2/pkg/commands"
Expand Down Expand Up @@ -134,7 +133,7 @@ func execTranspile(cfg *transpileCfg, args []string, io commands.IO) error {
}

// load packages
conf := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
conf := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher, Deps: true}
pkgs, err := packages.Load(conf, args...)
if err != nil {
return fmt.Errorf("load pkgs: %w", err)
Expand Down Expand Up @@ -214,11 +213,7 @@ func transpilePkg(pkg *packages.Package, pkgs map[string]*packages.Package, opts
}
opts.markAsTranspiled(dirPath)

gmod, err := gnomod.ParseAt(dirPath)
if err != nil && !errors.Is(err, gnomod.ErrGnoModNotFound) {
return err
}
if err == nil && gmod.Draft {
if pkg.Draft {
if opts.cfg.verbose {
opts.io.ErrPrintfln("%s (skipped, gno.mod marks module as draft)", filepath.Clean(dirPath))
}
Expand Down
17 changes: 13 additions & 4 deletions gnovm/pkg/packages/analyze_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package packages

import (
"errors"
"go/parser"
"go/token"
"os"
"path"
Expand All @@ -16,17 +17,18 @@ import (
func readPackages(matches []*pkgMatch) []*Package {
pkgs := make([]*Package, 0, len(matches))
for _, pkgMatch := range matches {
pkg := readPkg(pkgMatch.Dir)
pkg := readPkg(pkgMatch.Dir, "")
pkg.Match = pkgMatch.Match
pkgs = append(pkgs, pkg)
}
return pkgs
}

func readPkg(pkgDir string) *Package {
func readPkg(pkgDir string, importPath string) *Package {
pkg := &Package{
Dir: pkgDir,
Files: make(FilesMap),
Dir: pkgDir,
Files: make(FilesMap),
ImportPath: importPath,
}

entries, err := os.ReadDir(pkgDir)
Expand Down Expand Up @@ -64,6 +66,13 @@ func readPkg(pkgDir string) *Package {
continue
}

// ignore files with invalid package clause
_, err = parser.ParseFile(fset, fpath, nil, parser.PackageClauseOnly)
if err != nil {
pkg.Error = errors.Join(pkg.Error, err)
continue
}

mempkg.Files = append(mempkg.Files, &gnovm.MemFile{Name: base, Body: body})
pkg.Files[fileKind] = append(pkg.Files[fileKind], base)
}
Expand Down
Loading

0 comments on commit 68b2453

Please sign in to comment.