Skip to content

Commit e2384c4

Browse files
committed
add tests for error case in NormalizeUrl
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
1 parent 6ecd349 commit e2384c4

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

controllers/helmchart_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ func (r *HelmChartReconciler) indexHelmRepositoryByURL(o client.Object) []string
11871187
panic(fmt.Sprintf("Expected a HelmRepository, got %T", o))
11881188
}
11891189
u, err := repository.NormalizeURL(repo.Spec.URL)
1190-
if u != "" && err != nil {
1190+
if u != "" && err == nil {
11911191
return []string{u}
11921192
}
11931193
return nil

internal/helm/chart/dependency_manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (dm *DependencyManager) resolveRepository(url string) (repo repository.Down
268268

269269
nUrl, err := repository.NormalizeURL(url)
270270
if err != nil {
271-
return nil, err
271+
return
272272
}
273273
err = repository.ValidateDepURL(nUrl)
274274
if err != nil {

internal/helm/repository/utils.go

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ var (
3737

3838
// NormalizeURL normalizes a ChartRepository URL by its scheme.
3939
func NormalizeURL(repositoryURL string) (string, error) {
40+
if repositoryURL == "" {
41+
return "", nil
42+
}
4043
u, err := url.Parse(repositoryURL)
4144
if err != nil {
4245
return "", err

internal/helm/repository/utils_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424

2525
func TestNormalizeURL(t *testing.T) {
2626
tests := []struct {
27-
name string
28-
url string
29-
want string
27+
name string
28+
url string
29+
want string
30+
wantErr bool
3031
}{
3132
{
3233
name: "with slash",
@@ -63,12 +64,27 @@ func TestNormalizeURL(t *testing.T) {
6364
url: "http://example.com/?st=pr",
6465
want: "http://example.com/?st=pr",
6566
},
67+
{
68+
name: "empty url",
69+
url: "",
70+
want: "",
71+
},
72+
{
73+
name: "bad url",
74+
url: "://badurl.",
75+
wantErr: true,
76+
},
6677
}
6778
for _, tt := range tests {
6879
t.Run(tt.name, func(t *testing.T) {
6980
g := NewWithT(t)
7081

7182
got, err := NormalizeURL(tt.url)
83+
if tt.wantErr {
84+
g.Expect(err).To(HaveOccurred())
85+
return
86+
}
87+
7288
g.Expect(err).To(Not(HaveOccurred()))
7389
g.Expect(got).To(Equal(tt.want))
7490
})

0 commit comments

Comments
 (0)