cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.13.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/git_test.go

226lines · modecode

1package git_test
2
3import (
4 "fmt"
5 "strconv"
6 "testing"
7
8 "github.com/cloudflare/pint/internal/git"
9
10 "github.com/google/go-cmp/cmp"
11)
12
13func blameLine(sha string, line int, filename, content string) string {
14 return fmt.Sprintf(`%s %d %d 1
15author Alice Mock
16author-mail <alice@example.com>
17author-time 1559927997
18author-tz 0000
19committer Alice Mock
20committer-mail <alice@example.com>
21committer-time 1559927997
22committer-tz 0000
23summary Mock commit title
24boundary
25filename %s
26 %s
27`, sha, line, line, filename, content)
28}
29
30func TestGitBlame(t *testing.T) {
31 type testCaseT struct {
32 mock git.CommandRunner
33 path string
34 output git.LineBlames
35 shouldError bool
36 }
37
38 testCases := []testCaseT{
39 {
40 mock: func(args ...string) ([]byte, error) {
41 return nil, nil
42 },
43 path: "foo.txt",
44 output: nil,
45 },
46 {
47 mock: func(args ...string) ([]byte, error) {
48 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, "foo.txt", "")
49 return []byte(content), nil
50 },
51 path: "foo.txt",
52 output: git.LineBlames{
53 {
54 Filename: "foo.txt",
55 Line: 1,
56 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
57 },
58 },
59 },
60 {
61 mock: func(args ...string) ([]byte, error) {
62 content := blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 1, "foo.txt", "") +
63 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 2, "foo.txt", "") +
64 blameLine("82987dec74ba8e434ba393d83491ace784473291", 3, "foo.txt", "") +
65 blameLine("b33a88cea35abc47f9973983626e1c6f3f3abc44", 4, "bar.txt", "")
66 return []byte(content), nil
67 },
68 path: "foo.txt",
69 output: git.LineBlames{
70 {
71 Filename: "foo.txt",
72 Line: 1,
73 Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44",
74 },
75 {Filename: "foo.txt", Line: 2, Commit: "b33a88cea35abc47f9973983626e1c6f3f3abc44"},
76 {Filename: "foo.txt", Line: 3, Commit: "82987dec74ba8e434ba393d83491ace784473291"},
77 },
78 },
79 }
80
81 for i, tc := range testCases {
82 t.Run(strconv.Itoa(i), func(t *testing.T) {
83 output, err := git.Blame(tc.path, tc.mock)
84
85 hadError := (err != nil)
86 if hadError != tc.shouldError {
87 t.Errorf("git.Blame() returned err=%v, expected=%v", err, tc.shouldError)
88 return
89 }
90
91 if diff := cmp.Diff(tc.output, output); diff != "" {
92 t.Errorf("git.Blame() returned wrong output (-want +got):\n%s", diff)
93 }
94 })
95 }
96}
97
98func TestCommitRange(t *testing.T) {
99 type testCaseT struct {
100 mock git.CommandRunner
101 output git.CommitRangeResults
102 shouldError bool
103 }
104
105 testCases := []testCaseT{
106 {
107 mock: func(args ...string) ([]byte, error) {
108 return nil, fmt.Errorf("mock error")
109 },
110 output: git.CommitRangeResults{},
111 shouldError: true,
112 },
113 {
114 mock: func(args ...string) ([]byte, error) {
115 return []byte([]byte("")), nil
116 },
117 output: git.CommitRangeResults{},
118 shouldError: true,
119 },
120 {
121 mock: func(args ...string) ([]byte, error) {
122 return []byte([]byte("commit1\n")), nil
123 },
124 output: git.CommitRangeResults{
125 From: "commit1",
126 To: "commit1",
127 },
128 },
129 {
130 mock: func(args ...string) ([]byte, error) {
131 return []byte([]byte("commit1\ncommit2\ncommit3\n")), nil
132 },
133 output: git.CommitRangeResults{
134 From: "commit1",
135 To: "commit3",
136 },
137 },
138 {
139 mock: func(args ...string) ([]byte, error) {
140 return []byte("commit2\ncommit1"), nil
141 },
142 output: git.CommitRangeResults{
143 From: "commit2",
144 To: "commit1",
145 },
146 },
147 {
148 mock: func(args ...string) ([]byte, error) {
149 return []byte("commit2\ncommit1\n"), nil
150 },
151 output: git.CommitRangeResults{
152 From: "commit2",
153 To: "commit1",
154 },
155 },
156 }
157
158 for i, tc := range testCases {
159 t.Run(strconv.Itoa(i), func(t *testing.T) {
160 output, err := git.CommitRange(tc.mock, "main")
161
162 hadError := (err != nil)
163 if hadError != tc.shouldError {
164 t.Errorf("git.CommitRange() returned err=%v, expected=%v", err, tc.shouldError)
165 return
166 }
167
168 if diff := cmp.Diff(tc.output, output); diff != "" {
169 t.Errorf("git.CommitRange() returned wrong output (-want +got):\n%s", diff)
170 }
171 })
172 }
173}
174
175func TestCurrentBranch(t *testing.T) {
176 type testCaseT struct {
177 mock git.CommandRunner
178 output string
179 shouldError bool
180 }
181
182 testCases := []testCaseT{
183 {
184 mock: func(args ...string) ([]byte, error) {
185 return nil, fmt.Errorf("mock error")
186 },
187 output: "",
188 shouldError: true,
189 },
190 {
191 mock: func(args ...string) ([]byte, error) {
192 return []byte([]byte("")), nil
193 },
194 output: "",
195 shouldError: false,
196 },
197 {
198 mock: func(args ...string) ([]byte, error) {
199 return []byte([]byte("foo")), nil
200 },
201 output: "foo",
202 },
203 {
204 mock: func(args ...string) ([]byte, error) {
205 return []byte([]byte("foo bar\n")), nil
206 },
207 output: "foo bar",
208 },
209 }
210
211 for i, tc := range testCases {
212 t.Run(strconv.Itoa(i), func(t *testing.T) {
213 output, err := git.CurrentBranch(tc.mock)
214
215 hadError := (err != nil)
216 if hadError != tc.shouldError {
217 t.Errorf("git.CurrentBranch() returned err=%v, expected=%v", err, tc.shouldError)
218 return
219 }
220
221 if diff := cmp.Diff(tc.output, output); diff != "" {
222 t.Errorf("git.CurrentBranch() returned wrong output (-want +got):\n%s", diff)
223 }
224 })
225 }
226}
227