| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/go-git/go-git/v5/plumbing" |
| 5 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 6 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 7 |
|
| 8 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 9 |
. "gopkg.in/check.v1" |
| 10 |
) |
| 11 |
|
| 12 |
type PatchSuite struct { |
| 13 |
BaseObjectsSuite |
| 14 |
} |
| 15 |
|
| 16 |
var _ = Suite(&PatchSuite{}) |
| 17 |
|
| 18 |
func (s *PatchSuite) TestStatsWithSubmodules(c *C) { |
| 19 |
storer := filesystem.NewStorage( |
| 20 |
fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit(), cache.NewObjectLRUDefault()) |
| 21 |
|
| 22 |
commit, err := GetCommit(storer, plumbing.NewHash("b685400c1f9316f350965a5993d350bc746b0bf4")) |
| 23 |
c.Assert(err, IsNil) |
| 24 |
|
| 25 |
tree, err := commit.Tree() |
| 26 |
c.Assert(err, IsNil) |
| 27 |
|
| 28 |
e, err := tree.entry("basic") |
| 29 |
c.Assert(err, IsNil) |
| 30 |
|
| 31 |
ch := &Change{ |
| 32 |
From: ChangeEntry{ |
| 33 |
Name: "basic", |
| 34 |
Tree: tree, |
| 35 |
TreeEntry: *e, |
| 36 |
}, |
| 37 |
To: ChangeEntry{ |
| 38 |
Name: "basic", |
| 39 |
Tree: tree, |
| 40 |
TreeEntry: *e, |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
p, err := getPatch("", ch) |
| 45 |
c.Assert(err, IsNil) |
| 46 |
c.Assert(p, NotNil) |
| 47 |
} |
| 48 |
|
| 49 |
func (s *PatchSuite) TestFileStatsString(c *C) { |
| 50 |
testCases := []struct { |
| 51 |
description string |
| 52 |
input FileStats |
| 53 |
expected string |
| 54 |
}{ |
| 55 |
|
| 56 |
{ |
| 57 |
description: "no files changed", |
| 58 |
input: []FileStat{}, |
| 59 |
expected: "", |
| 60 |
}, |
| 61 |
{ |
| 62 |
description: "one file touched - no changes", |
| 63 |
input: []FileStat{ |
| 64 |
{ |
| 65 |
Name: "file1", |
| 66 |
}, |
| 67 |
}, |
| 68 |
expected: " file1 | 0 \n", |
| 69 |
}, |
| 70 |
{ |
| 71 |
description: "one file changed", |
| 72 |
input: []FileStat{ |
| 73 |
{ |
| 74 |
Name: "file1", |
| 75 |
Addition: 1, |
| 76 |
}, |
| 77 |
}, |
| 78 |
expected: " file1 | 1 +\n", |
| 79 |
}, |
| 80 |
{ |
| 81 |
description: "one file changed with one addition and one deletion", |
| 82 |
input: []FileStat{ |
| 83 |
{ |
| 84 |
Name: ".github/workflows/git.yml", |
| 85 |
Addition: 1, |
| 86 |
Deletion: 1, |
| 87 |
}, |
| 88 |
}, |
| 89 |
expected: " .github/workflows/git.yml | 2 +-\n", |
| 90 |
}, |
| 91 |
{ |
| 92 |
description: "two files changed", |
| 93 |
input: []FileStat{ |
| 94 |
{ |
| 95 |
Name: ".github/workflows/git.yml", |
| 96 |
Addition: 1, |
| 97 |
Deletion: 1, |
| 98 |
}, |
| 99 |
{ |
| 100 |
Name: "cli/go-git/go.mod", |
| 101 |
Addition: 4, |
| 102 |
Deletion: 4, |
| 103 |
}, |
| 104 |
}, |
| 105 |
expected: " .github/workflows/git.yml | 2 +-\n cli/go-git/go.mod | 8 ++++----\n", |
| 106 |
}, |
| 107 |
{ |
| 108 |
description: "three files changed", |
| 109 |
input: []FileStat{ |
| 110 |
{ |
| 111 |
Name: ".github/workflows/git.yml", |
| 112 |
Addition: 3, |
| 113 |
Deletion: 3, |
| 114 |
}, |
| 115 |
{ |
| 116 |
Name: "worktree.go", |
| 117 |
Addition: 107, |
| 118 |
}, |
| 119 |
{ |
| 120 |
Name: "worktree_test.go", |
| 121 |
Addition: 75, |
| 122 |
}, |
| 123 |
}, |
| 124 |
expected: " .github/workflows/git.yml | 6 +++---\n" + |
| 125 |
" worktree.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n" + |
| 126 |
" worktree_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n", |
| 127 |
}, |
| 128 |
{ |
| 129 |
description: "three files changed with deletions and additions", |
| 130 |
input: []FileStat{ |
| 131 |
{ |
| 132 |
Name: ".github/workflows/git.yml", |
| 133 |
Addition: 3, |
| 134 |
Deletion: 3, |
| 135 |
}, |
| 136 |
{ |
| 137 |
Name: "worktree.go", |
| 138 |
Addition: 107, |
| 139 |
Deletion: 217, |
| 140 |
}, |
| 141 |
{ |
| 142 |
Name: "worktree_test.go", |
| 143 |
Addition: 75, |
| 144 |
Deletion: 275, |
| 145 |
}, |
| 146 |
}, |
| 147 |
expected: " .github/workflows/git.yml | 6 +++---\n" + |
| 148 |
" worktree.go | 324 ++++++++++++++++++-----------------------------------\n" + |
| 149 |
" worktree_test.go | 350 ++++++++++++-----------------------------------------\n", |
| 150 |
}, |
| 151 |
} |
| 152 |
|
| 153 |
for _, tc := range testCases { |
| 154 |
c.Log("Executing test cases:", tc.description) |
| 155 |
c.Assert(printStat(tc.input), Equals, tc.expected) |
| 156 |
} |
| 157 |
} |