11package rename
22
33import (
4+ "net/http"
45 "testing"
56
67 "github.com/cli/cli/v2/pkg/cmdutil"
8+ "github.com/cli/cli/v2/pkg/httpmock"
79 "github.com/cli/cli/v2/pkg/iostreams"
810 "github.com/google/shlex"
911 "github.com/stretchr/testify/assert"
@@ -65,4 +67,90 @@ func TestNewCmdRename(t *testing.T) {
6567 assert .Equal (t , tt .wantOpts .newRepoName , opts .newRepoName )
6668 })
6769 }
70+ }
71+
72+ func TestRenameRun (t * testing.T ) {
73+ testCases := []struct {
74+ name string
75+ opts RenameOptions
76+ httpStubs func (* httpmock.Registry )
77+ stdoutTTY bool
78+ wantOut string
79+ }{
80+ {
81+ name : "owner repo change name tty" ,
82+ opts : RenameOptions {
83+ oldRepoName : "OWNER/REPO" ,
84+ newRepoName : "NEW_REPO" ,
85+ },
86+ wantOut : "✓ Renamed repository pxrth9/team2-hack" ,
87+ httpStubs : func (reg * httpmock.Registry ) {
88+ reg .Register (httpmock .REST ("PATCH" , "repos/OWNER/REPO" ),
89+ httpmock .StatusStringResponse (200 , "{}" ))
90+ },
91+ stdoutTTY : true ,
92+ },
93+ {
94+ name : "owner repo change name notty" ,
95+ opts : RenameOptions {
96+ oldRepoName : "OWNER/REPO" ,
97+ newRepoName : "NEW_REPO" ,
98+ },
99+ wantOut : "✓ Renamed repository pxrth9/team2-hack" ,
100+ httpStubs : func (reg * httpmock.Registry ) {
101+ reg .Register (httpmock .REST ("PATCH" , "repos/OWNER/REPO" ),
102+ httpmock .StatusStringResponse (200 , "{}" ))
103+ },
104+ stdoutTTY : false ,
105+ },
106+ {
107+ name : "nonowner repo change name tty" ,
108+ opts : RenameOptions {
109+ oldRepoName : "NON_OWNER/REPO" ,
110+ newRepoName : "NEW_REPO" ,
111+ },
112+ wantOut : "X you do not own this repository" ,
113+ httpStubs : func (reg * httpmock.Registry ) {
114+ reg .Register (httpmock .REST ("PATCH" , "repos/NON_OWNER/REPO" ),
115+ httpmock .StatusStringResponse (200 , "{}" ))
116+ },
117+ stdoutTTY : true ,
118+ },
119+ {
120+ name : "non owner repo change name notty" ,
121+ opts : RenameOptions {
122+ oldRepoName : "NON_OWNER/REPO" ,
123+ newRepoName : "NEW_REPO" ,
124+ },
125+ wantOut : "X you do not own this repository" ,
126+ httpStubs : func (reg * httpmock.Registry ) {
127+ reg .Register (httpmock .REST ("PATCH" , "repos/NON_OWNER/REPO" ),
128+ httpmock .StatusStringResponse (200 , "{}" ))
129+ },
130+ stdoutTTY : false ,
131+ },
132+ }
133+
134+ for _ , tt := range testCases {
135+ reg := & httpmock.Registry {}
136+ if tt .httpStubs != nil {
137+ tt .httpStubs (reg )
138+ }
139+ tt .opts .HttpClient = func () (* http.Client , error ) {
140+ return & http.Client {Transport : reg }, nil
141+ }
142+
143+ io , _ , stdout , _ := iostreams .Test ()
144+ tt .opts .IO = io
145+
146+ t .Run (tt .name , func (t * testing.T ) {
147+ defer reg .Verify (t )
148+ io .SetStderrTTY (tt .stdoutTTY )
149+
150+ err := renameRun (& tt .opts )
151+ assert .NoError (t , err )
152+ assert .Equal (t , tt .wantOut , stdout .String ())
153+ })
154+ }
155+
68156}
0 commit comments