Skip to content

test: use tmp_path instead of tmpdir fixture #2884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions tests/async/test_browsercontext_storage_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async def test_should_set_local_storage(browser: Browser) -> None:


async def test_should_round_trip_through_the_file(
browser: Browser, context: BrowserContext, tmpdir: Path
browser: Browser, context: BrowserContext, tmp_path: Path
) -> None:
page1 = await context.new_page()
await page1.route(
Expand All @@ -113,7 +113,7 @@ async def test_should_round_trip_through_the_file(
}"""
)

path = tmpdir / "storage-state.json"
path = tmp_path / "storage-state.json"
state = await context.storage_state(path=path)
with open(path, "r") as f:
written = json.load(f)
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_browsertype_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ def handle_download(request: TestServerRequest) -> None:
async def test_prevent_getting_video_path(
browser_type: BrowserType,
launch_server: Callable[[], RemoteServer],
tmpdir: Path,
tmp_path: Path,
server: Server,
) -> None:
remote_server = launch_server()
browser = await browser_type.connect(remote_server.ws_endpoint)
page = await browser.new_page(record_video_dir=tmpdir)
page = await browser.new_page(record_video_dir=tmp_path)
await page.goto(server.PREFIX + "/grid.html")
await browser.close()
assert page.video
Expand Down
22 changes: 11 additions & 11 deletions tests/async/test_chromium_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

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

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

@pytest.mark.only_browser("chromium")
async def test_should_run_with_custom_categories_if_provided(
browser: Browser, page: Page, tmpdir: Path
browser: Browser, page: Page, tmp_path: Path
) -> None:
output_file = tmpdir / "trace.json"
output_file = tmp_path / "trace.json"
await browser.start_tracing(
page=page,
screenshots=True,
Expand All @@ -66,21 +66,21 @@ async def test_should_run_with_custom_categories_if_provided(

@pytest.mark.only_browser("chromium")
async def test_should_throw_if_tracing_on_two_pages(
browser: Browser, page: Page, tmpdir: Path
browser: Browser, page: Page, tmp_path: Path
) -> None:
output_file_1 = tmpdir / "trace1.json"
output_file_1 = tmp_path / "trace1.json"
await browser.start_tracing(page=page, screenshots=True, path=output_file_1)
output_file_2 = tmpdir / "trace2.json"
output_file_2 = tmp_path / "trace2.json"
with pytest.raises(Exception):
await browser.start_tracing(page=page, screenshots=True, path=output_file_2)
await browser.stop_tracing()


@pytest.mark.only_browser("chromium")
async def test_should_return_a_buffer(
browser: Browser, page: Page, server: Server, tmpdir: Path
browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
output_file = tmpdir / "trace.json"
output_file = tmp_path / "trace.json"
await browser.start_tracing(page=page, path=output_file, screenshots=True)
await page.goto(server.PREFIX + "/grid.html")
value = await browser.stop_tracing()
Expand Down
14 changes: 7 additions & 7 deletions tests/async/test_defaultbrowsercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

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

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


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


async def test_should_restore_state_from_userDataDir(
Expand Down Expand Up @@ -426,11 +426,11 @@ async def test_should_have_default_url_when_launching_browser(

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


Expand Down
34 changes: 17 additions & 17 deletions tests/async/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,29 @@ async def test_should_report_downloads_with_accept_downloads_true(


async def test_should_save_to_user_specified_path(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
await download.save_as(user_path)
assert user_path.exists()
assert user_path.read_text("utf-8") == "Hello world"
await page.close()


async def test_should_save_to_user_specified_path_without_updating_original_path(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
await download.save_as(user_path)
assert user_path.exists()
assert user_path.read_text("utf-8") == "Hello world"
Expand All @@ -117,67 +117,67 @@ async def test_should_save_to_user_specified_path_without_updating_original_path


async def test_should_save_to_two_different_paths_with_multiple_save_as_calls(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
await download.save_as(user_path)
assert user_path.exists()
assert user_path.read_text("utf-8") == "Hello world"

anotheruser_path = tmpdir / "download (2).txt"
anotheruser_path = tmp_path / "download (2).txt"
await download.save_as(anotheruser_path)
assert anotheruser_path.exists()
assert anotheruser_path.read_text("utf-8") == "Hello world"
await page.close()


async def test_should_save_to_overwritten_filepath(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
await download.save_as(user_path)
assert len(list(Path(tmpdir).glob("*.*"))) == 1
assert len(list(tmp_path.glob("*.*"))) == 1
await download.save_as(user_path)
assert len(list(Path(tmpdir).glob("*.*"))) == 1
assert len(list(tmp_path.glob("*.*"))) == 1
assert user_path.exists()
assert user_path.read_text("utf-8") == "Hello world"
await page.close()


async def test_should_create_subdirectories_when_saving_to_non_existent_user_specified_path(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
nested_path = tmpdir / "these" / "are" / "directories" / "download.txt"
nested_path = tmp_path / "these" / "are" / "directories" / "download.txt"
await download.save_as(nested_path)
assert nested_path.exists()
assert nested_path.read_text("utf-8") == "Hello world"
await page.close()


async def test_should_error_when_saving_with_downloads_disabled(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=False)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
with pytest.raises(Error) as exc:
await download.save_as(user_path)
assert (
Expand All @@ -192,14 +192,14 @@ async def test_should_error_when_saving_with_downloads_disabled(


async def test_should_error_when_saving_after_deletion(
tmpdir: Path, browser: Browser, server: Server
tmp_path: Path, browser: Browser, server: Server
) -> None:
page = await browser.new_page(accept_downloads=True)
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
async with page.expect_download() as download_info:
await page.click("a")
download = await download_info.value
user_path = tmpdir / "download.txt"
user_path = tmp_path / "download.txt"
await download.delete()
with pytest.raises(Error) as exc:
await download.save_as(user_path)
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_fetch_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async def test_should_return_empty_body(playwright: Playwright, server: Server)


async def test_storage_state_should_round_trip_through_file(
playwright: Playwright, tmpdir: Path
playwright: Playwright, tmp_path: Path
) -> None:
expected: StorageState = {
"cookies": [
Expand All @@ -307,7 +307,7 @@ async def test_storage_state_should_round_trip_through_file(
"origins": [],
}
request = await playwright.request.new_context(storage_state=expected)
path = tmpdir / "storage-state.json"
path = tmp_path / "storage-state.json"
actual = await request.storage_state(path=path)
assert actual == expected

Expand Down
Loading
Loading