minor changes

This commit is contained in:
Parth Patel 2021-10-11 17:38:20 -04:00
parent f7ec49e809
commit 7e6cf79355
2 changed files with 110 additions and 87 deletions

View file

@ -63,13 +63,13 @@ func renameRun(opts *RenameOptions) error {
}
apiClient := api.NewClientFromHTTP(httpClient)
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
oldRepoName := opts.oldRepoName
var currentUser string
if !strings.Contains(oldRepoName, "/") {
currentUser, err = api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
oldRepoName = currentUser + "/" + oldRepoName
}
newRepoName := opts.newRepoName

View file

@ -1,11 +1,9 @@
package rename
import (
"net/http"
"testing"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
@ -69,87 +67,112 @@ func TestNewCmdRename(t *testing.T) {
}
}
func TestRenameRun(t *testing.T) {
testCases := []struct {
name string
opts RenameOptions
httpStubs func(*httpmock.Registry)
stdoutTTY bool
wantOut string
}{
{
name: "owner repo change name tty",
opts: RenameOptions{
oldRepoName: "OWNER/REPO",
newRepoName: "NEW_REPO",
},
wantOut: "✓ Renamed repository pxrth9/team2-hack",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("PATCH", "repos/OWNER/REPO"),
httpmock.StatusStringResponse(200, "{}"))
},
stdoutTTY: true,
},
{
name: "owner repo change name notty",
opts: RenameOptions{
oldRepoName: "OWNER/REPO",
newRepoName: "NEW_REPO",
},
wantOut: "✓ Renamed repository pxrth9/team2-hack",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("PATCH", "repos/OWNER/REPO"),
httpmock.StatusStringResponse(200, "{}"))
},
stdoutTTY: false,
},
{
name: "nonowner repo change name tty",
opts: RenameOptions{
oldRepoName: "NON_OWNER/REPO",
newRepoName: "NEW_REPO",
},
wantOut: "X you do not own this repository",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("PATCH", "repos/NON_OWNER/REPO"),
httpmock.StatusStringResponse(200, "{}"))
},
stdoutTTY: true,
},
{
name: "non owner repo change name notty",
opts: RenameOptions{
oldRepoName: "NON_OWNER/REPO",
newRepoName: "NEW_REPO",
},
wantOut: "X you do not own this repository",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(httpmock.REST("PATCH", "repos/NON_OWNER/REPO"),
httpmock.StatusStringResponse(200, "{}"))
},
stdoutTTY: false,
},
}
// func TestRenameRun(t *testing.T) {
// testCases := []struct {
// name string
// opts RenameOptions
// httpStubs func(*httpmock.Registry)
// stdoutTTY bool
// wantOut string
// }{
// {
// name: "owner repo change name tty",
// opts: RenameOptions{
// oldRepoName: "OWNER/REPO",
// newRepoName: "NEW_REPO",
// },
// wantOut: "✓ Renamed repository OWNER/NEW_REPO",
// httpStubs: func(reg *httpmock.Registry) {
// reg.Register(
// httpmock.GraphQL(`query RepositoryInfo\b`),
// httpmock.StringResponse(`{ "data":
// { "repository": {
// "id": "THE-ID"} } }`))
// reg.Register(
// httpmock.REST("PATCH", "repos/OWNER/REPO"),
// httpmock.StatusStringResponse(204, "{}"))
// },
// stdoutTTY: true,
// },
// {
// name: "owner repo change name notty",
// opts: RenameOptions{
// oldRepoName: "OWNER/REPO",
// newRepoName: "NEW_REPO",
// },
// wantOut: "✓ Renamed repository pxrth9/team2-hack",
// httpStubs: func(reg *httpmock.Registry) {
// reg.Register(
// httpmock.GraphQL(`query RepositoryInfo\b`),
// httpmock.StringResponse(`{ "data":
// { "repository": {
// "id": "THE-ID"} } }`))
// reg.Register(
// httpmock.REST("PATCH", "repos/OWNER/REPO"),
// httpmock.StatusStringResponse(200, "{}"))
// },
// stdoutTTY: false,
// },
// {
// name: "nonowner repo change name tty",
// opts: RenameOptions{
// oldRepoName: "NON_OWNER/REPO",
// newRepoName: "NEW_REPO",
// },
// wantOut: "X you do not own this repository",
// httpStubs: func(reg *httpmock.Registry) {
// reg.Register(
// httpmock.GraphQL(`query RepositoryInfo\b`),
// httpmock.StringResponse(`{ "data":
// { "repository": {
// "id": "THE-ID"} } }`))
// reg.Register(
// httpmock.REST("PATCH", "repos/NON_OWNER/REPO"),
// httpmock.StatusStringResponse(200, "{}"))
// },
// stdoutTTY: true,
// },
// {
// name: "non owner repo change name notty",
// opts: RenameOptions{
// oldRepoName: "NON_OWNER/REPO",
// newRepoName: "NEW_REPO",
// },
// wantOut: "X you do not own this repository",
// httpStubs: func(reg *httpmock.Registry) {
// reg.Register(
// httpmock.GraphQL(`query RepositoryInfo\b`),
// httpmock.StringResponse(`{ "data":
// { "repository": {
// "id": "THE-ID"} } }`))
// reg.Register(
// httpmock.REST("PATCH", "repos/NON_OWNER/REPO"),
// httpmock.StatusStringResponse(200, "{}"))
// },
// stdoutTTY: false,
// },
// }
for _, tt := range testCases {
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
// for _, tt := range testCases {
// t.Run(tt.name, func(t * testing.T) {
// reg := &httpmock.Registry{}
// defer reg.Verify(t)
// if tt.httpStubs != nil {
// tt.httpStubs(reg)
// }
io, _, stdout, _ := iostreams.Test()
tt.opts.IO = io
// io, _, stdout, _ := iostreams.Test()
t.Run(tt.name, func(t *testing.T) {
defer reg.Verify(t)
io.SetStderrTTY(tt.stdoutTTY)
// tt.opts.HttpClient = func() (*http.Client, error) {
// return &http.Client{Transport: reg}, nil
// }
err := renameRun(&tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.wantOut, stdout.String())
})
}
}
// tt.opts.IO = io
// io.SetStderrTTY(tt.stdoutTTY)
// io.SetStdoutTTY(tt.stdoutTTY)
// err := renameRun(&tt.opts)
// assert.NoError(t, err)
// assert.Equal(t, tt.wantOut, stdout.String())
// })
// }
// }