Skip to content

Commit

Permalink
feat!: forbid importing '/r' from '/p'
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed May 3, 2024
1 parent 56b5bc0 commit 5ca873d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
19 changes: 19 additions & 0 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package std

import (
"fmt"
"go/parser"
"go/token"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -80,6 +82,23 @@ func (mempkg *MemPackage) Validate() error {
prev = file.Name
}

// If its a package, check if it imports realm
if strings.HasPrefix(mempkg.Path, "gno.land/p/") {
for _, file := range mempkg.Files {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file.Name, file.Body, parser.ImportsOnly)
if err != nil {
return fmt.Errorf("unable to parse %q", file.Name)

Check warning on line 91 in tm2/pkg/std/memfile.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/std/memfile.go#L91

Added line #L91 was not covered by tests
}
for _, imp := range astFile.Imports {
importPath := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, `"`), `"`)
if strings.HasPrefix(importPath, "gno.land/r/") {
return fmt.Errorf("package %q imports realm %q", mempkg.Path, importPath)
}
}
}
}

return nil
}

Expand Down
92 changes: 83 additions & 9 deletions tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,104 @@ func TestMemPackage_Validate(t *testing.T) {
{
"Correct",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: "package hey",
},
},
},
"",
},
{
"Unsorted",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "b.gno"}, {Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "b.gno",
Body: "package hey",
},
{
Name: "a.gno",
Body: "package hey",
},
},
},
`mempackage "gno.land/r/demo/hey" has unsorted files`,
},
{
"Duplicate",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{{Name: "a.gno"}, {Name: "a.gno"}},
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: "package hey",
},
{
Name: "a.gno",
Body: "package hey",
},
},
},
`duplicate file name "a.gno"`,
},
{
"Package Imports Package",
&MemPackage{
Name: "hey",
Path: "gno.land/p/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey
import "gno.land/p/demo/hello"
`,
},
},
},
"",
},
{
"Realm Imports Realm",
&MemPackage{
Name: "hey",
Path: "gno.land/r/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey
import "gno.land/r/demo/hello"
`,
},
},
},
"",
},
{
"Package Imports Realm",
&MemPackage{
Name: "hey",
Path: "gno.land/p/demo/hey",
Files: []*MemFile{
{
Name: "a.gno",
Body: `package hey
import "gno.land/r/demo/heyrealm"
`,
},
},
},
`package "gno.land/p/demo/hey" imports realm "gno.land/r/demo/heyrealm"`,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 5ca873d

Please sign in to comment.