Skip to content
Merged
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
14 changes: 9 additions & 5 deletions libs/browser/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

browserpkg "github.com/pkg/browser"

"github.com/databricks/cli/libs/atomicfile"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/env"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -105,7 +106,9 @@ func TestOpenerSleeper(t *testing.T) {
t.Skip("runs only as the child of the browser command")
}
time.Sleep(openerSleepTime)
_ = os.WriteFile(file, nil, 0o600)
// Write atomically, so that the file appears only once it is closed, and the cleanup that waits for it can remove the
// directory (Windows does not remove a directory with an open file in it).
_ = atomicfile.Write(file, nil, 0o600)
}

// runOpener calls open with this test binary as the browser command, and returns what the command wrote to stdout and
Expand Down Expand Up @@ -183,12 +186,10 @@ func TestOpenCommandOutput(t *testing.T) {
// TestOpenDoesNotWaitForLauncherChild checks that the default opener returns when its launcher returns, although the
// browser the launcher started is still running. The launcher is a stub that runs this test binary the way pkg/browser's
// runCmd runs xdg-open: with the package's Stdout and Stderr, and waiting only for the command. The sleeper is the
// browser, and it writes sleepFile when it ends. Neither open may wait for it.
// browser, and it writes its sleepFile when it ends. Neither open may wait for it. Each open gets its own sleepFile, so
// that the cleanup waits for both sleepers: with a shared file, the second sleeper could write it while TempDir is removed.
func TestOpenDoesNotWaitForLauncherChild(t *testing.T) {
t.Setenv(openerChildEnv, "1")
sleepFile := filepath.Join(t.TempDir(), "sleeper-done")
t.Setenv(openerBackgroundEnv, sleepFile)
waitForSleeper(t, sleepFile)
replaceDefaultLauncher(t, func(url string) error {
cmd := exec.Command(os.Args[0], url)
cmd.Stdout = browserpkg.Stdout
Expand All @@ -198,6 +199,9 @@ func TestOpenDoesNotWaitForLauncherChild(t *testing.T) {
ctx := env.Set(t.Context(), "BROWSER", "")

for range 2 {
sleepFile := filepath.Join(t.TempDir(), "sleeper-done")
t.Setenv(openerBackgroundEnv, sleepFile)
waitForSleeper(t, sleepFile)
require.NoError(t, Open(ctx, "https://example.com/login"))
_, err := os.Stat(sleepFile)
assert.ErrorIs(t, err, fs.ErrNotExist, "the open waited for the browser that the launcher started")
Expand Down
Loading