Skip to content

Commit

Permalink
add tests for error case in NormalizeUrl
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
  • Loading branch information
somtochiama committed Feb 6, 2023
1 parent 6ecd349 commit e2384c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion controllers/helmchart_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ func (r *HelmChartReconciler) indexHelmRepositoryByURL(o client.Object) []string
panic(fmt.Sprintf("Expected a HelmRepository, got %T", o))
}
u, err := repository.NormalizeURL(repo.Spec.URL)
if u != "" && err != nil {
if u != "" && err == nil {
return []string{u}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/helm/chart/dependency_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (dm *DependencyManager) resolveRepository(url string) (repo repository.Down

nUrl, err := repository.NormalizeURL(url)
if err != nil {
return nil, err
return
}
err = repository.ValidateDepURL(nUrl)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/helm/repository/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (

// NormalizeURL normalizes a ChartRepository URL by its scheme.
func NormalizeURL(repositoryURL string) (string, error) {
if repositoryURL == "" {
return "", nil
}
u, err := url.Parse(repositoryURL)
if err != nil {
return "", err
Expand Down
22 changes: 19 additions & 3 deletions internal/helm/repository/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (

func TestNormalizeURL(t *testing.T) {
tests := []struct {
name string
url string
want string
name string
url string
want string
wantErr bool
}{
{
name: "with slash",
Expand Down Expand Up @@ -63,12 +64,27 @@ func TestNormalizeURL(t *testing.T) {
url: "http://example.com/?st=pr",
want: "http://example.com/?st=pr",
},
{
name: "empty url",
url: "",
want: "",
},
{
name: "bad url",
url: "://badurl.",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

got, err := NormalizeURL(tt.url)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}

g.Expect(err).To(Not(HaveOccurred()))
g.Expect(got).To(Equal(tt.want))
})
Expand Down

0 comments on commit e2384c4

Please sign in to comment.