gunk-dev/the-valley

ready_test.go

source1cb0f4ba6ed2main's published tip history
3.2 KiB
1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "the-valley/change"
14)
15
16// A withdrawal given the head it was read at writes only while the
17// change's active version is still at that head, as review's [r]eject
18// withdraws the version whose branch it deleted: a version readied since
19// is left as it is.
20func TestSetStateWithdrawsOnlyAtTheHeadRead(t *testing.T) {
21 dir := t.TempDir()
22 t.Setenv("HOME", dir)
23 t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
24 t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
25 origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work")
26 sh := func(args ...string) string {
27 t.Helper()
28 cmd := exec.Command("git", args...)
29 cmd.Dir = work
30 out, err := cmd.Output()
31 if err != nil {
32 t.Fatalf("git %s: %v", strings.Join(args, " "), err)
33 }
34 return strings.TrimSpace(string(out))
35 }
36 valley := func(args ...string) {
37 t.Helper()
38 var stdout, stderr bytes.Buffer
39 if code := run(args, &stdout, &stderr); code != 0 {
40 t.Fatalf("valley %s: %s", strings.Join(args, " "), stderr.String())
41 }
42 }
43 if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil {
44 t.Fatalf("git init: %v\n%s", err, out)
45 }
46 if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil {
47 t.Fatalf("git clone: %v\n%s", err, out)
48 }
49 sh("config", "user.name", "valley-go-test")
50 sh("config", "user.email", "valley-go-test@localhost")
51 sh("config", "valley.principal", "alice")
52 sh("checkout", "--quiet", "-b", "main")
53 sh("commit", "--quiet", "--allow-empty", "-m", "base")
54 sh("push", "--quiet", "origin", "main")
55 t.Chdir(work)
56
57 id := strings.Repeat("k", 32)
58 sh("checkout", "--quiet", "-b", "topic")
59 sh("commit", "--quiet", "--allow-empty", "-m", "one")
60 first := sh("rev-parse", "HEAD")
61 sh("push", "--quiet", "origin", "topic")
62 valley("ready", "--change", id, "topic")
63 sh("commit", "--quiet", "--allow-empty", "-m", "two")
64 second := sh("rev-parse", "HEAD")
65 sh("push", "--quiet", "origin", "topic")
66 valley("ready", "--change", id, "topic")
67
68 state := func() string {
69 t.Helper()
70 blob, err := exec.Command("git", "-C", origin, "cat-file", "blob", change.Ref(recordTarget, id)+":record.json").Output()
71 if err != nil {
72 t.Fatalf("origin holds no record of %s: %v", id, err)
73 }
74 var r change.Record
75 if err := json.Unmarshal(blob, &r); err != nil {
76 t.Fatalf("the record of %s: %v", id, err)
77 }
78 return r.State
79 }
80 g := git{env: gitEnvironment(os.Environ()), stderr: io.Discard}
81 err := setState(g, []string{id}, io.Discard, io.Discard, "withdraw", change.Withdrawn, first)
82 if err == nil || !strings.Contains(err.Error(), "is at v2 now") {
83 t.Errorf("a withdrawal at v1's head, with v2 active, was not refused: %v", err)
84 }
85 if s := state(); s != change.Ready {
86 t.Errorf("a refused withdrawal left the change %s", s)
87 }
88 if err := setState(g, []string{id}, io.Discard, io.Discard, "withdraw", change.Withdrawn, second); err != nil {
89 t.Fatalf("a withdrawal at the active head: %v", err)
90 }
91 if s := state(); s != change.Withdrawn {
92 t.Errorf("a withdrawal at the active head left the change %s", s)
93 }
94}