Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Code

527 commit(s) on main

Put job execution behind an interface Keenan Nemetz · 2h ago de3eec16
history blame raw 3.9 KiB
1package git
2
3// A release tarball of a game project must contain the assets, not the LFS
4// pointers that stand in for them. These tests drive the tar rewrite directly,
5// with a resolver that returns bytes from a map, so the archive path is
6// exercised without a server or a store.
7
8import (
9 "archive/tar"
10 "bytes"
11 "compress/gzip"
12 "crypto/sha256"
13 "encoding/hex"
14 "errors"
15 "io"
16 "strings"
17 "testing"
18
19 "rodent.software/vole/core/lfs"
20)
21
22func pointerFor(t *testing.T, data []byte) string {
23 t.Helper()
24 sum := sha256.Sum256(data)
25 return string(lfs.Pointer{OID: hex.EncodeToString(sum[:]), Size: int64(len(data))}.Encode())
26}
27
28func readArchive(t *testing.T, body []byte, gzipped bool) map[string][]byte {
29 t.Helper()
30 var r io.Reader = bytes.NewReader(body)
31 if gzipped {
32 zr, err := gzip.NewReader(r)
33 if err != nil {
34 t.Fatalf("gzip: %v", err)
35 }
36 defer zr.Close()
37 r = zr
38 }
39 out := map[string][]byte{}
40 tr := tar.NewReader(r)
41 for {
42 hdr, err := tr.Next()
43 if errors.Is(err, io.EOF) {
44 break
45 }
46 if err != nil {
47 t.Fatalf("tar: %v", err)
48 }
49 if hdr.Typeflag != tar.TypeReg {
50 continue
51 }
52 data, err := io.ReadAll(tr)
53 if err != nil {
54 t.Fatalf("tar body %s: %v", hdr.Name, err)
55 }
56 out[hdr.Name] = data
57 }
58 return out
59}
60
61func archiveRepo(t *testing.T, files map[string][]byte) *Repo {
62 t.Helper()
63 return blobRepo(t, files)
64}
65
66func TestArchiveResolvingLFSReplacesPointers(t *testing.T) {
67 asset := []byte("real texture bytes\x00\x01\x02")
68 pointer := pointerFor(t, asset)
69 files := map[string][]byte{
70 ".gitattributes": []byte("*.bin filter=lfs diff=lfs merge=lfs -text\n"),
71 "art/hero.bin": []byte(pointer),
72 "readme.md": []byte("hello\n"),
73 "empty.txt": {},
74 // Larger than a pointer can be, and beginning with the same first line:
75 // this is a real file that must pass through untouched.
76 "big.bin": []byte(pointer + strings.Repeat("padding", 512)),
77 }
78 g := archiveRepo(t, files)
79 resolve := func(oid string, size int64) (io.ReadCloser, error) {
80 if size != int64(len(asset)) {
81 t.Fatalf("resolver asked for %d bytes, pointer says %d", len(asset), size)
82 }
83 return io.NopCloser(bytes.NewReader(asset)), nil
84 }
85
86 for _, tc := range []struct {
87 format string
88 gzipped bool
89 }{
90 {"tar", false},
91 {"tar.gz", true},
92 } {
93 var buf bytes.Buffer
94 if err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", tc.format, "proj-v1/", resolve); err != nil {
95 t.Fatalf("%s: %v", tc.format, err)
96 }
97 got := readArchive(t, buf.Bytes(), tc.gzipped)
98 if string(got["proj-v1/art/hero.bin"]) != string(asset) {
99 t.Fatalf("%s hero.bin = %q, want the stored asset", tc.format, got["proj-v1/art/hero.bin"])
100 }
101 if string(got["proj-v1/readme.md"]) != "hello\n" {
102 t.Fatalf("%s readme = %q", tc.format, got["proj-v1/readme.md"])
103 }
104 if string(got["proj-v1/big.bin"]) != string(files["big.bin"]) {
105 t.Fatalf("%s replaced a large file that only looks like a pointer", tc.format)
106 }
107 if !bytes.Equal(got["proj-v1/empty.txt"], nil) {
108 t.Fatalf("%s empty file gained content", tc.format)
109 }
110 }
111}
112
113func TestArchiveResolvingLFSWithoutAResolverKeepsPointers(t *testing.T) {
114 asset := []byte("asset")
115 pointer := pointerFor(t, asset)
116 g := archiveRepo(t, map[string][]byte{"art/hero.bin": []byte(pointer)})
117 var buf bytes.Buffer
118 if err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", "tar", "", nil); err != nil {
119 t.Fatal(err)
120 }
121 if got := readArchive(t, buf.Bytes(), false)["art/hero.bin"]; string(got) != pointer {
122 t.Fatalf("nil resolver changed the pointer to %q", got)
123 }
124}
125
126func TestArchiveResolvingLFSFailsWhenTheObjectIsMissing(t *testing.T) {
127 pointer := pointerFor(t, []byte("gone"))
128 g := archiveRepo(t, map[string][]byte{"art/hero.bin": []byte(pointer)})
129 resolve := func(string, int64) (io.ReadCloser, error) {
130 return nil, lfs.ErrNotFound
131 }
132 var buf bytes.Buffer
133 err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", "tar", "", resolve)
134 if !errors.Is(err, lfs.ErrNotFound) {
135 t.Fatalf("archive error = %v, want the missing object", err)
136 }
137}
138