Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test: Fix login from opening URLs automatically #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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