Skip to content

Commit

Permalink
Align snake_case norm with normalize_name.bzl
Browse files Browse the repository at this point in the history
  • Loading branch information
wingsofovnia committed Jun 28, 2024
1 parent 8928708 commit 02695fe
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 7 deletions.
12 changes: 7 additions & 5 deletions gazelle/pythonconfig/pythonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,15 @@ func (c *Config) FormatThirdPartyDependency(repositoryName string, distributionN
var normConventionalDistributionName string
switch norm := c.LabelNormalization(); norm {
case SnakeCaseLabelNormalizationType:
// See /python/private/normalize_name.bzl
normConventionalDistributionName = strings.ToLower(conventionalDistributionName)
normConventionalDistributionName = strings.ReplaceAll(normConventionalDistributionName, "-", "_")
normConventionalDistributionName = strings.ReplaceAll(normConventionalDistributionName, ".", "_")
normConventionalDistributionName = regexp.MustCompile(`[-_.]+`).ReplaceAllString(normConventionalDistributionName, "_")
normConventionalDistributionName = strings.Trim(normConventionalDistributionName, "_")
case Pep503LabelNormalizationType:
// See https://packaging.python.org/en/latest/specifications/name-normalization/#name-normalization
normConventionalDistributionName = strings.ToLower(conventionalDistributionName)
normConventionalDistributionName = regexp.MustCompile(`[-_.]+`).ReplaceAllString(normConventionalDistributionName, "-")
// See https://packaging.python.org/en/latest/specifications/name-normalization/#name-format
normConventionalDistributionName = strings.ToLower(conventionalDistributionName) // ... "should be lowercased"
normConventionalDistributionName = regexp.MustCompile(`[-_.]+`).ReplaceAllString(normConventionalDistributionName, "-") // ... "all runs of the characters ., -, or _ replaced with a single -"
normConventionalDistributionName = strings.Trim(normConventionalDistributionName, "-") // ... "must start and end with a letter or number"
default:
fallthrough
case NoLabelNormalizationType:
Expand Down
112 changes: 110 additions & 2 deletions gazelle/pythonconfig/pythonconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ func TestFormatThirdPartyDependency(t *testing.T) {
},
want: "@pip//dist_with_dashes",
},
"default / repeating dashes inside": {
input: testInput{
DistributionName: "friendly--bard",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//friendly_bard",
},
"default / repeating underscores inside": {
input: testInput{
DistributionName: "hello___something",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello_something",
},
"default / prefix repeating underscores": {
input: testInput{
DistributionName: "__hello-something",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello_something",
},
"default / suffix repeating underscores": {
input: testInput{
DistributionName: "hello-something___",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello_something",
},
"default / prefix repeating dashes": {
input: testInput{
DistributionName: "---hello-something",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello_something",
},
"default / suffix repeating dashes": {
input: testInput{
DistributionName: "hello-something----",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello_something",
},
"default / dots": {
input: testInput{
DistributionName: "dist.with.dots",
Expand All @@ -45,12 +99,12 @@ func TestFormatThirdPartyDependency(t *testing.T) {
},
"default / mixed": {
input: testInput{
DistributionName: "To-be.sanitized",
DistributionName: "FrIeNdLy-._.-bArD",
RepositoryName: "pip",
LabelNormalization: DefaultLabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//to_be_sanitized",
want: "@pip//friendly_bard",
},
"default / upper case / custom prefix & suffix": {
input: testInput{
Expand Down Expand Up @@ -97,6 +151,60 @@ func TestFormatThirdPartyDependency(t *testing.T) {
},
want: "@pip//dist-with-underscores",
},
"pep503 / repeating dashes inside": {
input: testInput{
DistributionName: "friendly--bard",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//friendly-bard",
},
"pep503 / repeating underscores inside": {
input: testInput{
DistributionName: "hello___something",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello-something",
},
"pep503 / prefix repeating underscores": {
input: testInput{
DistributionName: "__hello-something",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello-something",
},
"pep503 / suffix repeating underscores": {
input: testInput{
DistributionName: "hello-something___",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello-something",
},
"pep503 / prefix repeating dashes": {
input: testInput{
DistributionName: "---hello-something",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello-something",
},
"pep503 / suffix repeating dashes": {
input: testInput{
DistributionName: "hello-something----",
RepositoryName: "pip",
LabelNormalization: Pep503LabelNormalizationType,
LabelConvention: DefaultLabelConvention,
},
want: "@pip//hello-something",
},
"pep503 / dots": {
input: testInput{
DistributionName: "dist.with.dots",
Expand Down

0 comments on commit 02695fe

Please sign in to comment.