package web
import (
"net/url"
"strings"
"testing"
)
// asked pulls the ?clone= parameter out of a query string the way the
// handler does, so these cases are written the way a URL is.
func asked(query string) string {
v, err := url.ParseQuery(query)
if err != nil {
panic(err)
}
return v.Get("clone")
}
func TestFossilIsOfferedFirstWhereItWorks(t *testing.T) {
for _, tc := range []struct {
name string
fossilClone bool
query string
want string
}{
{"a fossil-compatible store defaults to fossil", true, "", "fossil"},
{"and zit is one click away", true, "clone=zit", "zit"},
{"asking for fossil explicitly still gets fossil", true, "clone=fossil", "fossil"},
// A store that emitted a sub-manifest cannot be cloned by
// fossil at all, so zit is the only offer — and asking for
// fossil must not produce a command that fails.
{"a sub-manifest store is zit only", false, "", "zit"},
{"even when fossil is asked for by name", false, "clone=fossil", "zit"},
// The submanifest probe fails closed: an unanswered question
// leaves fossilClone false, which this same branch handles.
{"an unprobed store is zit only", false, "clone=zit", "zit"},
} {
if got := cloneTool(tc.fossilClone, asked(tc.query)); got != tc.want {
t.Errorf("FAIL: %s: got %q, want %q", tc.name, got, tc.want)
}
}
}
// The picker is drawn only where there is a choice to make. On a
// sub-manifest store it must not appear at all.
func TestThePickerIsDrawnOnlyWhereThereIsAChoice(t *testing.T) {
for _, tc := range []struct {
fossilClone bool
wantPicker bool
}{{true, true}, {false, false}} {
var buf strings.Builder
err := noHooks.template(treePage).ExecuteTemplate(&buf, "content", pageData{
Repo: "alpha", CloneURL: "https://example.test/r/alpha",
FossilClone: tc.fossilClone,
CloneTool: cloneTool(tc.fossilClone, ""),
})
if err != nil {
t.Skipf("tree template needs more page data than this test builds: %v", err)
}
if got := strings.Contains(buf.String(), "clonepick"); got != tc.wantPicker {
t.Errorf("FAIL: fossilClone=%v drew a picker=%v, want %v", tc.fossilClone, got, tc.wantPicker)
}
}
}