From 8cf4a4c284db90dc0f0db52087d7a5a5509bdda5 Mon Sep 17 00:00:00 2001 From: Amolith Date: Fri, 23 Feb 2024 16:13:18 -0500 Subject: [PATCH] Add test for stringifyRepo --- git/git.go | 2 +- git/git_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 git/git_test.go diff --git a/git/git.go b/git/git.go index 1f3d5f3..9464bfb 100644 --- a/git/git.go +++ b/git/git.go @@ -201,7 +201,7 @@ func stringifyRepo(url string) (path string, err error) { if ep.Protocol == "http" || ep.Protocol == "https" { return "data/" + strings.Split(url, "://")[1], nil } else if ep.Protocol == "ssh" { - return "data/" + ep.Host + ep.Path, nil + return "data/" + ep.Host + "/" + ep.Path, nil } else { return "", errors.New("unsupported protocol") } diff --git a/git/git_test.go b/git/git_test.go new file mode 100644 index 0000000..2b6a8c9 --- /dev/null +++ b/git/git_test.go @@ -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) + } + }) + } +}