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

Skip to content

Commit 1a9fc85

Browse files
committed
test: Fix login from opening URLs automatically
When using VS Code's test on save, this was funny behavior 🤣.
1 parent 91bf863 commit 1a9fc85

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

cli/login.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func login() *cobra.Command {
153153

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

213213
// openURL opens the provided URL via user's default browser
214-
func openURL(urlToOpen string) error {
215-
var cmd string
216-
var args []string
217-
214+
func openURL(cmd *cobra.Command, urlToOpen string) error {
215+
noOpen, err := cmd.Flags().GetBool(varNoOpen)
216+
if err != nil {
217+
panic(err)
218+
}
219+
if noOpen {
220+
return xerrors.New("opening is blocked")
221+
}
218222
wsl, err := isWSL()
219223
if err != nil {
220224
return xerrors.Errorf("test running Windows Subsystem for Linux: %w", err)
221225
}
222226

223227
if wsl {
224-
cmd = "cmd.exe"
225-
args = []string{"/c", "start"}
226-
urlToOpen = strings.ReplaceAll(urlToOpen, "&", "^&")
227-
args = append(args, urlToOpen)
228-
return exec.Command(cmd, args...).Start()
228+
// #nosec
229+
return exec.Command("cmd.exe", "/c", "start", strings.ReplaceAll(urlToOpen, "&", "^&")).Start()
229230
}
230231

231232
return browser.OpenURL(urlToOpen)

cli/login_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestLogin(t *testing.T) {
6969
})
7070
require.NoError(t, err)
7171

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

97-
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
97+
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty", "--no-open")
9898
pty := ptytest.New(t)
9999
root.SetIn(pty.Input())
100100
root.SetOut(pty.Output())

cli/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
const (
2222
varGlobalConfig = "global-config"
23+
varNoOpen = "no-open"
2324
varForceTty = "force-tty"
2425
)
2526

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

7581
return cmd
7682
}

0 commit comments

Comments
 (0)