Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 4ac192c

Browse files
committed
Add a test to ensure data dir cleaned up
When the context is closed before we call browser.close() used to cause a panic on the cdp command to close the browser. Since we've changed this to log instead of panic, we just need to make sure that the data dir does indeed get removed when the cdp command fails.
1 parent 1bacf59 commit 4ac192c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/browser_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ func TestTmpDirCleanup(t *testing.T) {
9898
assert.Empty(t, matches, "a dir shouldn't exist which matches the pattern `xk6-browser-data-*`")
9999
}
100100

101+
func TestTmpDirCleanupOnContextClose(t *testing.T) {
102+
t.Parallel()
103+
104+
const tmpDirPath = "./2/"
105+
err := os.Mkdir(tmpDirPath, os.ModePerm)
106+
require.NoError(t, err)
107+
108+
defer func() {
109+
err = os.Remove(tmpDirPath)
110+
require.NoError(t, err)
111+
}()
112+
113+
b := newTestBrowser(
114+
t,
115+
withSkipClose(),
116+
withEnvLookup(env.ConstLookup("TMPDIR", tmpDirPath)),
117+
)
118+
119+
matches, err := filepath.Glob(tmpDirPath + "xk6-browser-data-*")
120+
assert.NoError(t, err)
121+
assert.NotEmpty(t, matches, "a dir should exist that matches the pattern `xk6-browser-data-*`")
122+
123+
b.cancelContext()
124+
<-b.ctx.Done()
125+
126+
require.NotPanicsf(t, b.Close, "first call to browser.close should not panic")
127+
128+
matches, err = filepath.Glob(tmpDirPath + "xk6-browser-data-*")
129+
assert.NoError(t, err)
130+
assert.Empty(t, matches, "a dir shouldn't exist which matches the pattern `xk6-browser-data-*`")
131+
}
132+
101133
func TestBrowserOn(t *testing.T) {
102134
t.Parallel()
103135

0 commit comments

Comments
 (0)