forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_test.go
More file actions
147 lines (123 loc) · 3.44 KB
/
transfer_test.go
File metadata and controls
147 lines (123 loc) · 3.44 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package transfer
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
func runCommand(rt http.RoundTripper, cli string) (*test.CmdOut, error) {
io, _, stdout, stderr := iostreams.Test()
factory := &cmdutil.Factory{
IOStreams: io,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: rt}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
io.SetStdoutTTY(true)
cmd := NewCmdTransfer(factory, nil)
argv, err := shlex.Split(cli)
if err != nil {
return nil, err
}
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(ioutil.Discard)
cmd.SetErr(ioutil.Discard)
_, err = cmd.ExecuteC()
return &test.CmdOut{
OutBuf: stdout,
ErrBuf: stderr,
}, err
}
func TestNewCmdTransfer(t *testing.T) {
tests := []struct {
name string
cli string
wants TransferOptions
wantErr string
}{
{
name: "issue name",
cli: "3252 OWNER/REPO",
wants: TransferOptions{
IssueSelector: "3252",
DestRepoSelector: "OWNER/REPO",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &cmdutil.Factory{}
argv, err := shlex.Split(tt.cli)
assert.NoError(t, err)
var gotOpts *TransferOptions
cmd := NewCmdTransfer(f, func(opts *TransferOptions) error {
gotOpts = opts
return nil
})
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
_, cErr := cmd.ExecuteC()
assert.NoError(t, cErr)
assert.Equal(t, tt.wants.IssueSelector, gotOpts.IssueSelector)
assert.Equal(t, tt.wants.DestRepoSelector, gotOpts.DestRepoSelector)
})
}
}
func Test_transferRun_noflags(t *testing.T) {
http := &httpmock.Registry{}
defer http.Verify(t)
output, err := runCommand(http, "")
if err != nil {
assert.Equal(t, "issue and destination repository are required", err.Error())
}
assert.Equal(t, "", output.String())
}
func Test_transferRunSuccessfulIssueTransfer(t *testing.T) {
http := &httpmock.Registry{}
defer http.Verify(t)
http.Register(
httpmock.GraphQL(`query IssueByNumber\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"hasIssuesEnabled": true,
"issue": { "id": "THE-ID", "number": 1234, "title": "The title of the issue"}
} } }`))
http.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"id": "dest-id",
"name": "REPO1",
"owner": { "login": "OWNER1" },
"viewerPermission": "WRITE",
"hasIssuesEnabled": true
}}}`))
http.Register(
httpmock.GraphQL(`mutation IssueTransfer\b`),
httpmock.GraphQLMutation(`{"data":{"transferIssue":{"issue":{"url":"https://github.com/OWNER1/REPO1/issues/1"}}}}`, func(input map[string]interface{}) {
assert.Equal(t, input["issueId"], "THE-ID")
assert.Equal(t, input["repositoryId"], "dest-id")
}))
output, err := runCommand(http, "1234 OWNER1/REPO1")
if err != nil {
t.Errorf("error running command `issue transfer`: %v", err)
}
assert.Equal(t, "https://github.com/OWNER1/REPO1/issues/1\n", output.String())
}