forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_test.go
More file actions
133 lines (126 loc) · 3.14 KB
/
list_test.go
File metadata and controls
133 lines (126 loc) · 3.14 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
package list
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
)
func TestListRun(t *testing.T) {
tests := []struct {
name string
opts ListOptions
isTTY bool
wantStdout string
wantStderr string
wantErr bool
}{
{
name: "list tty",
opts: ListOptions{
HTTPClient: func() (*http.Client, error) {
createdAt := time.Now().Add(time.Duration(-24) * time.Hour)
reg := &httpmock.Registry{}
reg.Register(
httpmock.REST("GET", "user/keys"),
httpmock.StringResponse(fmt.Sprintf(`[
{
"id": 1234,
"key": "ssh-rsa AAAABbBB123",
"title": "Mac",
"created_at": "%[1]s"
},
{
"id": 5678,
"key": "ssh-rsa EEEEEEEK247",
"title": "hubot@Windows",
"created_at": "%[1]s"
}
]`, createdAt.Format(time.RFC3339))),
)
return &http.Client{Transport: reg}, nil
},
},
isTTY: true,
wantStdout: heredoc.Doc(`
Mac ssh-rsa AAAABbBB123 1d
hubot@Windows ssh-rsa EEEEEEEK247 1d
`),
wantStderr: "",
},
{
name: "list non-tty",
opts: ListOptions{
HTTPClient: func() (*http.Client, error) {
createdAt, _ := time.Parse(time.RFC3339, "2020-08-31T15:44:24+02:00")
reg := &httpmock.Registry{}
reg.Register(
httpmock.REST("GET", "user/keys"),
httpmock.StringResponse(fmt.Sprintf(`[
{
"id": 1234,
"key": "ssh-rsa AAAABbBB123",
"title": "Mac",
"created_at": "%[1]s"
},
{
"id": 5678,
"key": "ssh-rsa EEEEEEEK247",
"title": "hubot@Windows",
"created_at": "%[1]s"
}
]`, createdAt.Format(time.RFC3339))),
)
return &http.Client{Transport: reg}, nil
},
},
isTTY: false,
wantStdout: heredoc.Doc(`
Mac ssh-rsa AAAABbBB123 2020-08-31T15:44:24+02:00
hubot@Windows ssh-rsa EEEEEEEK247 2020-08-31T15:44:24+02:00
`),
wantStderr: "",
},
{
name: "no keys",
opts: ListOptions{
HTTPClient: func() (*http.Client, error) {
reg := &httpmock.Registry{}
reg.Register(
httpmock.REST("GET", "user/keys"),
httpmock.StringResponse(`[]`),
)
return &http.Client{Transport: reg}, nil
},
},
wantStdout: "",
wantStderr: "No SSH keys present in GitHub account.\n",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(tt.isTTY)
io.SetStdinTTY(tt.isTTY)
io.SetStderrTTY(tt.isTTY)
opts := tt.opts
opts.IO = io
opts.Config = func() (config.Config, error) { return config.NewBlankConfig(), nil }
err := listRun(&opts)
if (err != nil) != tt.wantErr {
t.Errorf("listRun() return error: %v", err)
return
}
if stdout.String() != tt.wantStdout {
t.Errorf("wants stdout %q, got %q", tt.wantStdout, stdout.String())
}
if stderr.String() != tt.wantStderr {
t.Errorf("wants stderr %q, got %q", tt.wantStderr, stderr.String())
}
})
}
}