Skip to content

Commit 4cc9197

Browse files
committed
improve glob feature and also exclude hidden folders from the path walk
1 parent c4465d3 commit 4cc9197

File tree

7 files changed

+171
-17
lines changed

7 files changed

+171
-17
lines changed

starport/pkg/cosmosanalysis/module/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (d *moduleDiscoverer) discover(pkg protoanalysis.Package) (Module, error) {
224224

225225
func (d *moduleDiscoverer) findModuleProtoPkgs(ctx context.Context) ([]protoanalysis.Package, error) {
226226
// find out all proto packages inside blockchain.
227-
allprotopkgs, err := protoanalysis.Parse(ctx, protoanalysis.PatternRecursive(d.sourcePath))
227+
allprotopkgs, err := protoanalysis.Parse(ctx, d.sourcePath)
228228
if err != nil {
229229
return nil, err
230230
}

starport/pkg/cosmosgen/generate_go.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (g *generator) generateGo() error {
3535

3636
// discover proto packages in the app.
3737
pp := filepath.Join(g.appPath, g.protoDir)
38-
pkgs, err := protoanalysis.Parse(g.ctx, protoanalysis.PatternRecursive(pp))
38+
pkgs, err := protoanalysis.Parse(g.ctx, pp)
3939
if err != nil {
4040
return err
4141
}

starport/pkg/localfs/search.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
package localfs
22

33
import (
4+
"os"
5+
"path/filepath"
46
"sort"
5-
6-
"github.com/mattn/go-zglob"
7+
"strings"
78
)
89

910
// Search searches for files in the fs with given glob pattern by ensuring that
1011
// returned file paths are sorted.
11-
func Search(pattern string) (paths []string, err error) {
12-
files, err := zglob.Glob(pattern)
12+
func Search(path, pattern string) (paths []string, err error) {
13+
files, err := glob(path, pattern)
1314
if err != nil {
1415
return nil, err
1516
}
16-
sort.Strings(files)
1717
return files, nil
1818
}
19+
20+
// glob returns the names of all files matching pattern or nil
21+
// if there is no matching file in all sub-paths recursively. The
22+
// syntax of patterns is the same as in Match. The pattern may
23+
// describe hierarchical names such as /usr/*/bin/ed (assuming
24+
// the Separator is '/').
25+
func glob(path, pattern string) ([]string, error) {
26+
files := make([]string, 0)
27+
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
28+
if err != nil {
29+
return err
30+
}
31+
base := filepath.Base(path)
32+
// skip hidden folders
33+
if f.IsDir() && strings.HasPrefix(base, ".") {
34+
return filepath.SkipDir
35+
}
36+
// avoid check directories
37+
if f.IsDir() {
38+
return nil
39+
}
40+
// check if the file name and pattern matches
41+
matched, err := filepath.Match(pattern, base)
42+
if err == nil && matched {
43+
files = append(files, path)
44+
}
45+
return nil
46+
})
47+
sort.Strings(files)
48+
return files, err
49+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package localfs
2+
3+
import (
4+
"io/fs"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"syscall"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func setupGlobTests() (string, error) {
16+
dir, err := os.Getwd()
17+
if err != nil {
18+
return "", err
19+
}
20+
tmpdir, err := ioutil.TempDir(dir, "glob-test")
21+
if err != nil {
22+
return "", err
23+
}
24+
dirs := []string{"foo/bar", "foo/baz"}
25+
for _, dir := range dirs {
26+
err = os.MkdirAll(filepath.Join(tmpdir, dir), 0755)
27+
if err != nil {
28+
return "", err
29+
}
30+
}
31+
files := []string{
32+
"foo/file.proto",
33+
"foo/bar/file1.proto",
34+
"foo/bar/file2.proto",
35+
"foo/baz/file.proto",
36+
"foo/file",
37+
"foo/baz/file",
38+
}
39+
for _, file := range files {
40+
err = ioutil.WriteFile(filepath.Join(tmpdir, file), []byte{}, 0644)
41+
if err != nil {
42+
return "", err
43+
}
44+
}
45+
return tmpdir, nil
46+
}
47+
48+
func Test_glob(t *testing.T) {
49+
tmpdir, err := setupGlobTests()
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
defer os.RemoveAll(tmpdir)
54+
55+
type args struct {
56+
path string
57+
pattern string
58+
}
59+
tests := []struct {
60+
name string
61+
args args
62+
want []string
63+
err error
64+
}{
65+
{
66+
name: "get all proto files by pattern",
67+
args: args{
68+
path: tmpdir,
69+
pattern: "*.proto",
70+
},
71+
want: []string{
72+
tmpdir + "/foo/bar/file1.proto",
73+
tmpdir + "/foo/bar/file2.proto",
74+
tmpdir + "/foo/baz/file.proto",
75+
tmpdir + "/foo/file.proto",
76+
},
77+
err: nil,
78+
}, {
79+
name: "get only one proto file by name",
80+
args: args{
81+
path: tmpdir,
82+
pattern: "file1.proto",
83+
},
84+
want: []string{tmpdir + "/foo/bar/file1.proto"},
85+
err: nil,
86+
}, {
87+
name: "get two proto files by name",
88+
args: args{
89+
path: tmpdir,
90+
pattern: "file.proto",
91+
},
92+
want: []string{tmpdir + "/foo/baz/file.proto", tmpdir + "/foo/file.proto"},
93+
err: nil,
94+
}, {
95+
name: "get a specific file by name",
96+
args: args{
97+
path: tmpdir,
98+
pattern: "file",
99+
},
100+
want: []string{tmpdir + "/foo/baz/file", tmpdir + "/foo/file"},
101+
err: nil,
102+
}, {
103+
name: "error invalid directory",
104+
args: args{
105+
path: "no-directory",
106+
pattern: "file",
107+
},
108+
want: []string{tmpdir + "/foo/baz/file", tmpdir + "/foo/file"},
109+
err: &fs.PathError{Op: "lstat", Path: "no-directory", Err: syscall.ENOENT},
110+
},
111+
}
112+
for _, tt := range tests {
113+
t.Run(tt.name, func(t *testing.T) {
114+
got, err := glob(tt.args.path, tt.args.pattern)
115+
if tt.err != nil {
116+
require.Error(t, err)
117+
assert.EqualValues(t, tt.err, err)
118+
return
119+
}
120+
require.NoError(t, err)
121+
assert.EqualValues(t, tt.want, got)
122+
})
123+
}
124+
}

starport/pkg/protoanalysis/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type parser struct {
1818

1919
// parse parses proto files in the fs that matches with pattern and returns
2020
// the low level representations of proto packages.
21-
func parse(ctx context.Context, pattern string) ([]*pkg, error) {
21+
func parse(ctx context.Context, path, pattern string) ([]*pkg, error) {
2222
pr := &parser{}
2323

24-
paths, err := localfs.Search(pattern)
24+
paths, err := localfs.Search(path, pattern)
2525
if err != nil {
2626
return nil, err
2727
}

starport/pkg/protoanalysis/protoanalysis.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"github.com/tendermint/starport/starport/pkg/localfs"
88
)
99

10+
const protoFilePattern = "*.proto"
11+
1012
// Parse parses proto packages by finding them with given glob pattern.
11-
func Parse(ctx context.Context, pattern string) ([]Package, error) {
12-
parsed, err := parse(ctx, pattern)
13+
func Parse(ctx context.Context, path string) ([]Package, error) {
14+
parsed, err := parse(ctx, path, protoFilePattern)
1315
if err != nil {
1416
return nil, err
1517
}
@@ -24,9 +26,6 @@ func Parse(ctx context.Context, pattern string) ([]Package, error) {
2426
}
2527

2628
// SearchRecursive recursively finds all proto files under path.
27-
func SearchRecursive(dir string) ([]string, error) {
28-
return localfs.Search(PatternRecursive(dir))
29+
func SearchRecursive(path string) ([]string, error) {
30+
return localfs.Search(path, protoFilePattern)
2931
}
30-
31-
// PatternRecursive returns a recursive glob search pattern to find all proto files under path.
32-
func PatternRecursive(dir string) string { return dir + "/**/*.proto" }

starport/pkg/protoanalysis/protoanalysis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestLiquidity(t *testing.T) {
11-
packages, err := Parse(context.Background(), PatternRecursive("testdata/liquidity"))
11+
packages, err := Parse(context.Background(), "testdata/liquidity")
1212
require.NoError(t, err)
1313

1414
expected := []Package{

0 commit comments

Comments
 (0)