Skip to content
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
93 changes: 82 additions & 11 deletions gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,99 @@ func (c *runCmd) RegisterFlags(fs *flag.FlagSet) {
)
}

func packageNameFromFirstFile(args []string) (string, error) {
func packageNameFromFiles(args []string) (string, error) {
var (
firstPkgName string
firstPkgFile string
foundAny bool
)

for _, arg := range args {
if s, err := os.Stat(arg); err == nil && s.IsDir() {
s, err := os.Stat(arg)
if err != nil {
return "", err
}

// ---- Directory case ----
if s.IsDir() {
files, err := os.ReadDir(arg)
if err != nil {
return "", err
}

dirFoundAny := false

for _, f := range files {
n := f.Name()
if isGnoFile(f) &&
!strings.HasSuffix(n, "_test.gno") &&
!strings.HasSuffix(n, "_filetest.gno") {
return gno.ParseFilePackageName(filepath.Join(arg, n))
if !isGnoFile(f) ||
strings.HasSuffix(n, "_test.gno") ||
strings.HasSuffix(n, "_filetest.gno") {
continue
}

fullPath := filepath.Join(arg, n)
firstPkgName, firstPkgFile, err = updatePackageInfo(fullPath, firstPkgName, firstPkgFile)
if err != nil {
return "", err
}
foundAny = true
dirFoundAny = true
}
} else if err != nil {

// when directory has only test files
if !dirFoundAny {
return "", fmt.Errorf("gno: no non-test Gno files in %s", arg)
}

continue
}

// ---- File case ----
n := filepath.Base(arg)
if strings.HasSuffix(n, "_test.gno") || strings.HasSuffix(n, "_filetest.gno") {
return "", fmt.Errorf("gno run: cannot run test files (%s), use gno test instead", n)
}

firstPkgName, firstPkgFile, err = updatePackageInfo(arg, firstPkgName, firstPkgFile)
if err != nil {
return "", err
} else {
return gno.ParseFilePackageName(arg)
}
foundAny = true
}

if !foundAny {
return "", fmt.Errorf("no valid gno file found")
}
return "", nil

return firstPkgName, nil
}

// updatePackageInfo parses the package name of a given .gno file
// and compares it with the first known package. It returns updated values
// for firstPkgName and firstPkgFile, or an error if a mismatch is found.
func updatePackageInfo(
path string,
firstPkgName, firstPkgFile string,
) (string, string, error) {
pkgName, err := gno.ParseFilePackageName(path)
if err != nil {
return firstPkgName, firstPkgFile, err
}

if firstPkgName == "" {
// First valid file sets the base package
return pkgName, path, nil
}

if pkgName != firstPkgName {
return firstPkgName, firstPkgFile, fmt.Errorf(
"found mismatched packages %s (%s) and %s (%s)",
firstPkgName, filepath.Base(firstPkgFile),
pkgName, filepath.Base(path),
)
}

return firstPkgName, firstPkgFile, nil
}

func execRun(cfg *runCmd, args []string, cio commands.IO) error {
Expand All @@ -125,7 +196,7 @@ func execRun(cfg *runCmd, args []string, cio commands.IO) error {
}

var send std.Coins
pkgPath, err := packageNameFromFirstFile(args)
pkgPath, err := packageNameFromFiles(args)
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions gnovm/cmd/gno/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func TestRunApp(t *testing.T) {
args: []string{"run", "../../tests/integ/run_package"},
errShouldContain: "name main not declared",
},
{
args: []string{"run", "../../tests/integ/package_mismatched"},
errShouldContain: "found mismatched packages",
},
{
args: []string{"run", "../../tests/integ/run_main/main.gno", "../../tests/integ/run_namedpkg"},
errShouldContain: "found mismatched packages",
},
{
args: []string{"run", "-expr", "Hello()", "../../tests/integ/run_package"},
stdoutShouldContain: "called Hello",
Expand Down Expand Up @@ -66,10 +74,6 @@ func TestRunApp(t *testing.T) {
args: []string{"run", "-expr", "WithArg(-255)", "../../tests/integ/run_package"},
stdoutShouldContain: "out of range!",
},
{
args: []string{"run", "../../tests/integ/undefined_variable/undefined_variables_test.gno"},
recoverShouldContain: "--- preprocess stack ---", // should contain preprocess debug stack trace
},
{
args: []string{"run", "-debug", "../../tests/integ/debugger/sample.gno"},
stdoutShouldContain: "Welcome to the Gnovm debugger",
Expand Down Expand Up @@ -99,7 +103,14 @@ func TestRunApp(t *testing.T) {
}(),
errShouldBe: "exit code: 1",
},
// TODO: a test file
{
args: []string{"run", "../../tests/integ/undefined_variable/undefined_variables_test.gno"},
errShouldContain: "gno run: cannot run test files (undefined_variables_test.gno)",
},
{
args: []string{"run", "../../tests/integ/package_testonly"},
errShouldContain: "no non-test Gno files in ../../tests/integ/package_testonly",
},
// TODO: args
// TODO: nativeLibs VS stdlibs
// TODO: with gas meter
Expand Down
5 changes: 5 additions & 0 deletions gnovm/tests/integ/package_mismatched/hello.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello

func hello() {
println(hello)
}
5 changes: 5 additions & 0 deletions gnovm/tests/integ/package_mismatched/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
hello()
}
5 changes: 5 additions & 0 deletions gnovm/tests/integ/package_testonly/hello_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
hello()
}
5 changes: 5 additions & 0 deletions gnovm/tests/integ/package_testonly/hello_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hello

func hello() {
println(hello)
}
Loading