Skip to content

Commit 5476582

Browse files
authored
test: use tmp_path instead of tmpdir fixture (#2884)
1 parent e87e340 commit 5476582

20 files changed

+210
-204
lines changed

tests/async/test_browsercontext_storage_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def test_should_set_local_storage(browser: Browser) -> None:
9797

9898

9999
async def test_should_round_trip_through_the_file(
100-
browser: Browser, context: BrowserContext, tmpdir: Path
100+
browser: Browser, context: BrowserContext, tmp_path: Path
101101
) -> None:
102102
page1 = await context.new_page()
103103
await page1.route(
@@ -113,7 +113,7 @@ async def test_should_round_trip_through_the_file(
113113
}"""
114114
)
115115

116-
path = tmpdir / "storage-state.json"
116+
path = tmp_path / "storage-state.json"
117117
state = await context.storage_state(path=path)
118118
with open(path, "r") as f:
119119
written = json.load(f)

tests/async/test_browsertype_connect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ def handle_download(request: TestServerRequest) -> None:
208208
async def test_prevent_getting_video_path(
209209
browser_type: BrowserType,
210210
launch_server: Callable[[], RemoteServer],
211-
tmpdir: Path,
211+
tmp_path: Path,
212212
server: Server,
213213
) -> None:
214214
remote_server = launch_server()
215215
browser = await browser_type.connect(remote_server.ws_endpoint)
216-
page = await browser.new_page(record_video_dir=tmpdir)
216+
page = await browser.new_page(record_video_dir=tmp_path)
217217
await page.goto(server.PREFIX + "/grid.html")
218218
await browser.close()
219219
assert page.video

tests/async/test_chromium_tracing.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
@pytest.mark.only_browser("chromium")
2626
async def test_should_output_a_trace(
27-
browser: Browser, page: Page, server: Server, tmpdir: Path
27+
browser: Browser, page: Page, server: Server, tmp_path: Path
2828
) -> None:
29-
output_file = tmpdir / "trace.json"
29+
output_file = tmp_path / "trace.json"
3030
await browser.start_tracing(page=page, screenshots=True, path=output_file)
3131
await page.goto(server.PREFIX + "/grid.html")
3232
await browser.stop_tracing()
@@ -35,9 +35,9 @@ async def test_should_output_a_trace(
3535

3636
@pytest.mark.only_browser("chromium")
3737
async def test_should_create_directories_as_needed(
38-
browser: Browser, page: Page, server: Server, tmpdir: Path
38+
browser: Browser, page: Page, server: Server, tmp_path: Path
3939
) -> None:
40-
output_file = tmpdir / "these" / "are" / "directories" / "trace.json"
40+
output_file = tmp_path / "these" / "are" / "directories" / "trace.json"
4141
await browser.start_tracing(page=page, screenshots=True, path=output_file)
4242
await page.goto(server.PREFIX + "/grid.html")
4343
await browser.stop_tracing()
@@ -46,9 +46,9 @@ async def test_should_create_directories_as_needed(
4646

4747
@pytest.mark.only_browser("chromium")
4848
async def test_should_run_with_custom_categories_if_provided(
49-
browser: Browser, page: Page, tmpdir: Path
49+
browser: Browser, page: Page, tmp_path: Path
5050
) -> None:
51-
output_file = tmpdir / "trace.json"
51+
output_file = tmp_path / "trace.json"
5252
await browser.start_tracing(
5353
page=page,
5454
screenshots=True,
@@ -66,21 +66,21 @@ async def test_should_run_with_custom_categories_if_provided(
6666

6767
@pytest.mark.only_browser("chromium")
6868
async def test_should_throw_if_tracing_on_two_pages(
69-
browser: Browser, page: Page, tmpdir: Path
69+
browser: Browser, page: Page, tmp_path: Path
7070
) -> None:
71-
output_file_1 = tmpdir / "trace1.json"
71+
output_file_1 = tmp_path / "trace1.json"
7272
await browser.start_tracing(page=page, screenshots=True, path=output_file_1)
73-
output_file_2 = tmpdir / "trace2.json"
73+
output_file_2 = tmp_path / "trace2.json"
7474
with pytest.raises(Exception):
7575
await browser.start_tracing(page=page, screenshots=True, path=output_file_2)
7676
await browser.stop_tracing()
7777

7878

7979
@pytest.mark.only_browser("chromium")
8080
async def test_should_return_a_buffer(
81-
browser: Browser, page: Page, server: Server, tmpdir: Path
81+
browser: Browser, page: Page, server: Server, tmp_path: Path
8282
) -> None:
83-
output_file = tmpdir / "trace.json"
83+
output_file = tmp_path / "trace.json"
8484
await browser.start_tracing(page=page, path=output_file, screenshots=True)
8585
await page.goto(server.PREFIX + "/grid.html")
8686
value = await browser.stop_tracing()

tests/async/test_defaultbrowsercontext.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
@pytest.fixture()
4747
async def launch_persistent(
48-
tmpdir: Path, launch_arguments: Dict, browser_type: BrowserType
48+
tmp_path: Path, launch_arguments: Dict, browser_type: BrowserType
4949
) -> AsyncGenerator[Callable[..., Awaitable[Tuple[Page, BrowserContext]]], None]:
5050
context: Optional[BrowserContext] = None
5151

@@ -54,7 +54,7 @@ async def _launch(**options: Any) -> Tuple[Page, BrowserContext]:
5454
if context:
5555
raise ValueError("can only launch one persistent context")
5656
context = await browser_type.launch_persistent_context(
57-
str(tmpdir), **{**launch_arguments, **options}
57+
str(tmp_path), **{**launch_arguments, **options}
5858
)
5959
assert context
6060
return (context.pages[0], context)
@@ -373,14 +373,14 @@ async def test_should_support_extra_http_headers_option(
373373

374374

375375
async def test_should_accept_user_data_dir(
376-
tmpdir: Path,
376+
tmp_path: Path,
377377
launch_persistent: "Callable[..., asyncio.Future[Tuple[Page, BrowserContext]]]",
378378
) -> None:
379379
(page, context) = await launch_persistent()
380380
# Note: we need an open page to make sure its functional.
381-
assert len(os.listdir(tmpdir)) > 0
381+
assert len(os.listdir(tmp_path)) > 0
382382
await context.close()
383-
assert len(os.listdir(tmpdir)) > 0
383+
assert len(os.listdir(tmp_path)) > 0
384384

385385

386386
async def test_should_restore_state_from_userDataDir(
@@ -426,11 +426,11 @@ async def test_should_have_default_url_when_launching_browser(
426426

427427
@pytest.mark.skip_browser("firefox")
428428
async def test_should_throw_if_page_argument_is_passed(
429-
browser_type: BrowserType, server: Server, tmpdir: Path, launch_arguments: Dict
429+
browser_type: BrowserType, server: Server, tmp_path: Path, launch_arguments: Dict
430430
) -> None:
431431
options = {**launch_arguments, "args": [server.EMPTY_PAGE]}
432432
with pytest.raises(Error) as exc:
433-
await browser_type.launch_persistent_context(tmpdir, **options)
433+
await browser_type.launch_persistent_context(tmp_path, **options)
434434
assert "can not specify page" in exc.value.message
435435

436436

tests/async/test_download.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,29 @@ async def test_should_report_downloads_with_accept_downloads_true(
8383

8484

8585
async def test_should_save_to_user_specified_path(
86-
tmpdir: Path, browser: Browser, server: Server
86+
tmp_path: Path, browser: Browser, server: Server
8787
) -> None:
8888
page = await browser.new_page(accept_downloads=True)
8989
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
9090
async with page.expect_download() as download_info:
9191
await page.click("a")
9292
download = await download_info.value
93-
user_path = tmpdir / "download.txt"
93+
user_path = tmp_path / "download.txt"
9494
await download.save_as(user_path)
9595
assert user_path.exists()
9696
assert user_path.read_text("utf-8") == "Hello world"
9797
await page.close()
9898

9999

100100
async def test_should_save_to_user_specified_path_without_updating_original_path(
101-
tmpdir: Path, browser: Browser, server: Server
101+
tmp_path: Path, browser: Browser, server: Server
102102
) -> None:
103103
page = await browser.new_page(accept_downloads=True)
104104
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
105105
async with page.expect_download() as download_info:
106106
await page.click("a")
107107
download = await download_info.value
108-
user_path = tmpdir / "download.txt"
108+
user_path = tmp_path / "download.txt"
109109
await download.save_as(user_path)
110110
assert user_path.exists()
111111
assert user_path.read_text("utf-8") == "Hello world"
@@ -117,67 +117,67 @@ async def test_should_save_to_user_specified_path_without_updating_original_path
117117

118118

119119
async def test_should_save_to_two_different_paths_with_multiple_save_as_calls(
120-
tmpdir: Path, browser: Browser, server: Server
120+
tmp_path: Path, browser: Browser, server: Server
121121
) -> None:
122122
page = await browser.new_page(accept_downloads=True)
123123
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
124124
async with page.expect_download() as download_info:
125125
await page.click("a")
126126
download = await download_info.value
127-
user_path = tmpdir / "download.txt"
127+
user_path = tmp_path / "download.txt"
128128
await download.save_as(user_path)
129129
assert user_path.exists()
130130
assert user_path.read_text("utf-8") == "Hello world"
131131

132-
anotheruser_path = tmpdir / "download (2).txt"
132+
anotheruser_path = tmp_path / "download (2).txt"
133133
await download.save_as(anotheruser_path)
134134
assert anotheruser_path.exists()
135135
assert anotheruser_path.read_text("utf-8") == "Hello world"
136136
await page.close()
137137

138138

139139
async def test_should_save_to_overwritten_filepath(
140-
tmpdir: Path, browser: Browser, server: Server
140+
tmp_path: Path, browser: Browser, server: Server
141141
) -> None:
142142
page = await browser.new_page(accept_downloads=True)
143143
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
144144
async with page.expect_download() as download_info:
145145
await page.click("a")
146146
download = await download_info.value
147-
user_path = tmpdir / "download.txt"
147+
user_path = tmp_path / "download.txt"
148148
await download.save_as(user_path)
149-
assert len(list(Path(tmpdir).glob("*.*"))) == 1
149+
assert len(list(tmp_path.glob("*.*"))) == 1
150150
await download.save_as(user_path)
151-
assert len(list(Path(tmpdir).glob("*.*"))) == 1
151+
assert len(list(tmp_path.glob("*.*"))) == 1
152152
assert user_path.exists()
153153
assert user_path.read_text("utf-8") == "Hello world"
154154
await page.close()
155155

156156

157157
async def test_should_create_subdirectories_when_saving_to_non_existent_user_specified_path(
158-
tmpdir: Path, browser: Browser, server: Server
158+
tmp_path: Path, browser: Browser, server: Server
159159
) -> None:
160160
page = await browser.new_page(accept_downloads=True)
161161
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
162162
async with page.expect_download() as download_info:
163163
await page.click("a")
164164
download = await download_info.value
165-
nested_path = tmpdir / "these" / "are" / "directories" / "download.txt"
165+
nested_path = tmp_path / "these" / "are" / "directories" / "download.txt"
166166
await download.save_as(nested_path)
167167
assert nested_path.exists()
168168
assert nested_path.read_text("utf-8") == "Hello world"
169169
await page.close()
170170

171171

172172
async def test_should_error_when_saving_with_downloads_disabled(
173-
tmpdir: Path, browser: Browser, server: Server
173+
tmp_path: Path, browser: Browser, server: Server
174174
) -> None:
175175
page = await browser.new_page(accept_downloads=False)
176176
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
177177
async with page.expect_download() as download_info:
178178
await page.click("a")
179179
download = await download_info.value
180-
user_path = tmpdir / "download.txt"
180+
user_path = tmp_path / "download.txt"
181181
with pytest.raises(Error) as exc:
182182
await download.save_as(user_path)
183183
assert (
@@ -192,14 +192,14 @@ async def test_should_error_when_saving_with_downloads_disabled(
192192

193193

194194
async def test_should_error_when_saving_after_deletion(
195-
tmpdir: Path, browser: Browser, server: Server
195+
tmp_path: Path, browser: Browser, server: Server
196196
) -> None:
197197
page = await browser.new_page(accept_downloads=True)
198198
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
199199
async with page.expect_download() as download_info:
200200
await page.click("a")
201201
download = await download_info.value
202-
user_path = tmpdir / "download.txt"
202+
user_path = tmp_path / "download.txt"
203203
await download.delete()
204204
with pytest.raises(Error) as exc:
205205
await download.save_as(user_path)

tests/async/test_fetch_global.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ async def test_should_return_empty_body(playwright: Playwright, server: Server)
289289

290290

291291
async def test_storage_state_should_round_trip_through_file(
292-
playwright: Playwright, tmpdir: Path
292+
playwright: Playwright, tmp_path: Path
293293
) -> None:
294294
expected: StorageState = {
295295
"cookies": [
@@ -307,7 +307,7 @@ async def test_storage_state_should_round_trip_through_file(
307307
"origins": [],
308308
}
309309
request = await playwright.request.new_context(storage_state=expected)
310-
path = tmpdir / "storage-state.json"
310+
path = tmp_path / "storage-state.json"
311311
actual = await request.storage_state(path=path)
312312
assert actual == expected
313313

0 commit comments

Comments
 (0)