| 1 | package main |
| 2 | |
| 3 | import ( |
| 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 --. |
| 13 | func 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. |
| 25 | func 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() { |
| 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() { |
| 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() { |
| 41 | t.Fatalf("%q is not read as a possible yes", text) |
| 42 | } |
| 43 | } |
| 44 | } |