Add test for stringifyRepo

This commit is contained in:
Amolith 2024-02-23 16:13:18 -05:00
parent edfeefee51
commit 8cf4a4c284
Signed by: Amolith
SSH Key Fingerprint: SHA256:JBKEeoO/72Fz03rtlzeO49PATFT2maMancH3opcT0h0
2 changed files with 60 additions and 1 deletions

View File

@ -201,7 +201,7 @@ func stringifyRepo(url string) (path string, err error) {
if ep.Protocol == "http" || ep.Protocol == "https" { if ep.Protocol == "http" || ep.Protocol == "https" {
return "data/" + strings.Split(url, "://")[1], nil return "data/" + strings.Split(url, "://")[1], nil
} else if ep.Protocol == "ssh" { } else if ep.Protocol == "ssh" {
return "data/" + ep.Host + ep.Path, nil return "data/" + ep.Host + "/" + ep.Path, nil
} else { } else {
return "", errors.New("unsupported protocol") return "", errors.New("unsupported protocol")
} }

59
git/git_test.go Normal file
View File

@ -0,0 +1,59 @@
package git
import (
"testing"
)
func TestStringifyRepo(t *testing.T) {
wantGitHub := "data/github.com/owner/repo"
wantSourceHut := "data/git.sr.ht/~owner/repo"
tests := []struct {
name string
input string
want string
}{
{
name: "GitHubHTTP",
input: "http://github.com/owner/repo",
want: wantGitHub,
},
{
name: "GitHubHTTPS",
input: "https://github.com/owner/repo",
want: wantGitHub,
},
{
name: "GitHubSSH",
input: "git@github.com:owner/repo",
want: wantGitHub,
},
{
name: "SourceHutHTTP",
input: "http://git.sr.ht/~owner/repo",
want: wantSourceHut,
},
{
name: "SourceHutHTTPS",
input: "https://git.sr.ht/~owner/repo",
want: wantSourceHut,
},
{
name: "SourceHutSSH",
input: "git@git.sr.ht:~owner/repo",
want: wantSourceHut,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringifyRepo(test.input)
if err != nil {
t.Errorf("stringifyRepo(%s) returned error: %v", test.input, err)
}
if got != test.want {
t.Errorf("stringifyRepo(%s) = %s, want %s", test.input, got, test.want)
}
})
}
}