diff --git a/command/issue.go b/command/issue.go index 5e1dacd861b..5de86da9147 100644 --- a/command/issue.go +++ b/command/issue.go @@ -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) } diff --git a/command/issue_test.go b/command/issue_test.go index 24dc094c5a4..b65ac2d0f5c 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -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 @@ -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 diff --git a/command/pr.go b/command/pr.go index 018488259d8..30228112d41 100644 --- a/command/pr.go +++ b/command/pr.go @@ -362,10 +362,6 @@ 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 @@ -373,7 +369,9 @@ func prView(cmd *cobra.Command, args []string) error { 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) } diff --git a/command/pr_create.go b/command/pr_create.go index 4b489774b3f..b66a7ff5d98 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -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") } } @@ -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") diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 22ea4da723a..e68427b0d40 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -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)() diff --git a/command/pr_test.go b/command/pr_test.go index 95f8dadef27..80785ea8391 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -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")