github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/issue/delete/delete_test.go (about)

     1  package delete
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/cli/cli/pkg/prompt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"regexp"
     9  	"testing"
    10  
    11  	"github.com/cli/cli/internal/config"
    12  	"github.com/cli/cli/internal/ghrepo"
    13  	"github.com/cli/cli/pkg/cmdutil"
    14  	"github.com/cli/cli/pkg/httpmock"
    15  	"github.com/cli/cli/pkg/iostreams"
    16  	"github.com/cli/cli/test"
    17  	"github.com/google/shlex"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
    22  	io, _, stdout, stderr := iostreams.Test()
    23  	io.SetStdoutTTY(isTTY)
    24  	io.SetStdinTTY(isTTY)
    25  	io.SetStderrTTY(isTTY)
    26  
    27  	factory := &cmdutil.Factory{
    28  		IOStreams: io,
    29  		HttpClient: func() (*http.Client, error) {
    30  			return &http.Client{Transport: rt}, nil
    31  		},
    32  		Config: func() (config.Config, error) {
    33  			return config.NewBlankConfig(), nil
    34  		},
    35  		BaseRepo: func() (ghrepo.Interface, error) {
    36  			return ghrepo.New("OWNER", "REPO"), nil
    37  		},
    38  	}
    39  
    40  	cmd := NewCmdDelete(factory, nil)
    41  
    42  	argv, err := shlex.Split(cli)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	cmd.SetArgs(argv)
    47  
    48  	cmd.SetIn(&bytes.Buffer{})
    49  	cmd.SetOut(ioutil.Discard)
    50  	cmd.SetErr(ioutil.Discard)
    51  
    52  	_, err = cmd.ExecuteC()
    53  	return &test.CmdOut{
    54  		OutBuf: stdout,
    55  		ErrBuf: stderr,
    56  	}, err
    57  }
    58  
    59  func TestIssueDelete(t *testing.T) {
    60  	httpRegistry := &httpmock.Registry{}
    61  	defer httpRegistry.Verify(t)
    62  
    63  	httpRegistry.Register(
    64  		httpmock.GraphQL(`query IssueByNumber\b`),
    65  		httpmock.StringResponse(`
    66  			{ "data": { "repository": {
    67  				"hasIssuesEnabled": true,
    68  				"issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"}
    69  			} } }`),
    70  	)
    71  	httpRegistry.Register(
    72  		httpmock.GraphQL(`mutation IssueDelete\b`),
    73  		httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
    74  			func(inputs map[string]interface{}) {
    75  				assert.Equal(t, inputs["issueId"], "THE-ID")
    76  			}),
    77  	)
    78  	as, teardown := prompt.InitAskStubber()
    79  	defer teardown()
    80  	as.StubOne("13")
    81  
    82  	output, err := runCommand(httpRegistry, true, "13")
    83  	if err != nil {
    84  		t.Fatalf("error running command `issue delete`: %v", err)
    85  	}
    86  
    87  	r := regexp.MustCompile(`Deleted issue #13 \(The title of the issue\)`)
    88  
    89  	if !r.MatchString(output.Stderr()) {
    90  		t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr())
    91  	}
    92  }
    93  
    94  func TestIssueDelete_cancel(t *testing.T) {
    95  	httpRegistry := &httpmock.Registry{}
    96  	defer httpRegistry.Verify(t)
    97  
    98  	httpRegistry.Register(
    99  		httpmock.GraphQL(`query IssueByNumber\b`),
   100  		httpmock.StringResponse(`
   101  			{ "data": { "repository": {
   102  				"hasIssuesEnabled": true,
   103  				"issue": { "id": "THE-ID", "number": 13, "title": "The title of the issue"}
   104  			} } }`),
   105  	)
   106  	as, teardown := prompt.InitAskStubber()
   107  	defer teardown()
   108  	as.StubOne("14")
   109  
   110  	output, err := runCommand(httpRegistry, true, "13")
   111  	if err != nil {
   112  		t.Fatalf("error running command `issue delete`: %v", err)
   113  	}
   114  
   115  	r := regexp.MustCompile(`Issue #13 was not deleted`)
   116  
   117  	if !r.MatchString(output.String()) {
   118  		t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.String())
   119  	}
   120  }
   121  
   122  func TestIssueDelete_doesNotExist(t *testing.T) {
   123  	httpRegistry := &httpmock.Registry{}
   124  	defer httpRegistry.Verify(t)
   125  
   126  	httpRegistry.Register(
   127  		httpmock.GraphQL(`query IssueByNumber\b`),
   128  		httpmock.StringResponse(`
   129  			{ "errors": [
   130  				{ "message": "Could not resolve to an Issue with the number of 13." }
   131  			] }
   132  			`),
   133  	)
   134  
   135  	_, err := runCommand(httpRegistry, true, "13")
   136  	if err == nil || err.Error() != "GraphQL error: Could not resolve to an Issue with the number of 13." {
   137  		t.Errorf("error running command `issue delete`: %v", err)
   138  	}
   139  }
   140  
   141  func TestIssueDelete_issuesDisabled(t *testing.T) {
   142  	httpRegistry := &httpmock.Registry{}
   143  	defer httpRegistry.Verify(t)
   144  
   145  	httpRegistry.Register(
   146  		httpmock.GraphQL(`query IssueByNumber\b`),
   147  		httpmock.StringResponse(`
   148  			{ "data": { "repository": {
   149  				"hasIssuesEnabled": false
   150  			} } }`),
   151  	)
   152  
   153  	_, err := runCommand(httpRegistry, true, "13")
   154  	if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" {
   155  		t.Fatalf("got error: %v", err)
   156  	}
   157  }