Skip to content

Commit

Permalink
restore nontty + web but suppress informational prints
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Jul 20, 2020
1 parent 8cb985d commit 1ca5ce8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
4 changes: 3 additions & 1 deletion command/issue.go
Expand Up @@ -517,7 +517,9 @@ func issueCreate(cmd *cobra.Command, args []string) error {
} else if len(nonLegacyTemplateFiles) > 1 {
openURL += "/choose"
}
cmd.Printf("Opening %s in your browser.\n", displayURL(openURL))
if connectedToTerminal(cmd) {
cmd.Printf("Opening %s in your browser.\n", displayURL(openURL))
}
return utils.OpenInBrowser(openURL)
}

Expand Down
4 changes: 4 additions & 0 deletions command/issue_test.go
Expand Up @@ -753,6 +753,8 @@ func TestIssueCreate_web(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

defer stubTerminal(true)()

var seenCmd *exec.Cmd
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
seenCmd = cmd
Expand All @@ -779,6 +781,8 @@ func TestIssueCreate_webTitleBody(t *testing.T) {
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

defer stubTerminal(true)()

var seenCmd *exec.Cmd
restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
seenCmd = cmd
Expand Down
8 changes: 3 additions & 5 deletions command/pr.go
Expand Up @@ -362,18 +362,16 @@ func prView(cmd *cobra.Command, args []string) error {
return err
}

if web && !connectedToTerminal(cmd) {
return errors.New("--web unsupported when not attached to a tty")
}

pr, _, err := prFromArgs(ctx, apiClient, cmd, args)
if err != nil {
return err
}
openURL := pr.URL

if web {
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
if connectedToTerminal(cmd) {
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
}
return utils.OpenInBrowser(openURL)
}

Expand Down
11 changes: 5 additions & 6 deletions command/pr_create.go
Expand Up @@ -226,10 +226,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
}

if !connectedToTerminal(cmd) {
if isWeb {
return errors.New("--web unsupported when not attached to a tty")
}
if !cmd.Flags().Changed("title") && !autofill {
if !isWeb && (!cmd.Flags().Changed("title") && !autofill) {
return errors.New("--title or --fill required when not attached to a tty")
}
}
Expand Down Expand Up @@ -368,8 +365,10 @@ func prCreate(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
// TODO could exceed max url length for explorer
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
if connectedToTerminal(cmd) {
// TODO could exceed max url length for explorer
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
}
return utils.OpenInBrowser(openURL)
} else {
panic("Unreachable state")
Expand Down
33 changes: 33 additions & 0 deletions command/pr_create_test.go
Expand Up @@ -15,6 +15,39 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPRCreate_nontty_web(t *testing.T) {
initBlankContext("", "OWNER/REPO", "feature")
defer stubTerminal(false)()
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "forks": { "nodes": [
] } } } }
`))

cs, cmdTeardown := test.InitCmdStubber()
defer cmdTeardown()

cs.Stub("") // git config --get-regexp (determineTrackingBranch)
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
cs.Stub("") // git status
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
cs.Stub("") // git push
cs.Stub("") // browser

output, err := RunCommand(`pr create --web`)
eq(t, err, nil)

eq(t, output.String(), "")
eq(t, output.Stderr(), "")

eq(t, len(cs.Calls), 6)
eq(t, strings.Join(cs.Calls[4].Args, " "), "git push --set-upstream origin HEAD:feature")
browserCall := cs.Calls[5].Args
eq(t, browserCall[len(browserCall)-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")

}

func TestPRCreate_nontty_insufficient_flags(t *testing.T) {
initBlankContext("", "OWNER/REPO", "feature")
defer stubTerminal(false)()
Expand Down
15 changes: 0 additions & 15 deletions command/pr_test.go
Expand Up @@ -1035,21 +1035,6 @@ func TestPRView_web_branchWithOwnerArg(t *testing.T) {
eq(t, url, "https://github.com/hubot/REPO/pull/23")
}

func TestPrView_web_nontty(t *testing.T) {
initBlankContext("", "OWNER/REPO", "master")
defer stubTerminal(false)()
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

output, err := RunCommand("pr view -w")
if err == nil {
t.Fatal("expected error")
}

assert.Equal(t, "--web unsupported when not attached to a tty", err.Error())
assert.Equal(t, "", output.String())
}

func TestReplaceExcessiveWhitespace(t *testing.T) {
eq(t, replaceExcessiveWhitespace("hello\ngoodbye"), "hello goodbye")
eq(t, replaceExcessiveWhitespace(" hello goodbye "), "hello goodbye")
Expand Down

0 comments on commit 1ca5ce8

Please sign in to comment.