gunk-dev/the-valley

command_test.go

source5d56575cb86dmain's published tip history
2.2 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}
22
23// Telemetry is off without asking git only where no configuration file
24// mentions it or includes another.
25func TestTelemetryMentioned(t *testing.T) {
26 home, repo := t.TempDir(), t.TempDir()
27 t.Setenv("HOME", home)
28 t.Setenv("XDG_CONFIG_HOME", "")
29 t.Chdir(repo)
30 if !telemetryMentioned(git{env: os.Environ()}) {
31 t.Fatal("outside a repository, git must answer")
32 }
33 os.MkdirAll(filepath.Join(repo, ".git"), 0o755)
34 os.WriteFile(filepath.Join(repo, ".git", "config"), []byte("[core]\n\tbare = false\n"), 0o644)
35 if telemetryMentioned(git{env: os.Environ()}) {
36 t.Fatal("no file mentions telemetry, yet git would be asked")
37 }
38 for _, text := range []string{"[valley]\n\tTelemetry = true\n", "[include]\n\tpath = elsewhere\n"} {
39 os.WriteFile(filepath.Join(home, ".gitconfig"), []byte(text), 0o644)
40 if !telemetryMentioned(git{env: os.Environ()}) {
41 t.Fatalf("%q is not read as a possible yes", text)
42 }
43 }
44 // A walk's step asks about its own repository, wherever the process is.
45 os.Remove(filepath.Join(home, ".gitconfig"))
46 step := t.TempDir()
47 os.MkdirAll(filepath.Join(step, ".git"), 0o755)
48 os.WriteFile(filepath.Join(step, ".git", "config"), []byte("[valley]\n\ttelemetry = true\n"), 0o644)
49 if !telemetryMentioned(git{env: os.Environ(), dir: step}) {
50 t.Fatal("the step's repository mentions telemetry, and it was not read")
51 }
52 if telemetryMentioned(git{env: os.Environ()}) {
53 t.Fatal("the working directory's repository mentions no telemetry, yet git would be asked")
54 }
55}