Skip to content

Commit 8b5c589

Browse files
author
xhqr
authored
[repo/create] Create local repo dir with non tty. (cli#2671)
This addresses issue cli#2587.
1 parent 86eb264 commit 8b5c589

File tree

2 files changed

+101
-18
lines changed

2 files changed

+101
-18
lines changed

pkg/cmd/repo/create/create.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,26 @@ func createRun(opts *CreateOptions) error {
273273
if isTTY {
274274
fmt.Fprintf(stderr, "%s Added remote %s\n", cs.SuccessIcon(), remoteURL)
275275
}
276-
} else if opts.IO.CanPrompt() {
277-
doSetup := createLocalDirectory
278-
if !doSetup {
279-
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &doSetup)
280-
if err != nil {
281-
return err
276+
} else {
277+
if opts.IO.CanPrompt() {
278+
if !createLocalDirectory {
279+
err := prompt.Confirm(fmt.Sprintf("Create a local project directory for %s?", ghrepo.FullName(repo)), &createLocalDirectory)
280+
if err != nil {
281+
return err
282+
}
282283
}
283284
}
284-
if doSetup {
285+
if createLocalDirectory {
285286
path := repo.Name
286287

287288
gitInit, err := git.GitCommand("init", path)
288289
if err != nil {
289290
return err
290291
}
291-
gitInit.Stdout = stdout
292+
isTTY := opts.IO.IsStdoutTTY()
293+
if isTTY {
294+
gitInit.Stdout = stdout
295+
}
292296
gitInit.Stderr = stderr
293297
err = run.PrepareCmd(gitInit).Run()
294298
if err != nil {
@@ -304,8 +308,9 @@ func createRun(opts *CreateOptions) error {
304308
if err != nil {
305309
return err
306310
}
307-
308-
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
311+
if isTTY {
312+
fmt.Fprintf(stderr, "%s Initialized repository in './%s/'\n", cs.SuccessIcon(), path)
313+
}
309314
}
310315
}
311316

pkg/cmd/repo/create/create_test.go

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package create
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"io/ioutil"
78
"net/http"
89
"os/exec"
@@ -20,10 +21,10 @@ import (
2021
"github.com/stretchr/testify/assert"
2122
)
2223

23-
func runCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) {
24+
func runCommand(httpClient *http.Client, cli string, isTTY bool) (*test.CmdOut, error) {
2425
io, _, stdout, stderr := iostreams.Test()
25-
io.SetStdoutTTY(true)
26-
io.SetStdinTTY(true)
26+
io.SetStdoutTTY(isTTY)
27+
io.SetStdinTTY(isTTY)
2728
fac := &cmdutil.Factory{
2829
IOStreams: io,
2930
HttpClient: func() (*http.Client, error) {
@@ -106,7 +107,7 @@ func TestRepoCreate(t *testing.T) {
106107
},
107108
})
108109

109-
output, err := runCommand(httpClient, "REPO")
110+
output, err := runCommand(httpClient, "REPO", true)
110111
if err != nil {
111112
t.Errorf("error running command `repo create`: %v", err)
112113
}
@@ -143,6 +144,83 @@ func TestRepoCreate(t *testing.T) {
143144
}
144145
}
145146

147+
func TestRepoCreate_outsideGitWorkDir(t *testing.T) {
148+
reg := &httpmock.Registry{}
149+
reg.Register(
150+
httpmock.GraphQL(`mutation RepositoryCreate\b`),
151+
httpmock.StringResponse(`
152+
{ "data": { "createRepository": {
153+
"repository": {
154+
"id": "REPOID",
155+
"url": "https://github.com/OWNER/REPO",
156+
"name": "REPO",
157+
"owner": {
158+
"login": "OWNER"
159+
}
160+
}
161+
} } }`))
162+
163+
httpClient := &http.Client{Transport: reg}
164+
165+
var seenCmds []*exec.Cmd
166+
cmdOutputs := []test.OutputStub{
167+
{
168+
Error: errors.New("Not a git repository"),
169+
},
170+
{},
171+
{},
172+
}
173+
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
174+
if len(cmdOutputs) == 0 {
175+
t.Fatal("Too many calls to git command")
176+
}
177+
out := cmdOutputs[0]
178+
cmdOutputs = cmdOutputs[1:]
179+
seenCmds = append(seenCmds, cmd)
180+
return &out
181+
})
182+
defer restoreCmd()
183+
184+
output, err := runCommand(httpClient, "REPO --private --confirm", false)
185+
if err != nil {
186+
t.Errorf("error running command `repo create`: %v", err)
187+
}
188+
189+
assert.Equal(t, "https://github.com/OWNER/REPO\n", output.String())
190+
assert.Equal(t, "", output.Stderr())
191+
192+
if len(seenCmds) != 3 {
193+
t.Fatal("expected three commands to run")
194+
}
195+
196+
assert.Equal(t, "git rev-parse --show-toplevel", strings.Join(seenCmds[0].Args, " "))
197+
assert.Equal(t, "git init REPO", strings.Join(seenCmds[1].Args, " "))
198+
assert.Equal(t, "git -C REPO remote add origin https://github.com/OWNER/REPO.git", strings.Join(seenCmds[2].Args, " "))
199+
200+
var reqBody struct {
201+
Query string
202+
Variables struct {
203+
Input map[string]interface{}
204+
}
205+
}
206+
207+
if len(reg.Requests) != 1 {
208+
t.Fatalf("expected 1 HTTP request, got %d", len(reg.Requests))
209+
}
210+
211+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body)
212+
_ = json.Unmarshal(bodyBytes, &reqBody)
213+
if repoName := reqBody.Variables.Input["name"].(string); repoName != "REPO" {
214+
t.Errorf("expected %q, got %q", "REPO", repoName)
215+
}
216+
if repoVisibility := reqBody.Variables.Input["visibility"].(string); repoVisibility != "PRIVATE" {
217+
t.Errorf("expected %q, got %q", "PRIVATE", repoVisibility)
218+
}
219+
if _, ownerSet := reqBody.Variables.Input["ownerId"]; ownerSet {
220+
t.Error("expected ownerId not to be set")
221+
}
222+
}
223+
146224
func TestRepoCreate_org(t *testing.T) {
147225
reg := &httpmock.Registry{}
148226
reg.Register(
@@ -188,7 +266,7 @@ func TestRepoCreate_org(t *testing.T) {
188266
},
189267
})
190268

191-
output, err := runCommand(httpClient, "ORG/REPO")
269+
output, err := runCommand(httpClient, "ORG/REPO", true)
192270
if err != nil {
193271
t.Errorf("error running command `repo create`: %v", err)
194272
}
@@ -270,7 +348,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
270348
},
271349
})
272350

273-
output, err := runCommand(httpClient, "ORG/REPO --team monkeys")
351+
output, err := runCommand(httpClient, "ORG/REPO --team monkeys", true)
274352
if err != nil {
275353
t.Errorf("error running command `repo create`: %v", err)
276354
}
@@ -353,7 +431,7 @@ func TestRepoCreate_template(t *testing.T) {
353431
},
354432
})
355433

356-
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'")
434+
output, err := runCommand(httpClient, "REPO --template='OWNER/REPO'", true)
357435
if err != nil {
358436
t.Errorf("error running command `repo create`: %v", err)
359437
}
@@ -441,7 +519,7 @@ func TestRepoCreate_withoutNameArg(t *testing.T) {
441519
},
442520
})
443521

444-
output, err := runCommand(httpClient, "")
522+
output, err := runCommand(httpClient, "", true)
445523
if err != nil {
446524
t.Errorf("error running command `repo create`: %v", err)
447525
}

0 commit comments

Comments
 (0)