aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/dotgit/writers_test.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-10-15 12:21:23 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2018-10-15 12:21:23 +0200
commit7a77bde9448c15251cdcca14db20b7bf5c798692 (patch)
tree4fa875f1fa57b01dcdfdb17d1b3ba2d0519bd493 /storage/filesystem/dotgit/writers_test.go
parent0b7d3fe0a47bd7da9c42ba34b9098441120bda02 (diff)
parent345ffd95a2cd1413d7f48280e21a035febbe4e10 (diff)
downloadgo-git-7a77bde9448c15251cdcca14db20b7bf5c798692.tar.gz
Merge branch 'master' of github.com:src-d/go-git into annotated
Diffstat (limited to 'storage/filesystem/dotgit/writers_test.go')
-rw-r--r--storage/filesystem/dotgit/writers_test.go157
1 files changed, 157 insertions, 0 deletions
diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go
new file mode 100644
index 0000000..5a5f7b4
--- /dev/null
+++ b/storage/filesystem/dotgit/writers_test.go
@@ -0,0 +1,157 @@
+package dotgit
+
+import (
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+ "strconv"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/idxfile"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
+
+ . "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-billy.v4/osfs"
+ "gopkg.in/src-d/go-git-fixtures.v3"
+)
+
+func (s *SuiteDotGit) TestNewObjectPack(c *C) {
+ f := fixtures.Basic().One()
+
+ dir, err := ioutil.TempDir("", "example")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ defer os.RemoveAll(dir)
+
+ fs := osfs.New(dir)
+ dot := New(fs)
+
+ w, err := dot.NewObjectPack()
+ c.Assert(err, IsNil)
+
+ _, err = io.Copy(w, f.Packfile())
+ c.Assert(err, IsNil)
+
+ c.Assert(w.Close(), IsNil)
+
+ 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(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) {
+ dir, err := ioutil.TempDir("", "example")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ defer os.RemoveAll(dir)
+
+ fs := osfs.New(dir)
+ dot := New(fs)
+
+ w, err := dot.NewObjectPack()
+ c.Assert(err, IsNil)
+
+ c.Assert(w.Close(), IsNil)
+
+ 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("")
+ c.Assert(err, IsNil)
+ for _, fi := range info {
+ c.Assert(fi.IsDir(), Equals, true)
+ }
+}
+
+func (s *SuiteDotGit) TestSyncedReader(c *C) {
+ tmpw, err := ioutil.TempFile("", "example")
+ c.Assert(err, IsNil)
+
+ tmpr, err := os.Open(tmpw.Name())
+ c.Assert(err, IsNil)
+
+ defer func() {
+ tmpw.Close()
+ tmpr.Close()
+ os.Remove(tmpw.Name())
+ }()
+
+ synced := newSyncedReader(tmpw, tmpr)
+
+ go func() {
+ for i := 0; i < 281; i++ {
+ _, err := synced.Write([]byte(strconv.Itoa(i) + "\n"))
+ c.Assert(err, IsNil)
+ }
+
+ synced.Close()
+ }()
+
+ o, err := synced.Seek(1002, io.SeekStart)
+ c.Assert(err, IsNil)
+ c.Assert(o, Equals, int64(1002))
+
+ head := make([]byte, 3)
+ n, err := io.ReadFull(synced, head)
+ c.Assert(err, IsNil)
+ c.Assert(n, Equals, 3)
+ c.Assert(string(head), Equals, "278")
+
+ o, err = synced.Seek(1010, io.SeekStart)
+ c.Assert(err, IsNil)
+ c.Assert(o, Equals, int64(1010))
+
+ n, err = io.ReadFull(synced, head)
+ c.Assert(err, IsNil)
+ c.Assert(n, Equals, 3)
+ c.Assert(string(head), Equals, "280")
+}
+
+func (s *SuiteDotGit) TestPackWriterUnusedNotify(c *C) {
+ dir, err := ioutil.TempDir("", "example")
+ if err != nil {
+ c.Assert(err, IsNil)
+ }
+
+ defer os.RemoveAll(dir)
+
+ fs := osfs.New(dir)
+
+ w, err := newPackWrite(fs)
+ c.Assert(err, IsNil)
+
+ w.Notify = func(h plumbing.Hash, idx *idxfile.Writer) {
+ c.Fatal("unexpected call to PackWriter.Notify")
+ }
+
+ c.Assert(w.Close(), IsNil)
+}