-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_test.go
163 lines (142 loc) · 4.09 KB
/
repo_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"io"
"strings"
"testing"
errors "github.com/cakemanny/git-remote-drive/errors"
store "github.com/cakemanny/git-remote-drive/store"
)
// mapStore is rooted at .git
type mapStore struct {
contents map[string]string
listings map[string][]store.File
}
var fakeStore store.SimpleFileStore = mapStore{
contents: map[string]string{
"refs/heads/master": "c5d2d737af4b6203aa37ca2ca13476624d11f4ee\n",
},
listings: map[string][]store.File{
"": []store.File{{true, "objects"}, {true, "refs"}},
"refs": []store.File{{true, "heads"}},
"refs/heads": []store.File{{false, "master"}},
"objects": []store.File{{true, "c5"}},
"objects/c5": []store.File{
{false, "d2d737af4b6203aa37ca2ca13476624d11f4ee"},
},
},
}
var basedStore store.SimpleFileStore = mapStore{
contents: map[string]string{
".git/refs/heads/master": "c5d2d737af4b6203aa37ca2ca13476624d11f4ee\n",
},
listings: map[string][]store.File{
"": []store.File{{true, ".git"}},
".git": []store.File{{true, "objects"}, {true, "refs"}},
".git/refs": []store.File{{true, "heads"}},
".git/refs/heads": []store.File{{false, "master"}},
".git/objects": []store.File{{true, "c5"}},
".git/objects/c5": []store.File{
{false, "d2d737af4b6203aa37ca2ca13476624d11f4ee"},
},
},
}
func (s mapStore) Read(name string, contents io.Writer) error {
m := s.contents
v, ok := m[name]
if !ok {
return errors.ErrNotFound{name}
}
contents.Write([]byte(v))
return nil
}
func (s mapStore) List(path string) ([]store.File, error) {
m := s.listings
v, ok := m[path]
if !ok {
return nil, errors.ErrNotFound{path}
}
return v, nil
}
func (mapStore) Create(path string, contents io.Reader) error {
return errors.NotImplemented()
}
func (mapStore) Update(path string, contents io.Reader) error {
return errors.NotImplemented()
}
func (mapStore) Delete(path string) error {
return errors.NotImplemented()
}
func (s mapStore) TestPath(path string) (bool, error) {
m := s.listings
_, ok := m[path]
return ok, nil
}
func TestReadRef(t *testing.T) {
stores := []struct {
storeName string
base string
store store.SimpleFileStore
}{
{"fakeStore", "", fakeStore},
{"basedStore", ".git", basedStore},
}
for _, s := range stores {
m := storeManager{s.base, s.store}
sha1, err := m.ReadRef("refs/heads/master")
if err != nil {
t.Errorf("%s: ReadRef: unexpected error: %v", s.storeName, err)
}
expected := "c5d2d737af4b6203aa37ca2ca13476624d11f4ee"
if sha1 != expected {
t.Error(s.storeName+":", "expected:", expected, "actual:", sha1)
}
}
}
func TestReadCommit(t *testing.T) {
rdr := strings.NewReader(
"tree 07b2986536979a8e6b6028c6a670012b4b4ac262\n" +
"parent 7879dfcfd2db5c052284d7077441e9500672a702\n" +
"parent 35a3be730435891d106bd4a7eefba3183ab14d54\n" +
"author cakemanny <goldingd89@gmail.com> 1524238149 +0100\n" +
"committer cakemanny <goldingd89@gmail.com> 1524238149 +0100\n" +
"\n" +
"Merge branch 'branch1'\n",
)
expected := Commit{
Tree: "07b2986536979a8e6b6028c6a670012b4b4ac262",
Parents: []string{
"7879dfcfd2db5c052284d7077441e9500672a702",
"35a3be730435891d106bd4a7eefba3183ab14d54",
},
}
actual, err := ReadCommit(rdr)
if err != nil {
t.Error("unexpected error:", err)
}
// can't directly compare
if expected.Tree != actual.Tree ||
len(expected.Parents) != len(actual.Parents) ||
expected.Parents[0] != actual.Parents[0] ||
expected.Parents[1] != actual.Parents[1] {
//
t.Error("expected:", expected, "actual:", actual)
}
}
func TestReadTree(t *testing.T) {
rdr := strings.NewReader(
"100644 blob 7311bd3ca3f61d3731a390d88422977b8d23a016\tREADME\n" +
"040000 tree 562d81834eec9f1701b7ee35ea50767edb2c4e8a\tsomedir\n",
)
expected := Tree{
{Type: BLOB, Ref: "7311bd3ca3f61d3731a390d88422977b8d23a016"},
{Type: TREE, Ref: "562d81834eec9f1701b7ee35ea50767edb2c4e8a"},
}
actual, err := ReadTree(rdr)
if err != nil {
t.Error("unexpected error:", err)
}
if len(actual) != 2 || expected[0] != actual[0] || expected[1] != actual[1] {
t.Error("expected:", expected, "actual:", actual)
}
}
//