cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.3.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/git_branch_test.go

120lines · modecode

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