Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"github.com/emirpasic/gods/lists/singlylinkedlist"

"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazel-contrib/rules_python/gazelle/manifest"
"github.com/bazelbuild/bazel-gazelle/label"
)

// Directives
Expand Down Expand Up @@ -125,21 +125,28 @@ const (

// defaultIgnoreFiles is the list of default values used in the
// python_ignore_files option.
var defaultIgnoreFiles = map[string]struct{}{
}
var defaultIgnoreFiles = map[string]struct{}{}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The precommit seems to be doing this? 🤷


// Configs is an extension of map[string]*Config. It provides finding methods
// on top of the mapping.
type Configs map[string]*Config

// ParentForPackage returns the parent Config for the given Bazel package.
func (c *Configs) ParentForPackage(pkg string) *Config {
dir := path.Dir(pkg)
if dir == "." {
dir = ""
func (c Configs) ParentForPackage(pkg string) *Config {
for {
dir := path.Dir(pkg)
if dir == "." {
dir = ""
}
parent := (map[string]*Config)(c)[dir]
if parent != nil {
return parent
}
if dir == "" {
return nil
}
pkg = dir
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unit test for this new logic? Just testing the Configs in isolation would be sufficient.

Right now it is a little bit hard to read and having tests would help understand what this has to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few basic tests for this method.

There should probably be some additional full tests for update_only and mixing that with the different python_generation_mode modes but I'm not familiar enough with it to do in this initial PR.

}
parent := (map[string]*Config)(*c)[dir]
return parent
}

// Config represents a config extension for a specific Bazel package.
Expand Down
32 changes: 32 additions & 0 deletions gazelle/pythonconfig/pythonconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,35 @@ func TestFormatThirdPartyDependency(t *testing.T) {
})
}
}

func TestConfigsMap(t *testing.T) {
t.Run("only root", func(t *testing.T) {
configs := Configs{"": New("root/dir", "")}

if configs.ParentForPackage("") == nil {
t.Fatal("expected non-nil for root config")
}

if configs.ParentForPackage("a/b/c") != configs[""] {
t.Fatal("expected root for subpackage")
}
})

t.Run("sparse child configs", func(t *testing.T) {
configs := Configs{"": New("root/dir", "")}
configs["a"] = configs[""].NewChild()
configs["a/b/c"] = configs["a"].NewChild()

if configs.ParentForPackage("a/b/c/d") != configs["a/b/c"] {
t.Fatal("child should match direct parent")
}

if configs.ParentForPackage("a/b/c/d/e") != configs["a/b/c"] {
t.Fatal("grandchild should match first parant")
}

if configs.ParentForPackage("other/root/path") != configs[""] {
t.Fatal("non-configured subpackage should match root")
}
})
}