gunk-dev/the-valley

command_test.go

source1cb0f4ba6ed2main's published tip history
1.7 KiB
1package main
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "testing"
8)
9
10// A record names the verb, a known subverb and known flags, and no value:
11// not a flag's, not a positional one, not one shaped like a flag, and
12// nothing after --.
13func TestCommandWordsKeepNoValue(t *testing.T) {
14 verb, sub, flags := commandWords([]string{"knot", "comment", "oc-1", "-m", "zx81q", "--body=zx81q", "--zx81q", "--", "--json"})
15 if verb != "knot" || sub != "comment" || !slices.Equal(flags, []string{"-m", "--body"}) {
16 t.Fatalf("got %q %q %q", verb, sub, flags)
17 }
18 if verb, sub, _ := commandWords([]string{"zx81q", "show"}); verb != "unknown" || sub != "" {
19 t.Fatalf("an unknown verb read as %q %q", verb, sub)
20 }
21 if verb, _, flags := commandWords([]string{"walk", "-v", "--repo", "app"}); verb != "review" || !slices.Equal(flags, []string{"-v", "--repo"}) {
22 t.Fatalf("review's walk read as %q %q", verb, flags)
23 }
24}
25
26// Telemetry is off without asking git only where no configuration file
27// mentions it or includes another.
28func TestTelemetryMentioned(t *testing.T) {
29 home, repo := t.TempDir(), t.TempDir()
30 t.Setenv("HOME", home)
31 t.Setenv("XDG_CONFIG_HOME", "")
32 t.Chdir(repo)
33 if !telemetryMentioned() {
34 t.Fatal("outside a repository, git must answer")
35 }
36 os.MkdirAll(filepath.Join(repo, ".git"), 0o755)
37 os.WriteFile(filepath.Join(repo, ".git", "config"), []byte("[core]\n\tbare = false\n"), 0o644)
38 if telemetryMentioned() {
39 t.Fatal("no file mentions telemetry, yet git would be asked")
40 }
41 for _, text := range []string{"[valley]\n\tTelemetry = true\n", "[include]\n\tpath = elsewhere\n"} {
42 os.WriteFile(filepath.Join(home, ".gitconfig"), []byte(text), 0o644)
43 if !telemetryMentioned() {
44 t.Fatalf("%q is not read as a possible yes", text)
45 }
46 }
47}