@@ -2,49 +2,42 @@ package archive
22
33import (
44 "bytes"
5+ "fmt"
56 "net/http"
67 "testing"
78
9+ "github.com/cli/cli/v2/internal/ghrepo"
810 "github.com/cli/cli/v2/pkg/cmdutil"
911 "github.com/cli/cli/v2/pkg/httpmock"
1012 "github.com/cli/cli/v2/pkg/iostreams"
13+ "github.com/cli/cli/v2/pkg/prompt"
1114 "github.com/google/shlex"
1215 "github.com/stretchr/testify/assert"
1316)
1417
15- // probably redundant
1618func TestNewCmdArchive (t * testing.T ) {
1719 tests := []struct {
1820 name string
1921 input string
20- tty bool
21- output ArchiveOptions
2222 wantErr bool
23+ output ArchiveOptions
2324 errMsg string
2425 }{
2526 {
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" ,
27+ name : "no arguments no tty" ,
3528 input : "" ,
29+ errMsg : "--confirm required when not running interactively" ,
3630 wantErr : true ,
37- tty : true ,
38- output : ArchiveOptions {
39- RepoArg : "" ,
40- },
31+ },
32+ {
33+ name : "repo argument tty" ,
34+ input : "OWNER/REPO --confirm" ,
35+ output : ArchiveOptions {RepoArg : "OWNER/REPO" , Confirmed : true },
4136 },
4237 }
4338 for _ , tt := range tests {
4439 t .Run (tt .name , func (t * testing.T ) {
4540 io , _ , _ , _ := iostreams .Test ()
46- io .SetStdinTTY (tt .tty )
47- io .SetStdoutTTY (tt .tty )
4841 f := & cmdutil.Factory {
4942 IOStreams : io ,
5043 }
@@ -67,90 +60,90 @@ func TestNewCmdArchive(t *testing.T) {
6760 }
6861 assert .NoError (t , err )
6962 assert .Equal (t , tt .output .RepoArg , gotOpts .RepoArg )
63+ assert .Equal (t , tt .output .Confirmed , gotOpts .Confirmed )
7064 })
7165 }
7266}
7367
7468func Test_ArchiveRun (t * testing.T ) {
69+ queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }`
7570 tests := []struct {
7671 name string
7772 opts ArchiveOptions
7873 httpStubs func (* httpmock.Registry )
74+ askStubs func (* prompt.AskStubber )
7975 isTTY bool
8076 wantStdout string
8177 wantStderr string
8278 }{
8379 {
8480 name : "unarchived repo tty" ,
85- opts : ArchiveOptions {RepoArg : "OWNER/REPO" },
8681 wantStdout : "✓ Archived repository OWNER/REPO\n " ,
87- isTTY : true ,
82+ askStubs : func (q * prompt.AskStubber ) {
83+ q .StubOne (true )
84+ },
85+ isTTY : true ,
86+ opts : ArchiveOptions {RepoArg : "OWNER/REPO" },
8887 httpStubs : func (reg * httpmock.Registry ) {
8988 reg .Register (
9089 httpmock .GraphQL (`query RepositoryInfo\b` ),
91- httpmock .StringResponse (`{ "data": { "repository": {
92- "id": "THE-ID",
93- "isArchived": false} } }` ))
90+ httpmock .StringResponse (fmt .Sprintf (queryResponse , "false" )))
9491 reg .Register (
9592 httpmock .GraphQL (`mutation ArchiveRepository\b` ),
9693 httpmock .StringResponse (`{}` ))
9794 },
9895 },
9996 {
100- name : "unarchived repo notty" ,
101- opts : ArchiveOptions {RepoArg : "OWNER/REPO" },
102- isTTY : false ,
97+ name : "infer base repo" ,
98+ wantStdout : "✓ Archived repository OWNER/REPO\n " ,
99+ opts : ArchiveOptions {},
100+ askStubs : func (q * prompt.AskStubber ) {
101+ q .StubOne (true )
102+ },
103+ isTTY : true ,
103104 httpStubs : func (reg * httpmock.Registry ) {
104105 reg .Register (
105106 httpmock .GraphQL (`query RepositoryInfo\b` ),
106- httpmock .StringResponse (`{ "data": { "repository": {
107- "id": "THE-ID",
108- "isArchived": false} } }` ))
107+ httpmock .StringResponse (fmt .Sprintf (queryResponse , "false" )))
109108 reg .Register (
110109 httpmock .GraphQL (`mutation ArchiveRepository\b` ),
111110 httpmock .StringResponse (`{}` ))
112111 },
113112 },
114113 {
115114 name : "archived repo tty" ,
116- opts : ArchiveOptions {RepoArg : "OWNER/REPO" },
117115 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" ,
129116 opts : ArchiveOptions {RepoArg : "OWNER/REPO" },
130- isTTY : false ,
131- wantStderr : "! Repository OWNER/REPO is already archived\n " ,
132117 httpStubs : func (reg * httpmock.Registry ) {
133118 reg .Register (
134119 httpmock .GraphQL (`query RepositoryInfo\b` ),
135- httpmock .StringResponse (`{ "data": { "repository": {
136- "id": "THE-ID",
137- "isArchived": true } } }` ))
120+ httpmock .StringResponse (fmt .Sprintf (queryResponse , "true" )))
138121 },
139122 },
140123 }
141124
142125 for _ , tt := range tests {
126+ repo , _ := ghrepo .FromFullName ("OWNER/REPO" )
143127 reg := & httpmock.Registry {}
144128 if tt .httpStubs != nil {
145129 tt .httpStubs (reg )
146130 }
131+
132+ tt .opts .BaseRepo = func () (ghrepo.Interface , error ) {
133+ return repo , nil
134+ }
147135 tt .opts .HttpClient = func () (* http.Client , error ) {
148136 return & http.Client {Transport : reg }, nil
149137 }
150-
151138 io , _ , stdout , stderr := iostreams .Test ()
152139 tt .opts .IO = io
153140
141+ q , teardown := prompt .InitAskStubber ()
142+ defer teardown ()
143+ if tt .askStubs != nil {
144+ tt .askStubs (q )
145+ }
146+
154147 t .Run (tt .name , func (t * testing.T ) {
155148 defer reg .Verify (t )
156149 io .SetStdoutTTY (tt .isTTY )
0 commit comments