write test

Signed-off-by: Joe Hattori <joe2ninja21@gmail.com>
This commit is contained in:
Joe Hattori 2020-06-03 22:49:09 +09:00
parent 035664a0a7
commit e89ddd745d
2 changed files with 39 additions and 20 deletions

View file

@ -125,16 +125,20 @@ func runClone(cloneURL string, args []string) (target string, err error) {
}
func repoClone(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}
cloneURL := args[0]
if !strings.Contains(cloneURL, ":") {
if !strings.Contains(cloneURL, "/") {
ctx := contextForCommand(cmd)
currentUsername, err := ctx.AuthLogin()
currentUser, err := api.CurrentLoginName(apiClient)
if err != nil {
return err
}
fmt.Printf("currentUesrname: %s\n", currentUsername)
cloneURL = currentUsername + "/" + cloneURL
cloneURL = currentUser + "/" + cloneURL
}
cloneURL = formatRemoteURL(cmd, cloneURL)
}
@ -149,12 +153,6 @@ func repoClone(cmd *cobra.Command, args []string) error {
}
if repo != nil {
ctx := contextForCommand(cmd)
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}
parentRepo, err = api.RepoParent(apiClient, repo)
if err != nil {
return err

View file

@ -15,6 +15,7 @@ import (
"github.com/cli/cli/context"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/pkg/httpmock"
"github.com/cli/cli/test"
"github.com/cli/cli/utils"
)
@ -429,11 +430,6 @@ func TestRepoClone(t *testing.T) {
args: "repo clone OWNER/REPO",
want: "git clone https://github.com/OWNER/REPO.git",
},
{
name: "shorthand without username",
args: "repo clone REPO",
want: "git clone https://github.com/OWNER/REPO.git",
},
{
name: "shorthand with directory",
args: "repo clone OWNER/REPO target_directory",
@ -462,11 +458,6 @@ func TestRepoClone(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.NewBlank()
ctx.SetAuthLogin("OWNER")
initContext = func() context.Context {
return ctx
}
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
@ -518,6 +509,36 @@ func TestRepoClone_hasParent(t *testing.T) {
eq(t, strings.Join(cs.Calls[1].Args, " "), "git -C REPO remote add -f upstream https://github.com/hubot/ORIG.git")
}
func TestRepo_withoutUsername(t *testing.T) {
http := initFakeHTTP()
http.Register(
httpmock.GraphQL(`\bviewer\b`),
httpmock.StringResponse(`
{ "data": { "viewer": {
"login": "OWNER"
}}}`))
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
"parent": null
} } }
`))
cs, restore := test.InitCmdStubber()
defer restore()
cs.Stub("") // git clone
output, err := RunCommand("repo clone REPO")
if err != nil {
t.Fatalf("error running command `repo clone`: %v", err)
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "")
eq(t, cs.Count, 1)
eq(t, strings.Join(cs.Calls[0].Args, " "), "git clone https://github.com/OWNER/REPO.git")
}
func TestRepoCreate(t *testing.T) {
ctx := context.NewBlank()
ctx.SetBranch("master")