Skip to content

Commit

Permalink
chore: fix debugger
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 10, 2025
1 parent 1d67048 commit c0eacfb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
24 changes: 15 additions & 9 deletions gnovm/pkg/gnolang/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"path/filepath"
"strings"
"testing"
"time"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/packages"
"github.com/gnolang/gno/gnovm/pkg/test"
"github.com/stretchr/testify/require"
)

type dtest struct{ in, out string }
Expand All @@ -25,7 +27,7 @@ type writeNopCloser struct{ io.Writer }
func (writeNopCloser) Close() error { return nil }

// TODO (Marc): move evalTest to gnovm/tests package and remove code duplicates
func evalTest(debugAddr, in, file string) (out, err string) {
func evalTest(debugAddr, in, file string, pkgs packages.PackagesMap) (out, err string) {
bout := bytes.NewBufferString("")
berr := bytes.NewBufferString("")
stdin := bytes.NewBufferString(in)
Expand All @@ -40,7 +42,7 @@ func evalTest(debugAddr, in, file string) (out, err string) {
err = strings.TrimSpace(strings.ReplaceAll(err, "../../tests/files/", "files/"))
}()

_, testStore := test.Store(gnoenv.RootDir(), map[string]*packages.Package{}, false, stdin, stdout, stderr)
_, testStore := test.Store(gnoenv.RootDir(), pkgs, false, stdin, stdout, stderr)

f := gnolang.MustReadFile(file)

Expand Down Expand Up @@ -69,12 +71,12 @@ func evalTest(debugAddr, in, file string) (out, err string) {
return
}

func runDebugTest(t *testing.T, targetPath string, tests []dtest) {
func runDebugTest(t *testing.T, targetPath string, tests []dtest, pkgs packages.PackagesMap) {
t.Helper()

for _, test := range tests {
t.Run("", func(t *testing.T) {
out, err := evalTest("", test.in, targetPath)
out, err := evalTest("", test.in, targetPath, pkgs)
t.Log("in:", test.in, "out:", out, "err:", err)
if !strings.Contains(out, test.out) {
t.Errorf("unexpected output\nwant\"%s\"\n got \"%s\"", test.out, out)
Expand All @@ -88,6 +90,10 @@ func TestDebug(t *testing.T) {
cont := brk + "continue\n"
cont2 := "break 21\ncontinue\n"

pkgs, err := packages.Load(&packages.LoadConfig{}, filepath.FromSlash("../../../examples/..."))
require.NoError(t, err)
pkgsMap := packages.NewPackagesMap(pkgs...)

runDebugTest(t, debugTarget, []dtest{
{in: "\n", out: "Welcome to the Gnovm debugger. Type 'help' for list of commands."},
{in: "help\n", out: "The following commands are available"},
Expand Down Expand Up @@ -144,18 +150,18 @@ func TestDebug(t *testing.T) {
{in: "b 27\nc\np b\n", out: `("!zero" string)`},
{in: "b 22\nc\np t.A[3]\n", out: "Command failed: &{(\"slice index out of bounds: 3 (len=3)\" string) <nil> }"},
{in: "b 43\nc\nc\nc\np i\ndetach\n", out: "(1 int)"},
})
}, pkgsMap)

runDebugTest(t, "../../tests/files/a1.gno", []dtest{
{in: "l\n", out: "unknown source file"},
{in: "b 5\n", out: "unknown source file"},
})
}, pkgsMap)

runDebugTest(t, "../../tests/integ/debugger/sample2.gno", []dtest{
{in: "s\np tests\n", out: "(package(tests gno.land/p/demo/tests) package{})"},
{in: "s\np tests.World\n", out: `("world" <untyped> string)`},
{in: "s\np tests.xxx\n", out: "Command failed: invalid selector: xxx"},
})
}, pkgsMap)
}

const debugAddress = "localhost:17358"
Expand All @@ -167,7 +173,7 @@ func TestRemoteDebug(t *testing.T) {
retry int
)

go evalTest(debugAddress, "", debugTarget)
go evalTest(debugAddress, "", debugTarget, nil)

for retry = 100; retry > 0; retry-- {
conn, err = net.Dial("tcp", debugAddress)
Expand All @@ -190,7 +196,7 @@ func TestRemoteDebug(t *testing.T) {
}

func TestRemoteError(t *testing.T) {
_, err := evalTest(":xxx", "", debugTarget)
_, err := evalTest(":xxx", "", debugTarget, nil)
t.Log("err:", err)
if !strings.Contains(err, "tcp/xxx: unknown port") &&
!strings.Contains(err, "tcp/xxx: nodename nor servname provided, or not known") {
Expand Down
6 changes: 5 additions & 1 deletion gnovm/pkg/gnolang/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestFiles(t *testing.T) {
rootDir, err := filepath.Abs("../../../")
require.NoError(t, err)

pkgs, err := packages.Load(&packages.LoadConfig{}, filepath.Join(rootDir, "examples", "...."))
require.NoError(t, err)
pkgsMap := packages.NewPackagesMap(pkgs...)

newOpts := func() *test.TestOptions {
o := &test.TestOptions{
RootDir: rootDir,
Expand All @@ -47,7 +51,7 @@ func TestFiles(t *testing.T) {
Sync: *withSync,
}
o.BaseStore, o.TestStore = test.Store(
rootDir, make(map[string]*packages.Package), true,
rootDir, pkgsMap, true,
nopReader{}, o.WriterForStore(), io.Discard,
)
return o
Expand Down
3 changes: 2 additions & 1 deletion gnovm/pkg/packages/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/packages"
. "github.com/gnolang/gno/gnovm/pkg/packages"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -114,7 +115,7 @@ func TestImports(t *testing.T) {
// - ignore subdirs
// - ignore duplicate
// - should be sorted
expected := map[FileKind][]string{
expected := packages.ImportsMap{
FileKindPackageSource: {
"gno.land/p/demo/pkg1",
"gno.land/p/demo/pkg2",
Expand Down
2 changes: 0 additions & 2 deletions gnovm/pkg/packages/pkglist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func (pl PkgList) Sort() (SortedPkgList, error) {
return sortedPkgs, nil
}

var injectedTestingLibs = []string{"encoding/json", "fmt", "os", "crypto/sha256"}

// visitNode visits a package's and its dependencies dependencies and adds them to the sorted list.
func visitPackage(pkg *Package, pkgs []*Package, visited, onStack map[string]bool, sortedPkgs *[]*Package) error {
if onStack[pkg.ImportPath] {
Expand Down

0 comments on commit c0eacfb

Please sign in to comment.