|
| 1 | +package object_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "gopkg.in/src-d/go-git.v4" |
| 7 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 9 | + "gopkg.in/src-d/go-git.v4/storage/memory" |
| 10 | + |
| 11 | + . "gopkg.in/check.v1" |
| 12 | + "gopkg.in/src-d/go-billy.v4/memfs" |
| 13 | + "gopkg.in/src-d/go-billy.v4/util" |
| 14 | + "gopkg.in/src-d/go-git-fixtures.v3" |
| 15 | +) |
| 16 | + |
| 17 | +type CommitStatsSuite struct { |
| 18 | + fixtures.Suite |
| 19 | +} |
| 20 | + |
| 21 | +var _ = Suite(&CommitStatsSuite{}) |
| 22 | + |
| 23 | +func (s *CommitStatsSuite) TestStats(c *C) { |
| 24 | + r, hash := s.writeHisotry(c, []byte("foo\n"), []byte("foo\nbar\n")) |
| 25 | + |
| 26 | + aCommit, err := r.CommitObject(hash) |
| 27 | + c.Assert(err, IsNil) |
| 28 | + |
| 29 | + fileStats, err := aCommit.Stats() |
| 30 | + c.Assert(err, IsNil) |
| 31 | + |
| 32 | + c.Assert(fileStats[0].Name, Equals, "foo") |
| 33 | + c.Assert(fileStats[0].Addition, Equals, 1) |
| 34 | + c.Assert(fileStats[0].Deletion, Equals, 0) |
| 35 | + c.Assert(fileStats[0].String(), Equals, " foo | 1 +\n") |
| 36 | +} |
| 37 | + |
| 38 | +func (s *CommitStatsSuite) TestStats_WithoutNewLine(c *C) { |
| 39 | + r, hash := s.writeHisotry(c, []byte("foo\nbar"), []byte("foo\nbar\n")) |
| 40 | + |
| 41 | + aCommit, err := r.CommitObject(hash) |
| 42 | + c.Assert(err, IsNil) |
| 43 | + |
| 44 | + fileStats, err := aCommit.Stats() |
| 45 | + c.Assert(err, IsNil) |
| 46 | + |
| 47 | + c.Assert(fileStats[0].Name, Equals, "foo") |
| 48 | + c.Assert(fileStats[0].Addition, Equals, 1) |
| 49 | + c.Assert(fileStats[0].Deletion, Equals, 1) |
| 50 | + c.Assert(fileStats[0].String(), Equals, " foo | 2 +-\n") |
| 51 | +} |
| 52 | + |
| 53 | +func (s *CommitStatsSuite) writeHisotry(c *C, files ...[]byte) (*git.Repository, plumbing.Hash) { |
| 54 | + cm := &git.CommitOptions{ |
| 55 | + Author: &object. Signature{ Name: "Foo", Email: "[email protected]", When: time. Now()}, |
| 56 | + } |
| 57 | + |
| 58 | + fs := memfs.New() |
| 59 | + r, err := git.Init(memory.NewStorage(), fs) |
| 60 | + c.Assert(err, IsNil) |
| 61 | + |
| 62 | + w, err := r.Worktree() |
| 63 | + c.Assert(err, IsNil) |
| 64 | + |
| 65 | + var hash plumbing.Hash |
| 66 | + for _, content := range files { |
| 67 | + util.WriteFile(fs, "foo", content, 0644) |
| 68 | + |
| 69 | + _, err = w.Add("foo") |
| 70 | + c.Assert(err, IsNil) |
| 71 | + |
| 72 | + hash, err = w.Commit("foo\n", cm) |
| 73 | + c.Assert(err, IsNil) |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + return r, hash |
| 78 | +} |
0 commit comments