Skip to content
quarry Artifact [ca451694ba]
Login

Artifact [ca451694ba]

Artifact ca451694ba1147a895204c74d79bb3cff42bfd4b9b9686ac8fa37fe63b3510f2:


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 names the front-door verbs, taken from the command table:
// every verb the table lists as front-door appears on the screen, and the
// screen holds no verb that is not in the table.
func TestBareQuarryShowsTheFrontDoorVerbs(t *testing.T) {
	code, out, _ := run(t)
	if code != 0 {
		t.Errorf("FAIL: bare quarry exit %d", code)
	}
	front := FrontDoor()
	if len(front) != 3 {
		t.Errorf("FAIL: %d front-door verbs, want 3", len(front))
	}
	for _, c := range front {
		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)
		}
	}
	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")
	}
}