From 137053399e53c87dd747db18047a197701be07e8 Mon Sep 17 00:00:00 2001 From: Gowtham Munukutla Date: Thu, 17 Jun 2021 10:17:26 +0530 Subject: [PATCH] tweak tests and add extra validations --- api/queries_repo.go | 7 +- pkg/cmd/repo/create/create.go | 10 +- pkg/cmd/repo/create/create_test.go | 163 ++++++++++++++++++++++++++--- 3 files changed, 162 insertions(+), 18 deletions(-) diff --git a/api/queries_repo.go b/api/queries_repo.go index 616e1a2c3..0fcf517d6 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -442,11 +442,12 @@ func InitRepoHostname(repo *Repository, hostname string) *Repository { // RepositoryV3 is the repository result from GitHub API v3 type repositoryV3 struct { - NodeID string + NodeID string `json:"node_id"` Name string CreatedAt time.Time `json:"created_at"` Owner struct { Login string + ID string } Private bool HTMLUrl string `json:"html_url"` @@ -1133,8 +1134,10 @@ func CreateRepoTransformToV4(apiClient *Client, hostname string, method string, CreatedAt: responsev3.CreatedAt, Owner: RepositoryOwner{ Login: responsev3.Owner.Login, + ID: responsev3.Owner.ID, }, - hostname: responsev3.hostname, + ID: responsev3.NodeID, + hostname: hostname, URL: responsev3.HTMLUrl, IsPrivate: responsev3.Private, }, nil diff --git a/pkg/cmd/repo/create/create.go b/pkg/cmd/repo/create/create.go index fd2f07e7d..f42b93515 100644 --- a/pkg/cmd/repo/create/create.go +++ b/pkg/cmd/repo/create/create.go @@ -98,6 +98,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are added only when a specific repository name is passed")} } + if opts.Template != "" && (opts.GitIgnoreTemplate != "" || opts.LicenseTemplate != "") { + return &cmdutil.FlagError{Err: errors.New(".gitignore and license templates are not added when template is provided")} + } + if !opts.IO.CanPrompt() { if opts.Name == "" { return &cmdutil.FlagError{Err: errors.New("name argument required when not running interactively")} @@ -217,7 +221,8 @@ func createRun(opts *CreateOptions) error { return err } - if gitIgnoreTemplate == "" { + // GitIgnore and License templates not added when a template repository is passed. + if gitIgnoreTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() { gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host) if err != nil { return err @@ -225,7 +230,7 @@ func createRun(opts *CreateOptions) error { gitIgnoreTemplate = gt } - if repoLicenseTemplate == "" { + if repoLicenseTemplate == "" && opts.Template == "" && opts.IO.CanPrompt() { lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host) if err != nil { return err @@ -243,6 +248,7 @@ func createRun(opts *CreateOptions) error { return fmt.Errorf("argument error: %w", err) } } else { + fmt.Println("came inside") host, err := cfg.DefaultHost() if err != nil { return err diff --git a/pkg/cmd/repo/create/create_test.go b/pkg/cmd/repo/create/create_test.go index 5682545a0..780c0881a 100644 --- a/pkg/cmd/repo/create/create_test.go +++ b/pkg/cmd/repo/create/create_test.go @@ -3,7 +3,6 @@ package create import ( "bytes" "encoding/json" - "fmt" "io/ioutil" "net/http" "testing" @@ -98,6 +97,21 @@ func TestRepoCreate(t *testing.T) { Value: "PRIVATE", }, }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addGitIgnore", + Value: false, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addLicense", + Value: false, + }, + }) + as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -231,6 +245,21 @@ func TestRepoCreate_org(t *testing.T) { Value: "PRIVATE", }, }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addGitIgnore", + Value: false, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addLicense", + Value: false, + }, + }) + as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -307,6 +336,21 @@ func TestRepoCreate_orgWithTeam(t *testing.T) { Value: "PRIVATE", }, }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addGitIgnore", + Value: false, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addLicense", + Value: false, + }, + }) + as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -458,13 +502,6 @@ func TestRepoCreate_withoutNameArg(t *testing.T) { }, }) - as.Stub([]*prompt.QuestionStub{ - { - Name: "addGitIgnoreLicense", - Value: false, - }, - }) - as.Stub([]*prompt.QuestionStub{ { Name: "confirmSubmit", @@ -504,7 +541,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) { } } -func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) { +func TestRepoCreate_WithGitIgnore(t *testing.T) { cs, cmdTeardown := run.Stub() defer cmdTeardown(t) @@ -561,7 +598,7 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) { httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`)) httpClient := &http.Client{Transport: reg} - output, err := runCommand(httpClient, "REPO", true) + output, err := runCommand(httpClient, "OWNER/REPO", true) if err != nil { t.Errorf("error running command `repo create`: %v", err) } @@ -576,13 +613,111 @@ func TestRepoCreate_WithGitIgnoreLicense(t *testing.T) { LicenseTemplate string } - if len(reg.Requests) != 2 { - t.Fatalf("expected 2 HTTP request, got %d", len(reg.Requests)) + if len(reg.Requests) != 3 { + t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests)) } - bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body) + bodyBytes, _ := ioutil.ReadAll(reg.Requests[2].Body) + _ = json.Unmarshal(bodyBytes, &reqBody) + if repoName := reqBody.Name; repoName != "REPO" { + t.Errorf("expected %q, got %q", "REPO", repoName) + } + if repoVisibility := reqBody.Visibility; repoVisibility != "PRIVATE" { + t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility) + } + if ownerId := reqBody.OwnerId; ownerId != "OWNERID" { + t.Errorf("expected %q, got %q", "OWNERID", ownerId) + } +} + +func TestRepoCreate_WithBothGitIgnoreLicense(t *testing.T) { + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) + + cs.Register(`git remote add -f origin https://github\.com/OWNER/REPO\.git`, 0, "") + cs.Register(`git rev-parse --show-toplevel`, 0, "") + + as, surveyTearDown := prompt.InitAskStubber() + defer surveyTearDown() + + as.Stub([]*prompt.QuestionStub{ + { + Name: "repoVisibility", + Value: "PRIVATE", + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addGitIgnore", + Value: true, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "chooseGitIgnore", + Value: "Go", + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "addLicense", + Value: true, + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "chooseLicense", + Value: "GNU Affero General Public License v3.0", + }, + }) + + as.Stub([]*prompt.QuestionStub{ + { + Name: "confirmSubmit", + Value: true, + }, + }) + + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST("GET", "users/OWNER"), + httpmock.StringResponse(`{ "node_id": "OWNERID" }`)) + reg.Register( + httpmock.REST("GET", "gitignore/templates"), + httpmock.StringResponse(`["Actionscript","Android","AppceleratorTitanium","Autotools","Bancha","C","C++","Go"]`)) + reg.Register( + httpmock.REST("GET", "licenses"), + httpmock.StringResponse(`[{"key": "mit","name": "MIT License"},{"key": "lgpl-3.0","name": "GNU Lesser General Public License v3.0"}]`)) + reg.Register( + httpmock.REST("POST", "user/repos"), + httpmock.StringResponse(`{"name":"REPO", "owner":{"login": "OWNER"}, "html_url":"https://github.com/OWNER/REPO"}`)) + httpClient := &http.Client{Transport: reg} + + output, err := runCommand(httpClient, "OWNER/REPO", true) + if err != nil { + t.Errorf("error running command `repo create`: %v", err) + } + + assert.Equal(t, "", output.String()) + assert.Equal(t, "āœ“ Created repository OWNER/REPO on GitHub\nāœ“ Added remote https://github.com/OWNER/REPO.git\n", output.Stderr()) + + var reqBody struct { + Name string + Visibility string + OwnerId string + LicenseTemplate string + } + + if len(reg.Requests) != 4 { + t.Fatalf("expected 4 HTTP request, got %d", len(reg.Requests)) + } + + bodyBytes, _ := ioutil.ReadAll(reg.Requests[3].Body) _ = json.Unmarshal(bodyBytes, &reqBody) - fmt.Println(reqBody, "==========") if repoName := reqBody.Name; repoName != "REPO" { t.Errorf("expected %q, got %q", "REPO", repoName) }