github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/reopen/reopen_test.go (about)

     1  package reopen
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/ungtb10d/cli/v2/api"
    10  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    11  	"github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared"
    12  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    13  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    14  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    15  	"github.com/ungtb10d/cli/v2/test"
    16  	"github.com/google/shlex"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
    21  	ios, _, stdout, stderr := iostreams.Test()
    22  	ios.SetStdoutTTY(isTTY)
    23  	ios.SetStdinTTY(isTTY)
    24  	ios.SetStderrTTY(isTTY)
    25  
    26  	factory := &cmdutil.Factory{
    27  		IOStreams: ios,
    28  		HttpClient: func() (*http.Client, error) {
    29  			return &http.Client{Transport: rt}, nil
    30  		},
    31  	}
    32  
    33  	cmd := NewCmdReopen(factory, nil)
    34  
    35  	argv, err := shlex.Split(cli)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	cmd.SetArgs(argv)
    40  
    41  	cmd.SetIn(&bytes.Buffer{})
    42  	cmd.SetOut(io.Discard)
    43  	cmd.SetErr(io.Discard)
    44  
    45  	_, err = cmd.ExecuteC()
    46  	return &test.CmdOut{
    47  		OutBuf: stdout,
    48  		ErrBuf: stderr,
    49  	}, err
    50  }
    51  
    52  func TestPRReopen(t *testing.T) {
    53  	http := &httpmock.Registry{}
    54  	defer http.Verify(t)
    55  
    56  	shared.RunCommandFinder("123", &api.PullRequest{
    57  		ID:     "THE-ID",
    58  		Number: 123,
    59  		State:  "CLOSED",
    60  		Title:  "The title of the PR",
    61  	}, ghrepo.New("OWNER", "REPO"))
    62  
    63  	http.Register(
    64  		httpmock.GraphQL(`mutation PullRequestReopen\b`),
    65  		httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
    66  			func(inputs map[string]interface{}) {
    67  				assert.Equal(t, inputs["pullRequestId"], "THE-ID")
    68  			}),
    69  	)
    70  
    71  	output, err := runCommand(http, true, "123")
    72  	assert.NoError(t, err)
    73  	assert.Equal(t, "", output.String())
    74  	assert.Equal(t, "✓ Reopened pull request #123 (The title of the PR)\n", output.Stderr())
    75  }
    76  
    77  func TestPRReopen_alreadyOpen(t *testing.T) {
    78  	http := &httpmock.Registry{}
    79  	defer http.Verify(t)
    80  
    81  	shared.RunCommandFinder("123", &api.PullRequest{
    82  		ID:     "THE-ID",
    83  		Number: 123,
    84  		State:  "OPEN",
    85  		Title:  "The title of the PR",
    86  	}, ghrepo.New("OWNER", "REPO"))
    87  
    88  	output, err := runCommand(http, true, "123")
    89  	assert.NoError(t, err)
    90  	assert.Equal(t, "", output.String())
    91  	assert.Equal(t, "! Pull request #123 (The title of the PR) is already open\n", output.Stderr())
    92  }
    93  
    94  func TestPRReopen_alreadyMerged(t *testing.T) {
    95  	http := &httpmock.Registry{}
    96  	defer http.Verify(t)
    97  
    98  	shared.RunCommandFinder("123", &api.PullRequest{
    99  		ID:     "THE-ID",
   100  		Number: 123,
   101  		State:  "MERGED",
   102  		Title:  "The title of the PR",
   103  	}, ghrepo.New("OWNER", "REPO"))
   104  
   105  	output, err := runCommand(http, true, "123")
   106  	assert.EqualError(t, err, "SilentError")
   107  	assert.Equal(t, "", output.String())
   108  	assert.Equal(t, "X Pull request #123 (The title of the PR) can't be reopened because it was already merged\n", output.Stderr())
   109  }
   110  
   111  func TestPRReopen_withComment(t *testing.T) {
   112  	http := &httpmock.Registry{}
   113  	defer http.Verify(t)
   114  
   115  	shared.RunCommandFinder("123", &api.PullRequest{
   116  		ID:     "THE-ID",
   117  		Number: 123,
   118  		State:  "CLOSED",
   119  		Title:  "The title of the PR",
   120  	}, ghrepo.New("OWNER", "REPO"))
   121  
   122  	http.Register(
   123  		httpmock.GraphQL(`mutation CommentCreate\b`),
   124  		httpmock.GraphQLMutation(`
   125  		{ "data": { "addComment": { "commentEdge": { "node": {
   126  			"url": "https://github.com/OWNER/REPO/issues/123#issuecomment-456"
   127  		} } } } }`,
   128  			func(inputs map[string]interface{}) {
   129  				assert.Equal(t, "THE-ID", inputs["subjectId"])
   130  				assert.Equal(t, "reopening comment", inputs["body"])
   131  			}),
   132  	)
   133  	http.Register(
   134  		httpmock.GraphQL(`mutation PullRequestReopen\b`),
   135  		httpmock.GraphQLMutation(`{"id": "THE-ID"}`,
   136  			func(inputs map[string]interface{}) {
   137  				assert.Equal(t, inputs["pullRequestId"], "THE-ID")
   138  			}),
   139  	)
   140  
   141  	output, err := runCommand(http, true, "123 --comment 'reopening comment'")
   142  	assert.NoError(t, err)
   143  	assert.Equal(t, "", output.String())
   144  	assert.Equal(t, "✓ Reopened pull request #123 (The title of the PR)\n", output.Stderr())
   145  }