cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.10.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/git_branch_test.go

146lines · modecode

1package discovery_test
2
3import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "testing"
8
9 "github.com/google/go-cmp/cmp"
10
11 "github.com/cloudflare/pint/internal/discovery"
12)
13
14func TestModifiedFiles(t *testing.T) {
15 type testCaseT struct {
16 detector discovery.FileFinder
17 output []discovery.File
18 shouldError bool
19 }
20
21 testCases := []testCaseT{
22 {
23 detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
24 return nil, fmt.Errorf("mock error")
25 }, nil, "main"),
26 output: nil,
27 shouldError: true,
28 },
29 {
30 detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
31 if args[0] == "rev-list" {
32 return []byte("commit1\ncommit2\n"), nil
33 }
34 return nil, fmt.Errorf("mock error")
35 }, nil, "main"),
36 output: nil,
37 shouldError: true,
38 },
39 {
40 detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
41 if args[0] == "rev-list" {
42 return []byte("commit1\ncommit3\n"), nil
43 }
44 content := `commit1
45M foo/bar/1.txt
46M foo/bar/3.txt
47commit2
48M 5.txt
49M foo/bar/2.txt
50commit3
51
52A foo/bar/4.txt
53R053 src1.txt dst1.txt
54R100 foo/bar/src2.txt src2.txt
55M 5.txt
56C50 foo/bar/cp1.txt foo/cp1.txt
57`
58 return []byte([]byte(content)), nil
59 }, nil, "main"),
60 output: []discovery.File{
61 {Path: "5.txt", Commits: []string{"commit2", "commit3"}},
62 {Path: "dst1.txt", Commits: []string{"commit3"}},
63 {Path: "foo/bar/1.txt", Commits: []string{"commit1"}},
64 {Path: "foo/bar/2.txt", Commits: []string{"commit2"}},
65 {Path: "foo/bar/3.txt", Commits: []string{"commit1"}},
66 {Path: "foo/bar/4.txt", Commits: []string{"commit3"}},
67 {Path: "foo/cp1.txt", Commits: []string{"commit3"}},
68 {Path: "src2.txt", Commits: []string{"commit3"}},
69 },
70 },
71 {
72 detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
73 if args[0] == "rev-list" {
74 return []byte("commit1\ncommit3\n"), nil
75 }
76 content := `commit1
77M foo/1.txt
78M 3.txt
79commit2
80M 5.txt
81M bar/2.txt
82commit3
83
84A xxx/bar/4.txt
85R053 src1.txt dst1.txt
86R100 foo/bar/src2.txt src2.txt
87M 5.txt
88C50 foo/bar/cp1.txt foo/cp1.del
89`
90 return []byte([]byte(content)), nil
91 }, []*regexp.Regexp{
92 regexp.MustCompile("^foo/.+.txt$"),
93 regexp.MustCompile("^bar/.+.txt$"),
94 }, "main"),
95 output: []discovery.File{
96 {Path: "bar/2.txt", Commits: []string{"commit2"}},
97 {Path: "foo/1.txt", Commits: []string{"commit1"}},
98 },
99 },
100 {
101 detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
102 if args[0] == "rev-list" {
103 return []byte("commit1\ncommit3\n"), nil
104 }
105 content := `commit1
106M file1
107M file2
108
109commit2
110M file1
111M file3
112
113commit3
114R100 file1 file4
115M file2
116`
117 return []byte([]byte(content)), nil
118 }, nil, "main"),
119 output: []discovery.File{
120 {Path: "file2", Commits: []string{"commit1", "commit3"}},
121 {Path: "file3", Commits: []string{"commit2"}},
122 {Path: "file4", Commits: []string{"commit1", "commit2", "commit3"}},
123 },
124 },
125 }
126
127 for i, tc := range testCases {
128 t.Run(strconv.Itoa(i), func(t *testing.T) {
129 output, err := tc.detector.Find()
130
131 hadError := (err != nil)
132 if hadError != tc.shouldError {
133 t.Errorf("git.ModifiedFiles() returned err=%v, expected=%v", err, tc.shouldError)
134 return
135 }
136
137 if hadError {
138 return
139 }
140
141 if diff := cmp.Diff(tc.output, output.Results()); diff != "" {
142 t.Errorf("git.ModifiedFiles() returned wrong output (-want +got):\n%s", diff)
143 }
144 })
145 }
146}
147