File tree Expand file tree Collapse file tree 2 files changed +48
-9
lines changed Expand file tree Collapse file tree 2 files changed +48
-9
lines changed Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ import (
2222
2323 "github.com/emirpasic/gods/lists/singlylinkedlist"
2424
25- "github.com/bazelbuild/bazel-gazelle/label"
2625 "github.com/bazel-contrib/rules_python/gazelle/manifest"
26+ "github.com/bazelbuild/bazel-gazelle/label"
2727)
2828
2929// Directives
@@ -125,21 +125,28 @@ const (
125125
126126// defaultIgnoreFiles is the list of default values used in the
127127// python_ignore_files option.
128- var defaultIgnoreFiles = map [string ]struct {}{
129- }
128+ var defaultIgnoreFiles = map [string ]struct {}{}
130129
131130// Configs is an extension of map[string]*Config. It provides finding methods
132131// on top of the mapping.
133132type Configs map [string ]* Config
134133
135134// ParentForPackage returns the parent Config for the given Bazel package.
136- func (c * Configs ) ParentForPackage (pkg string ) * Config {
137- dir := path .Dir (pkg )
138- if dir == "." {
139- dir = ""
135+ func (c Configs ) ParentForPackage (pkg string ) * Config {
136+ for {
137+ dir := path .Dir (pkg )
138+ if dir == "." {
139+ dir = ""
140+ }
141+ parent := (map [string ]* Config )(c )[dir ]
142+ if parent != nil {
143+ return parent
144+ }
145+ if dir == "" {
146+ return nil
147+ }
148+ pkg = dir
140149 }
141- parent := (map [string ]* Config )(* c )[dir ]
142- return parent
143150}
144151
145152// Config represents a config extension for a specific Bazel package.
Original file line number Diff line number Diff line change @@ -248,3 +248,35 @@ func TestFormatThirdPartyDependency(t *testing.T) {
248248 })
249249 }
250250}
251+
252+ func TestConfigsMap (t * testing.T ) {
253+ t .Run ("only root" , func (t * testing.T ) {
254+ configs := Configs {"" : New ("root/dir" , "" )}
255+
256+ if configs .ParentForPackage ("" ) == nil {
257+ t .Fatal ("expected non-nil for root config" )
258+ }
259+
260+ if configs .ParentForPackage ("a/b/c" ) != configs ["" ] {
261+ t .Fatal ("expected root for subpackage" )
262+ }
263+ })
264+
265+ t .Run ("sparse child configs" , func (t * testing.T ) {
266+ configs := Configs {"" : New ("root/dir" , "" )}
267+ configs ["a" ] = configs ["" ].NewChild ()
268+ configs ["a/b/c" ] = configs ["a" ].NewChild ()
269+
270+ if configs .ParentForPackage ("a/b/c/d" ) != configs ["a/b/c" ] {
271+ t .Fatal ("child should match direct parent" )
272+ }
273+
274+ if configs .ParentForPackage ("a/b/c/d/e" ) != configs ["a/b/c" ] {
275+ t .Fatal ("grandchild should match first parant" )
276+ }
277+
278+ if configs .ParentForPackage ("other/root/path" ) != configs ["" ] {
279+ t .Fatal ("non-configured subpackage should match root" )
280+ }
281+ })
282+ }
You can’t perform that action at this time.
0 commit comments