From 1887fc07c9ae697004ff126f00cd4ffa4a0bebd9 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 15 Sep 2020 09:39:30 -0500 Subject: [PATCH] working on list tests, need to debug --- pkg/cmd/gist/list/http.go | 3 ++ pkg/cmd/gist/list/list.go | 7 +++- pkg/cmd/gist/list/list_test.go | 76 ++++++++++++++++++++++++++++++++++ pkg/cmd/gist/shared/shared.go | 2 +- pkg/httpmock/stub.go | 3 ++ 5 files changed, 89 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/gist/list/http.go b/pkg/cmd/gist/list/http.go index abc497423..4e5445ad8 100644 --- a/pkg/cmd/gist/list/http.go +++ b/pkg/cmd/gist/list/http.go @@ -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 { diff --git a/pkg/cmd/gist/list/list.go b/pkg/cmd/gist/list/list.go index dbb9969fb..ec9e45c0d 100644 --- a/pkg/cmd/gist/list/list.go +++ b/pkg/cmd/gist/list/list.go @@ -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) diff --git a/pkg/cmd/gist/list/list_test.go b/pkg/cmd/gist/list/list_test.go index b2e50e99c..dcae4db60 100644 --- a/pkg/cmd/gist/list/list_test.go +++ b/pkg/cmd/gist/list/list_test.go @@ -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) + }) + } +} diff --git a/pkg/cmd/gist/shared/shared.go b/pkg/cmd/gist/shared/shared.go index 75e24c197..95d8bae31 100644 --- a/pkg/cmd/gist/shared/shared.go +++ b/pkg/cmd/gist/shared/shared.go @@ -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) { diff --git a/pkg/httpmock/stub.go b/pkg/httpmock/stub.go index a1dcefaa3..e4572efe9 100644 --- a/pkg/httpmock/stub.go +++ b/pkg/httpmock/stub.go @@ -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 } }