Skip to content

Commit 5a88971

Browse files
committed
fix: prevent tests from opening browser windows
Replace hardcoded openBrowser with injectable openBrowserFunc so TestLoginInteractive_* tests use a no-op instead of exec open.
1 parent 3204043 commit 5a88971

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

internal/auth/login.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func pollOnce(pollURL string) (*cliPollResponse, bool, error) {
181181
return nil, false, nil
182182
}
183183

184-
func openBrowser(url string) error {
184+
// openBrowserFunc is the function used to open URLs in the browser.
185+
// It is a variable so tests can replace it with a no-op.
186+
var openBrowserFunc = func(url string) error {
185187
return exec.Command("open", url).Start()
186188
}
189+
190+
func openBrowser(url string) error {
191+
return openBrowserFunc(url)
192+
}

internal/auth/login_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ func withFastPoll(t *testing.T) {
2222
t.Cleanup(func() { pollInterval = origInterval })
2323
}
2424

25+
func withNoBrowser(t *testing.T) {
26+
t.Helper()
27+
orig := openBrowserFunc
28+
openBrowserFunc = func(url string) error { return nil }
29+
t.Cleanup(func() { openBrowserFunc = orig })
30+
}
31+
2532
func TestStartAuthSession_Success(t *testing.T) {
2633
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2734
assert.Equal(t, "POST", r.Method)
@@ -300,6 +307,7 @@ func TestPollOnce_InvalidJSON(t *testing.T) {
300307

301308
func TestLoginInteractive_SuccessRFC3339(t *testing.T) {
302309
withFastPoll(t)
310+
withNoBrowser(t)
303311
tmpDir := t.TempDir()
304312
t.Setenv("HOME", tmpDir)
305313

@@ -340,6 +348,7 @@ func TestLoginInteractive_SuccessRFC3339(t *testing.T) {
340348

341349
func TestLoginInteractive_SuccessSQLiteFormat(t *testing.T) {
342350
withFastPoll(t)
351+
withNoBrowser(t)
343352
tmpDir := t.TempDir()
344353
t.Setenv("HOME", tmpDir)
345354

@@ -369,6 +378,7 @@ func TestLoginInteractive_SuccessSQLiteFormat(t *testing.T) {
369378
}
370379

371380
func TestLoginInteractive_StartAuthSessionError(t *testing.T) {
381+
withNoBrowser(t)
372382
tmpDir := t.TempDir()
373383
t.Setenv("HOME", tmpDir)
374384

@@ -385,6 +395,7 @@ func TestLoginInteractive_StartAuthSessionError(t *testing.T) {
385395

386396
func TestLoginInteractive_PollForApprovalError(t *testing.T) {
387397
withFastPoll(t)
398+
withNoBrowser(t)
388399
tmpDir := t.TempDir()
389400
t.Setenv("HOME", tmpDir)
390401

@@ -409,6 +420,7 @@ func TestLoginInteractive_PollForApprovalError(t *testing.T) {
409420

410421
func TestLoginInteractive_InvalidExpirationFormat(t *testing.T) {
411422
withFastPoll(t)
423+
withNoBrowser(t)
412424
tmpDir := t.TempDir()
413425
t.Setenv("HOME", tmpDir)
414426

@@ -438,6 +450,7 @@ func TestLoginInteractive_InvalidExpirationFormat(t *testing.T) {
438450

439451
func TestLoginInteractive_SaveTokenError(t *testing.T) {
440452
withFastPoll(t)
453+
withNoBrowser(t)
441454
tmpDir := t.TempDir()
442455
authDir := filepath.Join(tmpDir, ".openboot")
443456
require.NoError(t, os.MkdirAll(authDir, 0500))
@@ -473,6 +486,7 @@ func TestLoginInteractive_SaveTokenError(t *testing.T) {
473486

474487
func TestLoginInteractive_CreatedAtTimestamp(t *testing.T) {
475488
withFastPoll(t)
489+
withNoBrowser(t)
476490
tmpDir := t.TempDir()
477491
t.Setenv("HOME", tmpDir)
478492

@@ -506,6 +520,7 @@ func TestLoginInteractive_CreatedAtTimestamp(t *testing.T) {
506520

507521
func TestLoginInteractive_TokenPersisted(t *testing.T) {
508522
withFastPoll(t)
523+
withNoBrowser(t)
509524
tmpDir := t.TempDir()
510525
t.Setenv("HOME", tmpDir)
511526

@@ -551,6 +566,7 @@ func TestGetAPIBase_EnvOverride(t *testing.T) {
551566

552567
func TestLoginInteractive_MultiplePolls(t *testing.T) {
553568
withFastPoll(t)
569+
withNoBrowser(t)
554570

555571
tmpDir := t.TempDir()
556572
t.Setenv("HOME", tmpDir)
@@ -626,6 +642,7 @@ func TestPollForApproval_QueryParameter(t *testing.T) {
626642

627643
func TestLoginInteractive_ExpiresAtParsing(t *testing.T) {
628644
withFastPoll(t)
645+
withNoBrowser(t)
629646
tmpDir := t.TempDir()
630647
t.Setenv("HOME", tmpDir)
631648

@@ -686,6 +703,7 @@ func TestPollOnce_UnknownStatus(t *testing.T) {
686703

687704
func TestLoginInteractive_EmptyUsername(t *testing.T) {
688705
withFastPoll(t)
706+
withNoBrowser(t)
689707
tmpDir := t.TempDir()
690708
t.Setenv("HOME", tmpDir)
691709

0 commit comments

Comments
 (0)