|
| 1 | +package close |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
| 7 | + "regexp" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/cli/cli/internal/config" |
| 11 | + "github.com/cli/cli/internal/ghrepo" |
| 12 | + "github.com/cli/cli/pkg/cmdutil" |
| 13 | + "github.com/cli/cli/pkg/httpmock" |
| 14 | + "github.com/cli/cli/pkg/iostreams" |
| 15 | + "github.com/cli/cli/test" |
| 16 | + "github.com/google/shlex" |
| 17 | +) |
| 18 | + |
| 19 | +func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, error) { |
| 20 | + io, _, stdout, stderr := iostreams.Test() |
| 21 | + io.SetStdoutTTY(isTTY) |
| 22 | + io.SetStdinTTY(isTTY) |
| 23 | + io.SetStderrTTY(isTTY) |
| 24 | + |
| 25 | + factory := &cmdutil.Factory{ |
| 26 | + IOStreams: io, |
| 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 := NewCmdClose(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(ioutil.Discard) |
| 48 | + cmd.SetErr(ioutil.Discard) |
| 49 | + |
| 50 | + _, err = cmd.ExecuteC() |
| 51 | + return &test.CmdOut{ |
| 52 | + OutBuf: stdout, |
| 53 | + ErrBuf: stderr, |
| 54 | + }, err |
| 55 | +} |
| 56 | + |
| 57 | +func TestIssueClose(t *testing.T) { |
| 58 | + http := &httpmock.Registry{} |
| 59 | + defer http.Verify(t) |
| 60 | + |
| 61 | + http.StubResponse(200, bytes.NewBufferString(` |
| 62 | + { "data": { "repository": { |
| 63 | + "hasIssuesEnabled": true, |
| 64 | + "issue": { "number": 13, "title": "The title of the issue"} |
| 65 | + } } } |
| 66 | + `)) |
| 67 | + |
| 68 | + http.StubResponse(200, bytes.NewBufferString(`{"id": "THE-ID"}`)) |
| 69 | + |
| 70 | + output, err := runCommand(http, true, "13") |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("error running command `issue close`: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + r := regexp.MustCompile(`Closed issue #13 \(The title of the issue\)`) |
| 76 | + |
| 77 | + if !r.MatchString(output.Stderr()) { |
| 78 | + t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestIssueClose_alreadyClosed(t *testing.T) { |
| 83 | + http := &httpmock.Registry{} |
| 84 | + defer http.Verify(t) |
| 85 | + |
| 86 | + http.StubResponse(200, bytes.NewBufferString(` |
| 87 | + { "data": { "repository": { |
| 88 | + "hasIssuesEnabled": true, |
| 89 | + "issue": { "number": 13, "title": "The title of the issue", "closed": true} |
| 90 | + } } } |
| 91 | + `)) |
| 92 | + |
| 93 | + output, err := runCommand(http, true, "13") |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("error running command `issue close`: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + r := regexp.MustCompile(`Issue #13 \(The title of the issue\) is already closed`) |
| 99 | + |
| 100 | + if !r.MatchString(output.Stderr()) { |
| 101 | + t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr()) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestIssueClose_issuesDisabled(t *testing.T) { |
| 106 | + http := &httpmock.Registry{} |
| 107 | + defer http.Verify(t) |
| 108 | + |
| 109 | + http.StubResponse(200, bytes.NewBufferString(` |
| 110 | + { "data": { "repository": { |
| 111 | + "hasIssuesEnabled": false |
| 112 | + } } } |
| 113 | + `)) |
| 114 | + |
| 115 | + _, err := runCommand(http, true, "13") |
| 116 | + if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { |
| 117 | + t.Fatalf("got error: %v", err) |
| 118 | + } |
| 119 | +} |
0 commit comments