diff --git a/libs/browser/browser_test.go b/libs/browser/browser_test.go index 7a709e8e71a..a450eb71454 100644 --- a/libs/browser/browser_test.go +++ b/libs/browser/browser_test.go @@ -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" @@ -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 @@ -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 @@ -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")