cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.29.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/git/git_test.go

315lines · modecode

1package git_test
2
3import (
4 "errors"
5 "fmt"
6 "regexp"
7 "strconv"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/require"
12
13 "github.com/cloudflare/pint/internal/git"
14)
15
16func blameLine(sha string, line int, filename, content string) string {
17 return fmt.Sprintf(`%s %d %d 1
18author Alice Mock
19author-mail <alice@example.com>
20author-time 1559927997
21author-tz 0000
22committer Alice Mock
23committer-mail <alice@example.com>
24committer-time 1559927997
25committer-tz 0000
26summary Mock commit title
27boundary
28filename %s
29 %s
30`, sha, line, line, filename, content)
31}
32
33func TestGitBlame(t *testing.T) {
34 type testCaseT struct {
35 mock git.CommandRunner
36 path string
37 output git.LineBlames
38 shouldError bool
39 }
40
41 testCases := []testCaseT{
42 {
43 mock: func(args ...string) ([]byte, error) {
44 return nil, nil
45 },
46 path: "foo.txt",
47 output: nil,
48 },
49 {
50 mock: func(args ...string) ([]byte, error) {
51 return nil, errors.New("mock error")
52 },
53 shouldError: true,
54 },
55 {
56 mock: func(args ...string) ([]byte, error) {
57 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, "foo.txt", "")
58 return []byte(content), nil
59 },
60 path: "foo.txt",
61 output: git.LineBlames{
62 {
63 Filename: "foo.txt",
64 Line: 1,
65 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
66 },
67 },
68 },
69 {
70 mock: func(args ...string) ([]byte, error) {
71 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, "foo.txt", "") +
72 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 2, "foo.txt", "") +
73 blameLine("82987dec74ba8e434ba393d83491ace784473291", 3, "foo.txt", "") +
74 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 4, "bar.txt", "")
75 return []byte(content), nil
76 },
77 path: "foo.txt",
78 output: git.LineBlames{
79 {
80 Filename: "foo.txt",
81 Line: 1,
82 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
83 },
84 {Filename: "foo.txt", Line: 2, Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44"},
85 {Filename: "foo.txt", Line: 3, Commit: "82987dec74ba8e434ba393d83491ace784473291"},
86 },
87 },
88 }
89
90 for i, tc := range testCases {
91 t.Run(strconv.Itoa(i), func(t *testing.T) {
92 output, err := git.Blame(tc.path, tc.mock)
93
94 hadError := (err != nil)
95 if hadError != tc.shouldError {
96 t.Errorf("git.Blame() returned err=%v, expected=%v", err, tc.shouldError)
97 return
98 }
99
100 require.Equal(t, tc.output, output, "git.Blame() returned wrong output ")
101 })
102 }
103}
104
105func TestCommitRange(t *testing.T) {
106 type testCaseT struct {
107 mock git.CommandRunner
108 output git.CommitRangeResults
109 shouldError bool
110 }
111
112 testCases := []testCaseT{
113 {
114 mock: func(args ...string) ([]byte, error) {
115 return nil, fmt.Errorf("mock error")
116 },
117 output: git.CommitRangeResults{Commits: []string{}},
118 shouldError: true,
119 },
120 {
121 mock: func(args ...string) ([]byte, error) {
122 return []byte(""), nil
123 },
124 output: git.CommitRangeResults{Commits: []string{}},
125 shouldError: true,
126 },
127 {
128 mock: func(args ...string) ([]byte, error) {
129 return []byte("commit1\n"), nil
130 },
131 output: git.CommitRangeResults{
132 Commits: []string{"commit1"},
133 From: "commit1",
134 To: "commit1",
135 },
136 },
137 {
138 mock: func(args ...string) ([]byte, error) {
139 return []byte("commit1\ncommit2\ncommit3\n"), nil
140 },
141 output: git.CommitRangeResults{
142 Commits: []string{"commit1", "commit2", "commit3"},
143 From: "commit1",
144 To: "commit3",
145 },
146 },
147 {
148 mock: func(args ...string) ([]byte, error) {
149 return []byte("commit2\ncommit1"), nil
150 },
151 output: git.CommitRangeResults{
152 Commits: []string{"commit2", "commit1"},
153 From: "commit2",
154 To: "commit1",
155 },
156 },
157 {
158 mock: func(args ...string) ([]byte, error) {
159 return []byte("commit2\ncommit1\n"), nil
160 },
161 output: git.CommitRangeResults{
162 Commits: []string{"commit2", "commit1"},
163 From: "commit2",
164 To: "commit1",
165 },
166 },
167 }
168
169 for i, tc := range testCases {
170 t.Run(strconv.Itoa(i), func(t *testing.T) {
171 output, err := git.CommitRange(tc.mock, "main")
172
173 hadError := (err != nil)
174 if hadError != tc.shouldError {
175 t.Errorf("git.CommitRange() returned err=%v, expected=%v", err, tc.shouldError)
176 return
177 }
178
179 require.Equal(t, tc.output, output, "git.CommitRange() returned wrong output")
180 })
181 }
182}
183
184func TestCurrentBranch(t *testing.T) {
185 type testCaseT struct {
186 mock git.CommandRunner
187 output string
188 shouldError bool
189 }
190
191 testCases := []testCaseT{
192 {
193 mock: func(args ...string) ([]byte, error) {
194 return nil, fmt.Errorf("mock error")
195 },
196 output: "",
197 shouldError: true,
198 },
199 {
200 mock: func(args ...string) ([]byte, error) {
201 return []byte(""), nil
202 },
203 output: "",
204 shouldError: false,
205 },
206 {
207 mock: func(args ...string) ([]byte, error) {
208 return []byte("foo"), nil
209 },
210 output: "foo",
211 },
212 {
213 mock: func(args ...string) ([]byte, error) {
214 return []byte("foo bar\n"), nil
215 },
216 output: "foo bar",
217 },
218 }
219
220 for i, tc := range testCases {
221 t.Run(strconv.Itoa(i), func(t *testing.T) {
222 output, err := git.CurrentBranch(tc.mock)
223
224 hadError := (err != nil)
225 if hadError != tc.shouldError {
226 t.Errorf("git.CurrentBranch() returned err=%v, expected=%v", err, tc.shouldError)
227 return
228 }
229
230 require.Equal(t, tc.output, output, "git.CurrentBranch() returned wrong output")
231 })
232 }
233}
234
235func TestRunGit(t *testing.T) {
236 type testCaseT struct {
237 args []string
238 output *regexp.Regexp
239 err string
240 }
241
242 testCases := []testCaseT{
243 {
244 args: []string{"version"},
245 output: regexp.MustCompile("^git version"),
246 },
247 {
248 args: []string{"xxx"},
249 err: "git: 'xxx' is not a git command. See 'git --help'.\n",
250 },
251 }
252
253 for _, tc := range testCases {
254 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
255 output, err := git.RunGit(tc.args...)
256 if tc.err != "" {
257 require.EqualError(t, err, tc.err)
258 } else {
259 require.NoError(t, err)
260 require.Regexp(t, tc.output, string(output))
261 }
262 })
263 }
264}
265
266func TestCommitMessage(t *testing.T) {
267 type testCaseT struct {
268 mock git.CommandRunner
269 output string
270 shouldError bool
271 }
272
273 testCases := []testCaseT{
274 {
275 mock: func(args ...string) ([]byte, error) {
276 return nil, fmt.Errorf("mock error")
277 },
278 output: "",
279 shouldError: true,
280 },
281 {
282 mock: func(args ...string) ([]byte, error) {
283 return []byte(""), nil
284 },
285 output: "",
286 shouldError: false,
287 },
288 {
289 mock: func(args ...string) ([]byte, error) {
290 return []byte("foo"), nil
291 },
292 output: "foo",
293 },
294 {
295 mock: func(args ...string) ([]byte, error) {
296 return []byte("foo bar\n"), nil
297 },
298 output: "foo bar\n",
299 },
300 }
301
302 for i, tc := range testCases {
303 t.Run(strconv.Itoa(i), func(t *testing.T) {
304 output, err := git.CommitMessage(tc.mock, "abc1234567890")
305
306 hadError := (err != nil)
307 if hadError != tc.shouldError {
308 t.Errorf("git.CommitMessage() returned err=%v, expected=%v", err, tc.shouldError)
309 return
310 }
311
312 require.Equal(t, tc.output, output, "git.CommitMessage() returned wrong output")
313 })
314 }
315}
316