cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e622a47c6fc350caabb3fe75c25cc3bfbd6feab6

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/git_test.go

177lines · modecode

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