cloudflare/pint

Public

mirrored fromhttps://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.75.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/git_test.go

269lines · 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, prevLine 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, prevLine, 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(_ ...string) ([]byte, error) {
44 return nil, nil
45 },
46 path: "foo.txt",
47 output: nil,
48 },
49 {
50 mock: func(_ ...string) ([]byte, error) {
51 return nil, errors.New("mock error")
52 },
53 shouldError: true,
54 },
55 {
56 mock: func(_ ...string) ([]byte, error) {
57 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, 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 PrevLine: 1,
66 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
67 },
68 },
69 },
70 {
71 mock: func(_ ...string) ([]byte, error) {
72 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, 5, "foo.txt", "")
73 return []byte(content), nil
74 },
75 path: "foo.txt",
76 output: git.LineBlames{
77 {
78 Filename: "foo.txt",
79 Line: 1,
80 PrevLine: 5,
81 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
82 },
83 },
84 },
85 {
86 mock: func(_ ...string) ([]byte, error) {
87 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, 1, "foo.txt", "") +
88 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 2, 2, "foo.txt", "") +
89 blameLine("82987dec74ba8e434ba393d83491ace784473291", 3, 3, "foo.txt", "") +
90 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 4, 4, "bar.txt", "")
91 return []byte(content), nil
92 },
93 path: "foo.txt",
94 output: git.LineBlames{
95 {
96 Filename: "foo.txt",
97 Line: 1,
98 PrevLine: 1,
99 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
100 },
101 {
102 Filename: "foo.txt",
103 Line: 2,
104 PrevLine: 2,
105 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
106 },
107 {
108 Filename: "foo.txt",
109 Line: 3,
110 PrevLine: 3,
111 Commit: "82987dec74ba8e434ba393d83491ace784473291",
112 },
113 {
114 Filename: "bar.txt",
115 Line: 4,
116 PrevLine: 4,
117 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
118 },
119 },
120 },
121 }
122
123 for i, tc := range testCases {
124 t.Run(strconv.Itoa(i), func(t *testing.T) {
125 output, err := git.Blame(tc.mock, tc.path, "")
126
127 hadError := (err != nil)
128 if hadError != tc.shouldError {
129 t.Errorf("git.Blame() returned err=%v, expected=%v", err, tc.shouldError)
130 return
131 }
132
133 require.Equal(t, tc.output, output, "git.Blame() returned wrong output ")
134 })
135 }
136}
137
138func TestCurrentBranch(t *testing.T) {
139 type testCaseT struct {
140 mock git.CommandRunner
141 output string
142 shouldError bool
143 }
144
145 testCases := []testCaseT{
146 {
147 mock: func(_ ...string) ([]byte, error) {
148 return nil, errors.New("mock error")
149 },
150 output: "",
151 shouldError: true,
152 },
153 {
154 mock: func(_ ...string) ([]byte, error) {
155 return []byte(""), nil
156 },
157 output: "",
158 shouldError: false,
159 },
160 {
161 mock: func(_ ...string) ([]byte, error) {
162 return []byte("foo"), nil
163 },
164 output: "foo",
165 },
166 {
167 mock: func(_ ...string) ([]byte, error) {
168 return []byte("foo bar\n"), nil
169 },
170 output: "foo bar",
171 },
172 }
173
174 for i, tc := range testCases {
175 t.Run(strconv.Itoa(i), func(t *testing.T) {
176 output, err := git.CurrentBranch(tc.mock)
177
178 hadError := (err != nil)
179 if hadError != tc.shouldError {
180 t.Errorf("git.CurrentBranch() returned err=%v, expected=%v", err, tc.shouldError)
181 return
182 }
183
184 require.Equal(t, tc.output, output, "git.CurrentBranch() returned wrong output")
185 })
186 }
187}
188
189func TestRunGit(t *testing.T) {
190 type testCaseT struct {
191 output *regexp.Regexp
192 err string
193 args []string
194 }
195
196 testCases := []testCaseT{
197 {
198 args: []string{"version"},
199 output: regexp.MustCompile("^git version"),
200 },
201 {
202 args: []string{"xxx"},
203 err: "git: 'xxx' is not a git command. See 'git --help'.\n",
204 },
205 }
206
207 for _, tc := range testCases {
208 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
209 output, err := git.RunGit(tc.args...)
210 if tc.err != "" {
211 require.EqualError(t, err, tc.err)
212 } else {
213 require.NoError(t, err)
214 require.Regexp(t, tc.output, string(output))
215 }
216 })
217 }
218}
219
220func TestCommitMessage(t *testing.T) {
221 type testCaseT struct {
222 mock git.CommandRunner
223 output string
224 shouldError bool
225 }
226
227 testCases := []testCaseT{
228 {
229 mock: func(_ ...string) ([]byte, error) {
230 return nil, errors.New("mock error")
231 },
232 output: "",
233 shouldError: true,
234 },
235 {
236 mock: func(_ ...string) ([]byte, error) {
237 return []byte(""), nil
238 },
239 output: "",
240 shouldError: false,
241 },
242 {
243 mock: func(_ ...string) ([]byte, error) {
244 return []byte("foo"), nil
245 },
246 output: "foo",
247 },
248 {
249 mock: func(_ ...string) ([]byte, error) {
250 return []byte("foo bar\n"), nil
251 },
252 output: "foo bar\n",
253 },
254 }
255
256 for i, tc := range testCases {
257 t.Run(strconv.Itoa(i), func(t *testing.T) {
258 output, err := git.CommitMessage(tc.mock, "abc1234567890")
259
260 hadError := (err != nil)
261 if hadError != tc.shouldError {
262 t.Errorf("git.CommitMessage() returned err=%v, expected=%v", err, tc.shouldError)
263 return
264 }
265
266 require.Equal(t, tc.output, output, "git.CommitMessage() returned wrong output")
267 })
268 }
269}
270