Skip to content
quarry Artifact [98501a02d3]
Login

Artifact [98501a02d3]

Artifact 98501a02d31ed66ff1473ea1cf6d599d9bebe8326b436d4529336ce3133bfd61:


package quarrycli

import (
	"bytes"
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func run(t *testing.T, args ...string) (int, string, string) {
	t.Helper()
	var out, errw bytes.Buffer
	e := &Env{Stdout: &out, Stderr: &errw, Version: "9.9.9-test"}
	e.Pickup.ConfigDir = t.TempDir()
	code := Run(context.Background(), args, e)
	return code, out.String(), errw.String()
}

// Bare quarry lists the verbs the table marks as working and none it
// marks not yet implemented; it grows as verbs ship.
func TestBareQuarryListsOnlyImplementedVerbs(t *testing.T) {
	code, out, _ := run(t)
	if code != 0 {
		t.Errorf("FAIL: bare quarry exit %d", code)
	}
	front := FrontDoor()
	if len(front) == 0 {
		t.Errorf("FAIL: the bare screen has no verbs")
	}
	for _, c := range front {
		if c.Planned {
			t.Errorf("FAIL: front-door verb %q is not implemented", c.Name)
		}
		if !strings.Contains(out, "quarry "+c.Name) {
			t.Errorf("FAIL: bare quarry does not show %q", c.Name)
		}
		if got, _ := Resolve(strings.Fields(c.Name)); got != c {
			t.Errorf("FAIL: %q does not resolve to itself", c.Name)
		}
	}
	for _, c := range Commands() {
		if c.Planned && strings.Contains(out, "quarry "+c.Name) {
			t.Errorf("FAIL: bare quarry shows %q, which is not implemented", c.Name)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestAliasesResolveToTheSameCommands(t *testing.T) {
	for alias, full := range map[string]string{"login": "auth login", "whoami": "auth status"} {
		a, _ := Resolve([]string{alias, "x"})
		b, rest := Resolve(append(strings.Fields(full), "x"))
		if a == nil || a != b {
			t.Errorf("FAIL: %q and %q are different commands", alias, full)
		}
		if len(rest) != 1 || rest[0] != "x" {
			t.Errorf("FAIL: %q left %v for the verb", full, rest)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestHelpListsEveryVerbAndJSONDescribesThem(t *testing.T) {
	_, out, _ := run(t, "help")
	code, js, _ := run(t, "help", "--json")
	if code != 0 {
		t.Errorf("FAIL: help --json exit %d", code)
	}
	var doc struct {
		Version  string `json:"version"`
		Commands []struct {
			Name    string   `json:"name"`
			Aliases []string `json:"aliases"`
			Usage   string   `json:"usage"`
			Summary string   `json:"summary"`
			Planned bool     `json:"planned"`
		} `json:"commands"`
	}
	if err := json.Unmarshal([]byte(js), &doc); err != nil {
		t.Fatalf("FAIL: help --json is not one JSON object: %v", err)
	}
	if len(doc.Commands) != len(Commands()) {
		t.Errorf("FAIL: json lists %d verbs, table has %d", len(doc.Commands), len(Commands()))
	}
	for _, c := range Commands() {
		if !strings.Contains(out, c.Name) {
			t.Errorf("FAIL: help does not list %q", c.Name)
		}
	}
	for _, c := range doc.Commands {
		if c.Name == "" || c.Usage == "" {
			t.Errorf("FAIL: json verb %+v lacks a name or usage", c)
		}
	}
	if doc.Version != "9.9.9-test" {
		t.Errorf("FAIL: json version %q", doc.Version)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestUnknownVerbExits2(t *testing.T) {
	if code, out, _ := run(t, "frobnicate"); code != 2 || out != "" {
		t.Errorf("FAIL: unknown verb exit %d, stdout %q", code, out)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestVersionNamesTheClientAndTheForgeWhenReachable(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/api/v1/version" {
			w.WriteHeader(404)
			return
		}
		w.Write([]byte(`{"version":"4.5.6-forge"}`))
	}))
	defer srv.Close()
	code, out, _ := run(t, "version", srv.URL)
	if code != 0 || !strings.Contains(out, "9.9.9-test") || !strings.Contains(out, "4.5.6-forge") {
		t.Errorf("FAIL: version exit %d output %q", code, out)
	}
	// An unreachable forge does not fail the verb.
	code, out, _ = run(t, "version", "http://127.0.0.1:1")
	if code != 0 || !strings.Contains(out, "9.9.9-test") || strings.Contains(out, "forge") && strings.Contains(out, "4.5") {
		t.Errorf("FAIL: unreachable forge: exit %d output %q", code, out)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// logout is a working verb that the bare screen does not list.
func TestLogoutIsImplementedAndOffTheBareScreen(t *testing.T) {
	c, rest := Resolve([]string{"auth", "logout", "x"})
	if c == nil || c.Name != "auth logout" || len(rest) != 1 {
		t.Fatalf("FAIL: auth logout resolves to %v, rest %v", c, rest)
	}
	if c.Planned || c.Front || c.Hidden {
		t.Errorf("FAIL: logout flags planned=%v front=%v hidden=%v", c.Planned, c.Front, c.Hidden)
	}
	_, bare, _ := run(t)
	if strings.Contains(bare, "logout") {
		t.Errorf("FAIL: the bare screen lists logout")
	}
	_, help, _ := run(t, "help")
	if !strings.Contains(help, "auth logout") {
		t.Errorf("FAIL: help does not list logout")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Help's exit line names every status the verbs use, 4 included.
func TestHelpNamesExitStatus4(t *testing.T) {
	_, help, _ := run(t, "help")
	var exit string
	for _, l := range strings.Split(help, "\n") {
		if strings.HasPrefix(l, "Exit:") {
			exit = l
		}
	}
	if !strings.Contains(exit, "4") {
		t.Errorf("FAIL: the exit line is %q", exit)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Through the program: a verb that needs a token, run with none, exits 4
// and --json carries the next command.
func TestVerbsNeedingATokenExit4Through(t *testing.T) {
	for _, verb := range [][]string{{"auth", "status"}, {"whoami"}, {"auth", "logout"}} {
		code, out, _ := run(t, append(append([]string{}, verb...), "--json", "http://127.0.0.1:1")...)
		var m struct {
			OK   bool   `json:"ok"`
			Code string `json:"code"`
			Next struct {
				Cmd string `json:"cmd"`
			} `json:"next"`
		}
		if err := json.Unmarshal([]byte(out), &m); err != nil || code != 4 || m.OK || m.Code != "no_login" || m.Next.Cmd != "quarry auth login" {
			t.Errorf("FAIL: %v: exit %d out %q", verb, code, out)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// An error prefix names the verb as it was typed: the full name, or the
// alias when the alias was used.
func TestErrorPrefixNamesTheVerbAsTyped(t *testing.T) {
	for _, typed := range []string{"auth status", "whoami", "auth logout", "auth login", "login"} {
		args := append(strings.Fields(typed), "--nope")
		if typed == "auth logout" || typed == "auth status" || typed == "whoami" {
			args = strings.Fields(typed) // no login stored: the not-logged-in refusal
			args = append(args, "http://127.0.0.1:1")
		}
		code, _, errw := run(t, args...)
		if code == 0 || !strings.HasPrefix(errw, "quarry "+typed+": ") {
			t.Errorf("FAIL: typed %q: exit %d, stderr %q", typed, code, errw)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// doctor is a verb that works: help does not mark it planned, the bare
// screen leads with it, and it runs.
func TestDoctorIsARealVerb(t *testing.T) {
	c, _ := Resolve([]string{"doctor"})
	if c == nil || c.Planned {
		t.Fatalf("FAIL: doctor is %v", c)
	}
	code, js, _ := run(t, "help", "--json")
	var doc struct {
		Commands []struct {
			Name    string `json:"name"`
			Planned bool   `json:"planned"`
		} `json:"commands"`
	}
	if code != 0 || json.Unmarshal([]byte(js), &doc) != nil {
		t.Fatalf("FAIL: help --json exit %d", code)
	}
	for _, c := range doc.Commands {
		if c.Name == "doctor" && c.Planned {
			t.Errorf("FAIL: help --json still marks doctor planned")
		}
	}
	_, bare, _ := run(t)
	if !strings.Contains(bare, "quarry doctor") {
		t.Errorf("FAIL: the bare screen does not lead to doctor")
	}
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`{"version":"9.9.9-test"}`))
	}))
	defer srv.Close()
	code, out, _ := run(t, "doctor", srv.URL, "--json")
	var obj struct {
		OK     bool `json:"ok"`
		Checks []struct {
			ID string `json:"id"`
		} `json:"checks"`
	}
	if json.Unmarshal([]byte(out), &obj) != nil || len(obj.Checks) == 0 {
		t.Errorf("FAIL: doctor --json printed %q", out)
	}
	if code == 2 {
		t.Errorf("FAIL: doctor exit %d", code)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// clone and setup-zit are verbs that work; the credential helper zit
// calls is real but left out of help.
func TestCloneAndSetupZitAreRealVerbs(t *testing.T) {
	for _, name := range []string{"repo clone", "auth setup-zit"} {
		c, rest := Resolve(append(strings.Fields(name), "x"))
		if c == nil || c.Name != name || c.Planned || c.Hidden || c.run == nil || len(rest) != 1 {
			t.Errorf("FAIL: %q resolves to %+v", name, c)
		}
	}
	c, _ := Resolve([]string{"auth", "credential", "get"})
	if c == nil || !c.Hidden || c.Planned || c.run == nil {
		t.Errorf("FAIL: auth credential is %+v", c)
	}
	for _, c := range Commands() {
		if c.Name == "auth credential" {
			t.Errorf("FAIL: help lists the credential helper")
		}
	}
	code, _, errw := run(t, "repo", "clone", "nobody")
	if code != 2 || !strings.HasPrefix(errw, "quarry repo clone: ") {
		t.Errorf("FAIL: exit %d stderr %q", code, errw)
	}
}

// quarry help COMMAND prints what quarry COMMAND --help prints.
func TestHelpCommandIsTheCommandsOwnHelp(t *testing.T) {
	for _, c := range table {
		name := strings.Fields(c.Name)
		hc, hout, _ := run(t, append([]string{"help"}, name...)...)
		fc, fout, ferr := run(t, append(append([]string{}, name...), "--help")...)
		if hc != fc || hout != fout {
			t.Errorf("FAIL: help %q exit %d out %q; --help exit %d out %q", c.Name, hc, hout, fc, fout)
		}
		if c.Name == "land" && (hc != 0 || hout == "" || ferr != "") {
			t.Errorf("FAIL: help land exit %d", hc)
		}
	}
	if code, _, _ := run(t, "help", "nosuchcommand"); code != 2 {
		t.Errorf("FAIL: help of an unknown command exits %d, want 2", code)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}