|
| 1 | +package archive |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cli/cli/v2/pkg/cmdutil" |
| 9 | + "github.com/cli/cli/v2/pkg/httpmock" |
| 10 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 11 | + "github.com/google/shlex" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +// probably redundant |
| 16 | +func TestNewCmdArchive(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + input string |
| 20 | + tty bool |
| 21 | + output ArchiveOptions |
| 22 | + wantErr bool |
| 23 | + errMsg string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "valid repo", |
| 27 | + input: "cli/cli", |
| 28 | + tty: true, |
| 29 | + output: ArchiveOptions{ |
| 30 | + RepoArg: "cli/cli", |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "no argument", |
| 35 | + input: "", |
| 36 | + wantErr: true, |
| 37 | + tty: true, |
| 38 | + output: ArchiveOptions{ |
| 39 | + RepoArg: "", |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + io, _, _, _ := iostreams.Test() |
| 46 | + io.SetStdinTTY(tt.tty) |
| 47 | + io.SetStdoutTTY(tt.tty) |
| 48 | + f := &cmdutil.Factory{ |
| 49 | + IOStreams: io, |
| 50 | + } |
| 51 | + argv, err := shlex.Split(tt.input) |
| 52 | + assert.NoError(t, err) |
| 53 | + var gotOpts *ArchiveOptions |
| 54 | + cmd := NewCmdArchive(f, func(opts *ArchiveOptions) error { |
| 55 | + gotOpts = opts |
| 56 | + return nil |
| 57 | + }) |
| 58 | + cmd.SetArgs(argv) |
| 59 | + cmd.SetIn(&bytes.Buffer{}) |
| 60 | + cmd.SetOut(&bytes.Buffer{}) |
| 61 | + cmd.SetErr(&bytes.Buffer{}) |
| 62 | + |
| 63 | + _, err = cmd.ExecuteC() |
| 64 | + if tt.wantErr { |
| 65 | + assert.Error(t, err) |
| 66 | + return |
| 67 | + } |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func Test_ArchiveRun(t *testing.T) { |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + opts ArchiveOptions |
| 78 | + httpStubs func(*httpmock.Registry) |
| 79 | + isTTY bool |
| 80 | + wantStdout string |
| 81 | + wantStderr string |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "unarchived repo tty", |
| 85 | + opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, |
| 86 | + wantStdout: "✓ Archived repository OWNER/REPO\n", |
| 87 | + isTTY: true, |
| 88 | + httpStubs: func(reg *httpmock.Registry) { |
| 89 | + reg.Register( |
| 90 | + httpmock.GraphQL(`query RepositoryInfo\b`), |
| 91 | + httpmock.StringResponse(`{ "data": { "repository": { |
| 92 | + "id": "THE-ID", |
| 93 | + "isArchived": false} } }`)) |
| 94 | + reg.Register( |
| 95 | + httpmock.GraphQL(`mutation ArchiveRepository\b`), |
| 96 | + httpmock.StringResponse(`{}`)) |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "unarchived repo notty", |
| 101 | + opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, |
| 102 | + isTTY: false, |
| 103 | + httpStubs: func(reg *httpmock.Registry) { |
| 104 | + reg.Register( |
| 105 | + httpmock.GraphQL(`query RepositoryInfo\b`), |
| 106 | + httpmock.StringResponse(`{ "data": { "repository": { |
| 107 | + "id": "THE-ID", |
| 108 | + "isArchived": false} } }`)) |
| 109 | + reg.Register( |
| 110 | + httpmock.GraphQL(`mutation ArchiveRepository\b`), |
| 111 | + httpmock.StringResponse(`{}`)) |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "archived repo tty", |
| 116 | + opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, |
| 117 | + wantStderr: "! Repository OWNER/REPO is already archived\n", |
| 118 | + isTTY: true, |
| 119 | + httpStubs: func(reg *httpmock.Registry) { |
| 120 | + reg.Register( |
| 121 | + httpmock.GraphQL(`query RepositoryInfo\b`), |
| 122 | + httpmock.StringResponse(`{ "data": { "repository": { |
| 123 | + "id": "THE-ID", |
| 124 | + "isArchived": true } } }`)) |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "archived repo notty", |
| 129 | + opts: ArchiveOptions{RepoArg: "OWNER/REPO"}, |
| 130 | + isTTY: false, |
| 131 | + wantStderr: "! Repository OWNER/REPO is already archived\n", |
| 132 | + httpStubs: func(reg *httpmock.Registry) { |
| 133 | + reg.Register( |
| 134 | + httpmock.GraphQL(`query RepositoryInfo\b`), |
| 135 | + httpmock.StringResponse(`{ "data": { "repository": { |
| 136 | + "id": "THE-ID", |
| 137 | + "isArchived": true } } }`)) |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + for _, tt := range tests { |
| 143 | + reg := &httpmock.Registry{} |
| 144 | + if tt.httpStubs != nil { |
| 145 | + tt.httpStubs(reg) |
| 146 | + } |
| 147 | + tt.opts.HttpClient = func() (*http.Client, error) { |
| 148 | + return &http.Client{Transport: reg}, nil |
| 149 | + } |
| 150 | + |
| 151 | + io, _, stdout, stderr := iostreams.Test() |
| 152 | + tt.opts.IO = io |
| 153 | + |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + defer reg.Verify(t) |
| 156 | + io.SetStdoutTTY(tt.isTTY) |
| 157 | + io.SetStderrTTY(tt.isTTY) |
| 158 | + |
| 159 | + err := archiveRun(&tt.opts) |
| 160 | + assert.NoError(t, err) |
| 161 | + assert.Equal(t, tt.wantStdout, stdout.String()) |
| 162 | + assert.Equal(t, tt.wantStderr, stderr.String()) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments