X Tutup
Skip to content

Commit 2bdffc8

Browse files
committed
Isolate flag processing tests in repo list
1 parent 1fa763f commit 2bdffc8

File tree

1 file changed

+135
-31
lines changed

1 file changed

+135
-31
lines changed

pkg/cmd/repo/list/list_test.go

Lines changed: 135 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,143 @@ import (
1414
"github.com/cli/cli/test"
1515
"github.com/google/shlex"
1616
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
1718
)
1819

20+
func TestNewCmdList(t *testing.T) {
21+
tests := []struct {
22+
name string
23+
cli string
24+
wants ListOptions
25+
wantsErr string
26+
}{
27+
{
28+
name: "no arguments",
29+
cli: "",
30+
wants: ListOptions{
31+
Limit: 30,
32+
Owner: "",
33+
Visibility: "",
34+
Fork: false,
35+
Source: false,
36+
},
37+
},
38+
{
39+
name: "with owner",
40+
cli: "monalisa",
41+
wants: ListOptions{
42+
Limit: 30,
43+
Owner: "monalisa",
44+
Visibility: "",
45+
Fork: false,
46+
Source: false,
47+
},
48+
},
49+
{
50+
name: "with limit",
51+
cli: "-L 101",
52+
wants: ListOptions{
53+
Limit: 101,
54+
Owner: "",
55+
Visibility: "",
56+
Fork: false,
57+
Source: false,
58+
},
59+
},
60+
{
61+
name: "only public",
62+
cli: "--public",
63+
wants: ListOptions{
64+
Limit: 30,
65+
Owner: "",
66+
Visibility: "public",
67+
Fork: false,
68+
Source: false,
69+
},
70+
},
71+
{
72+
name: "only private",
73+
cli: "--private",
74+
wants: ListOptions{
75+
Limit: 30,
76+
Owner: "",
77+
Visibility: "private",
78+
Fork: false,
79+
Source: false,
80+
},
81+
},
82+
{
83+
name: "only forks",
84+
cli: "--fork",
85+
wants: ListOptions{
86+
Limit: 30,
87+
Owner: "",
88+
Visibility: "",
89+
Fork: true,
90+
Source: false,
91+
},
92+
},
93+
{
94+
name: "only sources",
95+
cli: "--source",
96+
wants: ListOptions{
97+
Limit: 30,
98+
Owner: "",
99+
Visibility: "",
100+
Fork: false,
101+
Source: true,
102+
},
103+
},
104+
{
105+
name: "no public and private",
106+
cli: "--public --private",
107+
wantsErr: "specify only one of `--public` or `--private`",
108+
},
109+
{
110+
name: "no forks with sources",
111+
cli: "--fork --source",
112+
wantsErr: "specify only one of `--source` or `--fork`",
113+
},
114+
{
115+
name: "too many arguments",
116+
cli: "monalisa hubot",
117+
wantsErr: "accepts at most 1 arg(s), received 2",
118+
},
119+
}
120+
121+
for _, tt := range tests {
122+
t.Run(tt.name, func(t *testing.T) {
123+
f := &cmdutil.Factory{}
124+
125+
argv, err := shlex.Split(tt.cli)
126+
assert.NoError(t, err)
127+
128+
var gotOpts *ListOptions
129+
cmd := NewCmdList(f, func(opts *ListOptions) error {
130+
gotOpts = opts
131+
return nil
132+
})
133+
cmd.SetArgs(argv)
134+
cmd.SetIn(&bytes.Buffer{})
135+
cmd.SetOut(&bytes.Buffer{})
136+
cmd.SetErr(&bytes.Buffer{})
137+
138+
_, err = cmd.ExecuteC()
139+
if tt.wantsErr != "" {
140+
assert.EqualError(t, err, tt.wantsErr)
141+
return
142+
}
143+
require.NoError(t, err)
144+
145+
assert.Equal(t, tt.wants.Limit, gotOpts.Limit)
146+
assert.Equal(t, tt.wants.Owner, gotOpts.Owner)
147+
assert.Equal(t, tt.wants.Visibility, gotOpts.Visibility)
148+
assert.Equal(t, tt.wants.Fork, gotOpts.Fork)
149+
assert.Equal(t, tt.wants.Source, gotOpts.Source)
150+
})
151+
}
152+
}
153+
19154
func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
20155
io, _, stdout, stderr := iostreams.Test()
21156
io.SetStdoutTTY(isTTY)
@@ -147,34 +282,3 @@ func TestRepoList_filtering(t *testing.T) {
147282
assert.Equal(t, "", output.Stderr())
148283
assert.Equal(t, "\nNo results match your search\n\n", output.String())
149284
}
150-
151-
func TestRepoList_withInvalidFlagCombinations(t *testing.T) {
152-
tests := []struct {
153-
name string
154-
cli string
155-
wantStderr string
156-
}{
157-
{
158-
name: "invalid limit",
159-
cli: "--limit 0",
160-
wantStderr: "invalid limit: 0",
161-
},
162-
{
163-
name: "both private and public",
164-
cli: "--private --public",
165-
wantStderr: "specify only one of `--public` or `--private`",
166-
},
167-
{
168-
name: "both source and fork",
169-
cli: "--source --fork",
170-
wantStderr: "specify only one of `--source` or `--fork`",
171-
},
172-
}
173-
174-
for _, tt := range tests {
175-
httpReg := &httpmock.Registry{}
176-
177-
_, err := runCommand(httpReg, true, tt.cli)
178-
assert.EqualError(t, err, tt.wantStderr)
179-
}
180-
}

0 commit comments

Comments
 (0)
X Tutup