cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.83.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/git_test.go

176lines · modecode

1package git_test
2
3import (
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
15func TestDescribe(t *testing.T) {
16 type testCaseT struct {
17 name string
18 mock git.CommandRunner
19 expected git.Info
20 err string
21 }
22
23 testCases := []testCaseT{
24 {
25 // First git command (rev-parse --verify HEAD) fails.
26 name: "head commit command fails",
27 mock: func(_ ...string) ([]byte, error) {
28 return nil, errors.New("mock error")
29 },
30 err: "mock error",
31 },
32 {
33 // First command succeeds, second (rev-parse --abbrev-ref HEAD) fails.
34 name: "current branch command fails",
35 mock: func() git.CommandRunner {
36 calls := 0
37 return func(_ ...string) ([]byte, error) {
38 calls++
39 if calls == 1 {
40 return []byte("abc123\n"), nil
41 }
42 return nil, errors.New("branch error")
43 }
44 }(),
45 err: "branch error",
46 },
47 {
48 // Both commands succeed.
49 name: "both commands succeed",
50 mock: func() git.CommandRunner {
51 calls := 0
52 return func(_ ...string) ([]byte, error) {
53 calls++
54 if calls == 1 {
55 return []byte("abc123\n"), nil
56 }
57 return []byte("feature/foo\n"), nil
58 }
59 }(),
60 expected: git.Info{
61 HeadCommit: "abc123",
62 CurrentBranch: "feature/foo",
63 },
64 },
65 {
66 // Both commands succeed with empty output.
67 name: "both commands succeed with empty output",
68 mock: func(_ ...string) ([]byte, error) {
69 return []byte(""), nil
70 },
71 expected: git.Info{
72 HeadCommit: "",
73 CurrentBranch: "",
74 },
75 },
76 }
77
78 for _, tc := range testCases {
79 t.Run(tc.name, func(t *testing.T) {
80 info, err := git.Describe(tc.mock)
81 if tc.err != "" {
82 require.EqualError(t, err, tc.err)
83 } else {
84 require.NoError(t, err)
85 require.Equal(t, tc.expected, info)
86 }
87 })
88 }
89}
90
91func TestRunGit(t *testing.T) {
92 type testCaseT struct {
93 output *regexp.Regexp
94 err string
95 args []string
96 }
97
98 testCases := []testCaseT{
99 {
100 args: []string{"version"},
101 output: regexp.MustCompile("^git version"),
102 },
103 {
104 args: []string{"xxx"},
105 err: "git: 'xxx' is not a git command. See 'git --help'.\n",
106 },
107 {
108 // config --get with non-existent key exits with 1 but no stderr
109 args: []string{"config", "--get", "nonexistent.key.that.does.not.exist"},
110 err: "exit status 1",
111 },
112 }
113
114 for _, tc := range testCases {
115 t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
116 output, err := git.RunGit(tc.args...)
117 if tc.err != "" {
118 require.EqualError(t, err, tc.err)
119 } else {
120 require.NoError(t, err)
121 require.Regexp(t, tc.output, string(output))
122 }
123 })
124 }
125}
126
127func TestCommitMessage(t *testing.T) {
128 type testCaseT struct {
129 mock git.CommandRunner
130 output string
131 shouldError bool
132 }
133
134 testCases := []testCaseT{
135 {
136 mock: func(_ ...string) ([]byte, error) {
137 return nil, errors.New("mock error")
138 },
139 output: "",
140 shouldError: true,
141 },
142 {
143 mock: func(_ ...string) ([]byte, error) {
144 return []byte(""), nil
145 },
146 output: "",
147 shouldError: false,
148 },
149 {
150 mock: func(_ ...string) ([]byte, error) {
151 return []byte("foo"), nil
152 },
153 output: "foo",
154 },
155 {
156 mock: func(_ ...string) ([]byte, error) {
157 return []byte("foo bar\n"), nil
158 },
159 output: "foo bar\n",
160 },
161 }
162
163 for i, tc := range testCases {
164 t.Run(strconv.Itoa(i), func(t *testing.T) {
165 output, err := git.CommitMessage(tc.mock, "abc1234567890")
166
167 hadError := (err != nil)
168 if hadError != tc.shouldError {
169 t.Errorf("git.CommitMessage() returned err=%v, expected=%v", err, tc.shouldError)
170 return
171 }
172
173 require.Equal(t, tc.output, output, "git.CommitMessage() returned wrong output")
174 })
175 }
176}
177