cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/git/git_test.go
189lines · modecode
| 1 | package git_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/cloudflare/pint/internal/git" |
| 13 | ) |
| 14 | |
| 15 | func TestCurrentBranch(t *testing.T) { |
| 16 | type testCaseT struct { |
| 17 | mock git.CommandRunner |
| 18 | output string |
| 19 | shouldError bool |
| 20 | } |
| 21 | |
| 22 | testCases := []testCaseT{ |
| 23 | { |
| 24 | mock: func(_ ...string) ([]byte, error) { |
| 25 | return nil, errors.New("mock error") |
| 26 | }, |
| 27 | output: "", |
| 28 | shouldError: true, |
| 29 | }, |
| 30 | { |
| 31 | mock: func(_ ...string) ([]byte, error) { |
| 32 | return []byte(""), nil |
| 33 | }, |
| 34 | output: "", |
| 35 | shouldError: false, |
| 36 | }, |
| 37 | { |
| 38 | mock: func(_ ...string) ([]byte, error) { |
| 39 | return []byte("foo"), nil |
| 40 | }, |
| 41 | output: "foo", |
| 42 | }, |
| 43 | { |
| 44 | mock: func(_ ...string) ([]byte, error) { |
| 45 | return []byte("foo bar\n"), nil |
| 46 | }, |
| 47 | output: "foo bar", |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | for i, tc := range testCases { |
| 52 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 53 | output, err := git.CurrentBranch(tc.mock) |
| 54 | |
| 55 | hadError := (err != nil) |
| 56 | if hadError != tc.shouldError { |
| 57 | t.Errorf("git.CurrentBranch() returned err=%v, expected=%v", err, tc.shouldError) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | require.Equal(t, tc.output, output, "git.CurrentBranch() returned wrong output") |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestRunGit(t *testing.T) { |
| 67 | type testCaseT struct { |
| 68 | output *regexp.Regexp |
| 69 | err string |
| 70 | args []string |
| 71 | } |
| 72 | |
| 73 | testCases := []testCaseT{ |
| 74 | { |
| 75 | args: []string{"version"}, |
| 76 | output: regexp.MustCompile("^git version"), |
| 77 | }, |
| 78 | { |
| 79 | args: []string{"xxx"}, |
| 80 | err: "git: 'xxx' is not a git command. See 'git --help'.\n", |
| 81 | }, |
| 82 | { |
| 83 | // config --get with non-existent key exits with 1 but no stderr |
| 84 | args: []string{"config", "--get", "nonexistent.key.that.does.not.exist"}, |
| 85 | err: "exit status 1", |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | for _, tc := range testCases { |
| 90 | t.Run(strings.Join(tc.args, " "), func(t *testing.T) { |
| 91 | output, err := git.RunGit(tc.args...) |
| 92 | if tc.err != "" { |
| 93 | require.EqualError(t, err, tc.err) |
| 94 | } else { |
| 95 | require.NoError(t, err) |
| 96 | require.Regexp(t, tc.output, string(output)) |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestHeadCommit(t *testing.T) { |
| 103 | type testCaseT struct { |
| 104 | mock git.CommandRunner |
| 105 | output string |
| 106 | shouldError bool |
| 107 | } |
| 108 | |
| 109 | testCases := []testCaseT{ |
| 110 | { |
| 111 | mock: func(_ ...string) ([]byte, error) { |
| 112 | return nil, errors.New("mock error") |
| 113 | }, |
| 114 | output: "", |
| 115 | shouldError: true, |
| 116 | }, |
| 117 | { |
| 118 | mock: func(_ ...string) ([]byte, error) { |
| 119 | return []byte("abc123\n"), nil |
| 120 | }, |
| 121 | output: "abc123", |
| 122 | }, |
| 123 | } |
| 124 | |
| 125 | for i, tc := range testCases { |
| 126 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 127 | output, err := git.HeadCommit(tc.mock) |
| 128 | |
| 129 | hadError := (err != nil) |
| 130 | if hadError != tc.shouldError { |
| 131 | t.Errorf("git.HeadCommit() returned err=%v, expected=%v", err, tc.shouldError) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | require.Equal(t, tc.output, output, "git.HeadCommit() returned wrong output") |
| 136 | }) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestCommitMessage(t *testing.T) { |
| 141 | type testCaseT struct { |
| 142 | mock git.CommandRunner |
| 143 | output string |
| 144 | shouldError bool |
| 145 | } |
| 146 | |
| 147 | testCases := []testCaseT{ |
| 148 | { |
| 149 | mock: func(_ ...string) ([]byte, error) { |
| 150 | return nil, errors.New("mock error") |
| 151 | }, |
| 152 | output: "", |
| 153 | shouldError: true, |
| 154 | }, |
| 155 | { |
| 156 | mock: func(_ ...string) ([]byte, error) { |
| 157 | return []byte(""), nil |
| 158 | }, |
| 159 | output: "", |
| 160 | shouldError: false, |
| 161 | }, |
| 162 | { |
| 163 | mock: func(_ ...string) ([]byte, error) { |
| 164 | return []byte("foo"), nil |
| 165 | }, |
| 166 | output: "foo", |
| 167 | }, |
| 168 | { |
| 169 | mock: func(_ ...string) ([]byte, error) { |
| 170 | return []byte("foo bar\n"), nil |
| 171 | }, |
| 172 | output: "foo bar\n", |
| 173 | }, |
| 174 | } |
| 175 | |
| 176 | for i, tc := range testCases { |
| 177 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 178 | output, err := git.CommitMessage(tc.mock, "abc1234567890") |
| 179 | |
| 180 | hadError := (err != nil) |
| 181 | if hadError != tc.shouldError { |
| 182 | t.Errorf("git.CommitMessage() returned err=%v, expected=%v", err, tc.shouldError) |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | require.Equal(t, tc.output, output, "git.CommitMessage() returned wrong output") |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |