@@ -15,7 +15,6 @@ import (
15
15
"github.com/gnolang/gno/gnovm/pkg/packages"
16
16
)
17
17
18
- // TODO: only allow injected testing libs in tests
19
18
var injectedTestingLibs = []string {"encoding/json" , "fmt" , "os" , "internal/os_test" }
20
19
21
20
func main () {
@@ -50,9 +49,10 @@ type testPkg struct {
50
49
Imports packages.ImportsMap
51
50
}
52
51
53
- func listPkgs (rootPkg gnomod.Pkg ) []testPkg {
52
+ // listPkgs lists all packages in rootMod
53
+ func listPkgs (rootMod gnomod.Pkg ) []testPkg {
54
54
res := []testPkg {}
55
- rootDir := rootPkg .Dir
55
+ rootDir := rootMod .Dir
56
56
visited := map [string ]struct {}{}
57
57
if err := fs .WalkDir (os .DirFS (rootDir ), "." , func (path string , d fs.DirEntry , err error ) error {
58
58
if d .IsDir () {
@@ -68,7 +68,7 @@ func listPkgs(rootPkg gnomod.Pkg) []testPkg {
68
68
}
69
69
visited [dir ] = struct {}{}
70
70
71
- subPkgPath := pathlib .Join (rootPkg .Name , subPath )
71
+ subPkgPath := pathlib .Join (rootMod .Name , subPath )
72
72
73
73
pkg := testPkg {
74
74
Dir : dir ,
@@ -100,26 +100,34 @@ func fileImportsToStrings(fis []packages.FileImport) []string {
100
100
return res
101
101
}
102
102
103
- // detectCycles detect import cycles
103
+ // detectCycles detects import cycles
104
+ //
105
+ // We need to check
106
+ // 3 kinds of nodes
104
107
//
105
- // We need to check 3 kinds of nodes
106
108
// - normal pkg: compiled source
109
+ //
107
110
// - xtest pkg: external test source (include xtests and filetests), can be treated as their own package
108
- // - test pkg: embedded test sources, these should not have their corresponding normal package in the stack
109
111
//
110
- // the tricky thing is that we need to split test sources and normal source
112
+ // - test pkg: embedded test sources,
113
+ // these should not have their corresponding normal package in their dependencies tree
114
+ //
115
+ // The tricky thing is that we need to split test sources and normal source
111
116
// while not considering them as distincitive packages.
112
- // Because if we don't we will have false positive if for example we have these edges:
117
+ // Because if we don't we will have false positive for example if we have these edges:
118
+ //
119
+ // - foo_pkg/foo_test.go imports bar_pkg
120
+ //
121
+ // - bar_pkg/bar_test.go import foo_pkg
113
122
//
114
- // foo_pkg/foo_test.go -> bar_pkg/bar.go
115
- // bar_pkg/bar_test.go -> foo_pkg/foo.go
123
+ // In go, the above example is allowed
124
+ // but the following is not
116
125
//
117
- // # The above example if allowed but the following is not
126
+ // - foo_pkg/foo.go imports bar_pkg
118
127
//
119
- // foo_pkg/foo.go -> bar_pkg/bar.go
120
- // bar_pkg/bar_test.go -> foo_pkg/foo.go
128
+ // - bar_pkg/bar_test.go imports foo_pkg
121
129
//
122
- // This implementation can probably be optimized with better graph theory
130
+ // This implementation can be optimized with better graph theory
123
131
func detectCycles (root testPkg , pkgs []testPkg ) {
124
132
// check normal cycles
125
133
{
@@ -218,6 +226,7 @@ func visitPackage(pkg testPkg, pkgs []testPkg, visited map[string]bool, stack []
218
226
return nil
219
227
}
220
228
229
+ // readPkg reads the sources of a package. It includes all .gno files but ignore the package name
221
230
func readPkg (dir string , pkgPath string ) (* gnovm.MemPackage , error ) {
222
231
list , err := os .ReadDir (dir )
223
232
if err != nil {
0 commit comments