@@ -13,42 +13,52 @@ import (
1313 . "gopkg.in/check.v1"
1414)
1515
16- var dirFixtures = [... ]struct {
16+ var dirFixturesInit = [... ]struct {
1717 name string
1818 tgz string
19+ head string
1920}{
2021 {
2122 name : "binrels" ,
2223 tgz : "storage/seekable/internal/gitdir/fixtures/alcortesm-binary-relations.tgz" ,
24+ head : "c44b5176e99085c8fe36fa27b045590a7b9d34c9" ,
2325 },
2426}
2527
28+ type dirFixture struct {
29+ path string
30+ head core.Hash
31+ }
32+
2633type SuiteRepository struct {
27- repos map [string ]* Repository
28- dirFixturePaths map [string ]string
34+ repos map [string ]* Repository
35+ dirFixtures map [string ]dirFixture
2936}
3037
3138var _ = Suite (& SuiteRepository {})
3239
3340func (s * SuiteRepository ) SetUpSuite (c * C ) {
3441 s .repos = unpackFixtures (c , tagFixtures , treeWalkerFixtures )
3542
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 {
3845 com := Commentf ("fixture name = %s\n " , fix .name )
3946
4047 path , err := tgz .Extract (fix .tgz )
4148 c .Assert (err , IsNil , com )
4249
43- s .dirFixturePaths [fix .name ] = path
50+ s .dirFixtures [fix .name ] = dirFixture {
51+ path : path ,
52+ head : core .NewHash (fix .head ),
53+ }
4454 }
4555}
4656
4757func (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 )
5060 c .Assert (err , IsNil , Commentf ("cannot delete tmp dir for fixture %s: %s\n " ,
51- name , path ))
61+ name , fix . path ))
5262 }
5363}
5464
@@ -66,9 +76,9 @@ func (s *SuiteRepository) TestNewRepositoryWithAuth(c *C) {
6676}
6777
6878func (s * SuiteRepository ) TestNewRepositoryFromFS (c * C ) {
69- for name , path := range s .dirFixturePaths {
79+ for name , fix := range s .dirFixtures {
7080 fs := fs .NewOS ()
71- gitPath := fs .Join (path , ".git/" )
81+ gitPath := fs .Join (fix . path , ".git/" )
7282 com := Commentf ("dir fixture %q → %q\n " , name , gitPath )
7383 repo , err := NewRepositoryFromFS (fs , gitPath )
7484 c .Assert (err , IsNil , com )
@@ -205,3 +215,53 @@ func (s *SuiteRepository) TestCommitIterClosePanic(c *C) {
205215 c .Assert (err , IsNil )
206216 commits .Close ()
207217}
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