|
| 1 | +package delete |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "github.com/cli/cli/pkg/cmd/gist/shared" |
| 6 | + "github.com/cli/cli/pkg/cmdutil" |
| 7 | + "github.com/cli/cli/pkg/httpmock" |
| 8 | + "github.com/cli/cli/pkg/iostreams" |
| 9 | + "github.com/cli/cli/pkg/prompt" |
| 10 | + "github.com/google/shlex" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "net/http" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func TestNewCmdDelete(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + cli string |
| 20 | + wants DeleteOptions |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "valid selector", |
| 24 | + cli: "123", |
| 25 | + wants: DeleteOptions{ |
| 26 | + Selector: "123", |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tt := range tests { |
| 32 | + t.Run(tt.name, func(t *testing.T) { |
| 33 | + f := &cmdutil.Factory{} |
| 34 | + |
| 35 | + argv, err := shlex.Split(tt.cli) |
| 36 | + assert.NoError(t, err) |
| 37 | + var gotOpts *DeleteOptions |
| 38 | + cmd := NewCmdDelete(f, func(opts *DeleteOptions) error { |
| 39 | + gotOpts = opts |
| 40 | + return nil |
| 41 | + }) |
| 42 | + |
| 43 | + cmd.SetArgs(argv) |
| 44 | + cmd.SetIn(&bytes.Buffer{}) |
| 45 | + cmd.SetOut(&bytes.Buffer{}) |
| 46 | + cmd.SetErr(&bytes.Buffer{}) |
| 47 | + |
| 48 | + _, err = cmd.ExecuteC() |
| 49 | + assert.NoError(t, err) |
| 50 | + |
| 51 | + assert.Equal(t, tt.wants.Selector, gotOpts.Selector) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func Test_deleteRun(t *testing.T) { |
| 57 | + tests := []struct { |
| 58 | + name string |
| 59 | + opts *DeleteOptions |
| 60 | + gist *shared.Gist |
| 61 | + httpStubs func(*httpmock.Registry) |
| 62 | + askStubs func(*prompt.AskStubber) |
| 63 | + nontty bool |
| 64 | + wantErr bool |
| 65 | + wantStderr string |
| 66 | + wantParams map[string]interface{} |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "no such gist", |
| 70 | + wantErr: true, |
| 71 | + }, { |
| 72 | + name: "another user's gist", |
| 73 | + gist: &shared.Gist{ |
| 74 | + ID: "1234", |
| 75 | + Files: map[string]*shared.GistFile{ |
| 76 | + "cicada.txt": { |
| 77 | + Filename: "cicada.txt", |
| 78 | + Content: "bwhiizzzbwhuiiizzzz", |
| 79 | + Type: "text/plain", |
| 80 | + }, |
| 81 | + }, |
| 82 | + Owner: &shared.GistOwner{Login: "octocat2"}, |
| 83 | + }, |
| 84 | + wantErr: true, |
| 85 | + wantStderr: "You do not own this gist.", |
| 86 | + }, { |
| 87 | + name: "successfully delete", |
| 88 | + gist: &shared.Gist{ |
| 89 | + ID: "1234", |
| 90 | + Files: map[string]*shared.GistFile{ |
| 91 | + "cicada.txt": { |
| 92 | + Filename: "cicada.txt", |
| 93 | + Content: "bwhiizzzbwhuiiizzzz", |
| 94 | + Type: "text/plain", |
| 95 | + }, |
| 96 | + }, |
| 97 | + Owner: &shared.GistOwner{Login: "octocat"}, |
| 98 | + }, |
| 99 | + httpStubs: func(reg *httpmock.Registry) { |
| 100 | + reg.Register(httpmock.REST("DELETE", "gists/1234"), |
| 101 | + httpmock.StatusStringResponse(200, "{}")) |
| 102 | + }, |
| 103 | + wantErr: false, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tt := range tests { |
| 108 | + reg := &httpmock.Registry{} |
| 109 | + if tt.gist == nil { |
| 110 | + reg.Register(httpmock.REST("GET", "gists/1234"), |
| 111 | + httpmock.StatusStringResponse(404, "Not Found")) |
| 112 | + } else { |
| 113 | + reg.Register(httpmock.REST("GET", "gists/1234"), |
| 114 | + httpmock.JSONResponse(tt.gist)) |
| 115 | + reg.Register(httpmock.GraphQL(`query UserCurrent\b`), |
| 116 | + httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`)) |
| 117 | + } |
| 118 | + |
| 119 | + if tt.httpStubs != nil { |
| 120 | + tt.httpStubs(reg) |
| 121 | + } |
| 122 | + |
| 123 | + as, teardown := prompt.InitAskStubber() |
| 124 | + defer teardown() |
| 125 | + if tt.askStubs != nil { |
| 126 | + tt.askStubs(as) |
| 127 | + } |
| 128 | + |
| 129 | + if tt.opts == nil { |
| 130 | + tt.opts = &DeleteOptions{} |
| 131 | + } |
| 132 | + |
| 133 | + tt.opts.HttpClient = func() (*http.Client, error) { |
| 134 | + return &http.Client{Transport: reg}, nil |
| 135 | + } |
| 136 | + io, _, _, _ := iostreams.Test() |
| 137 | + io.SetStdoutTTY(!tt.nontty) |
| 138 | + io.SetStdinTTY(!tt.nontty) |
| 139 | + tt.opts.IO = io |
| 140 | + tt.opts.Selector = "1234" |
| 141 | + |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + err := deleteRun(tt.opts) |
| 144 | + reg.Verify(t) |
| 145 | + if tt.wantErr { |
| 146 | + assert.Error(t, err) |
| 147 | + if tt.wantStderr != "" { |
| 148 | + assert.EqualError(t, err, tt.wantStderr) |
| 149 | + } |
| 150 | + return |
| 151 | + } |
| 152 | + assert.NoError(t, err) |
| 153 | + |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments