| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | // elsewhere has the test run in a directory of its own that is in no |
| 17 | // repository, with a home of its own. |
| 18 | func elsewhere(t *testing.T) { |
| 19 | t.Helper() |
| 20 | dir := t.TempDir() |
| 21 | t.Setenv("HOME", dir) |
| 22 | t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(dir)) |
| 23 | t.Chdir(dir) |
| 24 | } |
| 25 | |
| 26 | // command runs the valley command line args, elsewhere. |
| 27 | func command(t *testing.T, args ...string) (status int, stdout, stderr string) { |
| 28 | t.Helper() |
| 29 | elsewhere(t) |
| 30 | return commandHere(t, args...) |
| 31 | } |
| 32 | |
| 33 | // commandHere runs the valley command line args here. |
| 34 | func commandHere(t *testing.T, args ...string) (status int, stdout, stderr string) { |
| 35 | t.Helper() |
| 36 | var out, errb bytes.Buffer |
| 37 | status = cli(args, &out, &errb, func(int) { t.Fatal("a verb ended the process") }) |
| 38 | return status, out.String(), errb.String() |
| 39 | } |
| 40 | |
| 41 | // No verb is help. An unknown verb prints the usage on stderr and exits 1. |
| 42 | // Each public verb is its own: given an argument it refuses, it says so |
| 43 | // itself. |
| 44 | func TestDispatch(t *testing.T) { |
| 45 | if status, out, errs := command(t); status != 0 || out != helpText("") || errs != "" { |
| 46 | t.Errorf("no verb: %d %q %q", status, out, errs) |
| 47 | } |
| 48 | status, out, errs := command(t, "zx81q", "status") |
| 49 | if want := "valley: unknown verb: zx81q\n\n" + helpPart("usage"); status != 1 || out != "" || errs != want { |
| 50 | t.Errorf("an unknown verb: %d %q %q", status, out, errs) |
| 51 | } |
| 52 | refused := map[string][]string{"knot": {"zx81q"}, "telemetry": {"zx81q"}, "tail": {"a", "b"}, "replay": {"a", "b"}} |
| 53 | for verb := range helpTopics { |
| 54 | args, ok := refused[verb] |
| 55 | if !ok { |
| 56 | args = []string{"--zx81q"} |
| 57 | } |
| 58 | status, out, errs := command(t, append([]string{verb}, args...)...) |
| 59 | if status == 0 || out != "" || errs == "" || strings.Contains(errs, "unknown verb: "+verb) { |
| 60 | t.Errorf("valley %s %s: %d %q %q", verb, strings.Join(args, " "), status, out, errs) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // -h or --help, alone after a public verb, is its help topic. With |
| 66 | // another argument the verb reads them as its own. |
| 67 | func TestVerbHelp(t *testing.T) { |
| 68 | for verb := range helpTopics { |
| 69 | for _, flag := range []string{"-h", "--help"} { |
| 70 | status, out, errs := command(t, verb, flag) |
| 71 | if status != 0 || out != helpText(verb) || errs != "" { |
| 72 | t.Errorf("valley %s %s: %d %q", verb, flag, status, errs) |
| 73 | } |
| 74 | } |
| 75 | if status, out, _ := command(t, "help", verb); status != 0 || out != helpText(verb) { |
| 76 | t.Errorf("valley help %s: %d", verb, status) |
| 77 | } |
| 78 | } |
| 79 | for _, args := range [][]string{{"tail", "-h", "x"}, {"rebase", "--help", "x"}} { |
| 80 | status, out, errs := command(t, args...) |
| 81 | if status != 1 || out != "" || !strings.HasPrefix(errs, "valley: usage: valley "+args[0]+" ") { |
| 82 | t.Errorf("valley %s: %d %q %q", strings.Join(args, " "), status, out, errs) |
| 83 | } |
| 84 | } |
| 85 | for _, args := range [][]string{{"help", "a", "b"}, {"help", "zx81q"}, {"help", "resolve"}} { |
| 86 | if status, out, errs := command(t, args...); status != 1 || out != "" || errs == "" { |
| 87 | t.Errorf("valley %s: %d %q %q", strings.Join(args, " "), status, out, errs) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // --version prints the stamp, or says there is none. |
| 93 | func TestVersion(t *testing.T) { |
| 94 | kept := versionStamp |
| 95 | t.Cleanup(func() { versionStamp = kept }) |
| 96 | versionStamp = "0.1.0-97db597" |
| 97 | if _, out, _ := command(t, "--version"); out != "valley 0.1.0-97db597\n" { |
| 98 | t.Errorf("stamped: %q", out) |
| 99 | } |
| 100 | versionStamp = "" |
| 101 | if _, out, _ := command(t, "--version"); !strings.HasPrefix(out, "valley unstamped (") { |
| 102 | t.Errorf("unstamped: %q", out) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Every verb the usage lists has a help topic, and every help file is a |
| 107 | // public verb's topic or a part topics share. |
| 108 | func TestHelpTopics(t *testing.T) { |
| 109 | listed := regexp.MustCompile(`(?m)^ ([a-z-]+) +[a-z]`).FindAllStringSubmatch(helpPart("usage"), -1) |
| 110 | if len(listed) != len(helpTopics) { |
| 111 | t.Errorf("the usage lists %d verbs, and %d have a topic", len(listed), len(helpTopics)) |
| 112 | } |
| 113 | for _, m := range listed { |
| 114 | if !publicVerb(m[1]) { |
| 115 | t.Errorf("the usage lists %s, which has no topic", m[1]) |
| 116 | } |
| 117 | } |
| 118 | shared := []string{"usage", "naming", "requests", "runtime"} |
| 119 | files, err := fs.Glob(helpFiles, "help/*") |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | for _, f := range files { |
| 124 | name := strings.TrimSuffix(filepath.Base(f), ".txt") |
| 125 | if !publicVerb(name) && !slices.Contains(shared, name) || !strings.HasSuffix(f, ".txt") { |
| 126 | t.Errorf("%s is no verb's topic and no shared part", f) |
| 127 | } |
| 128 | } |
| 129 | for verb, parts := range helpTopics { |
| 130 | if parts[0] != verb || !strings.HasPrefix(helpText(verb), "usage: valley "+verb+" ") { |
| 131 | t.Errorf("%s's topic is not its own: %q", verb, parts) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // rebase takes one change and the signer flags, which it passes on in |
| 137 | // front of the change, and it refuses anything else before it fetches. |
| 138 | func TestRebaseArguments(t *testing.T) { |
| 139 | got, err := rebaseCommandArgs([]string{"--known-signers", "f", "topic", "--integrator-name=i"}) |
| 140 | if want := []string{"--known-signers", "f", "--integrator-name=i", "topic"}; err != nil || !slices.Equal(got, want) { |
| 141 | t.Errorf("got %q %v, want %q", got, err, want) |
| 142 | } |
| 143 | for _, args := range [][]string{{}, {"a", "b"}, {"--head", "0123456", "a"}, {"a", "--known-signers"}, {"-", "a"}, {"--", "a"}, {""}} { |
| 144 | if _, err := rebaseCommandArgs(args); err == nil || err.Error() != rebaseUsage { |
| 145 | t.Errorf("rebase %q: %v", args, err) |
| 146 | } |
| 147 | // Outside a repository a fetch would fail with git's 128. |
| 148 | status, out, errs := command(t, append([]string{"rebase"}, args...)...) |
| 149 | if status != 1 || out != "" || errs != "valley: "+rebaseUsage+"\n" { |
| 150 | t.Errorf("valley rebase %q: %d %q %q", args, status, out, errs) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // pending and rebase fetch origin first, and a fetch that fails ends them |
| 156 | // with git's own status. |
| 157 | func TestFailedFetchExitsWithGitsStatus(t *testing.T) { |
| 158 | elsewhere(t) |
| 159 | gitIn := func(args ...string) error { |
| 160 | cmd := exec.Command("git", args...) |
| 161 | cmd.Env = gitEnvironment(os.Environ()) |
| 162 | return cmd.Run() |
| 163 | } |
| 164 | if err := gitIn("init", "--quiet"); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | if err := gitIn("remote", "add", "origin", filepath.Join(t.TempDir(), "gone.git")); err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | want := exitCode(gitIn("fetch", "--quiet", "origin")) |
| 171 | if want == 0 { |
| 172 | t.Fatal("a fetch of a missing origin succeeded") |
| 173 | } |
| 174 | for _, args := range [][]string{{"pending"}, {"rebase", "topic"}} { |
| 175 | if status, out, errs := commandHere(t, args...); status != want || out != "" { |
| 176 | t.Errorf("valley %s: %d, want git's %d: %s", strings.Join(args, " "), status, want, errs) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // replay publishes the payload the host's publisher hook does: a " in a |
| 182 | // refname escaped as the hook escapes it, and < > & as they are. |
| 183 | func TestReplayPayload(t *testing.T) { |
| 184 | oid := strings.Repeat("a1", 20) |
| 185 | got := refUpdated("app", `refs/heads/a"b<&>`, oid) |
| 186 | want := `{"event":"ref-updated","repo":"app","ref":"refs/heads/a\"b<&>","old":"` + strings.Repeat("0", 40) + `","new":"` + oid + `"}` |
| 187 | if got != want { |
| 188 | t.Fatalf("got %s\nwant %s", got, want) |
| 189 | } |
| 190 | var read struct{ Ref string } |
| 191 | if err := json.Unmarshal([]byte(got), &read); err != nil || read.Ref != `refs/heads/a"b<&>` { |
| 192 | t.Fatalf("the payload reads as %q: %v", read.Ref, err) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // The stamp is taken out of the environment, so a program valley starts |
| 197 | // does not inherit it, and self-update and the command record still read |
| 198 | // it. git there ignores replacement refs. |
| 199 | func TestStampStaysOutOfStartedPrograms(t *testing.T) { |
| 200 | kept := versionStamp |
| 201 | t.Cleanup(func() { versionStamp = kept }) |
| 202 | t.Setenv("VALLEY_VERSION", "0.1.0-97db597") |
| 203 | t.Setenv("GIT_NO_REPLACE_OBJECTS", "") |
| 204 | t.Setenv("GIT_GRAFT_FILE", "/elsewhere") |
| 205 | takeEnvironment() |
| 206 | out, err := exec.Command("sh", "-c", `printf '%s %s %s' "${VALLEY_VERSION-unset}" "$GIT_NO_REPLACE_OBJECTS" "${GIT_GRAFT_FILE-unset}"`).Output() |
| 207 | if err != nil || string(out) != "unset 1 unset" { |
| 208 | t.Fatalf("a started program's environment: %q %v", out, err) |
| 209 | } |
| 210 | if inv := commandOf([]string{"status", "topic"}); inv == nil || inv.version != "0.1.0-97db597" { |
| 211 | t.Errorf("the command record's version: %+v", inv) |
| 212 | } |
| 213 | u, err := newUpdater(selfUpdateArgs{repo: "/elsewhere.git", branch: "main"}, &bytes.Buffer{}) |
| 214 | if err != nil || u.Version != "0.1.0-97db597" { |
| 215 | t.Errorf("self-update's version: %+v %v", u, err) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // A public verb run as the command records it, from the process's start; |
| 220 | // help, a verb's help, --version, an unknown or internal verb and the |
| 221 | // background flusher record nothing. |
| 222 | func TestCommandOf(t *testing.T) { |
| 223 | if inv := commandOf([]string{"pending", "--json"}); inv == nil || !inv.since.Equal(phases.began) { |
| 224 | t.Errorf("pending: %+v", inv) |
| 225 | } |
| 226 | for _, args := range [][]string{{}, {"help", "pending"}, {"pending", "-h"}, {"review", "--help"}, {"--version"}, |
| 227 | {"zx81q"}, {"resolve", "topic"}, {"checkline", "x"}, {"style"}, {"work"}, {"telemetry", "flush", "--watch", "1"}} { |
| 228 | if inv := commandOf(args); inv != nil { |
| 229 | t.Errorf("%q records %+v", args, inv) |
| 230 | } |
| 231 | } |
| 232 | } |