From 83b52c1d079fa53b01027e6bf27cf0f613d3d0d6 Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Sun, 31 Dec 2023 15:26:17 +0100 Subject: [PATCH] feat: Add makeURL test --- internal/pkg/command/clone.go | 6 ++-- internal/pkg/command/clone_test.go | 55 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 internal/pkg/command/clone_test.go diff --git a/internal/pkg/command/clone.go b/internal/pkg/command/clone.go index ee82875..278d507 100644 --- a/internal/pkg/command/clone.go +++ b/internal/pkg/command/clone.go @@ -45,7 +45,7 @@ func cloneRepo(repo string) (string, error) { if err != nil { return "", fmt.Errorf("Error parsing repo URL: %s", err) } - gitURL, err := makeURL(repoURL) + gitURL, err := makeURL(repoURL, viper.GetStringMapString("renameRepo")) if err != nil { return "", fmt.Errorf("Error making git URL: %s", err) } @@ -64,9 +64,7 @@ func cloneRepo(repo string) (string, error) { } -func makeURL(u *url.URL) (string, error) { - renameRepo := viper.GetStringMapString("renameRepo") - +func makeURL(u *url.URL, renameRepo map[string]string) (string, error) { for match, host := range renameRepo { r := regexp.MustCompile(regexp.QuoteMeta(match)) if r.MatchString(u.String()) { diff --git a/internal/pkg/command/clone_test.go b/internal/pkg/command/clone_test.go new file mode 100644 index 0000000..9684ffe --- /dev/null +++ b/internal/pkg/command/clone_test.go @@ -0,0 +1,55 @@ +package command + +import ( + "net/url" + "testing" +) + +func Test_makeURL(t *testing.T) { + tests := []struct { + name string + renameMap map[string]string + URL string + want string + wantErr bool + }{ + { + name: "nomap", + renameMap: map[string]string{}, + URL: "ssh://git@github.com/bla/bla", + want: "ssh://git@github.com/bla/bla", + wantErr: false, + }, + { + name: "notinma", + renameMap: map[string]string{ + "bitbucket.org/bla/bla": "bb-personal", + }, + URL: "ssh://git@github.com/bla/bla", + want: "ssh://git@github.com/bla/bla", + wantErr: false, + }, + { + name: "replace", + renameMap: map[string]string{ + "github.com/bla": "gh-personal", + }, + URL: "ssh://git@github.com/bla/bla", + want: "ssh://git@gh-personal/bla/bla", + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + url, _ := url.Parse(tt.URL) + got, gotErr := makeURL(url, tt.renameMap) + if (gotErr != nil) != tt.wantErr { + t.Errorf("makeURL() error = %v, wantErr %v", gotErr, tt.wantErr) + } + if got != tt.want { + t.Errorf("makeURL() = %v, want %v", got, tt.want) + } + }) + } +}