From 15cf786c5a253601ace15885b8380e0273931a0e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 15 Sep 2020 17:20:36 -0500 Subject: [PATCH] wip tests --- pkg/cmd/gist/edit/edit_test.go | 126 +++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index eab8c214e..8b39e95cd 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -2,9 +2,17 @@ package edit import ( "bytes" + "encoding/json" + "io/ioutil" + "net/http" "testing" + "github.com/cli/cli/internal/config" + "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/cli/cli/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -58,8 +66,116 @@ func TestNewCmdEdit(t *testing.T) { } } -// TODO execution tests -// TODO no such gist -// TODO one files -// TODO multiple files, submit -// TODO multiple files, cancel +func Test_editRun(t *testing.T) { + tests := []struct { + name string + opts *EditOptions + gist *shared.Gist + httpStubs func(*httpmock.Registry) + askStubs func(*prompt.AskStubber) + nontty bool + wantErr bool + wantParams map[string]interface{} + }{ + { + name: "no such gist", + wantErr: true, + }, + { + name: "one file", + gist: &shared.Gist{ + ID: "1234", + Files: map[string]*shared.GistFile{ + "cicada.txt": { + Filename: "cicada.txt", + Content: "bwhiizzzbwhuiiizzzz", + Type: "text/plain", + }, + }, + }, + //askStubs: func(as *prompt.AskStubber) { + // as.StubOne("new file content") + //}, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.REST("POST", "gists/1234"), + httpmock.StatusStringResponse(201, "{}")) + }, + wantParams: map[string]interface{}{ + "description": "", + "updated_at": "0001-01-01T00:00:00Z", + "public": false, + "files": map[string]interface{}{ + "cicada.txt": map[string]interface{}{ + "content": "new file content", + "filename": "cicada.txt", + "type": "text/plain", + }, + }, + }, + }, + // TODO multiple files, submit + // TODO multiple files, cancel + } + + for _, tt := range tests { + reg := &httpmock.Registry{} + if tt.gist == nil { + reg.Register(httpmock.REST("GET", "gists/1234"), + httpmock.StatusStringResponse(404, "Not Found")) + } else { + reg.Register(httpmock.REST("GET", "gists/1234"), + httpmock.JSONResponse(tt.gist)) + } + + if tt.httpStubs != nil { + tt.httpStubs(reg) + } + + as, teardown := prompt.InitAskStubber() + defer teardown() + if tt.askStubs != nil { + tt.askStubs(as) + } + + if tt.opts == nil { + tt.opts = &EditOptions{} + } + + tt.opts.Edit = func(_, _, _ string, _ *iostreams.IOStreams) (string, error) { + return "new file content", nil + } + + tt.opts.HttpClient = func() (*http.Client, error) { + return &http.Client{Transport: reg}, nil + } + io, _, _, _ := iostreams.Test() + io.SetStdoutTTY(!tt.nontty) + io.SetStdinTTY(!tt.nontty) + tt.opts.IO = io + + tt.opts.Selector = "1234" + + tt.opts.Config = func() (config.Config, error) { + return config.NewBlankConfig(), nil + } + + t.Run(tt.name, func(t *testing.T) { + err := editRun(tt.opts) + if tt.wantErr { + assert.Error(t, err) + return + } + assert.NoError(t, err) + + reg.Verify(t) + + bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) + reqBody := make(map[string]interface{}) + err = json.Unmarshal(bodyBytes, &reqBody) + if err != nil { + t.Fatalf("error decoding JSON: %v", err) + } + assert.Equal(t, tt.wantParams, reqBody) + }) + } +}