-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitfs_test.go
110 lines (86 loc) · 2.25 KB
/
gitfs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gitfs
import (
"github.com/libgit2/git2go"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"runtime"
"testing"
"time"
)
func checkFatal(t *testing.T, err error) {
if err == nil {
return
}
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatal("the runtime seems quite broken")
}
t.Fatalf("Fail at %v:%v; %v", file, line, err)
}
func createBareTestRepo(t *testing.T) *git.Repository {
// figure out where we can create the test repo
path, err := ioutil.TempDir("", "gitfs")
checkFatal(t, err)
repo, err := git.InitRepository(path, true)
checkFatal(t, err)
return repo
}
func seedRepo(t *testing.T, repo *git.Repository) (*git.Oid, *git.Oid) {
loc, err := time.LoadLocation("Europe/Berlin")
checkFatal(t, err)
sig := &git.Signature{
Name: "Rand Om Hacker",
Email: "[email protected]",
When: time.Date(2013, 03, 06, 14, 30, 0, 0, loc),
}
odb, err := repo.Odb()
checkFatal(t, err)
blobID, err := odb.Write([]byte("foo\n"), git.ObjectBlob)
checkFatal(t, err)
idx, err := git.NewIndex()
checkFatal(t, err)
entry := git.IndexEntry{
Path: "README",
Id: blobID,
Mode: git.FilemodeBlob,
}
err = idx.Add(&entry)
checkFatal(t, err)
treeId, err := idx.WriteTreeTo(repo)
checkFatal(t, err)
message := "This is a commit\n"
tree, err := repo.LookupTree(treeId)
checkFatal(t, err)
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)
checkFatal(t, err)
return commitId, treeId
}
func TestGetFile(t *testing.T) {
repo := createBareTestRepo(t)
defer os.RemoveAll(repo.Path())
//_commitID, _treeID := seedRepo(t, repo)
_, treeID := seedRepo(t, repo)
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
fs := http.FileServer(NewFromTree(tree))
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://localhost/README", nil)
checkFatal(t, err)
fs.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("Request did not succeed %v\n", w.Code)
}
if w.Body.String() != "foo\n" {
t.Fatal("bad content served")
}
w = httptest.NewRecorder()
req, err = http.NewRequest("GET", "http://localhost/NOTHERE", nil)
checkFatal(t, err)
fs.ServeHTTP(w, req)
if w.Code != 404 {
t.Fatalf("Should not have found that file %v", w.Code)
}
}