@@ -13,42 +13,52 @@ import (
13
13
. "gopkg.in/check.v1"
14
14
)
15
15
16
- var dirFixtures = [... ]struct {
16
+ var dirFixturesInit = [... ]struct {
17
17
name string
18
18
tgz string
19
+ head string
19
20
}{
20
21
{
21
22
name : "binrels" ,
22
23
tgz : "storage/seekable/internal/gitdir/fixtures/alcortesm-binary-relations.tgz" ,
24
+ head : "c44b5176e99085c8fe36fa27b045590a7b9d34c9" ,
23
25
},
24
26
}
25
27
28
+ type dirFixture struct {
29
+ path string
30
+ head core.Hash
31
+ }
32
+
26
33
type SuiteRepository struct {
27
- repos map [string ]* Repository
28
- dirFixturePaths map [string ]string
34
+ repos map [string ]* Repository
35
+ dirFixtures map [string ]dirFixture
29
36
}
30
37
31
38
var _ = Suite (& SuiteRepository {})
32
39
33
40
func (s * SuiteRepository ) SetUpSuite (c * C ) {
34
41
s .repos = unpackFixtures (c , tagFixtures , treeWalkerFixtures )
35
42
36
- s .dirFixturePaths = make (map [string ]string , len (dirFixtures ))
37
- for _ , fix := range dirFixtures {
43
+ s .dirFixtures = make (map [string ]dirFixture , len (dirFixturesInit ))
44
+ for _ , fix := range dirFixturesInit {
38
45
com := Commentf ("fixture name = %s\n " , fix .name )
39
46
40
47
path , err := tgz .Extract (fix .tgz )
41
48
c .Assert (err , IsNil , com )
42
49
43
- s .dirFixturePaths [fix .name ] = path
50
+ s .dirFixtures [fix .name ] = dirFixture {
51
+ path : path ,
52
+ head : core .NewHash (fix .head ),
53
+ }
44
54
}
45
55
}
46
56
47
57
func (s * SuiteRepository ) TearDownSuite (c * C ) {
48
- for name , path := range s .dirFixturePaths {
49
- err := os .RemoveAll (path )
58
+ for name , fix := range s .dirFixtures {
59
+ err := os .RemoveAll (fix . path )
50
60
c .Assert (err , IsNil , Commentf ("cannot delete tmp dir for fixture %s: %s\n " ,
51
- name , path ))
61
+ name , fix . path ))
52
62
}
53
63
}
54
64
@@ -66,9 +76,9 @@ func (s *SuiteRepository) TestNewRepositoryWithAuth(c *C) {
66
76
}
67
77
68
78
func (s * SuiteRepository ) TestNewRepositoryFromFS (c * C ) {
69
- for name , path := range s .dirFixturePaths {
79
+ for name , fix := range s .dirFixtures {
70
80
fs := fs .NewOS ()
71
- gitPath := fs .Join (path , ".git/" )
81
+ gitPath := fs .Join (fix . path , ".git/" )
72
82
com := Commentf ("dir fixture %q → %q\n " , name , gitPath )
73
83
repo , err := NewRepositoryFromFS (fs , gitPath )
74
84
c .Assert (err , IsNil , com )
@@ -205,3 +215,53 @@ func (s *SuiteRepository) TestCommitIterClosePanic(c *C) {
205
215
c .Assert (err , IsNil )
206
216
commits .Close ()
207
217
}
218
+
219
+ func (s * SuiteRepository ) TestHeadFromFs (c * C ) {
220
+ for name , fix := range s .dirFixtures {
221
+ fs := fs .NewOS ()
222
+ gitPath := fs .Join (fix .path , ".git/" )
223
+ com := Commentf ("dir fixture %q → %q\n " , name , gitPath )
224
+ repo , err := NewRepositoryFromFS (fs , gitPath )
225
+ c .Assert (err , IsNil , com )
226
+
227
+ head , err := repo .Head ("" )
228
+ c .Assert (err , IsNil )
229
+
230
+ c .Assert (head , Equals , fix .head )
231
+ }
232
+ }
233
+
234
+ func (s * SuiteRepository ) TestHeadFromRemote (c * C ) {
235
+ r , err := NewRepository (RepositoryFixture , nil )
236
+ c .Assert (err , IsNil )
237
+
238
+ upSrv := & MockGitUploadPackService {}
239
+ r .Remotes [DefaultRemoteName ].upSrv = upSrv
240
+ err = r .Remotes [DefaultRemoteName ].Connect ()
241
+ c .Assert (err , IsNil )
242
+
243
+ info , err := upSrv .Info ()
244
+ c .Assert (err , IsNil )
245
+ expected := info .Head
246
+
247
+ obtained , err := r .Head (DefaultRemoteName )
248
+ c .Assert (err , IsNil )
249
+
250
+ c .Assert (obtained , Equals , expected )
251
+ }
252
+
253
+ func (s * SuiteRepository ) TestHeadErrors (c * C ) {
254
+ r , err := NewRepository (RepositoryFixture , nil )
255
+ c .Assert (err , IsNil )
256
+
257
+ upSrv := & MockGitUploadPackService {}
258
+ r .Remotes [DefaultRemoteName ].upSrv = upSrv
259
+
260
+ remote := "not found"
261
+ _ , err = r .Head (remote )
262
+ c .Assert (err , ErrorMatches , fmt .Sprintf ("unable to find remote %q" , remote ))
263
+
264
+ remote = ""
265
+ _ , err = r .Head (remote )
266
+ c .Assert (err , ErrorMatches , "cannot retrieve local head: no local data found" )
267
+ }
0 commit comments