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.
|
54bbbe45
Keenan Nemetz
13d ago
|
1 | package server_test |
| 2 | ||
| 3 | // A file tracked by Git LFS is a pointer in the tree. The code browser has to | |
| 4 | // resolve it, or an <img> in a README and the preview page both show a 130-byte | |
| 5 | // stub where a texture belongs. | |
| 6 | ||
| 7 | import ( | |
| 8 | "fmt" | |
| 9 | "io" | |
| 10 | "net/http" | |
| 11 | "strings" | |
| 12 | "testing" | |
| 13 | ) | |
| 14 | ||
| 15 | func lfsPointer(oid string, size int) string { | |
| 16 | return fmt.Sprintf("version https://git-lfs.github.com/spec/v1\noid sha256:%s\nsize %d\n", oid, size) | |
| 17 | } | |
| 18 | ||
| 19 | // lfsWebRepo stores data as an LFS object in the fixture repository, commits a | |
| 20 | // pointer at path, and pushes. The store is what makes the push pass the hook's | |
| 21 | // "pointer with no object" rule. | |
| 22 | func lfsWebRepo(t *testing.T, f *fixture, path string, data []byte) (oid string) { | |
| 23 | t.Helper() | |
| 24 | oid, err := f.srv.LFS.PutBytes(f.repo, data) | |
| 25 | if err != nil { | |
| 26 | t.Fatal(err) | |
| 27 | } | |
| 28 | work := workRepo(t, map[string]string{ | |
| 29 | ".gitattributes": "*.png filter=lfs diff=lfs merge=lfs -text\n*.unity filter=lfs diff=lfs merge=lfs -text\n", | |
| 30 | path: lfsPointer(oid, len(data)), | |
| 31 | "readme.md": "x\n", | |
| 32 | }) | |
| 33 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { | |
| 34 | t.Fatalf("push: %s", errs) | |
| 35 | } | |
| 36 | return oid | |
| 37 | } | |
| 38 | ||
| 39 | func TestRawServesAnLFSObjectInsteadOfItsPointer(t *testing.T) { | |
| 40 | t.Parallel() | |
| 41 | f := newFixture(t) | |
| 42 | f.publish(f.repo) | |
| 43 | asset := []byte(png) | |
| 44 | oid := lfsWebRepo(t, f, "art/hero.png", asset) | |
| 45 | ||
| 46 | url := f.http.URL + "/" + f.repo + "/raw/main/art/hero.png" | |
| 47 | resp, body := getWithHeaders(t, url) | |
| 48 | if resp.StatusCode != http.StatusOK { | |
| 49 | t.Fatalf("raw = %d: %s", resp.StatusCode, body) | |
| 50 | } | |
| 51 | if got := resp.Header.Get("Content-Type"); got != "image/png" { | |
| 52 | t.Fatalf("Content-Type = %q, want image/png", got) | |
| 53 | } | |
| 54 | if body != string(asset) { | |
| 55 | t.Fatalf("raw body = %q, want the stored object", body) | |
| 56 | } | |
| 57 | if got := resp.Header.Get("ETag"); got != `"`+oid+`"` { | |
| 58 | t.Fatalf("ETag = %q, want the object id", got) | |
| 59 | } | |
| 60 | ||
| 61 | // A byte range applies to the object, not to the pointer that names it. | |
| 62 | req, err := http.NewRequest(http.MethodGet, url, nil) | |
| 63 | if err != nil { | |
| 64 | t.Fatal(err) | |
| 65 | } | |
| 66 | req.Header.Set("Range", "bytes=1-4") | |
| 67 | resp, err = http.DefaultClient.Do(req) | |
| 68 | if err != nil { | |
| 69 | t.Fatal(err) | |
| 70 | } | |
| 71 | defer resp.Body.Close() | |
| 72 | part, _ := io.ReadAll(resp.Body) | |
| 73 | if resp.StatusCode != http.StatusPartialContent || string(part) != string(asset[1:5]) { | |
| 74 | t.Fatalf("range = %d %q, want 206 %q", resp.StatusCode, part, asset[1:5]) | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | func TestBlobPreviewsAnLFSObject(t *testing.T) { | |
| 79 | t.Parallel() | |
| 80 | f := newFixture(t) | |
| 81 | f.publish(f.repo) | |
| 82 | lfsWebRepo(t, f, "art/hero.png", []byte(png)) | |
| 83 | ||
| 84 | code, page := f.get("/" + f.repo + "/blob/main/art/hero.png") | |
| 85 | if code != http.StatusOK { | |
| 86 | t.Fatalf("blob = %d", code) | |
| 87 | } | |
| 88 | if !strings.Contains(page, `class="media-preview"`) || | |
| 89 | !strings.Contains(page, `src="/demo/raw/main/art/hero.png"`) { | |
| 90 | t.Fatalf("blob page does not preview the stored image:\n%s", page) | |
| 91 | } | |
| 92 | if strings.Contains(page, "git-lfs.github.com") { | |
| 93 | t.Fatalf("blob page still shows the pointer:\n%s", page) | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | // A Unity scene or a Godot resource may be textual and still tracked in LFS. | |
| 98 | // The page should read the object, not the pointer. | |
| 99 | func TestBlobShowsTheSourceOfATextLFSObject(t *testing.T) { | |
| 100 | t.Parallel() | |
| 101 | f := newFixture(t) | |
| 102 | f.publish(f.repo) | |
| 103 | scene := []byte("%YAML 1.1\n--- !u!1 &1\nGameObject:\n m_Name: Hero\n") | |
| 104 | lfsWebRepo(t, f, "Assets/Scenes/Hero.unity", scene) | |
| 105 | ||
| 106 | code, page := f.get("/" + f.repo + "/blob/main/Assets/Scenes/Hero.unity") | |
| 107 | if code != http.StatusOK { | |
| 108 | t.Fatalf("blob = %d", code) | |
| 109 | } | |
| 110 | if !strings.Contains(page, "m_Name: Hero") { | |
| 111 | t.Fatalf("blob page does not show the object's source:\n%s", page) | |
| 112 | } | |
| 113 | if strings.Contains(page, "git-lfs.github.com") { | |
| 114 | t.Fatalf("blob page still shows the pointer:\n%s", page) | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | func TestAMissingLFSObjectSaysSo(t *testing.T) { | |
| 119 | t.Parallel() | |
| 120 | f := newFixture(t) | |
| 121 | f.publish(f.repo) | |
| 122 | oid := lfsWebRepo(t, f, "art/hero.png", []byte(png)) | |
| 123 | ||
| 124 | // Remove this repository's view of the object, as a store that lost bytes | |
| 125 | // would look to a reader. The pool still holds them for another repository. | |
| 126 | if err := f.srv.LFS.Unlink(f.repo, oid); err != nil { | |
| 127 | t.Fatal(err) | |
| 128 | } | |
| 129 | ||
| 130 | if code, body := f.get("/" + f.repo + "/raw/main/art/hero.png"); code != http.StatusNotFound || | |
| 131 | !strings.Contains(body, "LFS object") { | |
| 132 | t.Fatalf("raw with a missing object = %d %q", code, body) | |
| 133 | } | |
| 134 | code, page := f.get("/" + f.repo + "/blob/main/art/hero.png") | |
| 135 | if code != http.StatusOK { | |
| 136 | t.Fatalf("blob with a missing object = %d", code) | |
| 137 | } | |
| 138 | if !strings.Contains(page, "not\nstored on this instance") { | |
| 139 | t.Fatalf("blob page does not say the object is missing:\n%s", page) | |
| 140 | } | |
| 141 | } |