Skip to content

Commit fba50a7

Browse files
committed
fix: go fmt and lint
Signed-off-by: Norman <norman@samourai.coop>
1 parent f5b7afc commit fba50a7

File tree

8 files changed

+40
-184
lines changed

8 files changed

+40
-184
lines changed

examples/no_cycles_test.go

+19-113
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ package examples_test
22

33
import (
44
"fmt"
5-
"io/fs"
6-
"os"
7-
pathlib "path"
85
"path/filepath"
96
"slices"
107
"strings"
118
"testing"
129

13-
"github.com/gnolang/gno/gnovm"
1410
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
1511
"github.com/gnolang/gno/gnovm/pkg/packages"
1612
"github.com/stretchr/testify/require"
@@ -20,29 +16,17 @@ var injectedTestingLibs = []string{"encoding/json", "fmt", "os", "internal/os_te
2016

2117
// TestNoCycles checks that there is no import cycles in stdlibs and non-draft examples
2218
func TestNoCycles(t *testing.T) {
23-
// find stdlibs
24-
gnoRoot := gnoenv.RootDir()
25-
pkgs, err := listPkgs(packages.Pkg{
26-
Dir: filepath.Join(gnoRoot, "gnovm", "stdlibs"),
27-
Name: "",
28-
})
19+
// find examples and stdlibs
20+
cfg := &packages.LoadConfig{SelfContained: true, Deps: true}
21+
pkgs, err := packages.Load(cfg, filepath.Join(gnoenv.RootDir(), "examples", "..."))
2922
require.NoError(t, err)
3023

31-
// find examples
32-
examples, err := packages.ListPkgs(filepath.Join(gnoRoot, "examples"))
33-
require.NoError(t, err)
34-
for _, example := range examples {
35-
if example.Draft {
36-
continue
37-
}
38-
examplePkgs, err := listPkgs(example)
39-
require.NoError(t, err)
40-
pkgs = append(pkgs, examplePkgs...)
41-
}
42-
4324
// detect cycles
4425
visited := make(map[string]bool)
4526
for _, p := range pkgs {
27+
if p.Draft {
28+
continue
29+
}
4630
require.NoError(t, detectCycles(p, pkgs, visited))
4731
}
4832
}
@@ -73,7 +57,7 @@ func TestNoCycles(t *testing.T) {
7357
// - foo_pkg/foo.go imports bar_pkg
7458
//
7559
// - bar_pkg/bar_test.go imports foo_pkg
76-
func detectCycles(root testPkg, pkgs []testPkg, visited map[string]bool) error {
60+
func detectCycles(root *packages.Package, pkgs []*packages.Package, visited map[string]bool) error {
7761
// check cycles in package's sources
7862
stack := []string{}
7963
if err := visitPackage(root, pkgs, visited, stack); err != nil {
@@ -86,8 +70,8 @@ func detectCycles(root testPkg, pkgs []testPkg, visited map[string]bool) error {
8670

8771
// check cycles in tests' imports by marking the current package as visited while visiting the tests' imports
8872
// we also consider PackageSource imports here because tests can call package code
89-
visited = map[string]bool{root.PkgPath: true}
90-
stack = []string{root.PkgPath}
73+
visited = map[string]bool{root.ImportPath: true}
74+
stack = []string{root.ImportPath}
9175
if err := visitImports([]packages.FileKind{packages.FileKindPackageSource, packages.FileKindTest}, root, pkgs, visited, stack); err != nil {
9276
return fmt.Errorf("test import: %w", err)
9377
}
@@ -96,14 +80,14 @@ func detectCycles(root testPkg, pkgs []testPkg, visited map[string]bool) error {
9680
}
9781

9882
// visitImports resolves and visits imports by kinds
99-
func visitImports(kinds []packages.FileKind, root testPkg, pkgs []testPkg, visited map[string]bool, stack []string) error {
83+
func visitImports(kinds []packages.FileKind, root *packages.Package, pkgs []*packages.Package, visited map[string]bool, stack []string) error {
10084
for _, imp := range root.Imports.Merge(kinds...) {
101-
if slices.Contains(injectedTestingLibs, imp.PkgPath) {
85+
if slices.Contains(injectedTestingLibs, imp) {
10286
continue
10387
}
104-
idx := slices.IndexFunc(pkgs, func(p testPkg) bool { return p.PkgPath == imp.PkgPath })
88+
idx := slices.IndexFunc(pkgs, func(p *packages.Package) bool { return p.ImportPath == imp })
10589
if idx == -1 {
106-
return fmt.Errorf("import %q not found for %q tests", imp.PkgPath, root.PkgPath)
90+
return fmt.Errorf("import %q not found for %q tests", imp, root.ImportPath)
10791
}
10892
if err := visitPackage(pkgs[idx], pkgs, visited, stack); err != nil {
10993
return fmt.Errorf("test import error: %w", err)
@@ -114,98 +98,20 @@ func visitImports(kinds []packages.FileKind, root testPkg, pkgs []testPkg, visit
11498
}
11599

116100
// visitNode visits a package and its imports recursively. It only considers imports in PackageSource
117-
func visitPackage(pkg testPkg, pkgs []testPkg, visited map[string]bool, stack []string) error {
118-
if slices.Contains(stack, pkg.PkgPath) {
119-
return fmt.Errorf("cycle detected: %s -> %s", strings.Join(stack, " -> "), pkg.PkgPath)
101+
func visitPackage(pkg *packages.Package, pkgs []*packages.Package, visited map[string]bool, stack []string) error {
102+
if slices.Contains(stack, pkg.ImportPath) {
103+
return fmt.Errorf("cycle detected: %s -> %s", strings.Join(stack, " -> "), pkg.ImportPath)
120104
}
121-
if visited[pkg.PkgPath] {
105+
if visited[pkg.ImportPath] {
122106
return nil
123107
}
124108

125-
visited[pkg.PkgPath] = true
126-
stack = append(stack, pkg.PkgPath)
109+
visited[pkg.ImportPath] = true
110+
stack = append(stack, pkg.ImportPath)
127111

128112
if err := visitImports([]packages.FileKind{packages.FileKindPackageSource}, pkg, pkgs, visited, stack); err != nil {
129113
return err
130114
}
131115

132116
return nil
133117
}
134-
135-
type testPkg struct {
136-
Dir string
137-
PkgPath string
138-
Imports packages.ImportsMap
139-
}
140-
141-
// listPkgs lists all packages in rootMod
142-
func listPkgs(rootMod packages.Pkg) ([]testPkg, error) {
143-
res := []testPkg{}
144-
rootDir := rootMod.Dir
145-
visited := map[string]struct{}{}
146-
if err := fs.WalkDir(os.DirFS(rootDir), ".", func(path string, d fs.DirEntry, err error) error {
147-
if err != nil {
148-
return err
149-
}
150-
if d.IsDir() {
151-
return nil
152-
}
153-
if !strings.HasSuffix(d.Name(), ".gno") {
154-
return nil
155-
}
156-
subPath := filepath.Dir(path)
157-
dir := filepath.Join(rootDir, subPath)
158-
if _, ok := visited[dir]; ok {
159-
return nil
160-
}
161-
visited[dir] = struct{}{}
162-
163-
subPkgPath := pathlib.Join(rootMod.Name, subPath)
164-
165-
pkg := testPkg{
166-
Dir: dir,
167-
PkgPath: subPkgPath,
168-
}
169-
170-
memPkg, err := readPkg(pkg.Dir, pkg.PkgPath)
171-
if err != nil {
172-
return fmt.Errorf("read pkg %q: %w", pkg.Dir, err)
173-
}
174-
pkg.Imports, err = packages.Imports(memPkg, nil)
175-
if err != nil {
176-
return fmt.Errorf("list imports of %q: %w", memPkg.Path, err)
177-
}
178-
179-
res = append(res, pkg)
180-
return nil
181-
}); err != nil {
182-
return nil, fmt.Errorf("walk dirs at %q: %w", rootDir, err)
183-
}
184-
return res, nil
185-
}
186-
187-
// readPkg reads the sources of a package. It includes all .gno files but ignores the package name
188-
func readPkg(dir string, pkgPath string) (*gnovm.MemPackage, error) {
189-
list, err := os.ReadDir(dir)
190-
if err != nil {
191-
return nil, err
192-
}
193-
memPkg := &gnovm.MemPackage{Path: pkgPath}
194-
for _, entry := range list {
195-
fpath := filepath.Join(dir, entry.Name())
196-
if !strings.HasSuffix(fpath, ".gno") {
197-
continue
198-
}
199-
fname := filepath.Base(fpath)
200-
bz, err := os.ReadFile(fpath)
201-
if err != nil {
202-
return nil, err
203-
}
204-
memPkg.Files = append(memPkg.Files,
205-
&gnovm.MemFile{
206-
Name: fname,
207-
Body: string(bz),
208-
})
209-
}
210-
return memPkg, nil
211-
}

gnovm/cmd/gno/test.go

-32
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"flag"
66
"fmt"
77
goio "io"
8-
"log"
9-
"path/filepath"
10-
"strings"
118
"time"
129

1310
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
@@ -248,32 +245,3 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {
248245

249246
return nil
250247
}
251-
252-
// attempts to determine the full gno pkg path by analyzing the directory.
253-
func pkgPathFromRootDir(pkgPath, rootDir string) string {
254-
abPkgPath, err := filepath.Abs(pkgPath)
255-
if err != nil {
256-
log.Printf("could not determine abs path: %v", err)
257-
return ""
258-
}
259-
abRootDir, err := filepath.Abs(rootDir)
260-
if err != nil {
261-
log.Printf("could not determine abs path: %v", err)
262-
return ""
263-
}
264-
abRootDir += string(filepath.Separator)
265-
if !strings.HasPrefix(abPkgPath, abRootDir) {
266-
return ""
267-
}
268-
impPath := strings.ReplaceAll(abPkgPath[len(abRootDir):], string(filepath.Separator), "/")
269-
for _, prefix := range [...]string{
270-
"examples/",
271-
"gnovm/stdlibs/",
272-
"gnovm/tests/stdlibs/",
273-
} {
274-
if strings.HasPrefix(impPath, prefix) {
275-
return impPath[len(prefix):]
276-
}
277-
}
278-
return ""
279-
}

gnovm/pkg/doc/dirs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func packageImportsRecursive(root string, pkgPath string) []string {
128128

129129
for _, imp := range sub {
130130
if !slices.Contains(res, imp) {
131-
res = append(res, imp) //nolint:makezero
131+
res = append(res, imp)
132132
}
133133
}
134134
}

gnovm/pkg/gnolang/debugger_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
1414
"github.com/gnolang/gno/gnovm/pkg/gnolang"
15+
"github.com/gnolang/gno/gnovm/pkg/packages"
1516
"github.com/gnolang/gno/gnovm/pkg/test"
1617
)
1718

@@ -39,7 +40,7 @@ func evalTest(debugAddr, in, file string) (out, err string) {
3940
err = strings.TrimSpace(strings.ReplaceAll(err, "../../tests/files/", "files/"))
4041
}()
4142

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

4445
f := gnolang.MustReadFile(file)
4546

gnovm/pkg/gnolang/files_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"github.com/gnolang/gno/gnovm/pkg/gnolang"
15+
"github.com/gnolang/gno/gnovm/pkg/packages"
1516
"github.com/gnolang/gno/gnovm/pkg/test"
1617
"github.com/stretchr/testify/require"
1718
)
@@ -46,7 +47,7 @@ func TestFiles(t *testing.T) {
4647
Sync: *withSync,
4748
}
4849
o.BaseStore, o.TestStore = test.Store(
49-
rootDir, true,
50+
rootDir, make(map[string]*packages.Package), true,
5051
nopReader{}, o.WriterForStore(), io.Discard,
5152
)
5253
return o
@@ -121,7 +122,7 @@ func TestStdlibs(t *testing.T) {
121122
capture = new(bytes.Buffer)
122123
out = capture
123124
}
124-
opts = test.NewTestOptions(rootDir, nopReader{}, out, out)
125+
opts = test.NewTestOptions(rootDir, make(map[string]*packages.Package), nopReader{}, out, out)
125126
opts.Verbose = true
126127
return
127128
}

gnovm/pkg/gnomod/parse_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gnomod
22

33
import (
4+
"os"
45
"path/filepath"
56
"testing"
67

@@ -237,3 +238,16 @@ func TestParseGnoMod(t *testing.T) {
237238
})
238239
}
239240
}
241+
242+
func createGnoModPkg(t *testing.T, dirPath, pkgName, modData string) {
243+
t.Helper()
244+
245+
// Create package dir
246+
pkgDirPath := filepath.Join(dirPath, pkgName)
247+
err := os.MkdirAll(pkgDirPath, 0o755)
248+
require.NoError(t, err)
249+
250+
// Create gno.mod
251+
err = os.WriteFile(filepath.Join(pkgDirPath, "gno.mod"), []byte(modData), 0o644)
252+
require.NoError(t, err)
253+
}

gnovm/pkg/packages/analyze_packages.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func readPkg(pkgDir string, importPath string) *Package {
9898
// TODO: check if stdlib
9999

100100
pkg.Root, err = gnomod.FindRootDir(pkgDir)
101-
if err == gnomod.ErrGnoModNotFound {
101+
if errors.Is(err, gnomod.ErrGnoModNotFound) {
102102
return pkg
103103
}
104104
if err != nil {

gnovm/pkg/packages/importer.go

-34
This file was deleted.

0 commit comments

Comments
 (0)