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

Commit 43ff9a0

Browse files
committed
Refactor mkdir in cleanup tests
1. Work with MkdirTemp to avoid dir name collisions. 2. Defer the cleanup before checking the error on MkdirTemp. 3. Work with RemoveAll to cleanup sub dirs and files incase the test fails so that the cleanup still occurs as expected. Resolves: #991 (comment) Resolves: #991 (comment)
1 parent 4ac192c commit 43ff9a0

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

tests/browser_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,12 @@ func TestBrowserNewContext(t *testing.T) {
6969
func TestTmpDirCleanup(t *testing.T) {
7070
t.Parallel()
7171

72-
const tmpDirPath = "./1/"
73-
err := os.Mkdir(tmpDirPath, os.ModePerm)
74-
require.NoError(t, err)
75-
72+
tmpDirPath, err := os.MkdirTemp("./", "")
7673
defer func() {
77-
err = os.Remove(tmpDirPath)
74+
err := os.RemoveAll(tmpDirPath)
7875
require.NoError(t, err)
7976
}()
77+
require.NoError(t, err)
8078

8179
b := newTestBrowser(
8280
t,
@@ -87,36 +85,34 @@ func TestTmpDirCleanup(t *testing.T) {
8785
err = p.Close(nil)
8886
require.NoError(t, err)
8987

90-
matches, err := filepath.Glob(tmpDirPath + "xk6-browser-data-*")
88+
matches, err := filepath.Glob(tmpDirPath + "/xk6-browser-data-*")
9189
assert.NoError(t, err)
9290
assert.NotEmpty(t, matches, "a dir should exist that matches the pattern `xk6-browser-data-*`")
9391

9492
b.Close()
9593

96-
matches, err = filepath.Glob(tmpDirPath + "xk6-browser-data-*")
94+
matches, err = filepath.Glob(tmpDirPath + "/xk6-browser-data-*")
9795
assert.NoError(t, err)
9896
assert.Empty(t, matches, "a dir shouldn't exist which matches the pattern `xk6-browser-data-*`")
9997
}
10098

10199
func TestTmpDirCleanupOnContextClose(t *testing.T) {
102100
t.Parallel()
103101

104-
const tmpDirPath = "./2/"
105-
err := os.Mkdir(tmpDirPath, os.ModePerm)
106-
require.NoError(t, err)
107-
102+
tmpDirPath, err := os.MkdirTemp("./", "")
108103
defer func() {
109-
err = os.Remove(tmpDirPath)
104+
err := os.RemoveAll(tmpDirPath)
110105
require.NoError(t, err)
111106
}()
107+
require.NoError(t, err)
112108

113109
b := newTestBrowser(
114110
t,
115111
withSkipClose(),
116112
withEnvLookup(env.ConstLookup("TMPDIR", tmpDirPath)),
117113
)
118114

119-
matches, err := filepath.Glob(tmpDirPath + "xk6-browser-data-*")
115+
matches, err := filepath.Glob(tmpDirPath + "/xk6-browser-data-*")
120116
assert.NoError(t, err)
121117
assert.NotEmpty(t, matches, "a dir should exist that matches the pattern `xk6-browser-data-*`")
122118

@@ -125,7 +121,7 @@ func TestTmpDirCleanupOnContextClose(t *testing.T) {
125121

126122
require.NotPanicsf(t, b.Close, "first call to browser.close should not panic")
127123

128-
matches, err = filepath.Glob(tmpDirPath + "xk6-browser-data-*")
124+
matches, err = filepath.Glob(tmpDirPath + "/xk6-browser-data-*")
129125
assert.NoError(t, err)
130126
assert.Empty(t, matches, "a dir shouldn't exist which matches the pattern `xk6-browser-data-*`")
131127
}

0 commit comments

Comments
 (0)