Skip to content

Commit 9b45112

Browse files
committed
Add tests for release list/delete
1 parent 9a4a5d8 commit 9b45112

File tree

4 files changed

+331
-2
lines changed

4 files changed

+331
-2
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package delete
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/cli/cli/internal/ghrepo"
11+
"github.com/cli/cli/pkg/cmdutil"
12+
"github.com/cli/cli/pkg/httpmock"
13+
"github.com/cli/cli/pkg/iostreams"
14+
"github.com/google/shlex"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func Test_NewCmdReady(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
args string
23+
isTTY bool
24+
want DeleteOptions
25+
wantErr string
26+
}{
27+
{
28+
name: "number argument",
29+
args: "v1.2.3",
30+
isTTY: true,
31+
want: DeleteOptions{
32+
TagName: "v1.2.3",
33+
SkipConfirm: false,
34+
},
35+
},
36+
{
37+
name: "skip confirm",
38+
args: "v1.2.3 -y",
39+
isTTY: true,
40+
want: DeleteOptions{
41+
TagName: "v1.2.3",
42+
SkipConfirm: true,
43+
},
44+
},
45+
{
46+
name: "no arguments",
47+
args: "",
48+
isTTY: true,
49+
wantErr: "accepts 1 arg(s), received 0",
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
io, _, _, _ := iostreams.Test()
55+
io.SetStdoutTTY(tt.isTTY)
56+
io.SetStdinTTY(tt.isTTY)
57+
io.SetStderrTTY(tt.isTTY)
58+
59+
f := &cmdutil.Factory{
60+
IOStreams: io,
61+
}
62+
63+
var opts *DeleteOptions
64+
cmd := NewCmdDelete(f, func(o *DeleteOptions) error {
65+
opts = o
66+
return nil
67+
})
68+
cmd.PersistentFlags().StringP("repo", "R", "", "")
69+
70+
argv, err := shlex.Split(tt.args)
71+
require.NoError(t, err)
72+
cmd.SetArgs(argv)
73+
74+
cmd.SetIn(&bytes.Buffer{})
75+
cmd.SetOut(ioutil.Discard)
76+
cmd.SetErr(ioutil.Discard)
77+
78+
_, err = cmd.ExecuteC()
79+
if tt.wantErr != "" {
80+
require.EqualError(t, err, tt.wantErr)
81+
return
82+
} else {
83+
require.NoError(t, err)
84+
}
85+
86+
assert.Equal(t, tt.want.TagName, opts.TagName)
87+
assert.Equal(t, tt.want.SkipConfirm, opts.SkipConfirm)
88+
})
89+
}
90+
}
91+
92+
func Test_deleteRun(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
isTTY bool
96+
opts DeleteOptions
97+
wantErr string
98+
wantStdout string
99+
wantStderr string
100+
}{
101+
{
102+
name: "skipping confirmation",
103+
isTTY: true,
104+
opts: DeleteOptions{
105+
TagName: "v1.2.3",
106+
SkipConfirm: true,
107+
},
108+
wantStdout: ``,
109+
wantStderr: heredoc.Doc(`
110+
✓ Deleted release v1.2.3
111+
! Note that the v1.2.3 git tag still remains in the repository
112+
`),
113+
},
114+
{
115+
name: "non-interactive",
116+
isTTY: false,
117+
opts: DeleteOptions{
118+
TagName: "v1.2.3",
119+
SkipConfirm: false,
120+
},
121+
wantStdout: ``,
122+
wantStderr: ``,
123+
},
124+
}
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
io, _, stdout, stderr := iostreams.Test()
128+
io.SetStdoutTTY(tt.isTTY)
129+
io.SetStdinTTY(tt.isTTY)
130+
io.SetStderrTTY(tt.isTTY)
131+
132+
fakeHTTP := &httpmock.Registry{}
133+
fakeHTTP.Register(httpmock.REST("GET", "repos/OWNER/REPO/releases/tags/v1.2.3"), httpmock.StringResponse(`{
134+
"tag_name": "v1.2.3",
135+
"draft": false,
136+
"url": "https://api.github.com/repos/OWNER/REPO/releases/23456"
137+
}`))
138+
fakeHTTP.Register(httpmock.REST("DELETE", "repos/OWNER/REPO/releases/23456"), httpmock.StatusStringResponse(204, ""))
139+
140+
tt.opts.IO = io
141+
tt.opts.HttpClient = func() (*http.Client, error) {
142+
return &http.Client{Transport: fakeHTTP}, nil
143+
}
144+
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
145+
return ghrepo.FromFullName("OWNER/REPO")
146+
}
147+
148+
err := deleteRun(&tt.opts)
149+
if tt.wantErr != "" {
150+
require.EqualError(t, err, tt.wantErr)
151+
return
152+
} else {
153+
require.NoError(t, err)
154+
}
155+
156+
assert.Equal(t, tt.wantStdout, stdout.String())
157+
assert.Equal(t, tt.wantStderr, stderr.String())
158+
})
159+
}
160+
}

