cmd/gitbay/summaries_test.go

fad7a633f45132422f6648c1c7508b4e19b6539c
gitbay/cmd/gitbay/summaries_test.go

85 lines · 2379 bytes

 1package main
 2
 3import (
 4	"flag"
 5	"fmt"
 6	"go/format"
 7	"os"
 8	"slices"
 9	"strings"
10	"testing"
11
12	"gitbay.org/gitbay/internal/control"
13)
14
15var updateSummaries = flag.Bool("update", false, "rewrite summaries_gen.go")
16
17// summaries_gen.go is the registry's one-line summaries, so the CLI's
18// command list and completions say what help says.
19func TestSummariesAreCurrent(t *testing.T) {
20	var b strings.Builder
21	b.WriteString("// Code generated by TestSummariesAreCurrent -update; DO NOT EDIT.\n\npackage main\n\nvar summaries = map[string]string{\n")
22	var lines []string
23	for _, cmd := range control.Commands() {
24		lines = append(lines, fmt.Sprintf("\t%q: %q,\n", strings.Join(cmd.Path, " "), cmd.Summary))
25	}
26	slices.Sort(lines)
27	b.WriteString(strings.Join(lines, ""))
28	b.WriteString("}\n")
29	want, err := format.Source([]byte(b.String()))
30	if err != nil {
31		t.Fatal(err)
32	}
33	if *updateSummaries {
34		os.WriteFile("summaries_gen.go", want, 0o644)
35	}
36	got, _ := os.ReadFile("summaries_gen.go")
37	if string(got) != string(want) {
38		t.Fatal("summaries_gen.go is stale: go test ./cmd/gitbay -run TestSummariesAreCurrent -update")
39	}
40}
41
42func TestRootSectionsCoverEveryCommand(t *testing.T) {
43	seen := map[string]int{}
44	for _, s := range rootSections {
45		for _, n := range s.names {
46			seen[n]++
47		}
48	}
49	for _, c := range newRoot().Commands() {
50		name := c.Name()
51		if name == "help" || name == "completion" {
52			continue
53		}
54		if seen[name] != 1 {
55			t.Errorf("%s is in %d root sections", name, seen[name])
56		}
57	}
58}
59
60func TestGroupsSayWhatTheServerSays(t *testing.T) {
61	nouns := control.NounSummaries()
62	for _, c := range newRoot().Commands() {
63		if s, ok := nouns[c.Name()]; ok && c.Short != s {
64			t.Errorf("%s: CLI says %q, server says %q", c.Name(), c.Short, s)
65		}
66	}
67}
68
69// aliasGroupNames is kept by hand because the CLI has no registry to
70// consult; this fails when it drifts from internal/control's nounAliases
71// keys instead of silently falling back to the registered forms (#267).
72func TestAliasGroupNamesMatchServer(t *testing.T) {
73	want := map[string]bool{}
74	for _, name := range control.NounAliasNames() {
75		want[name] = true
76	}
77	if len(aliasGroupNames) != len(want) {
78		t.Fatalf("aliasGroupNames has %d entries, server has %d", len(aliasGroupNames), len(want))
79	}
80	for name := range want {
81		if !aliasGroupNames[name] {
82			t.Errorf("aliasGroupNames is missing %q", name)
83		}
84	}
85}