Skip to content

Commit ba46362

Browse files
committed
add createRun tests
1 parent 5085a5e commit ba46362

File tree

2 files changed

+95
-58
lines changed

2 files changed

+95
-58
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"io/ioutil"
88
"net/http"
9-
"os"
109
"path"
1110
"strings"
1211

@@ -93,7 +92,7 @@ func createRun(opts *CreateOptions) error {
9392
fileArgs = []string{"-"}
9493
}
9594

96-
files, err := processFiles(os.Stdin, fileArgs)
95+
files, err := processFiles(opts.IO.In, fileArgs)
9796
if err != nil {
9897
return fmt.Errorf("failed to collect files for posting: %w", err)
9998
}

pkg/cmd/gist/create/create_test.go

Lines changed: 94 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package create
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io/ioutil"
67
"net/http"
78
"strings"
@@ -14,40 +15,6 @@ import (
1415
"github.com/stretchr/testify/assert"
1516
)
1617

17-
// func TestGistCreate(t *testing.T) {
18-
// initBlankContext("", "OWNER/REPO", "trunk")
19-
20-
// http := initFakeHTTP()
21-
// http.Register(httpmock.REST("POST", "gists"), httpmock.StringResponse(`
22-
// {
23-
// "html_url": "https://gist.github.com/aa5a315d61ae9438b18d"
24-
// }
25-
// `))
26-
27-
// output, err := RunCommand(`gist create "../test/fixtures/gistCreate.json" -d "Gist description" --public`)
28-
// assert.NoError(t, err)
29-
30-
// bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
31-
// reqBody := make(map[string]interface{})
32-
// err = json.Unmarshal(bodyBytes, &reqBody)
33-
// if err != nil {
34-
// t.Fatalf("error decoding JSON: %v", err)
35-
// }
36-
37-
// expectParams := map[string]interface{}{
38-
// "description": "Gist description",
39-
// "files": map[string]interface{}{
40-
// "gistCreate.json": map[string]interface{}{
41-
// "content": "{}",
42-
// },
43-
// },
44-
// "public": true,
45-
// }
46-
47-
// assert.Equal(t, expectParams, reqBody)
48-
// assert.Equal(t, "https://gist.github.com/aa5a315d61ae9438b18d\n", output.String())
49-
// }
50-
5118
func Test_processFiles(t *testing.T) {
5219
fakeStdin := strings.NewReader("hey cool how is it going")
5320
files, err := processFiles(ioutil.NopCloser(fakeStdin), []string{"-"})
@@ -169,50 +136,121 @@ func TestNewCmdCreate(t *testing.T) {
169136
}
170137
}
171138

139+
func testIOWithStdin(stdinContent string) *iostreams.IOStreams {
140+
tio, stdin, _, _ := iostreams.Test()
141+
stdin.WriteString(stdinContent)
142+
return tio
143+
}
144+
172145
func testIO() *iostreams.IOStreams {
173146
tio, _, _, _ := iostreams.Test()
174147
return tio
175148
}
176149

177150
func Test_createRun(t *testing.T) {
178151
tests := []struct {
179-
name string
180-
opts *CreateOptions
181-
want func(t *testing.T)
182-
wantErr bool
152+
name string
153+
opts *CreateOptions
154+
wantOut string
155+
wantParams map[string]interface{}
156+
wantErr bool
183157
}{
184-
// TODO stdin passed as -
185-
// TODO stdin as |
186-
// TODO multiple files
187-
// TODO description
188-
// TODO public
189158
{
190-
name: "basic",
159+
name: "public",
191160
opts: &CreateOptions{
192161
IO: testIO(),
193-
Filenames: []string{"-"},
194-
HttpClient: func() (*http.Client, error) {
195-
reg := &httpmock.Registry{}
196-
reg.Register(httpmock.REST("POST", "gists"), httpmock.StringResponse(`
197-
{
198-
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d"
199-
}`))
200-
201-
return &http.Client{Transport: reg}, nil
162+
Public: true,
163+
Filenames: []string{"../../../../test/fixtures/gistCreate.json"},
164+
},
165+
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
166+
wantErr: false,
167+
wantParams: map[string]interface{}{
168+
"public": true,
169+
"files": map[string]interface{}{
170+
"gistCreate.json": map[string]interface{}{
171+
"content": "{}",
172+
},
202173
},
203174
},
204-
want: func(t *testing.T) {
205-
175+
},
176+
{
177+
name: "with description",
178+
opts: &CreateOptions{
179+
IO: testIO(),
180+
Description: "an incredibly interesting gist",
181+
Filenames: []string{"../../../../test/fixtures/gistCreate.json"},
182+
},
183+
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
184+
wantErr: false,
185+
wantParams: map[string]interface{}{
186+
"description": "an incredibly interesting gist",
187+
"files": map[string]interface{}{
188+
"gistCreate.json": map[string]interface{}{
189+
"content": "{}",
190+
},
191+
},
192+
},
193+
},
194+
{
195+
name: "multiple files",
196+
opts: &CreateOptions{
197+
IO: testIOWithStdin("cool stdin content"),
198+
Filenames: []string{"../../../../test/fixtures/gistCreate.json", "-"},
206199
},
200+
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
207201
wantErr: false,
202+
wantParams: map[string]interface{}{
203+
"files": map[string]interface{}{
204+
"gistCreate.json": map[string]interface{}{
205+
"content": "{}",
206+
},
207+
"gistfile1.txt": map[string]interface{}{
208+
"content": "cool stdin content",
209+
},
210+
},
211+
},
212+
},
213+
{
214+
name: "stdin arg",
215+
opts: &CreateOptions{
216+
IO: testIOWithStdin("cool stdin content"),
217+
Filenames: []string{"-"},
218+
},
219+
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
220+
wantErr: false,
221+
wantParams: map[string]interface{}{
222+
"files": map[string]interface{}{
223+
"gistfile0.txt": map[string]interface{}{
224+
"content": "cool stdin content",
225+
},
226+
},
227+
},
208228
},
209229
}
210230
for _, tt := range tests {
231+
reg := &httpmock.Registry{}
232+
reg.Register(httpmock.REST("POST", "gists"), httpmock.StringResponse(`
233+
{ "html_url": "https://gist.github.com/aa5a315d61ae9438b18d"}`))
234+
235+
mockClient := func() (*http.Client, error) {
236+
return &http.Client{Transport: reg}, nil
237+
}
238+
239+
tt.opts.HttpClient = mockClient
240+
211241
t.Run(tt.name, func(t *testing.T) {
212242
if err := createRun(tt.opts); (err != nil) != tt.wantErr {
213243
t.Errorf("createRun() error = %v, wantErr %v", err, tt.wantErr)
214244
}
215-
tt.want(t)
245+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body)
246+
reqBody := make(map[string]interface{})
247+
err := json.Unmarshal(bodyBytes, &reqBody)
248+
if err != nil {
249+
t.Fatalf("error decoding JSON: %v", err)
250+
}
251+
assert.Equal(t, tt.wantOut, tt.opts.IO.Out.(*bytes.Buffer).String())
252+
assert.Equal(t, tt.wantParams, reqBody)
253+
reg.Verify(t)
216254
})
217255
}
218256
}

0 commit comments

Comments
 (0)