pkg/cmd/release/list/list_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package list
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"testing"
9+
"time"
10+
11+
"github.com/MakeNowJust/heredoc"
12+
"github.com/cli/cli/internal/ghrepo"
13+
"github.com/cli/cli/pkg/cmdutil"
14+
"github.com/cli/cli/pkg/httpmock"
15+
"github.com/cli/cli/pkg/iostreams"
16+
"github.com/google/shlex"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
func Test_NewCmdList(t *testing.T) {
22+
tests := []struct {
23+
name string
24+
args string
25+
isTTY bool
26+
want ListOptions
27+
wantErr string
28+
}{
29+
{
30+
name: "no arguments",
31+
args: "",
32+
isTTY: true,
33+
want: ListOptions{
34+
LimitResults: 30,
35+
},
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
io, _, _, _ := iostreams.Test()
41+
io.SetStdoutTTY(tt.isTTY)
42+
io.SetStdinTTY(tt.isTTY)
43+
io.SetStderrTTY(tt.isTTY)
44+
45+
f := &cmdutil.Factory{
46+
IOStreams: io,
47+
}
48+
49+
var opts *ListOptions
50+
cmd := NewCmdList(f, func(o *ListOptions) error {
51+
opts = o
52+
return nil
53+
})
54+
cmd.PersistentFlags().StringP("repo", "R", "", "")
55+
56+
argv, err := shlex.Split(tt.args)
57+
require.NoError(t, err)
58+
cmd.SetArgs(argv)
59+
60+
cmd.SetIn(&bytes.Buffer{})
61+
cmd.SetOut(ioutil.Discard)
62+
cmd.SetErr(ioutil.Discard)
63+
64+
_, err = cmd.ExecuteC()
65+
if tt.wantErr != "" {
66+
require.EqualError(t, err, tt.wantErr)
67+
return
68+
} else {
69+
require.NoError(t, err)
70+
}
71+
72+
assert.Equal(t, tt.want.LimitResults, opts.LimitResults)
73+
})
74+
}
75+
}
76+
77+
func Test_listRun(t *testing.T) {
78+
tests := []struct {
79+
name string
80+
isTTY bool
81+
opts ListOptions
82+
wantErr string
83+
wantStdout string
84+
wantStderr string
85+
}{
86+
{
87+
name: "list releases",
88+
isTTY: true,
89+
opts: ListOptions{
90+
LimitResults: 30,
91+
},
92+
wantStdout: heredoc.Doc(`
93+
v1.1.0 Draft (v1.1.0) about 1 day ago
94+
The big 1.0 Latest (v1.0.0) about 1 day ago
95+
1.0 release candidate Pre-release (v1.0.0-pre.2) about 1 day ago
96+
New features (v0.9.2) about 1 day ago
97+
`),
98+
wantStderr: ``,
99+
},
100+
}
101+
for _, tt := range tests {
102+
t.Run(tt.name, func(t *testing.T) {
103+
io, _, stdout, stderr := iostreams.Test()
104+
io.SetStdoutTTY(tt.isTTY)
105+
io.SetStdinTTY(tt.isTTY)
106+
io.SetStderrTTY(tt.isTTY)
107+
108+
relativeTime := time.Now().Add(time.Duration(-24) * time.Hour)
109+
110+
fakeHTTP := &httpmock.Registry{}
111+
fakeHTTP.Register(httpmock.GraphQL(`\bRepositoryReleaseList\(`), httpmock.StringResponse(fmt.Sprintf(`
112+
{ "data": { "repository": { "releases": {
113+
"nodes": [
114+
{
115+
"name": "",
116+
"tagName": "v1.1.0",
117+
"isDraft": true,
118+
"isPrerelease": false,
119+
"createdAt": "%[1]s",
120+
"publishedAt": "%[1]s"
121+
},
122+
{
123+
"name": "The big 1.0",
124+
"tagName": "v1.0.0",
125+
"isDraft": false,
126+
"isPrerelease": false,
127+
"createdAt": "%[1]s",
128+
"publishedAt": "%[1]s"
129+
},
130+
{
131+
"name": "1.0 release candidate",
132+
"tagName": "v1.0.0-pre.2",
133+
"isDraft": false,
134+
"isPrerelease": true,
135+
"createdAt": "%[1]s",
136+
"publishedAt": "%[1]s"
137+
},
138+
{
139+
"name": "New features",
140+
"tagName": "v0.9.2",
141+
"isDraft": false,
142+
"isPrerelease": false,
143+
"createdAt": "%[1]s",
144+
"publishedAt": "%[1]s"
145+
}
146+
]
147+
} } } }`, relativeTime.Format(time.RFC3339))))
148+
149+
tt.opts.IO = io
150+
tt.opts.HttpClient = func() (*http.Client, error) {
151+
return &http.Client{Transport: fakeHTTP}, nil
152+
}
153+
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
154+
return ghrepo.FromFullName("OWNER/REPO")
155+
}
156+
157+
err := listRun(&tt.opts)
158+
if tt.wantErr != "" {
159+
require.EqualError(t, err, tt.wantErr)
160+
return
161+
} else {
162+
require.NoError(t, err)
163+
}
164+
165+
assert.Equal(t, tt.wantStdout, stdout.String())
166+
assert.Equal(t, tt.wantStderr, stderr.String())
167+
})
168+
}
169+
}

pkg/iostreams/color.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ func (c *ColorScheme) SuccessIcon() string {
8282
}
8383

8484
func (c *ColorScheme) WarningIcon() string {
85-
return c.Yellow("")
85+
return c.Yellow("!")
8686
}

pkg/iostreams/iostreams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (s *IOStreams) TerminalWidth() int {
129129
}
130130

131131
func (s *IOStreams) ColorScheme() *ColorScheme {
132-
return NewColorScheme(s.IsStdoutTTY())
132+
return NewColorScheme(s.ColorEnabled())
133133
}
134134

135135
func System() *IOStreams {

0 commit comments

Comments
 (0)