Skip to content

Commit 1887fc0

Browse files
committed
working on list tests, need to debug
1 parent 00b4eab commit 1887fc0

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

pkg/cmd/gist/list/http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func listGists(client *http.Client, hostname string, limit int, visibility strin
2626
return nil, err
2727
}
2828

29+
// TODO in tests the api call is matching properly and encoding json properly but i'm getting no
30+
// result and no parse error, wtf?
31+
2932
gists := []shared.Gist{}
3033

3134
for _, gist := range result {

pkg/cmd/gist/list/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type ListOptions struct {
1616
IO *iostreams.IOStreams
1717
HttpClient func() (*http.Client, error)
1818

19+
Since func(t time.Time) time.Duration
20+
1921
Limit int
2022
Visibility string // all, secret, public
2123
}
@@ -24,6 +26,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
2426
opts := &ListOptions{
2527
IO: f.IOStreams,
2628
HttpClient: f.HttpClient,
29+
Since: func(t time.Time) time.Duration {
30+
return time.Since(t)
31+
},
2732
}
2833

2934
cmd := &cobra.Command{
@@ -88,7 +93,7 @@ func listRun(opts *ListOptions) error {
8893
visColor = cs.Red
8994
}
9095

91-
updatedAt := utils.FuzzyAgo(time.Since(gist.UpdatedAt))
96+
updatedAt := utils.FuzzyAgo(opts.Since(gist.UpdatedAt))
9297
tp.AddField(gist.ID, nil, nil)
9398
tp.AddField(gist.Description, nil, cs.Bold)
9499
tp.AddField(utils.Pluralize(fileCount, "file"), nil, nil)

pkg/cmd/gist/list/list_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package list
22

33
import (
44
"bytes"
5+
"net/http"
56
"testing"
7+
"time"
68

9+
"github.com/cli/cli/pkg/cmd/gist/shared"
710
"github.com/cli/cli/pkg/cmdutil"
11+
"github.com/cli/cli/pkg/httpmock"
12+
"github.com/cli/cli/pkg/iostreams"
813
"github.com/google/shlex"
914
"github.com/stretchr/testify/assert"
1015
)
@@ -84,3 +89,74 @@ func TestNewCmdList(t *testing.T) {
8489
}
8590

8691
// TODO execution tests
92+
93+
func Test_listRun(t *testing.T) {
94+
tests := []struct {
95+
name string
96+
opts *ListOptions
97+
wantOut string
98+
stubs func(*httpmock.Registry)
99+
}{
100+
{
101+
name: "no gists",
102+
opts: &ListOptions{},
103+
stubs: func(reg *httpmock.Registry) {
104+
reg.Register(httpmock.REST("GET", "gists"),
105+
httpmock.JSONResponse([]shared.Gist{}))
106+
107+
},
108+
wantOut: "",
109+
},
110+
111+
{
112+
name: "default behavior",
113+
opts: &ListOptions{},
114+
wantOut: "TODO",
115+
},
116+
// TODO public filter
117+
// TODO secret filter
118+
// TODO limit specified
119+
}
120+
121+
for _, tt := range tests {
122+
reg := &httpmock.Registry{}
123+
if tt.stubs == nil {
124+
reg.Register(httpmock.REST("GET", "gists"),
125+
httpmock.JSONResponse([]shared.Gist{
126+
{
127+
ID: "1234567890",
128+
Description: "",
129+
Files: map[string]*shared.GistFile{
130+
"cool.txt": {
131+
Content: "lol",
132+
},
133+
},
134+
Public: true,
135+
UpdatedAt: time.Time{},
136+
},
137+
}))
138+
} else {
139+
tt.stubs(reg)
140+
}
141+
142+
tt.opts.HttpClient = func() (*http.Client, error) {
143+
return &http.Client{Transport: reg}, nil
144+
}
145+
146+
tt.opts.Since = func(t time.Time) time.Duration {
147+
d, _ := time.ParseDuration("6h")
148+
return d
149+
}
150+
151+
io, _, stdout, _ := iostreams.Test()
152+
tt.opts.IO = io
153+
154+
t.Run(tt.name, func(t *testing.T) {
155+
err := listRun(tt.opts)
156+
assert.NoError(t, err)
157+
158+
assert.Equal(t, tt.wantOut, stdout.String())
159+
reg.Verify(t)
160+
})
161+
}
162+
}

pkg/cmd/gist/shared/shared.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Gist struct {
2222
Description string `json:"description"`
2323
Files map[string]*GistFile `json:"files"`
2424
UpdatedAt time.Time `json:"updated_at"`
25-
Public bool
25+
Public bool `json:"public"`
2626
}
2727

2828
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {

pkg/httpmock/stub.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httpmock
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"io/ioutil"
89
"net/http"
@@ -86,6 +87,8 @@ func StatusStringResponse(status int, body string) Responder {
8687
func JSONResponse(body interface{}) Responder {
8788
return func(req *http.Request) (*http.Response, error) {
8889
b, _ := json.Marshal(body)
90+
fmt.Printf("DEBUG %#v\n", "COOOOOL")
91+
fmt.Printf("DEBUG %#v\n", string(b))
8992
return httpResponse(200, req, bytes.NewBuffer(b)), nil
9093
}
9194
}

0 commit comments

Comments
 (0)