xref: /aosp_15_r20/external/bazelbuild-rules_python/gazelle/pythonconfig/pythonconfig_test.go (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1package pythonconfig
2
3import (
4	"testing"
5)
6
7func TestFormatThirdPartyDependency(t *testing.T) {
8	type testInput struct {
9		RepositoryName     string
10		DistributionName   string
11		LabelNormalization LabelNormalizationType
12		LabelConvention    string
13	}
14
15	tests := map[string]struct {
16		input testInput
17		want  string
18	}{
19		"default / upper case": {
20			input: testInput{
21				DistributionName:   "DistWithUpperCase",
22				RepositoryName:     "pip",
23				LabelNormalization: DefaultLabelNormalizationType,
24				LabelConvention:    DefaultLabelConvention,
25			},
26			want: "@pip//distwithuppercase",
27		},
28		"default / dashes": {
29			input: testInput{
30				DistributionName:   "dist-with-dashes",
31				RepositoryName:     "pip",
32				LabelNormalization: DefaultLabelNormalizationType,
33				LabelConvention:    DefaultLabelConvention,
34			},
35			want: "@pip//dist_with_dashes",
36		},
37		"default / repeating dashes inside": {
38			input: testInput{
39				DistributionName:   "friendly--bard",
40				RepositoryName:     "pip",
41				LabelNormalization: DefaultLabelNormalizationType,
42				LabelConvention:    DefaultLabelConvention,
43			},
44			want: "@pip//friendly_bard",
45		},
46		"default / repeating underscores inside": {
47			input: testInput{
48				DistributionName:   "hello___something",
49				RepositoryName:     "pip",
50				LabelNormalization: DefaultLabelNormalizationType,
51				LabelConvention:    DefaultLabelConvention,
52			},
53			want: "@pip//hello_something",
54		},
55		"default / prefix repeating underscores": {
56			input: testInput{
57				DistributionName:   "__hello-something",
58				RepositoryName:     "pip",
59				LabelNormalization: DefaultLabelNormalizationType,
60				LabelConvention:    DefaultLabelConvention,
61			},
62			want: "@pip//hello_something",
63		},
64		"default / suffix repeating underscores": {
65			input: testInput{
66				DistributionName:   "hello-something___",
67				RepositoryName:     "pip",
68				LabelNormalization: DefaultLabelNormalizationType,
69				LabelConvention:    DefaultLabelConvention,
70			},
71			want: "@pip//hello_something",
72		},
73		"default / prefix repeating dashes": {
74			input: testInput{
75				DistributionName:   "---hello-something",
76				RepositoryName:     "pip",
77				LabelNormalization: DefaultLabelNormalizationType,
78				LabelConvention:    DefaultLabelConvention,
79			},
80			want: "@pip//hello_something",
81		},
82		"default / suffix repeating dashes": {
83			input: testInput{
84				DistributionName:   "hello-something----",
85				RepositoryName:     "pip",
86				LabelNormalization: DefaultLabelNormalizationType,
87				LabelConvention:    DefaultLabelConvention,
88			},
89			want: "@pip//hello_something",
90		},
91		"default / dots": {
92			input: testInput{
93				DistributionName:   "dist.with.dots",
94				RepositoryName:     "pip",
95				LabelNormalization: DefaultLabelNormalizationType,
96				LabelConvention:    DefaultLabelConvention,
97			},
98			want: "@pip//dist_with_dots",
99		},
100		"default / mixed": {
101			input: testInput{
102				DistributionName:   "FrIeNdLy-._.-bArD",
103				RepositoryName:     "pip",
104				LabelNormalization: DefaultLabelNormalizationType,
105				LabelConvention:    DefaultLabelConvention,
106			},
107			want: "@pip//friendly_bard",
108		},
109		"default / upper case / custom prefix & suffix": {
110			input: testInput{
111				DistributionName:   "DistWithUpperCase",
112				RepositoryName:     "pip",
113				LabelNormalization: DefaultLabelNormalizationType,
114				LabelConvention:    "pReFiX-$distribution_name$-sUfFiX",
115			},
116			want: "@pip//prefix_distwithuppercase_suffix",
117		},
118		"noop normalization / mixed": {
119			input: testInput{
120				DistributionName:   "not-TO-be.sanitized",
121				RepositoryName:     "pip",
122				LabelNormalization: NoLabelNormalizationType,
123				LabelConvention:    DefaultLabelConvention,
124			},
125			want: "@pip//not-TO-be.sanitized",
126		},
127		"noop normalization / mixed / custom prefix & suffix": {
128			input: testInput{
129				DistributionName:   "not-TO-be.sanitized",
130				RepositoryName:     "pip",
131				LabelNormalization: NoLabelNormalizationType,
132				LabelConvention:    "pre___$distribution_name$___fix",
133			},
134			want: "@pip//pre___not-TO-be.sanitized___fix",
135		},
136		"pep503 / upper case": {
137			input: testInput{
138				DistributionName:   "DistWithUpperCase",
139				RepositoryName:     "pip",
140				LabelNormalization: Pep503LabelNormalizationType,
141				LabelConvention:    DefaultLabelConvention,
142			},
143			want: "@pip//distwithuppercase",
144		},
145		"pep503 / underscores": {
146			input: testInput{
147				DistributionName:   "dist_with_underscores",
148				RepositoryName:     "pip",
149				LabelNormalization: Pep503LabelNormalizationType,
150				LabelConvention:    DefaultLabelConvention,
151			},
152			want: "@pip//dist-with-underscores",
153		},
154		"pep503 / repeating dashes inside": {
155			input: testInput{
156				DistributionName:   "friendly--bard",
157				RepositoryName:     "pip",
158				LabelNormalization: Pep503LabelNormalizationType,
159				LabelConvention:    DefaultLabelConvention,
160			},
161			want: "@pip//friendly-bard",
162		},
163		"pep503 / repeating underscores inside": {
164			input: testInput{
165				DistributionName:   "hello___something",
166				RepositoryName:     "pip",
167				LabelNormalization: Pep503LabelNormalizationType,
168				LabelConvention:    DefaultLabelConvention,
169			},
170			want: "@pip//hello-something",
171		},
172		"pep503 / prefix repeating underscores": {
173			input: testInput{
174				DistributionName:   "__hello-something",
175				RepositoryName:     "pip",
176				LabelNormalization: Pep503LabelNormalizationType,
177				LabelConvention:    DefaultLabelConvention,
178			},
179			want: "@pip//hello-something",
180		},
181		"pep503 / suffix repeating underscores": {
182			input: testInput{
183				DistributionName:   "hello-something___",
184				RepositoryName:     "pip",
185				LabelNormalization: Pep503LabelNormalizationType,
186				LabelConvention:    DefaultLabelConvention,
187			},
188			want: "@pip//hello-something",
189		},
190		"pep503 / prefix repeating dashes": {
191			input: testInput{
192				DistributionName:   "---hello-something",
193				RepositoryName:     "pip",
194				LabelNormalization: Pep503LabelNormalizationType,
195				LabelConvention:    DefaultLabelConvention,
196			},
197			want: "@pip//hello-something",
198		},
199		"pep503 / suffix repeating dashes": {
200			input: testInput{
201				DistributionName:   "hello-something----",
202				RepositoryName:     "pip",
203				LabelNormalization: Pep503LabelNormalizationType,
204				LabelConvention:    DefaultLabelConvention,
205			},
206			want: "@pip//hello-something",
207		},
208		"pep503 / dots": {
209			input: testInput{
210				DistributionName:   "dist.with.dots",
211				RepositoryName:     "pip",
212				LabelNormalization: Pep503LabelNormalizationType,
213				LabelConvention:    DefaultLabelConvention,
214			},
215			want: "@pip//dist-with-dots",
216		},
217		"pep503 / mixed": {
218			input: testInput{
219				DistributionName:   "To-be.sanitized",
220				RepositoryName:     "pip",
221				LabelNormalization: Pep503LabelNormalizationType,
222				LabelConvention:    DefaultLabelConvention,
223			},
224			want: "@pip//to-be-sanitized",
225		},
226		"pep503 / underscores / custom prefix & suffix": {
227			input: testInput{
228				DistributionName:   "dist_with_underscores",
229				RepositoryName:     "pip",
230				LabelNormalization: Pep503LabelNormalizationType,
231				LabelConvention:    "pre___$distribution_name$___fix",
232			},
233			want: "@pip//pre-dist-with-underscores-fix",
234		},
235	}
236
237	for name, tc := range tests {
238		t.Run(name, func(t *testing.T) {
239			c := Config{
240				labelNormalization: tc.input.LabelNormalization,
241				labelConvention:    tc.input.LabelConvention,
242			}
243			gotLabel := c.FormatThirdPartyDependency(tc.input.RepositoryName, tc.input.DistributionName)
244			got := gotLabel.String()
245			if tc.want != got {
246				t.Fatalf("expected %q, got %q", tc.want, got)
247			}
248		})
249	}
250}
251