From ebc25df06ce7d11afe88339af7bb097926599b7d Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Tue, 18 Jul 2017 10:39:36 +0200 Subject: test: add more PackfileWriter tests --- storage/filesystem/internal/dotgit/writers_test.go | 28 +++++++++++++++-- storage/filesystem/storage_test.go | 1 + storage/memory/storage_test.go | 1 + storage/test/storage_suite.go | 36 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) (limited to 'storage') diff --git a/storage/filesystem/internal/dotgit/writers_test.go b/storage/filesystem/internal/dotgit/writers_test.go index d2c7b6f..1342396 100644 --- a/storage/filesystem/internal/dotgit/writers_test.go +++ b/storage/filesystem/internal/dotgit/writers_test.go @@ -12,6 +12,7 @@ import ( . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v3/osfs" + "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" ) func (s *SuiteDotGit) TestNewObjectPack(c *C) { @@ -35,13 +36,30 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { c.Assert(w.Close(), IsNil) - stat, err := fs.Stat(fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash)) + pfPath := fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash) + idxPath := fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash) + + stat, err := fs.Stat(pfPath) c.Assert(err, IsNil) c.Assert(stat.Size(), Equals, int64(84794)) - stat, err = fs.Stat(fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash)) + stat, err = fs.Stat(idxPath) c.Assert(err, IsNil) c.Assert(stat.Size(), Equals, int64(1940)) + + pf, err := fs.Open(pfPath) + c.Assert(err, IsNil) + pfs := packfile.NewScanner(pf) + _, objects, err := pfs.Header() + c.Assert(err, IsNil) + for i := uint32(0); i < objects; i++ { + _, err := pfs.NextObjectHeader() + if err != nil { + c.Assert(err, IsNil) + break + } + } + c.Assert(pfs.Close(), IsNil) } func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { @@ -63,6 +81,12 @@ func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { info, err := fs.ReadDir("objects/pack") c.Assert(err, IsNil) c.Assert(info, HasLen, 0) + + // check clean up of temporary files + info, err = fs.ReadDir("") + for _, fi := range info { + c.Assert(fi.IsDir(), Equals, true) + } } func (s *SuiteDotGit) TestSyncedReader(c *C) { diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index 2ced9dd..22709f5 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -26,6 +26,7 @@ func (s *StorageSuite) SetUpTest(c *C) { c.Assert(err, IsNil) s.BaseStorageSuite = test.NewBaseStorageSuite(storage) + s.BaseStorageSuite.SetUpTest(c) } func (s *StorageSuite) TestFilesystem(c *C) { diff --git a/storage/memory/storage_test.go b/storage/memory/storage_test.go index ee8fa93..31a27fa 100644 --- a/storage/memory/storage_test.go +++ b/storage/memory/storage_test.go @@ -17,4 +17,5 @@ var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { s.BaseStorageSuite = test.NewBaseStorageSuite(NewStorage()) + s.BaseStorageSuite.SetUpTest(c) } diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 5a7cf9d..7cb0fe3 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -13,6 +13,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage" + "github.com/src-d/go-git-fixtures" . "gopkg.in/check.v1" ) @@ -64,6 +65,14 @@ func NewBaseStorageSuite(s Storer) BaseStorageSuite { }} } +func (s *BaseStorageSuite) SetUpTest(c *C) { + c.Assert(fixtures.Init(), IsNil) +} + +func (s *BaseStorageSuite) TearDownTest(c *C) { + c.Assert(fixtures.Clean(), IsNil) +} + func (s *BaseStorageSuite) TestSetEncodedObjectAndEncodedObject(c *C) { for _, to := range s.testObjects { comment := Commentf("failed for type %s", to.Type.String()) @@ -143,6 +152,33 @@ func (s *BaseStorageSuite) TestIterEncodedObjects(c *C) { } } +func (s *BaseStorageSuite) TestPackfileWriter(c *C) { + pwr, ok := s.Storer.(storer.PackfileWriter) + if !ok { + c.Skip("not a storer.PackWriter") + } + + pw, err := pwr.PackfileWriter() + c.Assert(err, IsNil) + + f := fixtures.Basic().One() + _, err = io.Copy(pw, f.Packfile()) + c.Assert(err, IsNil) + + err = pw.Close() + c.Assert(err, IsNil) + + iter, err := s.Storer.IterEncodedObjects(plumbing.AnyObject) + c.Assert(err, IsNil) + objects := 0 + err = iter.ForEach(func(plumbing.EncodedObject) error { + objects++ + return nil + }) + c.Assert(err, IsNil) + c.Assert(objects, Equals, 31) +} + func (s *BaseStorageSuite) TestObjectStorerTxSetEncodedObjectAndCommit(c *C) { storer, ok := s.Storer.(storer.Transactioner) if !ok { -- cgit