package main
import (
"net/http"
"net/http/httptest"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
func TestGitHubRepoPathReadsEveryAddressForm(t *testing.T) {
for in, want := range map[string]string{
"https://github.com/danmestas/dagnats": "danmestas/dagnats",
"https://github.com/danmestas/dagnats.git": "danmestas/dagnats",
"https://GitHub.com/danmestas/dagnats/": "danmestas/dagnats",
"[email protected]:danmestas/dagnats.git": "danmestas/dagnats",
"ssh://[email protected]/danmestas/dagnats.git": "danmestas/dagnats",
"https://gitlab.com/danmestas/dagnats": "",
"https://github.com/danmestas": "",
"https://github.com/danmestas/dagnats/tree/x": "",
} {
if got := githubRepoPath(in); got != want {
t.Errorf("FAIL: %q -> %q, want %q", in, got, want)
}
}
t.Log("ok: GitHub addresses")
}
// An import from GitHub starts with GitHub's description, homepage and
// topics, filling only what is empty: a description given when the
// repository was created stays.
func TestAGitHubImportCopiesItsAboutIntoEmptyFields(t *testing.T) {
gh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/repos/danmestas/edgesync" {
http.NotFound(w, r)
return
}
w.Write([]byte(`{"description":"Fossil NATS Sync","homepage":"https://edgesync.example","topics":["fossil","nats"]}`))
}))
defer gh.Close()
rg := newTestRegistrar(t, 25)
rg.githubAPI = gh.URL
if err := rg.odb.AddRepo("EdgeSync", t.TempDir(), "", oltp.Public); err != nil {
t.Fatal(err)
}
rg.copyGitHubAbout("EdgeSync", "https://github.com/danmestas/edgesync.git", "")
row, _, _ := rg.odb.RepoByName("EdgeSync")
if row.Description != "Fossil NATS Sync" || row.Homepage != "https://edgesync.example" || row.Topics != "fossil nats" {
t.Errorf("FAIL: copied %q / %q / %q", row.Description, row.Homepage, row.Topics)
}
t.Log("ok: copied into empty fields")
if err := rg.odb.AddRepo("Kept", t.TempDir(), "our own words", oltp.Public); err != nil {
t.Fatal(err)
}
rg.copyGitHubAbout("Kept", "https://github.com/danmestas/edgesync", "")
if row, _, _ := rg.odb.RepoByName("Kept"); row.Description != "our own words" || row.Topics != "fossil nats" {
t.Errorf("FAIL: a set description was replaced or the empty topics not filled: %q / %q", row.Description, row.Topics)
}
t.Log("ok: a set field stays")
}