github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/repo/delete/delete_test.go (about)

     1  package delete
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     9  	"github.com/ungtb10d/cli/v2/internal/prompter"
    10  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    11  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    12  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    13  	"github.com/google/shlex"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestNewCmdDelete(t *testing.T) {
    18  	tests := []struct {
    19  		name    string
    20  		input   string
    21  		tty     bool
    22  		output  DeleteOptions
    23  		wantErr bool
    24  		errMsg  string
    25  	}{
    26  		{
    27  			name:   "confirm flag",
    28  			tty:    true,
    29  			input:  "OWNER/REPO --confirm",
    30  			output: DeleteOptions{RepoArg: "OWNER/REPO", Confirmed: true},
    31  		},
    32  		{
    33  			name:    "no confirmation notty",
    34  			input:   "OWNER/REPO",
    35  			output:  DeleteOptions{RepoArg: "OWNER/REPO"},
    36  			wantErr: true,
    37  			errMsg:  "--confirm required when not running interactively",
    38  		},
    39  		{
    40  			name:   "base repo resolution",
    41  			input:  "",
    42  			tty:    true,
    43  			output: DeleteOptions{},
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			ios, _, _, _ := iostreams.Test()
    49  			ios.SetStdinTTY(tt.tty)
    50  			ios.SetStdoutTTY(tt.tty)
    51  			f := &cmdutil.Factory{
    52  				IOStreams: ios,
    53  			}
    54  			argv, err := shlex.Split(tt.input)
    55  			assert.NoError(t, err)
    56  			var gotOpts *DeleteOptions
    57  			cmd := NewCmdDelete(f, func(opts *DeleteOptions) error {
    58  				gotOpts = opts
    59  				return nil
    60  			})
    61  			cmd.SetArgs(argv)
    62  			cmd.SetIn(&bytes.Buffer{})
    63  			cmd.SetOut(&bytes.Buffer{})
    64  			cmd.SetErr(&bytes.Buffer{})
    65  
    66  			_, err = cmd.ExecuteC()
    67  			if tt.wantErr {
    68  				assert.Error(t, err)
    69  				assert.Equal(t, tt.errMsg, err.Error())
    70  				return
    71  			}
    72  			assert.NoError(t, err)
    73  			assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg)
    74  		})
    75  	}
    76  }
    77  
    78  func Test_deleteRun(t *testing.T) {
    79  	tests := []struct {
    80  		name          string
    81  		tty           bool
    82  		opts          *DeleteOptions
    83  		httpStubs     func(*httpmock.Registry)
    84  		prompterStubs func(*prompter.PrompterMock)
    85  		wantStdout    string
    86  		wantErr       bool
    87  		errMsg        string
    88  	}{
    89  		{
    90  			name:       "prompting confirmation tty",
    91  			tty:        true,
    92  			opts:       &DeleteOptions{RepoArg: "OWNER/REPO"},
    93  			wantStdout: "✓ Deleted repository OWNER/REPO\n",
    94  			prompterStubs: func(p *prompter.PrompterMock) {
    95  				p.ConfirmDeletionFunc = func(_ string) error {
    96  					return nil
    97  				}
    98  			},
    99  			httpStubs: func(reg *httpmock.Registry) {
   100  				reg.Register(
   101  					httpmock.REST("DELETE", "repos/OWNER/REPO"),
   102  					httpmock.StatusStringResponse(204, "{}"))
   103  			},
   104  		},
   105  		{
   106  			name:       "infer base repo",
   107  			tty:        true,
   108  			opts:       &DeleteOptions{},
   109  			wantStdout: "✓ Deleted repository OWNER/REPO\n",
   110  			prompterStubs: func(p *prompter.PrompterMock) {
   111  				p.ConfirmDeletionFunc = func(_ string) error {
   112  					return nil
   113  				}
   114  			},
   115  			httpStubs: func(reg *httpmock.Registry) {
   116  				reg.Register(
   117  					httpmock.REST("DELETE", "repos/OWNER/REPO"),
   118  					httpmock.StatusStringResponse(204, "{}"))
   119  			},
   120  		},
   121  		{
   122  			name: "confirmation no tty",
   123  			opts: &DeleteOptions{
   124  				RepoArg:   "OWNER/REPO",
   125  				Confirmed: true,
   126  			},
   127  			httpStubs: func(reg *httpmock.Registry) {
   128  				reg.Register(
   129  					httpmock.REST("DELETE", "repos/OWNER/REPO"),
   130  					httpmock.StatusStringResponse(204, "{}"))
   131  			},
   132  		},
   133  		{
   134  			name:       "short repo name",
   135  			opts:       &DeleteOptions{RepoArg: "REPO"},
   136  			wantStdout: "✓ Deleted repository OWNER/REPO\n",
   137  			tty:        true,
   138  			prompterStubs: func(p *prompter.PrompterMock) {
   139  				p.ConfirmDeletionFunc = func(_ string) error {
   140  					return nil
   141  				}
   142  			},
   143  			httpStubs: func(reg *httpmock.Registry) {
   144  				reg.Register(
   145  					httpmock.GraphQL(`query UserCurrent\b`),
   146  					httpmock.StringResponse(`{"data":{"viewer":{"login":"OWNER"}}}`))
   147  				reg.Register(
   148  					httpmock.REST("DELETE", "repos/OWNER/REPO"),
   149  					httpmock.StatusStringResponse(204, "{}"))
   150  			},
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		pm := &prompter.PrompterMock{}
   155  		if tt.prompterStubs != nil {
   156  			tt.prompterStubs(pm)
   157  		}
   158  		tt.opts.Prompter = pm
   159  
   160  		tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
   161  			return ghrepo.New("OWNER", "REPO"), nil
   162  		}
   163  
   164  		reg := &httpmock.Registry{}
   165  		if tt.httpStubs != nil {
   166  			tt.httpStubs(reg)
   167  		}
   168  		tt.opts.HttpClient = func() (*http.Client, error) {
   169  			return &http.Client{Transport: reg}, nil
   170  		}
   171  
   172  		ios, _, stdout, _ := iostreams.Test()
   173  		ios.SetStdinTTY(tt.tty)
   174  		ios.SetStdoutTTY(tt.tty)
   175  		tt.opts.IO = ios
   176  
   177  		t.Run(tt.name, func(t *testing.T) {
   178  			defer reg.Verify(t)
   179  			err := deleteRun(tt.opts)
   180  			if tt.wantErr {
   181  				assert.Error(t, err)
   182  				assert.Equal(t, tt.errMsg, err.Error())
   183  				return
   184  			}
   185  			assert.NoError(t, err)
   186  			assert.Equal(t, tt.wantStdout, stdout.String())
   187  		})
   188  	}
   189  }