[repo/create] Create local repo dir with non tty. (#2671)
This addresses issue #2587.
This commit is contained in:
parent
86eb264277
commit
8b5c5896f2
2 changed files with 101 additions and 18 deletions
|
|
@ -273,22 +273,26 @@ func createRun(opts *CreateOptions) error {
|
||||||
if isTTY {
|
if isTTY {
|
||||||
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.SuccessIcon(), remoteURL)
|
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.SuccessIcon(), remoteURL)
|
||||||
}
|
}
|
||||||
} else if opts.IO.CanPrompt() {
|
} else {
|
||||||
doSetup := createLocalDirectory
|
if opts.IO.CanPrompt() {
|
||||||
if !doSetup {
|
if !createLocalDirectory {
|
||||||
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &doSetup)
|
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &createLocalDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if doSetup {
|
if createLocalDirectory {
|
||||||
path := repo.Name
|
path := repo.Name
|
||||||
|
|
||||||
gitInit, err := git.GitCommand("init", path)
|
gitInit, err := git.GitCommand("init", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gitInit.Stdout = stdout
|
isTTY := opts.IO.IsStdoutTTY()
|
||||||
|
if isTTY {
|
||||||
|
gitInit.Stdout = stdout
|
||||||
|
}
|
||||||
gitInit.Stderr = stderr
|
gitInit.Stderr = stderr
|
||||||
err = run.PrepareCmd(gitInit).Run()
|
err = run.PrepareCmd(gitInit).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -304,8 +308,9 @@ func createRun(opts *CreateOptions) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if isTTY {
|
||||||
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
|
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package create
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
@ -20,10 +21,10 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) {
|
func runCommand(httpClient *http.Client, cli string, isTTY bool) (*test.CmdOut, error) {
|
||||||
io, _, stdout, stderr := iostreams.Test()
|
io, _, stdout, stderr := iostreams.Test()
|
||||||
io.SetStdoutTTY(true)
|
io.SetStdoutTTY(isTTY)
|
||||||
io.SetStdinTTY(true)
|
io.SetStdinTTY(isTTY)
|
||||||
fac := &cmdutil.Factory{
|
fac := &cmdutil.Factory{
|
||||||
IOStreams: io,
|
IOStreams: io,
|
||||||
HttpClient: func() (*http.Client, error) {
|
HttpClient: func() (*http.Client, error) {
|
||||||
|
|
@ -106,7 +107,7 @@ func TestRepoCreate(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
output, err := runCommand(httpClient, "REPO")
|
output, err := runCommand(httpClient, "REPO", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error running command `repo create`: %v", err)
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -143,6 +144,83 @@ func TestRepoCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRepoCreate_outsideGitWorkDir(t *testing.T) {
|
||||||
|
reg := &httpmock.Registry{}
|
||||||
|
reg.Register(
|
||||||
|
httpmock.GraphQL(`mutation RepositoryCreate\b`),
|
||||||
|
httpmock.StringResponse(`
|
||||||
|
{ "data": { "createRepository": {
|
||||||
|
"repository": {
|
||||||
|
"id": "REPOID",
|
||||||
|
"url": "https://github.com/OWNER/REPO",
|
||||||
|
"name": "REPO",
|
||||||
|
"owner": {
|
||||||
|
"login": "OWNER"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} } }`))
|
||||||
|
|
||||||
|
httpClient := &http.Client{Transport: reg}
|
||||||
|
|
||||||
|
var seenCmds []*exec.Cmd
|
||||||
|
cmdOutputs := []test.OutputStub{
|
||||||
|
{
|
||||||
|
Error: errors.New("Not a git repository"),
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
}
|
||||||
|
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
|
||||||
|
if len(cmdOutputs) == 0 {
|
||||||
|
t.Fatal("Too many calls to git command")
|
||||||
|
}
|
||||||
|
out := cmdOutputs[0]
|
||||||
|
cmdOutputs = cmdOutputs[1:]
|
||||||
|
seenCmds = append(seenCmds, cmd)
|
||||||
|
return &out
|
||||||
|
})
|
||||||
|
defer restoreCmd()
|
||||||
|
|
||||||
|
output, err := runCommand(httpClient, "REPO --private --confirm", false)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "https://github.com/OWNER/REPO\n", output.String())
|
||||||
|
assert.Equal(t, "", output.Stderr())
|
||||||
|
|
||||||
|
if len(seenCmds) != 3 {
|
||||||
|
t.Fatal("expected three commands to run")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "git rev-parse --show-toplevel", strings.Join(seenCmds[0].Args, " "))
|
||||||
|
assert.Equal(t, "git init REPO", strings.Join(seenCmds[1].Args, " "))
|
||||||
|
assert.Equal(t, "git -C REPO remote add origin https://github.com/OWNER/REPO.git", strings.Join(seenCmds[2].Args, " "))
|
||||||
|
|
||||||
|
var reqBody struct {
|
||||||
|
Query string
|
||||||
|
Variables struct {
|
||||||
|
Input map[string]interface{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(reg.Requests) != 1 {
|
||||||
|
t.Fatalf("expected 1 HTTP request, got %d", len(reg.Requests))
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body)
|
||||||
|
_ = json.Unmarshal(bodyBytes, &reqBody)
|
||||||
|
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {
|
||||||
|
t.Errorf("expected %q, got %q", "REPO", repoName)
|
||||||
|
}
|
||||||
|
if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" {
|
||||||
|
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
|
||||||
|
}
|
||||||
|
if _, ownerSet := reqBody.Variables.Input["ownerId"]; ownerSet {
|
||||||
|
t.Error("expected ownerId not to be set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRepoCreate_org(t *testing.T) {
|
func TestRepoCreate_org(t *testing.T) {
|
||||||
reg := &httpmock.Registry{}
|
reg := &httpmock.Registry{}
|
||||||
reg.Register(
|
reg.Register(
|
||||||
|
|
@ -188,7 +266,7 @@ func TestRepoCreate_org(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
output, err := runCommand(httpClient, "ORG/REPO")
|
output, err := runCommand(httpClient, "ORG/REPO", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error running command `repo create`: %v", err)
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -270,7 +348,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
output, err := runCommand(httpClient, "ORG/REPO --team monkeys")
|
output, err := runCommand(httpClient, "ORG/REPO --team monkeys", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error running command `repo create`: %v", err)
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -353,7 +431,7 @@ func TestRepoCreate_template(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'")
|
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error running command `repo create`: %v", err)
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -441,7 +519,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
output, err := runCommand(httpClient, "")
|
output, err := runCommand(httpClient, "", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("error running command `repo create`: %v", err)
|
t.Errorf("error running command `repo create`: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue