From e43614a6e6735e90e4591f95be43dd4e873bef07 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Sun, 26 May 2024 15:01:39 +0200 Subject: [PATCH 1/5] feat: lint all files in folder before panicking --- gnovm/cmd/gno/lint.go | 39 ++++++++++++------- gnovm/cmd/gno/lint_test.go | 4 ++ gnovm/cmd/gno/test.go | 7 +++- gnovm/pkg/gnolang/nodes.go | 7 +++- .../several-files-multiple-errors/file2.gno | 5 +++ .../several-files-multiple-errors/gno.mod | 1 + .../several-files-multiple-errors/main.gno | 6 +++ 7 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 gnovm/tests/integ/several-files-multiple-errors/file2.gno create mode 100644 gnovm/tests/integ/several-files-multiple-errors/gno.mod create mode 100644 gnovm/tests/integ/several-files-multiple-errors/main.gno diff --git a/gnovm/cmd/gno/lint.go b/gnovm/cmd/gno/lint.go index e8f5e5d3824..90717edad45 100644 --- a/gnovm/cmd/gno/lint.go +++ b/gnovm/cmd/gno/lint.go @@ -170,27 +170,36 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha return } hasError = true - switch verr := r.(type) { - case *gno.PreprocessError: - err := verr.Unwrap() - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - case scanner.ErrorList: - for _, err := range verr { - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - } - case error: - fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") - case string: - fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n") - default: - panic(r) - } + printRuntimeError(r, pkgPath, stderr) }() action() return } +func printRuntimeError(r interface{}, pkgPath string, stderr io.WriteCloser) { + switch verr := r.(type) { + case *gno.PreprocessError: + err := verr.Unwrap() + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + case scanner.ErrorList: + for _, err := range verr { + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + } + case error: + fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") + case []error: + for _, err := range verr { + // recursive call to handle specifically each error type ex: scanner.ErrorList + printRuntimeError(err, pkgPath, stderr) + } + case string: + fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n") + default: + panic(r) + } +} + type lintCode int const ( diff --git a/gnovm/cmd/gno/lint_test.go b/gnovm/cmd/gno/lint_test.go index a5c0319cd00..20d21c05d05 100644 --- a/gnovm/cmd/gno/lint_test.go +++ b/gnovm/cmd/gno/lint_test.go @@ -23,6 +23,10 @@ func TestLintApp(t *testing.T) { args: []string{"lint", "../../tests/integ/several-lint-errors/main.gno"}, stderrShouldContain: "../../tests/integ/several-lint-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-lint-errors/main.gno:6", errShouldBe: "exit code: 1", + }, { + args: []string{"lint", "../../tests/integ/several-files-multiple-errors/main.gno"}, + stderrShouldContain: "../../tests/integ/several-files-multiple-errors/file2.gno:3: expected 'IDENT', found '{' (code=2).\n../../tests/integ/several-files-multiple-errors/file2.gno:5: expected type, found '}' (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:6: expected '}', found 'EOF' (code=2).\n", + errShouldBe: "exit code: 1", }, { args: []string{"lint", "../../tests/integ/run_main/"}, stderrShouldContain: "./../../tests/integ/run_main: missing 'gno.mod' file (code=1).", diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 2e966bd32a9..2da5bf27b1d 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -578,6 +578,7 @@ func loadTestFuncs(pkgName string, t *testFuncs, tfiles *gno.FileSet) *testFuncs func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { tset = &gno.FileSet{} itset = &gno.FileSet{} + errors := []error{} for _, mfile := range memPkg.Files { if !strings.HasSuffix(mfile.Name, ".gno") { continue // skip this file. @@ -587,7 +588,8 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { } n, err := gno.ParseFile(mfile.Name, mfile.Body) if err != nil { - panic(err) + errors = append(errors, err) + continue } if n == nil { panic("should not happen") @@ -607,6 +609,9 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { memPkg.Name, memPkg.Name, n.PkgName, mfile)) } } + if len(errors) > 0 { + panic(errors) + } return tset, itset } diff --git a/gnovm/pkg/gnolang/nodes.go b/gnovm/pkg/gnolang/nodes.go index 482f4850b6e..fc80a1251fd 100644 --- a/gnovm/pkg/gnolang/nodes.go +++ b/gnovm/pkg/gnolang/nodes.go @@ -1172,6 +1172,7 @@ func ReadMemPackageFromList(list []string, pkgPath string) *std.MemPackage { // or [ParseFile] returns an error, ParseMemPackage panics. func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { fset = &FileSet{} + errors := []error{} for _, mfile := range memPkg.Files { if !strings.HasSuffix(mfile.Name, ".gno") || endsWith(mfile.Name, []string{"_test.gno", "_filetest.gno"}) { @@ -1179,7 +1180,8 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { } n, err := ParseFile(mfile.Name, mfile.Body) if err != nil { - panic(err) + errors = append(errors, err) + continue } if memPkg.Name != string(n.PkgName) { panic(fmt.Sprintf( @@ -1189,6 +1191,9 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { // add package file. fset.AddFiles(n) } + if len(errors) > 0 { + panic(errors) + } return fset } diff --git a/gnovm/tests/integ/several-files-multiple-errors/file2.gno b/gnovm/tests/integ/several-files-multiple-errors/file2.gno new file mode 100644 index 00000000000..39ec59973ef --- /dev/null +++ b/gnovm/tests/integ/several-files-multiple-errors/file2.gno @@ -0,0 +1,5 @@ +package main + +type{ + +} \ No newline at end of file diff --git a/gnovm/tests/integ/several-files-multiple-errors/gno.mod b/gnovm/tests/integ/several-files-multiple-errors/gno.mod new file mode 100644 index 00000000000..88485411822 --- /dev/null +++ b/gnovm/tests/integ/several-files-multiple-errors/gno.mod @@ -0,0 +1 @@ +module gno.land/tests/severalerrors \ No newline at end of file diff --git a/gnovm/tests/integ/several-files-multiple-errors/main.gno b/gnovm/tests/integ/several-files-multiple-errors/main.gno new file mode 100644 index 00000000000..f29aa7ecd33 --- /dev/null +++ b/gnovm/tests/integ/several-files-multiple-errors/main.gno @@ -0,0 +1,6 @@ +package main + +func main() { + for { + _ example +} \ No newline at end of file From 904ca3efce05606550333b69c9b848b97da24e10 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Wed, 29 May 2024 19:13:35 +0200 Subject: [PATCH 2/5] fix comments on review --- gnovm/cmd/gno/lint.go | 46 ++++++++++++++++++-------------------- gnovm/cmd/gno/test.go | 8 +++---- gnovm/pkg/gnolang/nodes.go | 9 ++++---- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/gnovm/cmd/gno/lint.go b/gnovm/cmd/gno/lint.go index 90717edad45..2943628f0c8 100644 --- a/gnovm/cmd/gno/lint.go +++ b/gnovm/cmd/gno/lint.go @@ -170,36 +170,34 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha return } hasError = true - printRuntimeError(r, pkgPath, stderr) + switch verr := r.(type) { + case *gno.PreprocessError: + err := verr.Unwrap() + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + case error: + fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") + case []error: + for _, err := range verr { + errList, ok := err.(scanner.ErrorList) + if ok { + for _, errorInList := range errList { + fmt.Fprint(stderr, issueFromError(pkgPath, errorInList).String()+"\n") + } + } else { + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + } + } + case string: + fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n") + default: + panic(r) + } }() action() return } -func printRuntimeError(r interface{}, pkgPath string, stderr io.WriteCloser) { - switch verr := r.(type) { - case *gno.PreprocessError: - err := verr.Unwrap() - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - case scanner.ErrorList: - for _, err := range verr { - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - } - case error: - fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") - case []error: - for _, err := range verr { - // recursive call to handle specifically each error type ex: scanner.ErrorList - printRuntimeError(err, pkgPath, stderr) - } - case string: - fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n") - default: - panic(r) - } -} - type lintCode int const ( diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 2da5bf27b1d..9ce30655267 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -578,7 +578,7 @@ func loadTestFuncs(pkgName string, t *testFuncs, tfiles *gno.FileSet) *testFuncs func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { tset = &gno.FileSet{} itset = &gno.FileSet{} - errors := []error{} + var errs error for _, mfile := range memPkg.Files { if !strings.HasSuffix(mfile.Name, ".gno") { continue // skip this file. @@ -588,7 +588,7 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { } n, err := gno.ParseFile(mfile.Name, mfile.Body) if err != nil { - errors = append(errors, err) + errs = multierr.Append(errs, err) continue } if n == nil { @@ -609,8 +609,8 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { memPkg.Name, memPkg.Name, n.PkgName, mfile)) } } - if len(errors) > 0 { - panic(errors) + if errorList := multierr.Errors(errs); len(errorList) > 0 { + panic(errorList) } return tset, itset } diff --git a/gnovm/pkg/gnolang/nodes.go b/gnovm/pkg/gnolang/nodes.go index fc80a1251fd..326166b7ee4 100644 --- a/gnovm/pkg/gnolang/nodes.go +++ b/gnovm/pkg/gnolang/nodes.go @@ -13,6 +13,7 @@ import ( "github.com/gnolang/gno/tm2/pkg/errors" "github.com/gnolang/gno/tm2/pkg/std" + "go.uber.org/multierr" ) // ---------------------------------------- @@ -1172,7 +1173,7 @@ func ReadMemPackageFromList(list []string, pkgPath string) *std.MemPackage { // or [ParseFile] returns an error, ParseMemPackage panics. func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { fset = &FileSet{} - errors := []error{} + var errs error for _, mfile := range memPkg.Files { if !strings.HasSuffix(mfile.Name, ".gno") || endsWith(mfile.Name, []string{"_test.gno", "_filetest.gno"}) { @@ -1180,7 +1181,7 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { } n, err := ParseFile(mfile.Name, mfile.Body) if err != nil { - errors = append(errors, err) + errs = multierr.Append(errs, err) continue } if memPkg.Name != string(n.PkgName) { @@ -1191,8 +1192,8 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { // add package file. fset.AddFiles(n) } - if len(errors) > 0 { - panic(errors) + if errorList := multierr.Errors(errs); len(errorList) > 0 { + panic(errorList) } return fset } From 581947254caaf2922aecef1aa112185e13c0e61b Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Mon, 10 Jun 2024 08:04:54 -0500 Subject: [PATCH 3/5] Add run test --- gnovm/cmd/gno/lint.go | 6 ++++-- gnovm/cmd/gno/run_test.go | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gnovm/cmd/gno/lint.go b/gnovm/cmd/gno/lint.go index 2943628f0c8..41db550f603 100644 --- a/gnovm/cmd/gno/lint.go +++ b/gnovm/cmd/gno/lint.go @@ -174,6 +174,10 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha case *gno.PreprocessError: err := verr.Unwrap() fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + case scanner.ErrorList: + for _, err := range verr { + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") + } case error: fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") case []error: @@ -183,8 +187,6 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha for _, errorInList := range errList { fmt.Fprint(stderr, issueFromError(pkgPath, errorInList).String()+"\n") } - } else { - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") } } case string: diff --git a/gnovm/cmd/gno/run_test.go b/gnovm/cmd/gno/run_test.go index f78c15edb34..1713aab9b20 100644 --- a/gnovm/cmd/gno/run_test.go +++ b/gnovm/cmd/gno/run_test.go @@ -75,6 +75,11 @@ func TestRunApp(t *testing.T) { args: []string{"run", "-debug-addr", "invalidhost:17538", "../../tests/integ/debugger/sample.gno"}, errShouldContain: "listen tcp: lookup invalidhost", }, + { + args: []string{"run", "../../tests/integ/several-files-multiple-errors/"}, + stderrShouldContain: "../../tests/integ/several-files-multiple-errors/file2.gno:3: expected 'IDENT', found '{' (code=2).\n../../tests/integ/several-files-multiple-errors/file2.gno:5: expected type, found '}' (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:6: expected '}', found 'EOF' (code=2).", + errShouldBe: "exit code: 1", + }, // TODO: a test file // TODO: args // TODO: nativeLibs VS stdlibs From ef19eb2cb7ae0ca6e79af20caaa677f6e63629ce Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Thu, 17 Oct 2024 21:05:56 +0200 Subject: [PATCH 4/5] raise coverage --- gnovm/cmd/gno/lint.go | 6 +++--- gnovm/cmd/gno/test.go | 4 ++-- gnovm/pkg/gnolang/nodes.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gnovm/cmd/gno/lint.go b/gnovm/cmd/gno/lint.go index 5aaaf79fe8b..b29a98f5a2a 100644 --- a/gnovm/cmd/gno/lint.go +++ b/gnovm/cmd/gno/lint.go @@ -17,6 +17,7 @@ import ( "github.com/gnolang/gno/gnovm/tests" "github.com/gnolang/gno/tm2/pkg/commands" osm "github.com/gnolang/gno/tm2/pkg/os" + "go.uber.org/multierr" ) type lintCfg struct { @@ -179,9 +180,8 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") } case error: - fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n") - case []error: - for _, err := range verr { + errors := multierr.Errors(verr) + for _, err := range errors { errList, ok := err.(scanner.ErrorList) if ok { for _, errorInList := range errList { diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 640f3e55e14..af7fa28a14d 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -608,8 +608,8 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) { memPkg.Name, memPkg.Name, n.PkgName, mfile)) } } - if errorList := multierr.Errors(errs); len(errorList) > 0 { - panic(errorList) + if errs != nil { + panic(errs) } return tset, itset } diff --git a/gnovm/pkg/gnolang/nodes.go b/gnovm/pkg/gnolang/nodes.go index 91efa5fc3bd..e1a00f6fa04 100644 --- a/gnovm/pkg/gnolang/nodes.go +++ b/gnovm/pkg/gnolang/nodes.go @@ -1209,8 +1209,8 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) { // add package file. fset.AddFiles(n) } - if errorList := multierr.Errors(errs); len(errorList) > 0 { - panic(errorList) + if errs != nil { + panic(errs) } return fset } From 6db5fdc11ec7cbe7da0af7784bcf641fff27dae5 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Fri, 18 Oct 2024 08:49:11 +0200 Subject: [PATCH 5/5] fix tests --- gnovm/cmd/gno/lint.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gnovm/cmd/gno/lint.go b/gnovm/cmd/gno/lint.go index b29a98f5a2a..c6008117f13 100644 --- a/gnovm/cmd/gno/lint.go +++ b/gnovm/cmd/gno/lint.go @@ -175,10 +175,6 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha case *gno.PreprocessError: err := verr.Unwrap() fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - case scanner.ErrorList: - for _, err := range verr { - fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") - } case error: errors := multierr.Errors(verr) for _, err := range errors { @@ -187,6 +183,8 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha for _, errorInList := range errList { fmt.Fprint(stderr, issueFromError(pkgPath, errorInList).String()+"\n") } + } else { + fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n") } } case string: