gunk-dev/the-valley

bundle_test.go

source19d534c7c066main's published tip history
9.6 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// A bundle and a repository named relative to the working directory are
102// read and written there.
103func TestAcceptBundleTakesRelativePaths(t *testing.T) {
104 s := newSource(t, "sha1")
105 bundle := s.bundle(t, "main")
106 t.Chdir(filepath.Dir(bundle))
107 if err := export.AcceptBundle(context.Background(), filepath.Base(bundle), "app.git", s.tip); err != nil {
108 t.Fatal(err)
109 }
110 if _, err := os.Stat(filepath.Join(filepath.Dir(bundle), "app.git", "HEAD")); err != nil {
111 t.Error(err)
112 }
113}
114
115// What the acceptance procedure refuses, each leaving no repository.
116func TestAcceptBundleRefuses(t *testing.T) {
117 ctx := context.Background()
118 s := newSource(t, "sha1")
119 good := s.bundle(t, "main")
120 corrupt := func() string {
121 data, err := os.ReadFile(good)
122 if err != nil {
123 t.Fatal(err)
124 }
125 // Past the header, in the pack: flip one byte of the last object.
126 data[len(data)-30] ^= 0xff
127 path := filepath.Join(t.TempDir(), "corrupt.bundle")
128 if err := os.WriteFile(path, data, 0o644); err != nil {
129 t.Fatal(err)
130 }
131 return path
132 }
133 cases := []struct {
134 name, bundle, tip, says string
135 }{
136 {"a wrong tip", good, s.first, "the manifest declares"},
137 {"prerequisites", s.bundle(t, "main~1..main"), s.tip, "prerequisite"},
138 {"an extra ref", s.bundle(t, "main", "side"), s.tip, "2 refs"},
139 {"another ref than main", s.bundle(t, "side"), s.side, "refs/heads/side"},
140 {"a corrupt pack", corrupt(), s.tip, "git fetch"},
141 {"a malformed commit", malformedBundle(t), "", "fsck"},
142 {"no bundle", filepath.Join(t.TempDir(), "missing.bundle"), s.tip, "no such file"},
143 }
144 for _, c := range cases {
145 t.Run(c.name, func(t *testing.T) {
146 tip := c.tip
147 if tip == "" {
148 tip = bundleTip(t, c.bundle)
149 }
150 repo := filepath.Join(t.TempDir(), "app.git")
151 err := export.AcceptBundle(ctx, c.bundle, repo, tip)
152 if err == nil || !strings.Contains(err.Error(), c.says) {
153 t.Fatalf("AcceptBundle gave %v, want an error saying %q", err, c.says)
154 }
155 if c.name != "no bundle" {
156 wantCode(t, err, export.CodeBadBundle)
157 }
158 if _, err := os.Lstat(repo); !os.IsNotExist(err) {
159 t.Errorf("the refusal left %s behind", repo)
160 }
161 })
162 }
163 t.Run("a repository that exists", func(t *testing.T) {
164 repo := t.TempDir()
165 if err := export.AcceptBundle(ctx, good, repo, s.tip); err == nil {
166 t.Fatal("AcceptBundle fetched into a directory that existed")
167 }
168 if _, err := os.Lstat(repo); err != nil {
169 t.Errorf("the refusal removed the directory that was there: %v", err)
170 }
171 })
172}
173
174// bundleTip is the one ref a bundle's header names.
175func bundleTip(t *testing.T, path string) string {
176 t.Helper()
177 return strings.Fields(git(t, "", nil, "bundle", "list-heads", path))[0]
178}
179
180// malformedBundle is a bundle of a main whose tip is a commit fsck
181// refuses: its author has no email and no date.
182func malformedBundle(t *testing.T) string {
183 t.Helper()
184 repo := filepath.Join(t.TempDir(), "bad.git")
185 git(t, "", nil, "init", "--quiet", "--bare", "--initial-branch=main", repo)
186 tree := git(t, repo, nil, "mktree")
187 bad := git(t, repo, []byte("tree "+tree+"\nauthor nobody\ncommitter nobody\n\nmalformed\n"),
188 "hash-object", "-t", "commit", "--literally", "-w", "--stdin")
189 git(t, repo, nil, "update-ref", "refs/heads/main", bad)
190 path := filepath.Join(t.TempDir(), "bad.bundle")
191 git(t, repo, nil, "bundle", "create", "--quiet", path, "main")
192 return path
193}
194
195// unreachableBundle is a bundle of main whose pack also carries a commit
196// main does not reach, as a hostile publisher could write one.
197func unreachableBundle(t *testing.T, s source) string {
198 t.Helper()
199 objects := git(t, s.repo, nil, "rev-list", "--objects", "main", "side")
200 pack := packObjects(t, s.repo, objects)
201 header := "# v2 git bundle\n" + s.tip + " refs/heads/main\n\n"
202 path := filepath.Join(t.TempDir(), "unreachable.bundle")
203 if err := os.WriteFile(path, append([]byte(header), pack...), 0o644); err != nil {
204 t.Fatal(err)
205 }
206 return path
207}
208
209func packObjects(t *testing.T, repo, objects string) []byte {
210 t.Helper()
211 cmd := exec.Command("git", "-C", repo, "pack-objects", "--stdout", "--quiet")
212 cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null")
213 cmd.Stdin = strings.NewReader(objects + "\n")
214 out, err := cmd.Output()
215 if err != nil {
216 t.Fatal(err)
217 }
218 return out
219}
220
221// Reachable admits only commits main's tip reaches. A bundle can carry
222// other objects; they are in the repository and never reachable.
223func TestReachable(t *testing.T) {
224 ctx := context.Background()
225 s := newSource(t, "sha1")
226 repo := filepath.Join(t.TempDir(), "app.git")
227 if err := export.AcceptBundle(ctx, unreachableBundle(t, s), repo, s.tip); err != nil {
228 t.Fatal(err)
229 }
230 if got := git(t, repo, nil, "cat-file", "-t", s.side); got != "commit" {
231 t.Fatalf("the unreachable commit is not in the repository: %q", got)
232 }
233 tree := git(t, repo, nil, "rev-parse", s.tip+"^{tree}")
234 for _, c := range []struct {
235 name, commit string
236 want bool
237 }{
238 {"the tip", s.tip, true},
239 {"its parent", s.first, true},
240 {"a commit in the bundle that main does not reach", s.side, false},
241 {"a tree", tree, false},
242 {"an object the repository does not hold", strings.Repeat("1", 40), false},
243 {"an abbreviated ID", s.tip[:12], false},
244 {"a revision expression", s.tip + "~1", false},
245 {"an option", "--all", false},
246 } {
247 t.Run(c.name, func(t *testing.T) {
248 got, err := export.Reachable(ctx, repo, s.tip, c.commit)
249 if err != nil || got != c.want {
250 t.Errorf("Reachable gave %v, %v; want %v", got, err, c.want)
251 }
252 })
253 }
254 if _, err := export.Reachable(ctx, repo, "main", s.tip); err == nil {
255 t.Error("Reachable took a tip that is no object ID")
256 }
257}
258
259// A carried repository is fetched and checked as a bundle is, and holds
260// main alone.
261func TestCarryRepo(t *testing.T) {
262 ctx := context.Background()
263 s := newSource(t, "sha1")
264 held := filepath.Join(t.TempDir(), "held.git")
265 if err := export.AcceptBundle(ctx, s.bundle(t, "main"), held, s.tip); err != nil {
266 t.Fatal(err)
267 }
268 carried := filepath.Join(t.TempDir(), "carried.git")
269 if err := export.CarryRepo(ctx, held, carried, s.tip); err != nil {
270 t.Fatal(err)
271 }
272 if refs := git(t, carried, nil, "for-each-ref", "--format=%(refname) %(objectname)"); refs != "refs/heads/main "+s.tip {
273 t.Errorf("the carried repository holds %q", refs)
274 }
275 wrong := filepath.Join(t.TempDir(), "wrong.git")
276 if err := export.CarryRepo(ctx, held, wrong, s.first); err == nil {
277 t.Error("CarryRepo carried a repository at another tip")
278 }
279 if _, err := os.Lstat(wrong); !os.IsNotExist(err) {
280 t.Error("the refusal left a repository behind")
281 }
282}