109 lines
3.5 KiB
Go
109 lines
3.5 KiB
Go
package repo
|
|
|
|
import (
|
|
"os/exec"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseRemoteURL(t *testing.T) {
|
|
cases := []struct {
|
|
raw, host, webURL, owner, repo string
|
|
}{
|
|
{"https://gitea.example.com/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
{"https://gitea.example.com/foo/bar", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
{"https://user:secret@gitea.example.com/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
{"http://localhost:3000/foo/bar.git", "localhost", "http://localhost:3000", "foo", "bar"},
|
|
{"https://GitHub.com/Foo/Bar.git", "github.com", "https://github.com", "Foo", "Bar"},
|
|
{"git@gitea.example.com:foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
{"git@gitea.example.com:foo/bar", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
{"git@github.com:foo/bar.git", "github.com", "https://github.com", "foo", "bar"},
|
|
{"ssh://git@gitea.example.com:22/foo/bar.git", "gitea.example.com", "https://gitea.example.com", "foo", "bar"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.raw, func(t *testing.T) {
|
|
o, err := parseRemoteURL(c.raw)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
want := Origin{Host: c.host, WebURL: c.webURL, Owner: c.owner, Repo: c.repo}
|
|
if o != want {
|
|
t.Fatalf("got %+v want %+v", o, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseRemoteURLRejectsBadInput(t *testing.T) {
|
|
bad := []string{
|
|
"",
|
|
"not a url",
|
|
"https://gitea.example.com/",
|
|
"https://gitea.example.com/onlyowner",
|
|
}
|
|
for _, raw := range bad {
|
|
if _, err := parseRemoteURL(raw); err == nil {
|
|
t.Fatalf("expected error for %q", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseHost(t *testing.T) {
|
|
cases := []struct {
|
|
in, host, webURL string
|
|
}{
|
|
{"github.com", "github.com", "https://github.com"},
|
|
{"GitHub.com", "github.com", "https://github.com"},
|
|
{"gitea.example.com/", "gitea.example.com", "https://gitea.example.com"},
|
|
{"https://gitea.example.com/", "gitea.example.com", "https://gitea.example.com"},
|
|
{"http://localhost:3000", "localhost", "http://localhost:3000"},
|
|
{"https://example.com/gitea/", "example.com", "https://example.com/gitea"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.in, func(t *testing.T) {
|
|
host, webURL, err := ParseHost(c.in)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if host != c.host || webURL != c.webURL {
|
|
t.Fatalf("got (%s, %s) want (%s, %s)", host, webURL, c.host, c.webURL)
|
|
}
|
|
})
|
|
}
|
|
for _, bad := range []string{"", " ", "https://", "https:///foo"} {
|
|
if _, _, err := ParseHost(bad); err == nil {
|
|
t.Fatalf("expected error for %q", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDetectOriginReadsGitRemote(t *testing.T) {
|
|
dir := t.TempDir()
|
|
for _, args := range [][]string{
|
|
{"init", "-q"},
|
|
{"remote", "add", "origin", "git@github.com:acme/widgets.git"},
|
|
} {
|
|
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v 失败: %v: %s", args, err, out)
|
|
}
|
|
}
|
|
o, err := DetectOrigin(dir)
|
|
if err != nil {
|
|
t.Fatalf("DetectOrigin 出错: %v", err)
|
|
}
|
|
want := Origin{Host: "github.com", WebURL: "https://github.com", Owner: "acme", Repo: "widgets"}
|
|
if o != want {
|
|
t.Fatalf("got %+v want %+v", o, want)
|
|
}
|
|
}
|
|
|
|
func TestDetectOriginFailsWithoutRemote(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if out, err := exec.Command("git", "-C", dir, "init", "-q").CombinedOutput(); err != nil {
|
|
t.Fatalf("git init 失败: %v: %s", err, out)
|
|
}
|
|
if _, err := DetectOrigin(dir); err == nil {
|
|
t.Fatal("没有 origin 时应当报错")
|
|
}
|
|
}
|