X Tutup
Skip to content

Commit c4beed8

Browse files
committed
complete tests
1 parent 9b87b13 commit c4beed8

File tree

5 files changed

+133
-25
lines changed

5 files changed

+133
-25
lines changed

api/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ func (c Client) REST(hostname string, method string, p string, body io.Reader, d
220220
if err != nil {
221221
return err
222222
}
223-
224223
err = json.Unmarshal(b, &data)
225224
if err != nil {
226225
return err

api/queries_repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ type RepositoryV3 struct {
455455
Owner struct {
456456
Login string
457457
}
458+
Private bool
459+
HTMLUrl string `json:"html_url"`
458460
Parent *RepositoryV3
459461
hostname string
460462
}

pkg/cmd/repo/create/create.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ func createRun(opts *CreateOptions) error {
202202
return err
203203
}
204204

205-
gt, lt := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host)
205+
gt, lt, err := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host)
206+
if err != nil {
207+
return err
208+
}
209+
206210
gitIgnoreTemplate = gt
207211
repoLicenseTemplate = lt
208212
} else {
@@ -311,6 +315,7 @@ func createRun(opts *CreateOptions) error {
311315
if err != nil {
312316
return err
313317
}
318+
314319
remoteURL := ghrepo.FormatRemoteURL(repo, protocol)
315320

316321
if inLocalRepo {
@@ -354,21 +359,24 @@ func createRun(opts *CreateOptions) error {
354359
return nil
355360
}
356361

357-
func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string) {
362+
func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string, error) {
358363

359-
var answers []string
360364
var addBoth bool
361365
var initialQs []*survey.Question
362366

363-
optionToSkip := &survey.Question{
364-
Name: "optionToSkip",
367+
addGitIgnoreLicense := &survey.Question{
368+
Name: "addGitIgnoreLicense",
365369
Prompt: &survey.Confirm{
366370
Message: "Would you like to add a .gitignore or a license?",
367371
Default: false,
368372
},
369373
}
370-
initialQs = append(initialQs, optionToSkip)
371-
survey.Ask(initialQs, &addBoth)
374+
375+
initialQs = append(initialQs, addGitIgnoreLicense)
376+
err := prompt.SurveyAsk(initialQs, &addBoth)
377+
if err != nil {
378+
return "", "", err
379+
}
372380

373381
if addBoth {
374382
var addQs []*survey.Question
@@ -382,7 +390,11 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s
382390
}
383391
addQs = append(addQs, gitIgnoreLicenseQuestion)
384392

385-
survey.Ask(addQs, &answers)
393+
var answers []string
394+
err = prompt.SurveyAsk(addQs, &answers)
395+
if err != nil {
396+
return "", "", err
397+
}
386398

387399
wantGitIgnore, wantLicense := false, false
388400

@@ -439,14 +451,17 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s
439451
RepoLicense string
440452
}{}
441453

442-
survey.Ask(qs, &templateAnswers)
443-
return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense]
454+
err = prompt.SurveyAsk(qs, &templateAnswers)
455+
if err != nil {
456+
return "", "", err
457+
}
458+
return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense], nil
444459

445460
}
446-
return "", ""
461+
return "", "", nil
447462
}
448463

449-
return "", ""
464+
return "", "", nil
450465
}
451466

452467
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {

pkg/cmd/repo/create/create_test.go

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,15 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
455455
Name: "repoVisibility",
456456
Value: "PRIVATE",
457457
},
458+
})
459+
460+
as.Stub([]*prompt.QuestionStub{
458461
{
459-
Name: "optionToSkip",
460-
Value: true,
461-
},
462-
{
463-
Name: "gitIgnoreLicense",
464-
Value: "license",
465-
},
466-
{
467-
Name: "repoLicense",
468-
Value: "Apache License 2.0",
462+
Name: "addGitIgnoreLicense",
463+
Value: false,
469464
},
470465
})
466+
471467
as.Stub([]*prompt.QuestionStub{
472468
{
473469
Name: "confirmSubmit",
@@ -506,3 +502,100 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
506502
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
507503
}
508504
}
505+
506+
func TestRepoCreate_withoutNameArgWithGitIgnoreLicense(t *testing.T) {
507+
cs, cmdTeardown := run.Stub()
508+
defer cmdTeardown(t)
509+
510+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
511+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
512+
513+
as, surveyTearDown := prompt.InitAskStubber()
514+
defer surveyTearDown()
515+
516+
as.Stub([]*prompt.QuestionStub{
517+
{
518+
Name: "repoName",
519+
Value: "OWNER/REPO",
520+
},
521+
{
522+
Name: "repoDescription",
523+
Value: "DESCRIPTION",
524+
},
525+
{
526+
Name: "repoVisibility",
527+
Value: "PRIVATE",
528+
},
529+
})
530+
531+
as.Stub([]*prompt.QuestionStub{
532+
{
533+
Name: "addGitIgnoreLicense",
534+
Value: true,
535+
},
536+
})
537+
538+
as.Stub([]*prompt.QuestionStub{
539+
{
540+
Name: "gitIgnoreLicense",
541+
Value: []string{"license"},
542+
},
543+
})
544+
545+
as.Stub([]*prompt.QuestionStub{
546+
{
547+
Name: "repoLicense",
548+
Value: "Apache License 2.0",
549+
},
550+
})
551+
552+
as.Stub([]*prompt.QuestionStub{
553+
{
554+
Name: "confirmSubmit",
555+
Value: true,
556+
},
557+
})
558+
559+
reg := &httpmock.Registry{}
560+
reg.Register(
561+
httpmock.REST("GET", "users/OWNER"),
562+
httpmock.StringResponse(`{ "node_id": "OWNERID" }`))
563+
reg.Register(
564+
httpmock.REST("GET", "licenses"),
565+
httpmock.StringResponse(`[{"key":"apache-2.0", "name":"Apache License 2.0"}]`))
566+
reg.Register(
567+
httpmock.REST("POST", "user/repos"),
568+
httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`))
569+
httpClient := &http.Client{Transport: reg}
570+
571+
output, err := runCommand(httpClient, "", true)
572+
if err != nil {
573+
t.Errorf("error running command `repo create`: %v", err)
574+
}
575+
576+
assert.Equal(t, "", output.String())
577+
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
578+
579+
var reqBody struct {
580+
Name string
581+
Visibility string
582+
OwnerId string
583+
LicenseTemplate string
584+
}
585+
586+
if len(reg.Requests) != 3 {
587+
t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests))
588+
}
589+
590+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body)
591+
_ = json.Unmarshal(bodyBytes, &reqBody)
592+
if repoName := reqBody.Name; repoName != "REPO" {
593+
t.Errorf("expected %q, got %q", "REPO", repoName)
594+
}
595+
if repoVisibility := reqBody.Visibility; repoVisibility != "PRIVATE" {
596+
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
597+
}
598+
if ownerId := reqBody.OwnerId; ownerId != "OWNERID" {
599+
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
600+
}
601+
}

pkg/cmd/repo/create/http.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem
120120
if err != nil {
121121
return nil, err
122122
}
123-
fmt.Println(&responseV3, "==========response v3===========")
124-
return &responseV3, nil
123+
return api.InitRepoV3Hostname(&responseV3, hostname), nil
125124
}
126125

127126
err := apiClient.GraphQL(hostname, `

0 commit comments

Comments
 (0)
X Tutup