Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit ebc25df

Browse files
committed
test: add more PackfileWriter tests
1 parent f2e4b4d commit ebc25df

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

storage/filesystem/internal/dotgit/writers_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
. "gopkg.in/check.v1"
1414
"gopkg.in/src-d/go-billy.v3/osfs"
15+
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
1516
)
1617

1718
func (s *SuiteDotGit) TestNewObjectPack(c *C) {
@@ -35,13 +36,30 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) {
3536

3637
c.Assert(w.Close(), IsNil)
3738

38-
stat, err := fs.Stat(fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash))
39+
pfPath := fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash)
40+
idxPath := fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash)
41+
42+
stat, err := fs.Stat(pfPath)
3943
c.Assert(err, IsNil)
4044
c.Assert(stat.Size(), Equals, int64(84794))
4145

42-
stat, err = fs.Stat(fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash))
46+
stat, err = fs.Stat(idxPath)
4347
c.Assert(err, IsNil)
4448
c.Assert(stat.Size(), Equals, int64(1940))
49+
50+
pf, err := fs.Open(pfPath)
51+
c.Assert(err, IsNil)
52+
pfs := packfile.NewScanner(pf)
53+
_, objects, err := pfs.Header()
54+
c.Assert(err, IsNil)
55+
for i := uint32(0); i < objects; i++ {
56+
_, err := pfs.NextObjectHeader()
57+
if err != nil {
58+
c.Assert(err, IsNil)
59+
break
60+
}
61+
}
62+
c.Assert(pfs.Close(), IsNil)
4563
}
4664

4765
func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) {
@@ -63,6 +81,12 @@ func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) {
6381
info, err := fs.ReadDir("objects/pack")
6482
c.Assert(err, IsNil)
6583
c.Assert(info, HasLen, 0)
84+
85+
// check clean up of temporary files
86+
info, err = fs.ReadDir("")
87+
for _, fi := range info {
88+
c.Assert(fi.IsDir(), Equals, true)
89+
}
6690
}
6791

6892
func (s *SuiteDotGit) TestSyncedReader(c *C) {

storage/filesystem/storage_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (s *StorageSuite) SetUpTest(c *C) {
2626
c.Assert(err, IsNil)
2727

2828
s.BaseStorageSuite = test.NewBaseStorageSuite(storage)
29+
s.BaseStorageSuite.SetUpTest(c)
2930
}
3031

3132
func (s *StorageSuite) TestFilesystem(c *C) {

storage/memory/storage_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ var _ = Suite(&StorageSuite{})
1717

1818
func (s *StorageSuite) SetUpTest(c *C) {
1919
s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage())
20+
s.BaseStorageSuite.SetUpTest(c)
2021
}

storage/test/storage_suite.go

+36
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1414
"gopkg.in/src-d/go-git.v4/storage"
1515

16+
"github.com/src-d/go-git-fixtures"
1617
. "gopkg.in/check.v1"
1718
)
1819

@@ -64,6 +65,14 @@ func NewBaseStorageSuite(s Storer) BaseStorageSuite {
6465
}}
6566
}
6667

68+
func (s *BaseStorageSuite) SetUpTest(c *C) {
69+
c.Assert(fixtures.Init(), IsNil)
70+
}
71+
72+
func (s *BaseStorageSuite) TearDownTest(c *C) {
73+
c.Assert(fixtures.Clean(), IsNil)
74+
}
75+
6776
func (s *BaseStorageSuite) TestSetEncodedObjectAndEncodedObject(c *C) {
6877
for _, to := range s.testObjects {
6978
comment := Commentf("failed for type %s", to.Type.String())
@@ -143,6 +152,33 @@ func (s *BaseStorageSuite) TestIterEncodedObjects(c *C) {
143152
}
144153
}
145154

155+
func (s *BaseStorageSuite) TestPackfileWriter(c *C) {
156+
pwr, ok := s.Storer.(storer.PackfileWriter)
157+
if !ok {
158+
c.Skip("not a storer.PackWriter")
159+
}
160+
161+
pw, err := pwr.PackfileWriter()
162+
c.Assert(err, IsNil)
163+
164+
f := fixtures.Basic().One()
165+
_, err = io.Copy(pw, f.Packfile())
166+
c.Assert(err, IsNil)
167+
168+
err = pw.Close()
169+
c.Assert(err, IsNil)
170+
171+
iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject)
172+
c.Assert(err, IsNil)
173+
objects := 0
174+
err = iter.ForEach(func(plumbing.EncodedObject) error {
175+
objects++
176+
return nil
177+
})
178+
c.Assert(err, IsNil)
179+
c.Assert(objects, Equals, 31)
180+
}
181+
146182
func (s *BaseStorageSuite) TestObjectStorerTxSetEncodedObjectAndCommit(c *C) {
147183
storer, ok := s.Storer.(storer.Transactioner)
148184
if !ok {

0 commit comments

Comments
 (0)