complete tests

This commit is contained in:
Gowtham Munukutla 2021-05-30 13:42:39 +05:30
parent 9b87b13b80
commit c4beed8276
5 changed files with 133 additions and 25 deletions

View file

@ -220,7 +220,6 @@ func (c Client) REST(hostname string, method string, p string, body io.Reader, d
if err != nil {
return err
}
err = json.Unmarshal(b, &data)
if err != nil {
return err

View file

@ -455,6 +455,8 @@ type RepositoryV3 struct {
Owner struct {
Login string
}
Private bool
HTMLUrl string `json:"html_url"`
Parent *RepositoryV3
hostname string
}

View file

@ -202,7 +202,11 @@ func createRun(opts *CreateOptions) error {
return err
}
gt, lt := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host)
gt, lt, err := interactiveGitIgnoreLicense(api.NewClientFromHTTP(httpClient), host)
if err != nil {
return err
}
gitIgnoreTemplate = gt
repoLicenseTemplate = lt
} else {
@ -311,6 +315,7 @@ func createRun(opts *CreateOptions) error {
if err != nil {
return err
}
remoteURL := ghrepo.FormatRemoteURL(repo, protocol)
if inLocalRepo {
@ -354,21 +359,24 @@ func createRun(opts *CreateOptions) error {
return nil
}
func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string) {
func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, string, error) {
var answers []string
var addBoth bool
var initialQs []*survey.Question
optionToSkip := &survey.Question{
Name: "optionToSkip",
addGitIgnoreLicense := &survey.Question{
Name: "addGitIgnoreLicense",
Prompt: &survey.Confirm{
Message: "Would you like to add a .gitignore or a license?",
Default: false,
},
}
initialQs = append(initialQs, optionToSkip)
survey.Ask(initialQs, &addBoth)
initialQs = append(initialQs, addGitIgnoreLicense)
err := prompt.SurveyAsk(initialQs, &addBoth)
if err != nil {
return "", "", err
}
if addBoth {
var addQs []*survey.Question
@ -382,7 +390,11 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s
}
addQs = append(addQs, gitIgnoreLicenseQuestion)
survey.Ask(addQs, &answers)
var answers []string
err = prompt.SurveyAsk(addQs, &answers)
if err != nil {
return "", "", err
}
wantGitIgnore, wantLicense := false, false
@ -439,14 +451,17 @@ func interactiveGitIgnoreLicense(client *api.Client, hostname string) (string, s
RepoLicense string
}{}
survey.Ask(qs, &templateAnswers)
return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense]
err = prompt.SurveyAsk(qs, &templateAnswers)
if err != nil {
return "", "", err
}
return templateAnswers.RepoGitIgnore, licenseKey[templateAnswers.RepoLicense], nil
}
return "", ""
return "", "", nil
}
return "", ""
return "", "", nil
}
func localInit(io *iostreams.IOStreams, remoteURL, path, checkoutBranch string) error {

View file

@ -455,19 +455,15 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
Name: "repoVisibility",
Value: "PRIVATE",
},
})
as.Stub([]*prompt.QuestionStub{
{
Name: "optionToSkip",
Value: true,
},
{
Name: "gitIgnoreLicense",
Value: "license",
},
{
Name: "repoLicense",
Value: "Apache License 2.0",
Name: "addGitIgnoreLicense",
Value: false,
},
})
as.Stub([]*prompt.QuestionStub{
{
Name: "confirmSubmit",
@ -506,3 +502,100 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
t.Errorf("expected %q, got %q", "OWNERID", ownerId)
}
}
func TestRepoCreate_withoutNameArgWithGitIgnoreLicense(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: "repoName",
Value: "OWNER/REPO",
},
{
Name: "repoDescription",
Value: "DESCRIPTION",
},
{
Name: "repoVisibility",
Value: "PRIVATE",
},
})
as.Stub([]*prompt.QuestionStub{
{
Name: "addGitIgnoreLicense",
Value: true,
},
})
as.Stub([]*prompt.QuestionStub{
{
Name: "gitIgnoreLicense",
Value: []string{"license"},
},
})
as.Stub([]*prompt.QuestionStub{
{
Name: "repoLicense",
Value: "Apache License 2.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", "licenses"),
httpmock.StringResponse(`[{"key":"apache-2.0", "name":"Apache License 2.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, "", 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) != 3 {
t.Fatalf("expected 3 HTTP request, got %d", len(reg.Requests))
}
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)
}
}

View file

@ -120,8 +120,7 @@ func repoCreate(client *http.Client, hostname string, input repoCreateInput, tem
if err != nil {
return nil, err
}
fmt.Println(&responseV3, "==========response v3===========")
return &responseV3, nil
return api.InitRepoV3Hostname(&responseV3, hostname), nil
}
err := apiClient.GraphQL(hostname, `