Skip to content

Commit

Permalink
chore: alternative error checking implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
arunpoudel committed Dec 20, 2023
1 parent 2c006a3 commit d964ee6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
29 changes: 0 additions & 29 deletions errors.go

This file was deleted.

8 changes: 6 additions & 2 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package magekubernetes

import (
"fmt"
"strings"

"github.com/go-git/go-git/v5"
)

const (
unableToParseRemoteErr = "unable to parse remote url: %s"
)

func repoURL() (string, error) {
repo, err := git.PlainOpen("./")
if err != nil {
Expand All @@ -28,6 +33,5 @@ func gitRemoteParser(remote string) (string, error) {
toHTTPS := "https://github.com/" + strings.Split(url, ":")[1]
return strings.TrimSuffix(toHTTPS, ".git"), nil
}
return "", NewErrUnableToParseRemoteURL(url)

return "", fmt.Errorf(unableToParseRemoteErr, url)
}
11 changes: 8 additions & 3 deletions metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ func TestGitRemoteParser(t *testing.T) {
}{
{"origin [email protected]:coopnorge/helloworld.git (fetch)", "https://github.com/coopnorge/helloworld", nil},
{"origin https://github.com/coopnorge/helloworld.git (fetch)", "https://github.com/coopnorge/helloworld", nil},
{"origin http://github.com/coopnorge/helloworld.git (fetch)", "", NewErrUnableToParseRemoteURL("http://github.com/coopnorge/helloworld.git")},
{"origin http://github.com/coopnorge/helloworld.git (fetch)", "", fmt.Errorf(unableToParseRemoteErr, "http://github.com/coopnorge/helloworld.git")},
}
for _, tt := range tests {
testname := fmt.Sprintf("%s,%s", tt.remote, tt.want)
t.Run(testname, func(t *testing.T) {
got, err := gitRemoteParser(tt.remote)
if got != tt.want || !errors.Is(err, tt.err) {
t.Errorf("\n got: %s,%v \nwant: %s,%v", got, err, tt.want, tt.err)
if got == tt.want &&
(errors.Is(err, tt.err) || // This is to compare if the error is of the same type, which
// happen when both errors are nil,
// The line below is to compare if the error message is the same as string
err.Error() == tt.err.Error()) {
return
}
t.Errorf("\n got: %s,%v \nwant: %s,%v", got, err, tt.want, tt.err)
})
}
}

0 comments on commit d964ee6

Please sign in to comment.