cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/git/git_test.go
348lines · modecode
| 1 | package git_test |
| 2 | |
| 3 | import ( |
| 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 | |
| 16 | func blameLine(sha string, line, prevLine int, filename, content string) string { |
| 17 | return fmt.Sprintf(`%s %d %d 1 |
| 18 | author Alice Mock |
| 19 | author-mail <alice@example.com> |
| 20 | author-time 1559927997 |
| 21 | author-tz 0000 |
| 22 | committer Alice Mock |
| 23 | committer-mail <alice@example.com> |
| 24 | committer-time 1559927997 |
| 25 | committer-tz 0000 |
| 26 | summary Mock commit title |
| 27 | boundary |
| 28 | filename %s |
| 29 | %s |
| 30 | `, sha, prevLine, line, filename, content) |
| 31 | } |
| 32 | |
| 33 | func 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 | |
| 138 | func TestCommitRange(t *testing.T) { |
| 139 | type testCaseT struct { |
| 140 | mock git.CommandRunner |
| 141 | output git.CommitRangeResults |
| 142 | shouldError bool |
| 143 | } |
| 144 | |
| 145 | testCases := []testCaseT{ |
| 146 | { |
| 147 | mock: func(_ ...string) ([]byte, error) { |
| 148 | return nil, fmt.Errorf("mock error") |
| 149 | }, |
| 150 | output: git.CommitRangeResults{Commits: []string{}}, |
| 151 | shouldError: true, |
| 152 | }, |
| 153 | { |
| 154 | mock: func(_ ...string) ([]byte, error) { |
| 155 | return []byte(""), nil |
| 156 | }, |
| 157 | output: git.CommitRangeResults{Commits: []string{}}, |
| 158 | shouldError: true, |
| 159 | }, |
| 160 | { |
| 161 | mock: func(_ ...string) ([]byte, error) { |
| 162 | return []byte("commit1\n"), nil |
| 163 | }, |
| 164 | output: git.CommitRangeResults{ |
| 165 | Commits: []string{"commit1"}, |
| 166 | From: "commit1", |
| 167 | To: "commit1", |
| 168 | }, |
| 169 | }, |
| 170 | { |
| 171 | mock: func(_ ...string) ([]byte, error) { |
| 172 | return []byte("commit1\ncommit2\ncommit3\n"), nil |
| 173 | }, |
| 174 | output: git.CommitRangeResults{ |
| 175 | Commits: []string{"commit1", "commit2", "commit3"}, |
| 176 | From: "commit1", |
| 177 | To: "commit3", |
| 178 | }, |
| 179 | }, |
| 180 | { |
| 181 | mock: func(_ ...string) ([]byte, error) { |
| 182 | return []byte("commit2\ncommit1"), nil |
| 183 | }, |
| 184 | output: git.CommitRangeResults{ |
| 185 | Commits: []string{"commit2", "commit1"}, |
| 186 | From: "commit2", |
| 187 | To: "commit1", |
| 188 | }, |
| 189 | }, |
| 190 | { |
| 191 | mock: func(_ ...string) ([]byte, error) { |
| 192 | return []byte("commit2\ncommit1\n"), nil |
| 193 | }, |
| 194 | output: git.CommitRangeResults{ |
| 195 | Commits: []string{"commit2", "commit1"}, |
| 196 | From: "commit2", |
| 197 | To: "commit1", |
| 198 | }, |
| 199 | }, |
| 200 | } |
| 201 | |
| 202 | for i, tc := range testCases { |
| 203 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 204 | output, err := git.CommitRange(tc.mock, "main") |
| 205 | |
| 206 | hadError := (err != nil) |
| 207 | if hadError != tc.shouldError { |
| 208 | t.Errorf("git.CommitRange() returned err=%v, expected=%v", err, tc.shouldError) |
| 209 | return |
| 210 | } |
| 211 | |
| 212 | require.Equal(t, tc.output, output, "git.CommitRange() returned wrong output") |
| 213 | }) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func TestCurrentBranch(t *testing.T) { |
| 218 | type testCaseT struct { |
| 219 | mock git.CommandRunner |
| 220 | output string |
| 221 | shouldError bool |
| 222 | } |
| 223 | |
| 224 | testCases := []testCaseT{ |
| 225 | { |
| 226 | mock: func(_ ...string) ([]byte, error) { |
| 227 | return nil, fmt.Errorf("mock error") |
| 228 | }, |
| 229 | output: "", |
| 230 | shouldError: true, |
| 231 | }, |
| 232 | { |
| 233 | mock: func(_ ...string) ([]byte, error) { |
| 234 | return []byte(""), nil |
| 235 | }, |
| 236 | output: "", |
| 237 | shouldError: false, |
| 238 | }, |
| 239 | { |
| 240 | mock: func(_ ...string) ([]byte, error) { |
| 241 | return []byte("foo"), nil |
| 242 | }, |
| 243 | output: "foo", |
| 244 | }, |
| 245 | { |
| 246 | mock: func(_ ...string) ([]byte, error) { |
| 247 | return []byte("foo bar\n"), nil |
| 248 | }, |
| 249 | output: "foo bar", |
| 250 | }, |
| 251 | } |
| 252 | |
| 253 | for i, tc := range testCases { |
| 254 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 255 | output, err := git.CurrentBranch(tc.mock) |
| 256 | |
| 257 | hadError := (err != nil) |
| 258 | if hadError != tc.shouldError { |
| 259 | t.Errorf("git.CurrentBranch() returned err=%v, expected=%v", err, tc.shouldError) |
| 260 | return |
| 261 | } |
| 262 | |
| 263 | require.Equal(t, tc.output, output, "git.CurrentBranch() returned wrong output") |
| 264 | }) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | func TestRunGit(t *testing.T) { |
| 269 | type testCaseT struct { |
| 270 | output *regexp.Regexp |
| 271 | err string |
| 272 | args []string |
| 273 | } |
| 274 | |
| 275 | testCases := []testCaseT{ |
| 276 | { |
| 277 | args: []string{"version"}, |
| 278 | output: regexp.MustCompile("^git version"), |
| 279 | }, |
| 280 | { |
| 281 | args: []string{"xxx"}, |
| 282 | err: "git: 'xxx' is not a git command. See 'git --help'.\n", |
| 283 | }, |
| 284 | } |
| 285 | |
| 286 | for _, tc := range testCases { |
| 287 | t.Run(strings.Join(tc.args, " "), func(t *testing.T) { |
| 288 | output, err := git.RunGit(tc.args...) |
| 289 | if tc.err != "" { |
| 290 | require.EqualError(t, err, tc.err) |
| 291 | } else { |
| 292 | require.NoError(t, err) |
| 293 | require.Regexp(t, tc.output, string(output)) |
| 294 | } |
| 295 | }) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func TestCommitMessage(t *testing.T) { |
| 300 | type testCaseT struct { |
| 301 | mock git.CommandRunner |
| 302 | output string |
| 303 | shouldError bool |
| 304 | } |
| 305 | |
| 306 | testCases := []testCaseT{ |
| 307 | { |
| 308 | mock: func(_ ...string) ([]byte, error) { |
| 309 | return nil, fmt.Errorf("mock error") |
| 310 | }, |
| 311 | output: "", |
| 312 | shouldError: true, |
| 313 | }, |
| 314 | { |
| 315 | mock: func(_ ...string) ([]byte, error) { |
| 316 | return []byte(""), nil |
| 317 | }, |
| 318 | output: "", |
| 319 | shouldError: false, |
| 320 | }, |
| 321 | { |
| 322 | mock: func(_ ...string) ([]byte, error) { |
| 323 | return []byte("foo"), nil |
| 324 | }, |
| 325 | output: "foo", |
| 326 | }, |
| 327 | { |
| 328 | mock: func(_ ...string) ([]byte, error) { |
| 329 | return []byte("foo bar\n"), nil |
| 330 | }, |
| 331 | output: "foo bar\n", |
| 332 | }, |
| 333 | } |
| 334 | |
| 335 | for i, tc := range testCases { |
| 336 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 337 | output, err := git.CommitMessage(tc.mock, "abc1234567890") |
| 338 | |
| 339 | hadError := (err != nil) |
| 340 | if hadError != tc.shouldError { |
| 341 | t.Errorf("git.CommitMessage() returned err=%v, expected=%v", err, tc.shouldError) |
| 342 | return |
| 343 | } |
| 344 | |
| 345 | require.Equal(t, tc.output, output, "git.CommitMessage() returned wrong output") |
| 346 | }) |
| 347 | } |
| 348 | } |
| 349 | |