Skip to content

Commit

Permalink
fix: pass all tests except transpile
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 cef59f3 commit b73027b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 34 deletions.
4 changes: 3 additions & 1 deletion gnovm/cmd/gno/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func TestModApp(t *testing.T) {
args: []string{"mod", "why", "std"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
errShouldContain: "could not read gno.mod file",
stdoutShouldBe: `# std
(module . does not need package std)
`,
},
{
args: []string{"mod", "why", "std"},
Expand Down
47 changes: 33 additions & 14 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
goio "io"
"os"
"path/filepath"
"slices"
"time"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
Expand Down Expand Up @@ -151,15 +155,22 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
cfg.rootDir = gnoenv.RootDir()
}

// inject cwd, we need this to pass some txtars
args = append(args, fmt.Sprintf(".%c...", filepath.Separator))

// Find targets for test.
conf := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
pkgs, err := packages.Load(conf, args...)
if err != nil {
return fmt.Errorf("list targets from patterns: %w", err)
}
if len(pkgs) == 0 {
io.ErrPrintln("no packages to test")
return nil
return errors.New("no packages to test")
}
for _, pkg := range pkgs {
if len(pkg.Match) != 0 && !slices.ContainsFunc(pkg.Match, func(s string) bool { return !packages.PatternIsLiteral(s) }) && pkg.Files.Size() == 0 {
return fmt.Errorf("no Gno files in %s", pkg.Dir)
}
}

if cfg.timeout > 0 {
Expand Down Expand Up @@ -187,18 +198,24 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
buildErrCount := 0
testErrCount := 0
for _, pkg := range pkgs {
// ignore deps
// ignore side-loaded packages, mostly coming from siblings injection (TODO: explain siblings injection, why we chose it over go's forcing to have an arching context (go.mod, go.work))
if len(pkg.Match) == 0 {
continue
}

logName := pkg.ImportPath
if logName == "" {
logName = pkg.Dir
label := pkg.ImportPath
if label == "" {
label = pkg.Dir
}
if cwd, err := os.Getwd(); err == nil {
relLogName, err := filepath.Rel(cwd, label)
if err == nil {
label = relLogName
}
}

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]", logName)
io.ErrPrintfln("? %s \t[no test files]", label)
continue
}

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

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L220-L221

Added lines #L220 - L221 were not covered by tests
Expand All @@ -207,15 +224,17 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
depsConf.Cache = pkgsMap
deps, loadDepsErr := packages.Load(&depsConf, pkg.Dir)
if loadDepsErr != nil {
io.ErrPrintfln("%s: load deps: %v", logName, err)
return nil
io.ErrPrintfln("%s: load deps: %v", label, err)
buildErrCount++
continue
}
packages.Inject(pkgsMap, deps)

memPkg, err := gno.ReadMemPackage(pkg.Dir, logName)
memPkg, err := gno.ReadMemPackage(pkg.Dir, label)
if err != nil {
io.ErrPrintln(err)
return nil
buildErrCount++
continue
}

startedAt := time.Now()
Expand All @@ -228,14 +247,14 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {

if hasError || err != nil {
if err != nil {
io.ErrPrintfln("%s: test pkg: %v", logName, err)
io.ErrPrintfln("%s: test pkg: %v", label, err)
}
io.ErrPrintfln("FAIL")
io.ErrPrintfln("FAIL %s \t%s", logName, dstr)
io.ErrPrintfln("FAIL %s \t%s", label, dstr)
io.ErrPrintfln("FAIL")
testErrCount++
} else {
io.ErrPrintfln("ok %s \t%s", logName, dstr)
io.ErrPrintfln("ok %s \t%s", label, dstr)
}
}
if testErrCount > 0 || buildErrCount > 0 {
Expand Down
6 changes: 3 additions & 3 deletions gnovm/cmd/gno/testdata/test/empty_dir.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
! gno test .

! stdout .+
stderr 'no valid gno files'
stderr 'no Gno files in '

gno test ./...
! gno test ./...

! stdout .+
stderr 'no packages to test'
stderr 'gno: warning: "./..." matched no packages\nno packages to test'
5 changes: 4 additions & 1 deletion gnovm/cmd/gno/testdata/test/multitest_events.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ gno test -print-events .
! stdout .+
stderr 'EVENTS: \[{\"type\":\"EventA\",\"attrs\":\[\],\"pkg_path\":\"gno.land/r/.*\",\"func\":\"TestA\"}\]'
stderr 'EVENTS: \[{\"type\":\"EventB\",\"attrs\":\[{\"key\":\"keyA\",\"value\":\"valA\"}\],\"pkg_path\":\"gno.land/r/.*\",\"func\":\"TestB\"},{\"type\":\"EventC\",\"attrs\":\[{\"key\":\"keyD\",\"value\":\"valD\"}\],\"pkg_path\":\"gno.land/r/.*\",\"func\":\"TestB\"}\]'
stderr 'ok \. \d\.\d\ds'
stderr 'ok gno.land/r/.* \d\.\d\ds'

-- gno.mod --
module gno.land/r/tests/multitest_events

-- valid.gno --
package valid
Expand Down
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/test/realm_boundmethod.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (c *Counter) Inc() {
c.n++
}

-- examples/gno.land/r/demo/realm1/gno.mod --
module gno.land/r/demo/realm1

-- examples/gno.land/r/demo/realm1/realm1.gno --
package realm1

Expand All @@ -36,6 +39,9 @@ func GetCounter() *counter.Counter {
return &c
}

-- examples/gno.land/r/demo/realm2/gno.mod --
module gno.land/r/demo/realm2

-- examples/gno.land/r/demo/realm2/realm2.gno --
package realm2

Expand Down
47 changes: 34 additions & 13 deletions gnovm/pkg/packages/expand_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ type pkgMatch struct {
func expandPatterns(conf *LoadConfig, patterns ...string) ([]*pkgMatch, error) {
pkgMatches := []*pkgMatch{}

addMatch := func(dir string, match string) {
addPkgDir := func(dir string, match *string) {
idx := slices.IndexFunc(pkgMatches, func(sum *pkgMatch) bool { return sum.Dir == dir })
if idx == -1 {
pkgMatches = append(pkgMatches, &pkgMatch{Dir: dir, Match: []string{match}})
matches := []string{}
if match != nil {
matches = append(matches, *match)
}
pkgMatches = append(pkgMatches, &pkgMatch{Dir: dir, Match: matches})
return
}
if match == nil {
return
}
if slices.Contains(pkgMatches[idx].Match, match) {
if slices.Contains(pkgMatches[idx].Match, *match) {
return
}
pkgMatches[idx].Match = append(pkgMatches[idx].Match, match)
pkgMatches[idx].Match = append(pkgMatches[idx].Match, *match)
}

kinds := make([]patternKind, 0, len(patterns))
Expand All @@ -54,31 +61,42 @@ func expandPatterns(conf *LoadConfig, patterns ...string) ([]*pkgMatch, error) {
}

if slices.Contains(kinds, patternKindSingleFile) {
remaining := []string{}
remainingKinds := []patternKind{}

files := make([]string, 0, len(patterns))
for i, match := range patterns {
kind := kinds[i]
if kind != patternKindSingleFile || !strings.HasSuffix(match, ".gno") {
if kind != patternKindSingleFile {
remaining = append(remaining, match)
remainingKinds = append(remainingKinds, kind)
continue
}
if !strings.HasSuffix(match, ".gno") {
return nil, fmt.Errorf("named files must be .gno files: %s", match)
}
files = append(files, match)
}
return []*pkgMatch{{Dir: "command-line-arguments", Match: files}}, nil

pkgMatches = append(pkgMatches, &pkgMatch{Dir: "command-line-arguments", Match: files})

patterns = remaining
kinds = remainingKinds
}

for i, match := range patterns {
patKind := kinds[i]

switch patKind {
case patternKindSingleFile:
return nil, fmt.Errorf("%s: single file patterns are not supported", match)

case patternKindRecursiveRemote:
return nil, fmt.Errorf("%s: recursive remote patterns are not supported", match)

case patternKindRemote:
if conf.SelfContained {
return nil, fmt.Errorf("%s: remote patterns are not supported in self-contained mode", match)
}
case patternKindSingleFile:
return nil, fmt.Errorf("unexpected single pattern at this point")
}

pat, err := cleanPattern(match, patKind)
Expand All @@ -88,22 +106,25 @@ func expandPatterns(conf *LoadConfig, patterns ...string) ([]*pkgMatch, error) {

switch patKind {
case patternKindDirectory:
addMatch(pat, match)
addPkgDir(pat, &match)

case patternKindRemote:
dir := gnomod.PackageDir("", module.Version{Path: pat})
if err := downloadPackage(conf, pat, dir); err != nil {
return nil, err
}
addMatch(dir, match)
addPkgDir(dir, &match)

case patternKindRecursiveLocal:
dirs, err := expandRecursive(pat)
if err != nil {
return nil, fmt.Errorf("%s: %w", pat, err)
}
if len(dirs) == 0 {
conf.IO.ErrPrintfln(`gno: warning: %q matched no packages`, match)
}
for _, dir := range dirs {
addMatch(dir, match)
addPkgDir(dir, &match)
}
}
}
Expand Down Expand Up @@ -158,7 +179,7 @@ const (
)

func getPatternKind(pat string) (patternKind, error) {
isLitteral := patternIsLiteral(pat)
isLitteral := PatternIsLiteral(pat)

if patternIsRemote(pat) {
if isLitteral {
Expand Down
4 changes: 4 additions & 0 deletions gnovm/pkg/packages/filekind.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const (
FileKindFiletest = "Filetest"
)

func AllFileKinds() []FileKind {
return []FileKind{FileKindPackageSource, FileKindTest, FileKindXTest, FileKindFiletest}
}

// GetFileKind analyzes a file's name and body to get it's [FileKind], fset is optional
func GetFileKind(filename string, body string, fset *token.FileSet) (FileKind, error) {
if !strings.HasSuffix(filename, ".gno") {
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/packages/patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func patternIsRemote(path string) bool {
return strings.ContainsRune(path[:slashIdx], '.')
}

// patternIsLiteral reports whether the pattern is free of wildcards.
// PatternIsLiteral reports whether the pattern is free of wildcards.
//
// A literal pattern must match at most one package.
func patternIsLiteral(pattern string) bool {
func PatternIsLiteral(pattern string) bool {
return !strings.Contains(pattern, "...")
}
8 changes: 8 additions & 0 deletions gnovm/pkg/packages/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ type Package struct {

type FilesMap map[FileKind][]string

func (fm FilesMap) Size() int {
total := 0
for _, kind := range AllFileKinds() {
total += len(fm[kind])
}
return total
}

// Merge merges imports, it removes duplicates and sorts the result
func (imap FilesMap) Merge(kinds ...FileKind) []string {

Check failure on line 31 in gnovm/pkg/packages/types.go

View workflow job for this annotation

GitHub Actions / Run GnoVM suite / Go Lint / lint

ST1016: methods on the same type should have the same receiver name (seen 1x "fm", 1x "imap") (stylecheck)
res := make([]string, 0, 16)
Expand Down

0 comments on commit b73027b

Please sign in to comment.