final touches

This commit is contained in:
Parth Patel 2021-10-12 18:46:15 -04:00
parent 7e6cf79355
commit c639161394
2 changed files with 89 additions and 113 deletions

View file

@ -64,9 +64,8 @@ func renameRun(opts *RenameOptions) error {
apiClient := api.NewClientFromHTTP(httpClient)
oldRepoName := opts.oldRepoName
var currentUser string
if !strings.Contains(oldRepoName, "/") {
currentUser, err = api.CurrentLoginName(apiClient, ghinstance.Default())
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
@ -85,10 +84,6 @@ func renameRun(opts *RenameOptions) error {
return err
}
if currentUser != repoDetails.Owner.Login {
return fmt.Errorf("%s you do not own this repository", cs.FailureIcon())
}
input := renameRepo{
Owner: repo.RepoOwner(),
Repository: repo.RepoName(),
@ -97,11 +92,11 @@ func renameRun(opts *RenameOptions) error {
err = runRename(apiClient, repo.RepoHost(), repoDetails, input)
if err != nil {
return err
return fmt.Errorf("API called failed: %s, please check your parameters", err.Error())
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Renamed repository %s\n", cs.SuccessIcon(), currentUser+"/"+newRepoName)
fmt.Fprintf(opts.IO.Out, "%s Renamed repository %s\n", cs.SuccessIcon(), repo.RepoOwner()+"/"+newRepoName)
}
return nil

View file

@ -1,9 +1,11 @@
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"
@ -67,112 +69,91 @@ 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 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,
// },
// }
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\n",
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{
"data": {
"repository": {
"id": "THE-ID",
"name": "REPO",
"owner": {
"login": "OWNER"
}
}
}
}`))
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",
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{
"data": {
"repository": {
"id": "THE-ID",
"name": "REPO",
"owner": {
"login": "OWNER"
}
}
}
}`))
reg.Register(
httpmock.REST("PATCH", "repos/OWNER/REPO"),
httpmock.StatusStringResponse(200, "{}"))
},
stdoutTTY: false,
},
}
// 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)
// }
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
}
// io, _, stdout, _ := iostreams.Test()
io, _, stdout, _ := iostreams.Test()
tt.opts.IO = io
// tt.opts.HttpClient = func() (*http.Client, error) {
// return &http.Client{Transport: reg}, nil
// }
t.Run(tt.name, func(t *testing.T) {
defer reg.Verify(t)
io.SetStderrTTY(tt.stdoutTTY)
io.SetStdoutTTY(tt.stdoutTTY)
// 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())
// })
// }
// }
err := renameRun(&tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.wantOut, stdout.String())
})
}
}