cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/git_branch_test.go
120lines · modecode
| 1 | package discovery_test |
| 2 | |
| 3 | import ( |
| 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 | |
| 13 | func 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 |
| 44 | M foo/bar/1.txt |
| 45 | M foo/bar/3.txt |
| 46 | commit2 |
| 47 | M 5.txt |
| 48 | M foo/bar/2.txt |
| 49 | commit3 |
| 50 | |
| 51 | A foo/bar/4.txt |
| 52 | R053 src1.txt dst1.txt |
| 53 | R100 foo/bar/src2.txt src2.txt |
| 54 | M 5.txt |
| 55 | C50 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 |
| 76 | M foo/1.txt |
| 77 | M 3.txt |
| 78 | commit2 |
| 79 | M 5.txt |
| 80 | M bar/2.txt |
| 81 | commit3 |
| 82 | |
| 83 | A xxx/bar/4.txt |
| 84 | R053 src1.txt dst1.txt |
| 85 | R100 foo/bar/src2.txt src2.txt |
| 86 | M 5.txt |
| 87 | C50 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 | |