github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/status/status_test.go (about)

     1  package status
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/ungtb10d/cli/v2/internal/config"
    11  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    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  )
    18  
    19  func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) {
    20  	ios, _, stdout, stderr := iostreams.Test()
    21  	ios.SetStdoutTTY(isTTY)
    22  	ios.SetStdinTTY(isTTY)
    23  	ios.SetStderrTTY(isTTY)
    24  
    25  	factory := &cmdutil.Factory{
    26  		IOStreams: ios,
    27  		HttpClient: func() (*http.Client, error) {
    28  			return &http.Client{Transport: rt}, nil
    29  		},
    30  		Config: func() (config.Config, error) {
    31  			return config.NewBlankConfig(), nil
    32  		},
    33  		BaseRepo: func() (ghrepo.Interface, error) {
    34  			return ghrepo.New("OWNER", "REPO"), nil
    35  		},
    36  	}
    37  
    38  	cmd := NewCmdStatus(factory, nil)
    39  
    40  	argv, err := shlex.Split(cli)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	cmd.SetArgs(argv)
    45  
    46  	cmd.SetIn(&bytes.Buffer{})
    47  	cmd.SetOut(io.Discard)
    48  	cmd.SetErr(io.Discard)
    49  
    50  	_, err = cmd.ExecuteC()
    51  	return &test.CmdOut{
    52  		OutBuf: stdout,
    53  		ErrBuf: stderr,
    54  	}, err
    55  }
    56  
    57  func TestIssueStatus(t *testing.T) {
    58  	http := &httpmock.Registry{}
    59  	defer http.Verify(t)
    60  
    61  	http.Register(
    62  		httpmock.GraphQL(`query UserCurrent\b`),
    63  		httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
    64  	http.Register(
    65  		httpmock.GraphQL(`query IssueStatus\b`),
    66  		httpmock.FileResponse("./fixtures/issueStatus.json"))
    67  
    68  	output, err := runCommand(http, true, "")
    69  	if err != nil {
    70  		t.Errorf("error running command `issue status`: %v", err)
    71  	}
    72  
    73  	expectedIssues := []*regexp.Regexp{
    74  		regexp.MustCompile(`(?m)8.*carrots.*about.*ago`),
    75  		regexp.MustCompile(`(?m)9.*squash.*about.*ago`),
    76  		regexp.MustCompile(`(?m)10.*broccoli.*about.*ago`),
    77  		regexp.MustCompile(`(?m)11.*swiss chard.*about.*ago`),
    78  	}
    79  
    80  	for _, r := range expectedIssues {
    81  		if !r.MatchString(output.String()) {
    82  			t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
    83  			return
    84  		}
    85  	}
    86  }
    87  
    88  func TestIssueStatus_blankSlate(t *testing.T) {
    89  	http := &httpmock.Registry{}
    90  	defer http.Verify(t)
    91  
    92  	http.Register(
    93  		httpmock.GraphQL(`query UserCurrent\b`),
    94  		httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
    95  	http.Register(
    96  		httpmock.GraphQL(`query IssueStatus\b`),
    97  		httpmock.StringResponse(`
    98  		{ "data": { "repository": {
    99  			"hasIssuesEnabled": true,
   100  			"assigned": { "nodes": [] },
   101  			"mentioned": { "nodes": [] },
   102  			"authored": { "nodes": [] }
   103  		} } }`))
   104  
   105  	output, err := runCommand(http, true, "")
   106  	if err != nil {
   107  		t.Errorf("error running command `issue status`: %v", err)
   108  	}
   109  
   110  	expectedOutput := `
   111  Relevant issues in OWNER/REPO
   112  
   113  Issues assigned to you
   114    There are no issues assigned to you
   115  
   116  Issues mentioning you
   117    There are no issues mentioning you
   118  
   119  Issues opened by you
   120    There are no issues opened by you
   121  
   122  `
   123  	if output.String() != expectedOutput {
   124  		t.Errorf("expected %q, got %q", expectedOutput, output)
   125  	}
   126  }
   127  
   128  func TestIssueStatus_disabledIssues(t *testing.T) {
   129  	http := &httpmock.Registry{}
   130  	defer http.Verify(t)
   131  
   132  	http.Register(
   133  		httpmock.GraphQL(`query UserCurrent\b`),
   134  		httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
   135  	http.Register(
   136  		httpmock.GraphQL(`query IssueStatus\b`),
   137  		httpmock.StringResponse(`
   138  		{ "data": { "repository": {
   139  			"hasIssuesEnabled": false
   140  		} } }`))
   141  
   142  	_, err := runCommand(http, true, "")
   143  	if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" {
   144  		t.Errorf("error running command `issue status`: %v", err)
   145  	}
   146  }