Skip to content

Commit

Permalink
feat: Add makeURL test
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 31, 2023
1 parent 194c142 commit 83b52c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
6 changes: 2 additions & 4 deletions internal/pkg/command/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()) {
Expand Down
55 changes: 55 additions & 0 deletions internal/pkg/command/clone_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 83b52c1

Please sign in to comment.