forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_repo_test.go
More file actions
126 lines (119 loc) · 3.58 KB
/
local_repo_test.go
File metadata and controls
126 lines (119 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package git
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHelperProcess(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
args := os.Args[3:]
switch strings.Join(args, " ") {
case "git push origin HEAD:no-output":
case "git push origin HEAD:some-output":
fmt.Fprintln(os.Stdout, "STDOUT")
fmt.Fprintln(os.Stderr, "STDERR")
case "git push --progress origin HEAD:feature":
fmt.Fprint(os.Stderr, "progress: 0%\r")
fmt.Fprint(os.Stderr, "progress: 33%\r")
fmt.Fprint(os.Stderr, "progress: 66%\r")
fmt.Fprint(os.Stderr, "progress: 100%, done.\n")
fmt.Fprint(os.Stderr, "remote: \n")
fmt.Fprint(os.Stderr, "remote: Create a pull request for 'feature' on GitHub by visiting: \nremote: https://github.com/owner/repo/pull/new/feature \nremote: \n")
fmt.Fprint(os.Stderr, "To https://github.com/owner/repo.git\n * [new branch] HEAD -> feature\n")
case "git push origin HEAD:crlf":
fmt.Fprint(os.Stderr, "one\r\n")
fmt.Fprint(os.Stderr, "two\r\n")
fmt.Fprint(os.Stderr, "three\r")
fmt.Fprint(os.Stderr, "four")
default:
fmt.Fprintf(os.Stderr, "unrecognized arguments: %#v\n", args)
os.Exit(1)
}
os.Exit(0)
}
func Test_gitClientExec_Push(t *testing.T) {
gitCommand := func(ctx context.Context, args ...string) (*exec.Cmd, error) {
args = append([]string{"-test.run=TestHelperProcess", "--", "git"}, args...)
cmd := exec.Command(os.Args[0], args...)
cmd.Env = []string{"GH_WANT_HELPER_PROCESS=1"}
return cmd, nil
}
tests := []struct {
name string
args PushOptions
wantStdout string
wantStderr *logWriter
wantErr bool
}{
{
name: "no output",
args: PushOptions{RemoteName: "origin", TargetBranch: "no-output"},
wantStdout: "",
wantStderr: &logWriter{},
},
{
name: "some output",
args: PushOptions{RemoteName: "origin", TargetBranch: "some-output"},
wantStdout: "STDOUT\n",
wantStderr: &logWriter{"STDERR\n"},
},
{
name: "progress output",
args: PushOptions{RemoteName: "origin", TargetBranch: "feature", ShowProgress: true},
wantStdout: "",
wantStderr: &logWriter{
"progress: 0%\r",
"progress: 33%\r",
"progress: 66%\r",
"progress: 100%, done.\n",
"remote: \n",
"remote: \n",
"To https://github.com/owner/repo.git\n",
" * [new branch] HEAD -> feature\n",
},
},
{
name: "cr-lf",
args: PushOptions{RemoteName: "origin", TargetBranch: "crlf"},
wantStdout: "",
wantStderr: &logWriter{"one\r\n", "two\r\n", "three\r", "four"},
},
{
name: "failure",
args: PushOptions{RemoteName: "nonexist", TargetBranch: "feature"},
wantErr: true,
wantStderr: &logWriter{"unrecognized arguments: []string{\"git\", \"push\", \"nonexist\", \"HEAD:feature\"}\n"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &logWriter{}
g := &LocalRepo{
gitCommandInit: gitCommand,
Stdout: stdout,
Stderr: stderr,
}
if err := g.Push(context.Background(), tt.args); (err != nil) != tt.wantErr {
t.Errorf("gitClientExec.Push() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotStdout := stdout.String(); gotStdout != tt.wantStdout {
t.Errorf("gitClientExec.Push() stdout = %q, want %q", gotStdout, tt.wantStdout)
}
assert.Equal(t, tt.wantStderr, stderr)
})
}
}
type logWriter []string
func (w *logWriter) Write(p []byte) (int, error) {
*w = append(*w, string(p))
return len(p), nil
}