55 "encoding/json"
66 "io/ioutil"
77 "net/http"
8+ "path/filepath"
89 "testing"
910
1011 "github.com/cli/cli/internal/config"
@@ -15,8 +16,26 @@ import (
1516 "github.com/cli/cli/pkg/prompt"
1617 "github.com/google/shlex"
1718 "github.com/stretchr/testify/assert"
19+ "github.com/stretchr/testify/require"
1820)
1921
22+ func Test_getFilesToAdd (t * testing.T ) {
23+ fileToAdd := filepath .Join (t .TempDir (), "gist-test.txt" )
24+ err := ioutil .WriteFile (fileToAdd , []byte ("hello" ), 0600 )
25+ require .NoError (t , err )
26+
27+ gf , err := getFilesToAdd (fileToAdd )
28+ require .NoError (t , err )
29+
30+ filename := filepath .Base (fileToAdd )
31+ assert .Equal (t , map [string ]* shared.GistFile {
32+ filename : {
33+ Filename : filename ,
34+ Content : "hello" ,
35+ },
36+ }, gf )
37+ }
38+
2039func TestNewCmdEdit (t * testing.T ) {
2140 tests := []struct {
2241 name string
@@ -34,8 +53,16 @@ func TestNewCmdEdit(t *testing.T) {
3453 name : "filename" ,
3554 cli : "123 --filename cool.md" ,
3655 wants : EditOptions {
37- Selector : "123" ,
38- Filename : "cool.md" ,
56+ Selector : "123" ,
57+ EditFilename : "cool.md" ,
58+ },
59+ },
60+ {
61+ name : "add" ,
62+ cli : "123 --add cool.md" ,
63+ wants : EditOptions {
64+ Selector : "123" ,
65+ AddFilename : "cool.md" ,
3966 },
4067 },
4168 }
@@ -60,27 +87,31 @@ func TestNewCmdEdit(t *testing.T) {
6087 _ , err = cmd .ExecuteC ()
6188 assert .NoError (t , err )
6289
63- assert .Equal (t , tt .wants .Filename , gotOpts .Filename )
90+ assert .Equal (t , tt .wants .EditFilename , gotOpts .EditFilename )
91+ assert .Equal (t , tt .wants .AddFilename , gotOpts .AddFilename )
6492 assert .Equal (t , tt .wants .Selector , gotOpts .Selector )
6593 })
6694 }
6795}
6896
6997func Test_editRun (t * testing.T ) {
98+ fileToAdd := filepath .Join (t .TempDir (), "gist-test.txt" )
99+ err := ioutil .WriteFile (fileToAdd , []byte ("hello" ), 0600 )
100+ require .NoError (t , err )
101+
70102 tests := []struct {
71103 name string
72104 opts * EditOptions
73105 gist * shared.Gist
74106 httpStubs func (* httpmock.Registry )
75107 askStubs func (* prompt.AskStubber )
76108 nontty bool
77- wantErr bool
78- wantStderr string
109+ wantErr string
79110 wantParams map [string ]interface {}
80111 }{
81112 {
82113 name : "no such gist" ,
83- wantErr : true ,
114+ wantErr : "gist not found: 1234" ,
84115 },
85116 {
86117 name : "one file" ,
@@ -163,7 +194,7 @@ func Test_editRun(t *testing.T) {
163194 as .StubOne ("unix.md" )
164195 as .StubOne ("Cancel" )
165196 },
166- wantErr : true ,
197+ wantErr : "SilentError" ,
167198 gist : & shared.Gist {
168199 ID : "1234" ,
169200 Files : map [string ]* shared.GistFile {
@@ -208,8 +239,28 @@ func Test_editRun(t *testing.T) {
208239 },
209240 Owner : & shared.GistOwner {Login : "octocat2" },
210241 },
211- wantErr : true ,
212- wantStderr : "You do not own this gist." ,
242+ wantErr : "You do not own this gist." ,
243+ },
244+ {
245+ name : "add file to existing gist" ,
246+ gist : & shared.Gist {
247+ ID : "1234" ,
248+ Files : map [string ]* shared.GistFile {
249+ "sample.txt" : {
250+ Filename : "sample.txt" ,
251+ Content : "bwhiizzzbwhuiiizzzz" ,
252+ Type : "text/plain" ,
253+ },
254+ },
255+ Owner : & shared.GistOwner {Login : "octocat" },
256+ },
257+ httpStubs : func (reg * httpmock.Registry ) {
258+ reg .Register (httpmock .REST ("POST" , "gists/1234" ),
259+ httpmock .StatusStringResponse (201 , "{}" ))
260+ },
261+ opts : & EditOptions {
262+ AddFilename : fileToAdd ,
263+ },
213264 },
214265 }
215266
@@ -246,7 +297,7 @@ func Test_editRun(t *testing.T) {
246297 tt .opts .HttpClient = func () (* http.Client , error ) {
247298 return & http.Client {Transport : reg }, nil
248299 }
249- io , _ , _ , _ := iostreams .Test ()
300+ io , _ , stdout , stderr := iostreams .Test ()
250301 io .SetStdoutTTY (! tt .nontty )
251302 io .SetStdinTTY (! tt .nontty )
252303 tt .opts .IO = io
@@ -260,11 +311,8 @@ func Test_editRun(t *testing.T) {
260311 t .Run (tt .name , func (t * testing.T ) {
261312 err := editRun (tt .opts )
262313 reg .Verify (t )
263- if tt .wantErr {
264- assert .Error (t , err )
265- if tt .wantStderr != "" {
266- assert .EqualError (t , err , tt .wantStderr )
267- }
314+ if tt .wantErr != "" {
315+ assert .EqualError (t , err , tt .wantErr )
268316 return
269317 }
270318 assert .NoError (t , err )
@@ -278,6 +326,9 @@ func Test_editRun(t *testing.T) {
278326 }
279327 assert .Equal (t , tt .wantParams , reqBody )
280328 }
329+
330+ assert .Equal (t , "" , stdout .String ())
331+ assert .Equal (t , "" , stderr .String ())
281332 })
282333 }
283334}
0 commit comments