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