Download
44 lines 1.2 KiBGoModified 2026-10-28 raw
1234567891011121314151617181920212223242526272829303132333435363738394041424344
package mirror

import (
	"os"
	"path/filepath"
	"testing"
)

func TestHost(t *testing.T) {
	for in, want := range map[string]string{
		"git@github.com:me/x.git":          "github.com",
		"github.com:me/x.git":              "github.com",
		"ssh://git@example.com:2222/x.git": "[example.com]:2222",
		"ssh://example.com/x.git":          "example.com",
		"/srv/git/x.git":                   "",
		"./x:y":                            "",
		"file:///srv/x.git":                "",
		"https://github.com/me/x":          "",
		"git@bad host:x":                   "",
	} {
		h, p := Host(in)
		got := ""
		if h != "" {
			got = Entry(h, p)
		}
		if got != want {
			t.Errorf("Host(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestFailures(t *testing.T) {
	dir := t.TempDir()
	if Failures(dir) != nil {
		t.Error("failures with no file")
	}
	if err := os.WriteFile(filepath.Join(dir, FailedFile), []byte("mirror to git@a:x.git failed: no\n\nmirror to /b refused: its main is at 1\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	f := Failures(dir)
	if len(f) != 2 || !Failed(f, "git@a:x.git") || !Failed(f, "/b") || Failed(f, "/c") || Failed(f, "git@a") {
		t.Errorf("failures: %q", f)
	}
}