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

fix(pkg/doc): ensure mod packages are loaded correctly #1306

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 7 additions & 25 deletions gnovm/cmd/gno/doc_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
package main

import "testing"
import (
"testing"

func TestGnoDoc(t *testing.T) {
tc := []testMainCase{
{
args: []string{"doc", "io.Writer"},
stdoutShouldContain: "Writer is the interface that wraps",
},
{
args: []string{"doc", "avl"},
stdoutShouldContain: "func NewTree",
},
{
args: []string{"doc", "-u", "avl.Node"},
stdoutShouldContain: "node *Node",
},
{
args: []string{"doc", "dkfdkfkdfjkdfj"},
errShouldContain: "package not found",
},
{
args: []string{"doc", "There.Are.Too.Many.Dots"},
errShouldContain: "invalid arguments",
},
}
testMainCaseRun(t, tc)
"github.com/rogpeppe/go-internal/testscript"
)

func TestDoc(t *testing.T) {
testscript.Run(t, setupTestScript(t, "testdata/gno_doc"))
}
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/invalid_args.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Test with an invalid syntax for gno doc

! gno doc There.Are.Too.Many.Dots

! stdout .+
stderr 'invalid arguments'
53 changes: 53 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/mod_order.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Ensure that imported gno.mods are processed after a first run through modDirs
# Underlying cause of https://github.com/gnolang/gno/pull/1301

env GNOROOT=${WORK}
env GNO_HOME=${WORK}/home

gno doc pkg1

stdout Pkg1\(\)
! stderr .+

gno doc pkg2

stdout Pkg2\(\)
! stderr .+

gno doc pkg3

stdout Pkg3\(\)
! stderr .+

-- gnovm/stdlibs/pkg1/hello.gno --
package pkg1

func Pkg1() {}

-- examples/pkg2/gno.mod --
module pkg2

require (
pkg3 v0.0.0-latest
)

-- examples/pkg2/pkg2.gno --
package pkg2

func Pkg2() {}

-- examples/pkg3/gno.mod --
module pkg3

-- examples/pkg3/pkg3.gno --
package pkg3

func Pkg3() {}

-- home/pkg/mod/pkg3/gno.mod --
module pkg3

-- home/pkg/mod/pkg3/pkg3.gno --
package pkg3

func Pkg3Mod() {}
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/ok_avl.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Test getting documentation for package avl

gno doc avl

stdout 'func NewTree'
! stderr .+
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/ok_avl_unexp.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Test getting an unexported struct field from avl

gno doc -u avl.Tree

stdout 'node \*Node'
! stderr .+
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/ok_io_writer.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Gno doc on a standard library function

gno doc io.Writer

stdout 'Writer is the interface that wraps'
! stderr .+
6 changes: 6 additions & 0 deletions gnovm/cmd/gno/testdata/gno_doc/package_not_found.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# query an invalid package

! gno doc ajfdhalksdsadkj

! stdout .+
stderr 'package not found'
32 changes: 26 additions & 6 deletions gnovm/pkg/doc/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,48 @@
scan: make(chan bfsDir),
}

// to perform de-duplication.
added := make(map[bfsDir]struct{}, 256)

roots := make([]bfsDir, 0, len(dirs)+len(modDirs))
for _, dir := range dirs {
roots = append(roots, bfsDir{
d := bfsDir{
dir: dir,
importPath: "",
})
}
if _, ok := added[d]; !ok {
roots = append(roots, d)
added[d] = struct{}{}
}
}

// enforce putting first "explicit" dirs, then dirs imported through gno.mod.
var modDirRoots []bfsDir

for _, mdir := range modDirs {
gm, err := parseGnoMod(filepath.Join(mdir, "gno.mod"))
if err != nil {
log.Printf("%v", err)
continue
}
roots = append(roots, bfsDir{
d := bfsDir{
dir: mdir,
importPath: gm.Module.Mod.Path,
})
roots = append(roots, getGnoModDirs(gm)...)
}
if _, ok := added[d]; ok {
continue

Check warning on line 75 in gnovm/pkg/doc/dirs.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/doc/dirs.go#L75

Added line #L75 was not covered by tests
}
roots = append(roots, d)
added[d] = struct{}{}
for _, rt := range getGnoModDirs(gm) {
if _, ok := added[rt]; !ok {
modDirRoots = append(modDirRoots, rt)
added[rt] = struct{}{}
}
}
}

go d.walk(roots)
go d.walk(append(roots, modDirRoots...))
return d
}

Expand Down
Loading