restore nontty + web but suppress informational prints

This commit is contained in:
vilmibm 2020-07-20 12:33:25 -05:00
parent 168bd33bc9
commit bffcbce716
6 changed files with 48 additions and 27 deletions

View file

@ -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)
}

View file

@ -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

View file

@ -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)
}

View file

@ -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")

View file

@ -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)()

View file

@ -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")