gunk-dev/the-valley

repo_test.go

source5b20ed8dd76bmain's published tip history
7.3 KiB
1package review
2
3import (
4 "bytes"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "sort"
9 "strings"
10 "testing"
11 "time"
12
13 "the-valley/change"
14 "the-valley/delta"
15)
16
17// world is a scratch repository the tests build commits, change records
18// and review histories in, with real git.
19type world struct {
20 t *testing.T
21 dir string
22 env []string
23}
24
25func newWorld(t *testing.T) *world {
26 t.Helper()
27 dir := t.TempDir()
28 w := &world{t: t, dir: dir, env: append(os.Environ(),
29 "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", "HOME="+dir,
30 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@localhost", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@localhost")}
31 w.git(nil, "init", "--quiet", "--bare")
32 return w
33}
34
35// clone is another repository beside w, with w as its remote origin.
36func (w *world) clone() *world {
37 w.t.Helper()
38 c := &world{t: w.t, dir: w.t.TempDir(), env: w.env}
39 c.git(nil, "init", "--quiet", "--bare")
40 c.git(nil, "remote", "add", "origin", w.dir)
41 c.git(nil, "fetch", "--quiet", "origin", "+refs/*:refs/*")
42 return c
43}
44
45func (w *world) repo() Repo { return Repo{Dir: w.dir, Env: w.env} }
46
47func (w *world) git(stdin []byte, args ...string) string {
48 w.t.Helper()
49 out, err := w.run(stdin, args...)
50 if err != nil {
51 w.t.Fatalf("git %s: %v", strings.Join(args, " "), err)
52 }
53 return out
54}
55
56func (w *world) run(stdin []byte, args ...string) (string, error) {
57 cmd := exec.Command("git", args...)
58 cmd.Dir = w.dir
59 cmd.Env = w.env
60 cmd.Stdin = bytes.NewReader(stdin)
61 var stderr bytes.Buffer
62 cmd.Stderr = &stderr
63 out, err := cmd.Output()
64 if err != nil {
65 return "", &gitError{err, stderr.String()}
66 }
67 return strings.TrimSpace(string(out)), nil
68}
69
70type gitError struct {
71 err error
72 stderr string
73}
74
75func (e *gitError) Error() string { return e.err.Error() + ": " + e.stderr }
76
77func (w *world) blob(content string) string {
78 return w.git([]byte(content), "hash-object", "-w", "--stdin")
79}
80
81// tree writes the tree that holds exactly the files given, each a plain
82// file, through a scratch index.
83func (w *world) tree(files map[string]string) string {
84 w.t.Helper()
85 index := filepath.Join(w.t.TempDir(), "index")
86 paths := make([]string, 0, len(files))
87 for p := range files {
88 paths = append(paths, p)
89 }
90 sort.Strings(paths)
91 var input bytes.Buffer
92 for _, p := range paths {
93 input.WriteString("100644 " + w.blob(files[p]) + "\t" + p + "\n")
94 }
95 saved := w.env
96 w.env = append(w.env, "GIT_INDEX_FILE="+index)
97 defer func() { w.env = saved }()
98 w.git(input.Bytes(), "update-index", "--add", "--index-info")
99 return w.git(nil, "write-tree")
100}
101
102// commit writes a commit of the files given on the parents given.
103func (w *world) commit(files map[string]string, parents ...string) string {
104 w.t.Helper()
105 return w.commitTree(w.tree(files), parents...)
106}
107
108func (w *world) commitTree(tree string, parents ...string) string {
109 w.t.Helper()
110 args := []string{"commit-tree", tree, "-m", "c"}
111 for _, p := range parents {
112 args = append(args, "-p", p)
113 }
114 return w.git(nil, args...)
115}
116
117// oneFile writes a commit whose tree holds one plain file, on the parents
118// given.
119func (w *world) oneFile(name string, content []byte, parents ...string) string {
120 w.t.Helper()
121 blob := w.git(content, "hash-object", "-w", "--stdin")
122 tree := w.git([]byte("100644 blob "+blob+"\t"+name+"\n"), "mktree")
123 return w.commitTree(tree, parents...)
124}
125
126// record writes a change record commit holding r, on the parents the hook
127// would have it on.
128func (w *world) record(r *change.Record, parents ...string) string {
129 w.t.Helper()
130 data, err := change.Encode(r)
131 if err != nil {
132 w.t.Fatal(err)
133 }
134 return w.oneFile("record.json", data, parents...)
135}
136
137func (w *world) setRef(ref, commit string) {
138 w.t.Helper()
139 w.git(nil, "update-ref", ref, commit)
140}
141
142func (w *world) refAt(ref string) string {
143 w.t.Helper()
144 out, err := w.run(nil, "rev-parse", "--verify", "--quiet", ref)
145 if err != nil {
146 return ""
147 }
148 return out
149}
150
151const (
152 changeID = "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"
153 otherID = "kkkkllllmmmmnnnnooooppppqqqqrrrr"
154)
155
156// story is a change in w: main at tip, and a change readied at head1 and
157// then head2, both on base, with its record ref at rec2, whose version 2
158// is head2. rec1 is its first record, whose one version is head1.
159type story struct {
160 base, tip, head1, head2 string
161 rec1, rec2 string
162 record *change.Record
163}
164
165func (w *world) story() story {
166 w.t.Helper()
167 var s story
168 s.base = w.commit(map[string]string{"README.md": "hello\n", "docs/guide.md": "one\ntwo\nthree\n"})
169 s.tip = w.commit(map[string]string{"README.md": "hello, world\n", "docs/guide.md": "one\ntwo\nthree\n"}, s.base)
170 s.head1 = w.commit(map[string]string{"README.md": "hello\n", "docs/guide.md": "one\n2\nthree\n"}, s.base)
171 s.head2 = w.commit(map[string]string{"README.md": "hello\n", "docs/guide.md": "one\n2\nthree\nfour\n", "docs/new file.md": "new\n"}, s.base)
172 w.setRef("refs/heads/main", s.tip)
173 r := &change.Record{
174 Schema: change.Schema, ChangeID: changeID, Target: "main", Owner: "author", State: change.Ready, Seq: 1,
175 Versions: []change.Version{{N: 1, Head: change.HeadPrefix + s.head1, Title: "Edit the guide", Branch: "guide", Uploader: "author"}},
176 Active: 1,
177 }
178 s.rec1 = w.record(r, s.head1)
179 r.Seq, r.Active = 2, 2
180 r.Versions = append(r.Versions, change.Version{N: 2, Head: change.HeadPrefix + s.head2, Title: "Edit the guide", Branch: "guide", Uploader: "author"})
181 s.rec2 = w.record(r, s.rec1, s.head2)
182 s.record = r
183 w.setRef(change.Ref("main", changeID), s.rec2)
184 return s
185}
186
187// budget is one no test of a small repository comes near.
188func budget() delta.Budget {
189 return delta.Budget{Bytes: 64 << 20, Entries: 100000, Deadline: time.Now().Add(time.Minute)}
190}
191
192func soon() time.Time { return time.Now().Add(time.Minute) }
193
194// verdict is a review of version 2 of the story's change by reviewer,
195// bound with Bind.
196func (w *world) verdict(s story, reviewer, verdict string) *Record {
197 w.t.Helper()
198 _, b, err := Bind(w.repo(), s.rec2, 2, s.tip, budget())
199 if err != nil {
200 w.t.Fatal(err)
201 }
202 id, err := NewID()
203 if err != nil {
204 w.t.Fatal(err)
205 }
206 return &Record{
207 Schema: Schema, ReviewID: id, ChangeID: changeID, Target: "main", Review: "docs-style",
208 Reviewer: reviewer, Verdict: verdict, Binding: &b, Summary: "Read against " + s.tip[:7] + ".",
209 Notes: []Note{
210 {Side: After, Path: "docs/guide.md", Blob: change.HeadPrefix + w.git(nil, "rev-parse", s.head2+":docs/guide.md"),
211 Lines: &Lines{Start: 2, End: 4}, Body: "Spell out the number."},
212 {Side: Before, Path: "docs/guide.md", Blob: change.HeadPrefix + w.git(nil, "rev-parse", s.base+":docs/guide.md"),
213 Lines: &Lines{Start: 2, End: 2}, Body: "This line read better."},
214 {Side: After, Path: "docs/new file.md", Blob: change.HeadPrefix + w.git(nil, "rev-parse", s.head2+":docs/new file.md"),
215 Body: "A whole new file."},
216 },
217 }
218}
219
220// withdrawal is a withdrawal of the review given, by reviewer.
221func withdrawal(of *Record, reviewer string) *Record {
222 id, err := NewID()
223 if err != nil {
224 panic(err)
225 }
226 w := of.ReviewID
227 return &Record{
228 Schema: Schema, ReviewID: id, ChangeID: of.ChangeID, Target: of.Target, Review: of.Review,
229 Reviewer: reviewer, Verdict: Withdraw, Withdraws: &w, Summary: "Judged in error.",
230 }
231}
232
233// against is what the story's reviews verify against.
234func (w *world) against() Against {
235 return Against{
236 RecordTip: w.refAt(change.Ref("main", changeID)),
237 TargetTip: w.refAt("refs/heads/main"),
238 Budget: budget(),
239 }
240}