Skip to content

Commit 244c606

Browse files
authored
fix: update gazelle to properly handle dot in package name. (#1083)
1 parent c73dc0c commit 244c606

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

gazelle/pythonconfig/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "pythonconfig",
@@ -15,6 +15,12 @@ go_library(
1515
],
1616
)
1717

18+
go_test(
19+
name = "pythonconfig_test",
20+
srcs = ["pythonconfig_test.go"],
21+
deps = [":pythonconfig"],
22+
)
23+
1824
filegroup(
1925
name = "distribution",
2026
srcs = glob(["**"]),

gazelle/pythonconfig/pythonconfig.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ var defaultIgnoreFiles = map[string]struct{}{
9090
"setup.py": {},
9191
}
9292

93+
func SanitizeDistribution(distributionName string) string {
94+
sanitizedDistribution := strings.ToLower(distributionName)
95+
sanitizedDistribution = strings.ReplaceAll(sanitizedDistribution, "-", "_")
96+
sanitizedDistribution = strings.ReplaceAll(sanitizedDistribution, ".", "_")
97+
98+
return sanitizedDistribution
99+
}
100+
93101
// Configs is an extension of map[string]*Config. It provides finding methods
94102
// on top of the mapping.
95103
type Configs map[string]*Config
@@ -218,8 +226,7 @@ func (c *Config) FindThirdPartyDependency(modName string) (string, bool) {
218226
} else if gazelleManifest.PipRepository != nil {
219227
distributionRepositoryName = gazelleManifest.PipRepository.Name
220228
}
221-
sanitizedDistribution := strings.ToLower(distributionName)
222-
sanitizedDistribution = strings.ReplaceAll(sanitizedDistribution, "-", "_")
229+
sanitizedDistribution := SanitizeDistribution(distributionName)
223230

224231
if gazelleManifest.PipRepository != nil && gazelleManifest.PipRepository.UsePipRepositoryAliases {
225232
// @<repository_name>//<distribution_name>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pythonconfig
2+
3+
import (
4+
"testing"
5+
6+
"github.com/bazelbuild/rules_python/gazelle/pythonconfig"
7+
)
8+
9+
func TestDistributionSanitizing(t *testing.T) {
10+
tests := map[string]struct {
11+
input string
12+
want string
13+
}{
14+
"upper case": {input: "DistWithUpperCase", want: "distwithuppercase"},
15+
"dashes": {input: "dist-with-dashes", want: "dist_with_dashes"},
16+
"dots": {input: "dist.with.dots", want: "dist_with_dots"},
17+
"mixed": {input: "To-be.sanitized", want: "to_be_sanitized"},
18+
}
19+
20+
for name, tc := range tests {
21+
t.Run(name, func(t *testing.T) {
22+
got := pythonconfig.SanitizeDistribution(tc.input)
23+
if tc.want != got {
24+
t.Fatalf("expected %q, got %q", tc.want, got)
25+
}
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)