Skip to content

Commit

Permalink
test: Fix login from opening URLs automatically (#334)
Browse files Browse the repository at this point in the history
When using VS Code's test on save, this was funny behavior 馃ぃ.
  • Loading branch information
kylecarbs committed Feb 21, 2022
1 parent 693f457 commit 3b57619
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
21 changes: 11 additions & 10 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func login() *cobra.Command {

authURL := *serverURL
authURL.Path = serverURL.Path + "/cli-auth"
if err := openURL(authURL.String()); err != nil {
if err := openURL(cmd, authURL.String()); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Open the following in your browser:\n\n\t%s\n\n", authURL.String())
} else {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Your browser has been opened to visit:\n\n\t%s\n\n", authURL.String())
Expand Down Expand Up @@ -211,21 +211,22 @@ func isWSL() (bool, error) {
}

// openURL opens the provided URL via user's default browser
func openURL(urlToOpen string) error {
var cmd string
var args []string

func openURL(cmd *cobra.Command, urlToOpen string) error {
noOpen, err := cmd.Flags().GetBool(varNoOpen)
if err != nil {
panic(err)
}
if noOpen {
return xerrors.New("opening is blocked")
}
wsl, err := isWSL()
if err != nil {
return xerrors.Errorf("test running Windows Subsystem for Linux: %w", err)
}

if wsl {
cmd = "cmd.exe"
args = []string{"/c", "start"}
urlToOpen = strings.ReplaceAll(urlToOpen, "&", "^&")
args = append(args, urlToOpen)
return exec.Command(cmd, args...).Start()
// #nosec
return exec.Command("cmd.exe", "/c", "start", strings.ReplaceAll(urlToOpen, "&", "^&")).Start()
}

return browser.OpenURL(urlToOpen)
Expand Down
4 changes: 2 additions & 2 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestLogin(t *testing.T) {
})
require.NoError(t, err)

root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty", "--no-open")
pty := ptytest.New(t)
root.SetIn(pty.Input())
root.SetOut(pty.Output())
Expand All @@ -94,7 +94,7 @@ func TestLogin(t *testing.T) {
})
require.NoError(t, err)

root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty", "--no-open")
pty := ptytest.New(t)
root.SetIn(pty.Input())
root.SetOut(pty.Output())
Expand Down
6 changes: 6 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

const (
varGlobalConfig = "global-config"
varNoOpen = "no-open"
varForceTty = "force-tty"
)

Expand Down Expand Up @@ -71,6 +72,11 @@ func Root() *cobra.Command {
// This should never return an error, because we just added the `--force-tty`` flag prior to calling MarkHidden.
panic(err)
}
cmd.PersistentFlags().Bool(varNoOpen, false, "Block automatically opening URLs in the browser.")
err = cmd.PersistentFlags().MarkHidden(varNoOpen)
if err != nil {
panic(err)
}

return cmd
}
Expand Down

0 comments on commit 3b57619

Please sign in to comment.