Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lint all files in folder before panicking #2202

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"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 {
Expand Down Expand Up @@ -174,12 +175,18 @@
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")
errors := multierr.Errors(verr)
for _, err := range errors {
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")
}

Check warning on line 188 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L186-L188

Added lines #L186 - L188 were not covered by tests
}
case string:
fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n")
default:
Expand Down
4 changes: 4 additions & 0 deletions gnovm/cmd/gno/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
Expand Down
5 changes: 5 additions & 0 deletions gnovm/cmd/gno/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func TestRunApp(t *testing.T) {
args: []string{"run", "-expr", "Context()", "../../tests/integ/context/context.gno"},
stdoutShouldContain: "Context worked",
},
{
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
Expand Down
7 changes: 6 additions & 1 deletion gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@
func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) {
tset = &gno.FileSet{}
itset = &gno.FileSet{}
var errs error
for _, mfile := range memPkg.Files {
if !strings.HasSuffix(mfile.Name, ".gno") {
continue // skip this file.
Expand All @@ -586,7 +587,8 @@
}
n, err := gno.ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(err)
errs = multierr.Append(errs, err)
continue

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

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L590-L591

Added lines #L590 - L591 were not covered by tests
}
if n == nil {
panic("should not happen")
Expand All @@ -606,6 +608,9 @@
memPkg.Name, memPkg.Name, n.PkgName, mfile))
}
}
if errs != nil {
panic(errs)

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

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L612

Added line #L612 was not covered by tests
}
return tset, itset
}

Expand Down
8 changes: 7 additions & 1 deletion gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
"go.uber.org/multierr"
)

// ----------------------------------------
Expand Down Expand Up @@ -1189,14 +1190,16 @@
// or [ParseFile] returns an error, ParseMemPackage panics.
func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) {
fset = &FileSet{}
var errs error
for _, mfile := range memPkg.Files {
if !strings.HasSuffix(mfile.Name, ".gno") ||
endsWith(mfile.Name, []string{"_test.gno", "_filetest.gno"}) {
continue // skip spurious or test file.
}
n, err := ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(err)
errs = multierr.Append(errs, err)
continue

Check warning on line 1202 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L1201-L1202

Added lines #L1201 - L1202 were not covered by tests
}
if memPkg.Name != string(n.PkgName) {
panic(fmt.Sprintf(
Expand All @@ -1206,6 +1209,9 @@
// add package file.
fset.AddFiles(n)
}
if errs != nil {
panic(errs)

Check warning on line 1213 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L1213

Added line #L1213 was not covered by tests
}
return fset
}

Expand Down
5 changes: 5 additions & 0 deletions gnovm/tests/integ/several-files-multiple-errors/file2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type{

}
1 change: 1 addition & 0 deletions gnovm/tests/integ/several-files-multiple-errors/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/tests/severalerrors
6 changes: 6 additions & 0 deletions gnovm/tests/integ/several-files-multiple-errors/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
for {
_ example
}
Loading