gunk-dev/the-valley

bundle_test.go

sourcef626522c2906main's published tip history
9.2 KiB
1package export_test
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "the-valley/webui/export"
14)
15
16// git runs git for a test, with none of the caller's GIT_ variables and no
17// system or global configuration, and returns its trimmed output.
18func git(t *testing.T, dir string, stdin []byte, args ...string) string {
19 t.Helper()
20 if dir != "" {
21 args = append([]string{"-C", dir}, args...)
22 }
23 cmd := exec.Command("git", args...)
24 for _, kv := range os.Environ() {
25 if !strings.HasPrefix(kv, "GIT_") {
26 cmd.Env = append(cmd.Env, kv)
27 }
28 }
29 cmd.Env = append(cmd.Env, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null",
30 "GIT_AUTHOR_NAME=Example Author", "GIT_AUTHOR_EMAIL=author@example.org",
31 "GIT_COMMITTER_NAME=Example Author", "GIT_COMMITTER_EMAIL=author@example.org",
32 "GIT_AUTHOR_DATE=2026-10-01T09:00:00Z", "GIT_COMMITTER_DATE=2026-10-01T09:00:00Z")
33 if stdin != nil {
34 cmd.Stdin = bytes.NewReader(stdin)
35 }
36 var stderr bytes.Buffer
37 cmd.Stderr = &stderr
38 out, err := cmd.Output()
39 if err != nil {
40 t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String())
41 }
42 return strings.TrimSpace(string(out))
43}
44
45// source is a bare repository whose main has two commits, first and tip,
46// and whose side branch has a commit main does not reach.
47type source struct {
48 repo, first, tip, side string
49}
50
51func newSource(t *testing.T, format string) source {
52 t.Helper()
53 repo := filepath.Join(t.TempDir(), "source.git")
54 git(t, "", nil, "init", "--quiet", "--bare", "--initial-branch=main", "--object-format="+format, repo)
55 commit := func(parent, file, content string) string {
56 blob := git(t, repo, []byte(content), "hash-object", "-w", "--stdin")
57 tree := git(t, repo, []byte(fmt.Sprintf("100644 blob %s\t%s\n", blob, file)), "mktree")
58 args := []string{"commit-tree", tree, "-m", file}
59 if parent != "" {
60 args = append(args, "-p", parent)
61 }
62 return git(t, repo, nil, args...)
63 }
64 s := source{repo: repo}
65 s.first = commit("", "README.md", "# source\n")
66 s.tip = commit(s.first, "main.go", "package main\n")
67 s.side = commit(s.first, "side.txt", "not on main\n")
68 git(t, repo, nil, "update-ref", "refs/heads/main", s.tip)
69 git(t, repo, nil, "update-ref", "refs/heads/side", s.side)
70 return s
71}
72
73func (s source) bundle(t *testing.T, args ...string) string {
74 t.Helper()
75 path := filepath.Join(t.TempDir(), "b.bundle")
76 git(t, s.repo, nil, append([]string{"bundle", "create", "--quiet", path}, args...)...)
77 return path
78}
79
80// A full bundle of main at the declared tip is accepted into a fresh bare
81// repository holding main alone.
82func TestAcceptBundle(t *testing.T) {
83 ctx := context.Background()
84 for _, format := range []string{"sha1", "sha256"} {
85 t.Run(format, func(t *testing.T) {
86 s := newSource(t, format)
87 repo := filepath.Join(t.TempDir(), "app.git")
88 if err := export.AcceptBundle(ctx, s.bundle(t, "main"), repo, s.tip); err != nil {
89 t.Fatal(err)
90 }
91 if refs := git(t, repo, nil, "for-each-ref", "--format=%(refname) %(objectname)"); refs != "refs/heads/main "+s.tip {
92 t.Errorf("the repository holds %q", refs)
93 }
94 if got := git(t, repo, nil, "rev-parse", "--show-object-format"); got != format {
95 t.Errorf("the repository is %s", got)
96 }
97 })
98 }
99}
100
101// What the acceptance procedure refuses, each leaving no repository.
102func TestAcceptBundleRefuses(t *testing.T) {
103 ctx := context.Background()
104 s := newSource(t, "sha1")
105 good := s.bundle(t, "main")
106 corrupt := func() string {
107 data, err := os.ReadFile(good)
108 if err != nil {
109 t.Fatal(err)
110 }
111 // Past the header, in the pack: flip one byte of the last object.
112 data[len(data)-30] ^= 0xff
113 path := filepath.Join(t.TempDir(), "corrupt.bundle")
114 if err := os.WriteFile(path, data, 0o644); err != nil {
115 t.Fatal(err)
116 }
117 return path
118 }
119 cases := []struct {
120 name, bundle, tip, says string
121 }{
122 {"a wrong tip", good, s.first, "the manifest declares"},
123 {"prerequisites", s.bundle(t, "main~1..main"), s.tip, "prerequisite"},
124 {"an extra ref", s.bundle(t, "main", "side"), s.tip, "2 refs"},
125 {"another ref than main", s.bundle(t, "side"), s.side, "refs/heads/side"},
126 {"a corrupt pack", corrupt(), s.tip, "git fetch"},
127 {"a malformed commit", malformedBundle(t), "", "fsck"},
128 {"no bundle", filepath.Join(t.TempDir(), "missing.bundle"), s.tip, "no such file"},
129 }
130 for _, c := range cases {
131 t.Run(c.name, func(t *testing.T) {
132 tip := c.tip
133 if tip == "" {
134 tip = bundleTip(t, c.bundle)
135 }
136 repo := filepath.Join(t.TempDir(), "app.git")
137 err := export.AcceptBundle(ctx, c.bundle, repo, tip)
138 if err == nil || !strings.Contains(err.Error(), c.says) {
139 t.Fatalf("AcceptBundle gave %v, want an error saying %q", err, c.says)
140 }
141 if c.name != "no bundle" {
142 wantCode(t, err, export.CodeBadBundle)
143 }
144 if _, err := os.Lstat(repo); !os.IsNotExist(err) {
145 t.Errorf("the refusal left %s behind", repo)
146 }
147 })
148 }
149 t.Run("a repository that exists", func(t *testing.T) {
150 repo := t.TempDir()
151 if err := export.AcceptBundle(ctx, good, repo, s.tip); err == nil {
152 t.Fatal("AcceptBundle fetched into a directory that existed")
153 }
154 if _, err := os.Lstat(repo); err != nil {
155 t.Errorf("the refusal removed the directory that was there: %v", err)
156 }
157 })
158}
159
160// bundleTip is the one ref a bundle's header names.
161func bundleTip(t *testing.T, path string) string {
162 t.Helper()
163 return strings.Fields(git(t, "", nil, "bundle", "list-heads", path))[0]
164}
165
166// malformedBundle is a bundle of a main whose tip is a commit fsck
167// refuses: its author has no email and no date.
168func malformedBundle(t *testing.T) string {
169 t.Helper()
170 repo := filepath.Join(t.TempDir(), "bad.git")
171 git(t, "", nil, "init", "--quiet", "--bare", "--initial-branch=main", repo)
172 tree := git(t, repo, nil, "mktree")
173 bad := git(t, repo, []byte("tree "+tree+"\nauthor nobody\ncommitter nobody\n\nmalformed\n"),
174 "hash-object", "-t", "commit", "--literally", "-w", "--stdin")
175 git(t, repo, nil, "update-ref", "refs/heads/main", bad)
176 path := filepath.Join(t.TempDir(), "bad.bundle")
177 git(t, repo, nil, "bundle", "create", "--quiet", path, "main")
178 return path
179}
180
181// unreachableBundle is a bundle of main whose pack also carries a commit
182// main does not reach, as a hostile publisher could write one.
183func unreachableBundle(t *testing.T, s source) string {
184 t.Helper()
185 objects := git(t, s.repo, nil, "rev-list", "--objects", "main", "side")
186 pack := packObjects(t, s.repo, objects)
187 header := "# v2 git bundle\n" + s.tip + " refs/heads/main\n\n"
188 path := filepath.Join(t.TempDir(), "unreachable.bundle")
189 if err := os.WriteFile(path, append([]byte(header), pack...), 0o644); err != nil {
190 t.Fatal(err)
191 }
192 return path
193}
194
195func packObjects(t *testing.T, repo, objects string) []byte {
196 t.Helper()
197 cmd := exec.Command("git", "-C", repo, "pack-objects", "--stdout", "--quiet")
198 cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null")
199 cmd.Stdin = strings.NewReader(objects + "\n")
200 out, err := cmd.Output()
201 if err != nil {
202 t.Fatal(err)
203 }
204 return out
205}
206
207// Reachable admits only commits main's tip reaches. A bundle can carry
208// other objects; they are in the repository and never reachable.
209func TestReachable(t *testing.T) {
210 ctx := context.Background()
211 s := newSource(t, "sha1")
212 repo := filepath.Join(t.TempDir(), "app.git")
213 if err := export.AcceptBundle(ctx, unreachableBundle(t, s), repo, s.tip); err != nil {
214 t.Fatal(err)
215 }
216 if got := git(t, repo, nil, "cat-file", "-t", s.side); got != "commit" {
217 t.Fatalf("the unreachable commit is not in the repository: %q", got)
218 }
219 tree := git(t, repo, nil, "rev-parse", s.tip+"^{tree}")
220 for _, c := range []struct {
221 name, commit string
222 want bool
223 }{
224 {"the tip", s.tip, true},
225 {"its parent", s.first, true},
226 {"a commit in the bundle that main does not reach", s.side, false},
227 {"a tree", tree, false},
228 {"an object the repository does not hold", strings.Repeat("1", 40), false},
229 {"an abbreviated ID", s.tip[:12], false},
230 {"a revision expression", s.tip + "~1", false},
231 {"an option", "--all", false},
232 } {
233 t.Run(c.name, func(t *testing.T) {
234 got, err := export.Reachable(ctx, repo, s.tip, c.commit)
235 if err != nil || got != c.want {
236 t.Errorf("Reachable gave %v, %v; want %v", got, err, c.want)
237 }
238 })
239 }
240 if _, err := export.Reachable(ctx, repo, "main", s.tip); err == nil {
241 t.Error("Reachable took a tip that is no object ID")
242 }
243}
244
245// A carried repository is fetched and checked as a bundle is, and holds
246// main alone.
247func TestCarryRepo(t *testing.T) {
248 ctx := context.Background()
249 s := newSource(t, "sha1")
250 held := filepath.Join(t.TempDir(), "held.git")
251 if err := export.AcceptBundle(ctx, s.bundle(t, "main"), held, s.tip); err != nil {
252 t.Fatal(err)
253 }
254 carried := filepath.Join(t.TempDir(), "carried.git")
255 if err := export.CarryRepo(ctx, held, carried, s.tip); err != nil {
256 t.Fatal(err)
257 }
258 if refs := git(t, carried, nil, "for-each-ref", "--format=%(refname) %(objectname)"); refs != "refs/heads/main "+s.tip {
259 t.Errorf("the carried repository holds %q", refs)
260 }
261 wrong := filepath.Join(t.TempDir(), "wrong.git")
262 if err := export.CarryRepo(ctx, held, wrong, s.first); err == nil {
263 t.Error("CarryRepo carried a repository at another tip")
264 }
265 if _, err := os.Lstat(wrong); !os.IsNotExist(err) {
266 t.Error("the refusal left a repository behind")
267 }
268}