working on list tests, need to debug

This commit is contained in:
vilmibm 2020-09-15 09:39:30 -05:00
parent 00b4eab573
commit 1887fc07c9
5 changed files with 89 additions and 2 deletions

View file

@ -26,6 +26,9 @@ func listGists(client *http.Client, hostname string, limit int, visibility strin
return nil, err
}
// TODO in tests the api call is matching properly and encoding json properly but i'm getting no
// result and no parse error, wtf?
gists := []shared.Gist{}
for _, gist := range result {

View file

@ -16,6 +16,8 @@ type ListOptions struct {
IO *iostreams.IOStreams
HttpClient func() (*http.Client, error)
Since func(t time.Time) time.Duration
Limit int
Visibility string // all, secret, public
}
@ -24,6 +26,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
opts := &ListOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Since: func(t time.Time) time.Duration {
return time.Since(t)
},
}
cmd := &cobra.Command{
@ -88,7 +93,7 @@ func listRun(opts *ListOptions) error {
visColor = cs.Red
}
updatedAt := utils.FuzzyAgo(time.Since(gist.UpdatedAt))
updatedAt := utils.FuzzyAgo(opts.Since(gist.UpdatedAt))
tp.AddField(gist.ID, nil, nil)
tp.AddField(gist.Description, nil, cs.Bold)
tp.AddField(utils.Pluralize(fileCount, "file"), nil, nil)

View file

@ -2,9 +2,14 @@ package list
import (
"bytes"
"net/http"
"testing"
"time"
"github.com/cli/cli/pkg/cmd/gist/shared"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/httpmock"
"github.com/cli/cli/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
@ -84,3 +89,74 @@ func TestNewCmdList(t *testing.T) {
}
// TODO execution tests
func Test_listRun(t *testing.T) {
tests := []struct {
name string
opts *ListOptions
wantOut string
stubs func(*httpmock.Registry)
}{
{
name: "no gists",
opts: &ListOptions{},
stubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("GET", "gists"),
httpmock.JSONResponse([]shared.Gist{}))
},
wantOut: "",
},
{
name: "default behavior",
opts: &ListOptions{},
wantOut: "TODO",
},
// TODO public filter
// TODO secret filter
// TODO limit specified
}
for _, tt := range tests {
reg := &httpmock.Registry{}
if tt.stubs == nil {
reg.Register(httpmock.REST("GET", "gists"),
httpmock.JSONResponse([]shared.Gist{
{
ID: "1234567890",
Description: "",
Files: map[string]*shared.GistFile{
"cool.txt": {
Content: "lol",
},
},
Public: true,
UpdatedAt: time.Time{},
},
}))
} else {
tt.stubs(reg)
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
tt.opts.Since = func(t time.Time) time.Duration {
d, _ := time.ParseDuration("6h")
return d
}
io, _, stdout, _ := iostreams.Test()
tt.opts.IO = io
t.Run(tt.name, func(t *testing.T) {
err := listRun(tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.wantOut, stdout.String())
reg.Verify(t)
})
}
}

View file

@ -22,7 +22,7 @@ type Gist struct {
Description string `json:"description"`
Files map[string]*GistFile `json:"files"`
UpdatedAt time.Time `json:"updated_at"`
Public bool
Public bool `json:"public"`
}
func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {

View file

@ -3,6 +3,7 @@ package httpmock
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
@ -86,6 +87,8 @@ func StatusStringResponse(status int, body string) Responder {
func JSONResponse(body interface{}) Responder {
return func(req *http.Request) (*http.Response, error) {
b, _ := json.Marshal(body)
fmt.Printf("DEBUG %#v\n", "COOOOOL")
fmt.Printf("DEBUG %#v\n", string(b))
return httpResponse(200, req, bytes.NewBuffer(b)), nil
}
}