X Tutup
Skip to content

Commit 1370533

Browse files
committed
tweak tests and add extra validations
1 parent 3c8e163 commit 1370533

File tree

3 files changed

+162
-18
lines changed

3 files changed

+162
-18
lines changed

api/queries_repo.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,12 @@ func InitRepoHostname(repo *Repository, hostname string) *Repository {
442442

443443
// RepositoryV3 is the repository result from GitHub API v3
444444
type repositoryV3 struct {
445-
NodeID string
445+
NodeID string `json:"node_id"`
446446
Name string
447447
CreatedAt time.Time `json:"created_at"`
448448
Owner struct {
449449
Login string
450+
ID string
450451
}
451452
Private bool
452453
HTMLUrl string `json:"html_url"`
@@ -1133,8 +1134,10 @@ func CreateRepoTransformToV4(apiClient *Client, hostname string, method string,
11331134
CreatedAt: responsev3.CreatedAt,
11341135
Owner: RepositoryOwner{
11351136
Login: responsev3.Owner.Login,
1137+
ID: responsev3.Owner.ID,
11361138
},
1137-
hostname: responsev3.hostname,
1139+
ID: responsev3.NodeID,
1140+
hostname: hostname,
11381141
URL: responsev3.HTMLUrl,
11391142
IsPrivate: responsev3.Private,
11401143
}, nil

pkg/cmd/repo/create/create.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
9898
return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are added only when a specific repository name is passed")}
9999
}
100100

101+
if opts.Template != "" && (opts.GitIgnoreTemplate != "" || opts.LicenseTemplate != "") {
102+
return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are not added when template is provided")}
103+
}
104+
101105
if !opts.IO.CanPrompt() {
102106
if opts.Name == "" {
103107
return &cmdutil.FlagError{Err: errors.New("name argument required when not running interactively")}
@@ -217,15 +221,16 @@ func createRun(opts *CreateOptions) error {
217221
return err
218222
}
219223

220-
if gitIgnoreTemplate == "" {
224+
// GitIgnore and License templates not added when a template repository is passed.
225+
if gitIgnoreTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
221226
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
222227
if err != nil {
223228
return err
224229
}
225230
gitIgnoreTemplate = gt
226231
}
227232

228-
if repoLicenseTemplate == "" {
233+
if repoLicenseTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() {
229234
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
230235
if err != nil {
231236
return err
@@ -243,6 +248,7 @@ func createRun(opts *CreateOptions) error {
243248
return fmt.Errorf("argument error: %w", err)
244249
}
245250
} else {
251+
fmt.Println("came inside")
246252
host, err := cfg.DefaultHost()
247253
if err != nil {
248254
return err

pkg/cmd/repo/create/create_test.go

Lines changed: 149 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package create
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io/ioutil"
87
"net/http"
98
"testing"
@@ -98,6 +97,21 @@ func TestRepoCreate(t *testing.T) {
9897
Value: "PRIVATE",
9998
},
10099
})
100+
101+
as.Stub([]*prompt.QuestionStub{
102+
{
103+
Name: "addGitIgnore",
104+
Value: false,
105+
},
106+
})
107+
108+
as.Stub([]*prompt.QuestionStub{
109+
{
110+
Name: "addLicense",
111+
Value: false,
112+
},
113+
})
114+
101115
as.Stub([]*prompt.QuestionStub{
102116
{
103117
Name: "confirmSubmit",
@@ -231,6 +245,21 @@ func TestRepoCreate_org(t *testing.T) {
231245
Value: "PRIVATE",
232246
},
233247
})
248+
249+
as.Stub([]*prompt.QuestionStub{
250+
{
251+
Name: "addGitIgnore",
252+
Value: false,
253+
},
254+
})
255+
256+
as.Stub([]*prompt.QuestionStub{
257+
{
258+
Name: "addLicense",
259+
Value: false,
260+
},
261+
})
262+
234263
as.Stub([]*prompt.QuestionStub{
235264
{
236265
Name: "confirmSubmit",
@@ -307,6 +336,21 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
307336
Value: "PRIVATE",
308337
},
309338
})
339+
340+
as.Stub([]*prompt.QuestionStub{
341+
{
342+
Name: "addGitIgnore",
343+
Value: false,
344+
},
345+
})
346+
347+
as.Stub([]*prompt.QuestionStub{
348+
{
349+
Name: "addLicense",
350+
Value: false,
351+
},
352+
})
353+
310354
as.Stub([]*prompt.QuestionStub{
311355
{
312356
Name: "confirmSubmit",
@@ -458,13 +502,6 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
458502
},
459503
})
460504

461-
as.Stub([]*prompt.QuestionStub{
462-
{
463-
Name: "addGitIgnoreLicense",
464-
Value: false,
465-
},
466-
})
467-
468505
as.Stub([]*prompt.QuestionStub{
469506
{
470507
Name: "confirmSubmit",
@@ -504,7 +541,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
504541
}
505542
}
506543

507-
func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) {
544+
func TestRepoCreate_WithGitIgnore(t *testing.T) {
508545
cs, cmdTeardown := run.Stub()
509546
defer cmdTeardown(t)
510547

@@ -561,7 +598,7 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) {
561598
httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`))
562599
httpClient := &http.Client{Transport: reg}
563600

564-
output, err := runCommand(httpClient, "REPO", true)
601+
output, err := runCommand(httpClient, "OWNER/REPO", true)
565602
if err != nil {
566603
t.Errorf("error running command `repo create`: %v", err)
567604
}
@@ -576,13 +613,111 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) {
576613
LicenseTemplate string
577614
}
578615

579-
if len(reg.Requests) != 2 {
580-
t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests))
616+
if len(reg.Requests) != 3 {
617+
t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests))
581618
}
582619

583-
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
620+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body)
621+
_ = json.Unmarshal(bodyBytes, &reqBody)
622+
if repoName := reqBody.Name; repoName != "REPO" {
623+
t.Errorf("expected %q, got %q", "REPO", repoName)
624+
}
625+
if repoVisibility := reqBody.Visibility; repoVisibility != "PRIVATE" {
626+
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
627+
}
628+
if ownerId := reqBody.OwnerId; ownerId != "OWNERID" {
629+
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
630+
}
631+
}
632+
633+
func TestRepoCreate_WithBothGitIgnoreLicense(t *testing.T) {
634+
cs, cmdTeardown := run.Stub()
635+
defer cmdTeardown(t)
636+
637+
cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "")
638+
cs.Register(`git rev-parse --show-toplevel`, 0, "")
639+
640+
as, surveyTearDown := prompt.InitAskStubber()
641+
defer surveyTearDown()
642+
643+
as.Stub([]*prompt.QuestionStub{
644+
{
645+
Name: "repoVisibility",
646+
Value: "PRIVATE",
647+
},
648+
})
649+
650+
as.Stub([]*prompt.QuestionStub{
651+
{
652+
Name: "addGitIgnore",
653+
Value: true,
654+
},
655+
})
656+
657+
as.Stub([]*prompt.QuestionStub{
658+
{
659+
Name: "chooseGitIgnore",
660+
Value: "Go",
661+
},
662+
})
663+
664+
as.Stub([]*prompt.QuestionStub{
665+
{
666+
Name: "addLicense",
667+
Value: true,
668+
},
669+
})
670+
671+
as.Stub([]*prompt.QuestionStub{
672+
{
673+
Name: "chooseLicense",
674+
Value: "GNU Affero General Public License v3.0",
675+
},
676+
})
677+
678+
as.Stub([]*prompt.QuestionStub{
679+
{
680+
Name: "confirmSubmit",
681+
Value: true,
682+
},
683+
})
684+
685+
reg := &httpmock.Registry{}
686+
reg.Register(
687+
httpmock.REST("GET", "users/OWNER"),
688+
httpmock.StringResponse(`{ "node_id": "OWNERID" }`))
689+
reg.Register(
690+
httpmock.REST("GET", "gitignore/templates"),
691+
httpmock.StringResponse(`["Actionscript","Android","AppceleratorTitanium","Autotools","Bancha","C","C++","Go"]`))
692+
reg.Register(
693+
httpmock.REST("GET", "licenses"),
694+
httpmock.StringResponse(`[{"key": "mit","name": "MIT License"},{"key": "lgpl-3.0","name": "GNU Lesser General Public License v3.0"}]`))
695+
reg.Register(
696+
httpmock.REST("POST", "user/repos"),
697+
httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`))
698+
httpClient := &http.Client{Transport: reg}
699+
700+
output, err := runCommand(httpClient, "OWNER/REPO", true)
701+
if err != nil {
702+
t.Errorf("error running command `repo create`: %v", err)
703+
}
704+
705+
assert.Equal(t, "", output.String())
706+
assert.Equal(t, "✓ Created repository OWNER/REPO on GitHub\n✓ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr())
707+
708+
var reqBody struct {
709+
Name string
710+
Visibility string
711+
OwnerId string
712+
LicenseTemplate string
713+
}
714+
715+
if len(reg.Requests) != 4 {
716+
t.Fatalf("expected 4 HTTP request, got %d", len(reg.Requests))
717+
}
718+
719+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[3].Body)
584720
_ = json.Unmarshal(bodyBytes, &reqBody)
585-
fmt.Println(reqBody, "==========")
586721
if repoName := reqBody.Name; repoName != "REPO" {
587722
t.Errorf("expected %q, got %q", "REPO", repoName)
588723
}

0 commit comments

Comments
 (0)
X Tutup