cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/git_branch_test.go
146lines · modecode
| 1 | package discovery_test |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | func 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 |
| 45 | M foo/bar/1.txt |
| 46 | M foo/bar/3.txt |
| 47 | commit2 |
| 48 | M 5.txt |
| 49 | M foo/bar/2.txt |
| 50 | commit3 |
| 51 | |
| 52 | A foo/bar/4.txt |
| 53 | R053 src1.txt dst1.txt |
| 54 | R100 foo/bar/src2.txt src2.txt |
| 55 | M 5.txt |
| 56 | C50 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 |
| 77 | M foo/1.txt |
| 78 | M 3.txt |
| 79 | commit2 |
| 80 | M 5.txt |
| 81 | M bar/2.txt |
| 82 | commit3 |
| 83 | |
| 84 | A xxx/bar/4.txt |
| 85 | R053 src1.txt dst1.txt |
| 86 | R100 foo/bar/src2.txt src2.txt |
| 87 | M 5.txt |
| 88 | C50 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 |
| 106 | M file1 |
| 107 | M file2 |
| 108 | |
| 109 | commit2 |
| 110 | M file1 |
| 111 | M file3 |
| 112 | |
| 113 | commit3 |
| 114 | R100 file1 file4 |
| 115 | M 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 | |