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 edfbdf9
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
20 changes: 20 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,24 @@ func (mempkg *MemPackage) Validate() error {
prev = file.Name
}

// Check if package imports realm
// TODO(hariom): move this to a separate helper
if strings.HasPrefix(mempkg.Path, "gno.land/p") { // TODO(hariom): Use !gnolang.IsRealmPath() instead
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 92 in tm2/pkg/std/memfile.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/std/memfile.go#L92

Added line #L92 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") { // TODO(hariom): Use gnolang.IsRealmPath() instead
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 edfbdf9

Please sign in to comment.