Skip to content

Commit b73027b

Browse files
committed
fix: pass all tests except transpile
Signed-off-by: Norman <norman@samourai.coop>
1 parent cef59f3 commit b73027b

9 files changed

+97
-34
lines changed

gnovm/cmd/gno/mod_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ func TestModApp(t *testing.T) {
177177
args: []string{"mod", "why", "std"},
178178
testDir: "../../tests/integ/empty_dir",
179179
simulateExternalRepo: true,
180-
errShouldContain: "could not read gno.mod file",
180+
stdoutShouldBe: `# std
181+
(module . does not need package std)
182+
`,
181183
},
182184
{
183185
args: []string{"mod", "why", "std"},

gnovm/cmd/gno/test.go

+33-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
goio "io"
9+
"os"
10+
"path/filepath"
11+
"slices"
812
"time"
913

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

158+
// inject cwd, we need this to pass some txtars
159+
args = append(args, fmt.Sprintf(".%c...", filepath.Separator))
160+
154161
// Find targets for test.
155162
conf := &packages.LoadConfig{IO: io, Fetcher: testPackageFetcher}
156163
pkgs, err := packages.Load(conf, args...)
157164
if err != nil {
158165
return fmt.Errorf("list targets from patterns: %w", err)
159166
}
160167
if len(pkgs) == 0 {
161-
io.ErrPrintln("no packages to test")
162-
return nil
168+
return errors.New("no packages to test")
169+
}
170+
for _, pkg := range pkgs {
171+
if len(pkg.Match) != 0 && !slices.ContainsFunc(pkg.Match, func(s string) bool { return !packages.PatternIsLiteral(s) }) && pkg.Files.Size() == 0 {
172+
return fmt.Errorf("no Gno files in %s", pkg.Dir)
173+
}
163174
}
164175

165176
if cfg.timeout > 0 {
@@ -187,18 +198,24 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
187198
buildErrCount := 0
188199
testErrCount := 0
189200
for _, pkg := range pkgs {
190-
// ignore deps
201+
// 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))
191202
if len(pkg.Match) == 0 {
192203
continue
193204
}
194205

195-
logName := pkg.ImportPath
196-
if logName == "" {
197-
logName = pkg.Dir
206+
label := pkg.ImportPath
207+
if label == "" {
208+
label = pkg.Dir
209+
}
210+
if cwd, err := os.Getwd(); err == nil {
211+
relLogName, err := filepath.Rel(cwd, label)
212+
if err == nil {
213+
label = relLogName
214+
}
198215
}
199216

200217
if len(pkg.Files[packages.FileKindTest]) == 0 && len(pkg.Files[packages.FileKindXTest]) == 0 && len(pkg.Files[packages.FileKindFiletest]) == 0 {
201-
io.ErrPrintfln("? %s \t[no test files]", logName)
218+
io.ErrPrintfln("? %s \t[no test files]", label)
202219
continue
203220
}
204221

@@ -207,15 +224,17 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
207224
depsConf.Cache = pkgsMap
208225
deps, loadDepsErr := packages.Load(&depsConf, pkg.Dir)
209226
if loadDepsErr != nil {
210-
io.ErrPrintfln("%s: load deps: %v", logName, err)
211-
return nil
227+
io.ErrPrintfln("%s: load deps: %v", label, err)
228+
buildErrCount++
229+
continue
212230
}
213231
packages.Inject(pkgsMap, deps)
214232

215-
memPkg, err := gno.ReadMemPackage(pkg.Dir, logName)
233+
memPkg, err := gno.ReadMemPackage(pkg.Dir, label)
216234
if err != nil {
217235
io.ErrPrintln(err)
218-
return nil
236+
buildErrCount++
237+
continue
219238
}
220239

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

229248
if hasError || err != nil {
230249
if err != nil {
231-
io.ErrPrintfln("%s: test pkg: %v", logName, err)
250+
io.ErrPrintfln("%s: test pkg: %v", label, err)
232251
}
233252
io.ErrPrintfln("FAIL")
234-
io.ErrPrintfln("FAIL %s \t%s", logName, dstr)
253+
io.ErrPrintfln("FAIL %s \t%s", label, dstr)
235254
io.ErrPrintfln("FAIL")
236255
testErrCount++
237256
} else {
238-
io.ErrPrintfln("ok %s \t%s", logName, dstr)
257+
io.ErrPrintfln("ok %s \t%s", label, dstr)
239258
}
240259
}
241260
if testErrCount > 0 || buildErrCount > 0 {

gnovm/cmd/gno/testdata/test/empty_dir.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
! gno test .
44

55
! stdout .+
6-
stderr 'no valid gno files'
6+
stderr 'no Gno files in '
77

8-
gno test ./...
8+
! gno test ./...
99

1010
! stdout .+
11-
stderr 'no packages to test'
11+
stderr 'gno: warning: "./..." matched no packages\nno packages to test'

gnovm/cmd/gno/testdata/test/multitest_events.txtar

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ gno test -print-events .
55
! stdout .+
66
stderr 'EVENTS: \[{\"type\":\"EventA\",\"attrs\":\[\],\"pkg_path\":\"gno.land/r/.*\",\"func\":\"TestA\"}\]'
77
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\"}\]'
8-
stderr 'ok \. \d\.\d\ds'
8+
stderr 'ok gno.land/r/.* \d\.\d\ds'
9+
10+
-- gno.mod --
11+
module gno.land/r/tests/multitest_events
912

1013
-- valid.gno --
1114
package valid

gnovm/cmd/gno/testdata/test/realm_boundmethod.txtar

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func (c *Counter) Inc() {
2525
c.n++
2626
}
2727

28+
-- examples/gno.land/r/demo/realm1/gno.mod --
29+
module gno.land/r/demo/realm1
30+
2831
-- examples/gno.land/r/demo/realm1/realm1.gno --
2932
package realm1
3033

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

42+
-- examples/gno.land/r/demo/realm2/gno.mod --
43+
module gno.land/r/demo/realm2
44+
3945
-- examples/gno.land/r/demo/realm2/realm2.gno --
4046
package realm2
4147

gnovm/pkg/packages/expand_patterns.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,23 @@ type pkgMatch struct {
3232
func expandPatterns(conf *LoadConfig, patterns ...string) ([]*pkgMatch, error) {
3333
pkgMatches := []*pkgMatch{}
3434

35-
addMatch := func(dir string, match string) {
35+
addPkgDir := func(dir string, match *string) {
3636
idx := slices.IndexFunc(pkgMatches, func(sum *pkgMatch) bool { return sum.Dir == dir })
3737
if idx == -1 {
38-
pkgMatches = append(pkgMatches, &pkgMatch{Dir: dir, Match: []string{match}})
38+
matches := []string{}
39+
if match != nil {
40+
matches = append(matches, *match)
41+
}
42+
pkgMatches = append(pkgMatches, &pkgMatch{Dir: dir, Match: matches})
43+
return
44+
}
45+
if match == nil {
3946
return
4047
}
41-
if slices.Contains(pkgMatches[idx].Match, match) {
48+
if slices.Contains(pkgMatches[idx].Match, *match) {
4249
return
4350
}
44-
pkgMatches[idx].Match = append(pkgMatches[idx].Match, match)
51+
pkgMatches[idx].Match = append(pkgMatches[idx].Match, *match)
4552
}
4653

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

5663
if slices.Contains(kinds, patternKindSingleFile) {
64+
remaining := []string{}
65+
remainingKinds := []patternKind{}
66+
5767
files := make([]string, 0, len(patterns))
5868
for i, match := range patterns {
5969
kind := kinds[i]
60-
if kind != patternKindSingleFile || !strings.HasSuffix(match, ".gno") {
70+
if kind != patternKindSingleFile {
71+
remaining = append(remaining, match)
72+
remainingKinds = append(remainingKinds, kind)
73+
continue
74+
}
75+
if !strings.HasSuffix(match, ".gno") {
6176
return nil, fmt.Errorf("named files must be .gno files: %s", match)
6277
}
6378
files = append(files, match)
6479
}
65-
return []*pkgMatch{{Dir: "command-line-arguments", Match: files}}, nil
80+
81+
pkgMatches = append(pkgMatches, &pkgMatch{Dir: "command-line-arguments", Match: files})
82+
83+
patterns = remaining
84+
kinds = remainingKinds
6685
}
6786

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

7190
switch patKind {
72-
case patternKindSingleFile:
73-
return nil, fmt.Errorf("%s: single file patterns are not supported", match)
74-
7591
case patternKindRecursiveRemote:
7692
return nil, fmt.Errorf("%s: recursive remote patterns are not supported", match)
7793

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

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

89107
switch patKind {
90108
case patternKindDirectory:
91-
addMatch(pat, match)
109+
addPkgDir(pat, &match)
92110

93111
case patternKindRemote:
94112
dir := gnomod.PackageDir("", module.Version{Path: pat})
95113
if err := downloadPackage(conf, pat, dir); err != nil {
96114
return nil, err
97115
}
98-
addMatch(dir, match)
116+
addPkgDir(dir, &match)
99117

100118
case patternKindRecursiveLocal:
101119
dirs, err := expandRecursive(pat)
102120
if err != nil {
103121
return nil, fmt.Errorf("%s: %w", pat, err)
104122
}
123+
if len(dirs) == 0 {
124+
conf.IO.ErrPrintfln(`gno: warning: %q matched no packages`, match)
125+
}
105126
for _, dir := range dirs {
106-
addMatch(dir, match)
127+
addPkgDir(dir, &match)
107128
}
108129
}
109130
}
@@ -158,7 +179,7 @@ const (
158179
)
159180

160181
func getPatternKind(pat string) (patternKind, error) {
161-
isLitteral := patternIsLiteral(pat)
182+
isLitteral := PatternIsLiteral(pat)
162183

163184
if patternIsRemote(pat) {
164185
if isLitteral {

gnovm/pkg/packages/filekind.go

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const (
2626
FileKindFiletest = "Filetest"
2727
)
2828

29+
func AllFileKinds() []FileKind {
30+
return []FileKind{FileKindPackageSource, FileKindTest, FileKindXTest, FileKindFiletest}
31+
}
32+
2933
// GetFileKind analyzes a file's name and body to get it's [FileKind], fset is optional
3034
func GetFileKind(filename string, body string, fset *token.FileSet) (FileKind, error) {
3135
if !strings.HasSuffix(filename, ".gno") {

gnovm/pkg/packages/patterns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func patternIsRemote(path string) bool {
1919
return strings.ContainsRune(path[:slashIdx], '.')
2020
}
2121

22-
// patternIsLiteral reports whether the pattern is free of wildcards.
22+
// PatternIsLiteral reports whether the pattern is free of wildcards.
2323
//
2424
// A literal pattern must match at most one package.
25-
func patternIsLiteral(pattern string) bool {
25+
func PatternIsLiteral(pattern string) bool {
2626
return !strings.Contains(pattern, "...")
2727
}

gnovm/pkg/packages/types.go

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ type Package struct {
1919

2020
type FilesMap map[FileKind][]string
2121

22+
func (fm FilesMap) Size() int {
23+
total := 0
24+
for _, kind := range AllFileKinds() {
25+
total += len(fm[kind])
26+
}
27+
return total
28+
}
29+
2230
// Merge merges imports, it removes duplicates and sorts the result
2331
func (imap FilesMap) Merge(kinds ...FileKind) []string {
2432
res := make([]string, 0, 16)

0 commit comments

Comments
 (0)