Skip to content

Commit d8e1aa5

Browse files
authored
feat(pkg/protoanalysis): ignore hidden files (ignite#1296)
* improve glob feature and also exclude hidden folders from the path walk * fix the integration tests
1 parent f1645b7 commit d8e1aa5

File tree

12 files changed

+161
-95
lines changed

12 files changed

+161
-95
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ require (
3131
github.com/jpillora/chisel v1.7.3
3232
github.com/kr/pretty v0.2.1
3333
github.com/manifoldco/promptui v0.8.0
34-
github.com/mattn/go-zglob v0.0.3
3534
github.com/moby/sys/mount v0.2.0 // indirect
3635
github.com/olekukonko/tablewriter v0.0.5
3736
github.com/otiai10/copy v1.4.2
@@ -57,5 +56,5 @@ require (
5756

5857
replace (
5958
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
60-
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
59+
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
6160
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,6 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
825825
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
826826
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
827827
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
828-
github.com/mattn/go-zglob v0.0.3 h1:6Ry4EYsScDyt5di4OI6xw1bYhOqfE5S33Z1OPy+d+To=
829828
github.com/mattn/go-zglob v0.0.3/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
830829
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
831830
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=

starport/pkg/cosmosanalysis/module/module.go

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

208208
func (d *moduleDiscoverer) findModuleProtoPkgs(ctx context.Context) ([]protoanalysis.Package, error) {
209209
// find out all proto packages inside blockchain.
210-
allprotopkgs, err := protoanalysis.Parse(ctx, protoanalysis.PatternRecursive(d.sourcePath))
210+
allprotopkgs, err := protoanalysis.Parse(ctx, d.sourcePath)
211211
if err != nil {
212212
return nil, err
213213
}

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/cosmosgen/generate_javascript.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"strings"
99

1010
"github.com/iancoleman/strcase"
11-
"github.com/mattn/go-zglob"
1211
"github.com/tendermint/starport/starport/pkg/cosmosanalysis/module"
1312
"github.com/tendermint/starport/starport/pkg/giturl"
1413
"github.com/tendermint/starport/starport/pkg/gomodulepath"
14+
"github.com/tendermint/starport/starport/pkg/localfs"
1515
"github.com/tendermint/starport/starport/pkg/nodetime/programs/sta"
1616
tsproto "github.com/tendermint/starport/starport/pkg/nodetime/programs/ts-proto"
1717
"github.com/tendermint/starport/starport/pkg/nodetime/programs/tsc"
@@ -28,10 +28,10 @@ var (
2828
jsOpenAPIOut = []string{
2929
"--openapiv2_out=logtostderr=true,allow_merge=true,Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:.",
3030
}
31-
32-
vuexRootMarker = "vuex-root"
3331
)
3432

33+
const vuexRootMarker = "vuex-root"
34+
3535
type jsGenerator struct {
3636
g *generator
3737
}
@@ -163,7 +163,7 @@ func (g *jsGenerator) generateModule(ctx context.Context, tsprotoPluginPath, app
163163
}
164164

165165
func (g *jsGenerator) generateVuexModuleLoader() error {
166-
modulePaths, err := zglob.Glob(filepath.Join(g.g.o.vuexStoreRootPath, "/**/"+vuexRootMarker))
166+
modulePaths, err := localfs.Search(g.g.o.vuexStoreRootPath, vuexRootMarker)
167167
if err != nil {
168168
return err
169169
}

starport/pkg/localfs/search.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
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)
13-
if err != nil {
12+
func Search(path, pattern string) ([]string, error) {
13+
files := make([]string, 0)
14+
if _, err := os.Stat(path); err != nil {
15+
if os.IsNotExist(err) {
16+
return files, nil
17+
}
1418
return nil, err
1519
}
20+
21+
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
22+
if err != nil {
23+
return err
24+
}
25+
base := filepath.Base(path)
26+
// skip hidden folders
27+
if f.IsDir() && strings.HasPrefix(base, ".") {
28+
return filepath.SkipDir
29+
}
30+
// avoid check directories
31+
if f.IsDir() {
32+
return nil
33+
}
34+
// check if the file name and pattern matches
35+
matched, err := filepath.Match(pattern, base)
36+
if err != nil {
37+
return err
38+
}
39+
if matched {
40+
files = append(files, path)
41+
}
42+
return nil
43+
})
1644
sort.Strings(files)
17-
return files, nil
45+
return files, err
1846
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package localfs
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func setupGlobTests(t *testing.T, files []string) string {
13+
t.Helper()
14+
tmpdir, err := os.MkdirTemp("", "glob-test")
15+
require.NoError(t, err)
16+
17+
t.Cleanup(func() {
18+
os.RemoveAll(tmpdir)
19+
})
20+
for _, file := range files {
21+
fileDir := filepath.Dir(file)
22+
fileDir = filepath.Join(tmpdir, fileDir)
23+
err = os.MkdirAll(fileDir, 0755)
24+
require.NoError(t, err)
25+
err = os.WriteFile(filepath.Join(tmpdir, file), []byte{}, 0644)
26+
require.NoError(t, err)
27+
}
28+
return tmpdir
29+
}
30+
31+
func TestSearch(t *testing.T) {
32+
files := []string{
33+
"foo/file.proto",
34+
"foo/bar/file1.proto",
35+
"foo/bar/file2.proto",
36+
"foo/baz/file.proto",
37+
"foo/file",
38+
"foo/baz/file",
39+
}
40+
tmpdir := setupGlobTests(t, files)
41+
type args struct {
42+
path string
43+
pattern string
44+
}
45+
tests := []struct {
46+
name string
47+
args args
48+
want []string
49+
err error
50+
}{
51+
{
52+
name: "get all proto files by pattern",
53+
args: args{
54+
path: tmpdir,
55+
pattern: "*.proto",
56+
},
57+
want: []string{
58+
filepath.Join(tmpdir, "foo/bar/file1.proto"),
59+
filepath.Join(tmpdir, "foo/bar/file2.proto"),
60+
filepath.Join(tmpdir, "foo/baz/file.proto"),
61+
filepath.Join(tmpdir, "foo/file.proto"),
62+
},
63+
}, {
64+
name: "get only one proto file by name",
65+
args: args{
66+
path: tmpdir,
67+
pattern: "file1.proto",
68+
},
69+
want: []string{filepath.Join(tmpdir, "foo/bar/file1.proto")},
70+
}, {
71+
name: "get two proto files by name",
72+
args: args{
73+
path: tmpdir,
74+
pattern: "file.proto",
75+
},
76+
want: []string{
77+
filepath.Join(tmpdir, "foo/baz/file.proto"),
78+
filepath.Join(tmpdir, "foo/file.proto"),
79+
},
80+
}, {
81+
name: "get a specific file by name",
82+
args: args{
83+
path: tmpdir,
84+
pattern: "file",
85+
},
86+
want: []string{
87+
filepath.Join(tmpdir, "foo/baz/file"),
88+
filepath.Join(tmpdir, "foo/file"),
89+
},
90+
}, {
91+
name: "not found directory",
92+
args: args{
93+
path: "no-directory",
94+
pattern: "file",
95+
},
96+
want: []string{},
97+
},
98+
}
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
got, err := Search(tt.args.path, tt.args.pattern)
102+
if tt.err != nil {
103+
require.Error(t, err)
104+
assert.EqualValues(t, tt.err, err)
105+
return
106+
}
107+
require.NoError(t, err)
108+
assert.EqualValues(t, tt.want, got)
109+
})
110+
}
111+
}

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)