Skip to content

Commit 435ad46

Browse files
[PR #3955/8960063e backport][3.9] Replace all tmpdir fixtures with tmp_path (#3551) (#8076)
**This is a backport of PR #3955 as merged into master (8960063).** tmp_path is the replacement fixture in pytest for tmpdir; tmp_path uses the builtin pathlib.Path class. As it says on the tin, this commit replaces every instance of tmpdir in the test suite with tmp_path. Aside from s/tmpdir/tmp_path/ this also required changing instances of `tmpdir.join(foo)` to `tmp_path / foo`. This is intended to comprehensively address and close #3551, and should have no side effects. This does not affect end users. Co-authored-by: Matt VanEseltine <[email protected]>
1 parent d33bc21 commit 435ad46

7 files changed

+26
-24
lines changed

CHANGES/3551.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace all tmpdir fixtures with tmp_path in test suite.

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Martin Richard
225225
Martin Sucha
226226
Mathias Fröjdman
227227
Mathieu Dugré
228+
Matt VanEseltine
228229
Matthias Marquardt
229230
Matthieu Hauglustaine
230231
Matthieu Rigal

tests/test_client_request.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,8 @@ async def test_pass_falsy_data(loop) -> None:
762762
await req.close()
763763

764764

765-
async def test_pass_falsy_data_file(loop, tmpdir) -> None:
766-
testfile = tmpdir.join("tmpfile").open("w+b")
765+
async def test_pass_falsy_data_file(loop, tmp_path) -> None:
766+
testfile = (tmp_path / "tmpfile").open("w+b")
767767
testfile.write(b"data")
768768
testfile.seek(0)
769769
skip = frozenset([hdrs.CONTENT_TYPE])

tests/test_proxy_functional.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,12 @@ async def test_proxy_from_env_http_with_auth(proxy_test_server, get_request, moc
721721

722722

723723
async def test_proxy_from_env_http_with_auth_from_netrc(
724-
proxy_test_server, get_request, tmpdir, mocker
724+
proxy_test_server, get_request, tmp_path, mocker
725725
):
726726
url = "http://aiohttp.io/path"
727727
proxy = await proxy_test_server()
728728
auth = aiohttp.BasicAuth("user", "pass")
729-
netrc_file = tmpdir.join("test_netrc")
729+
netrc_file = tmp_path / "test_netrc"
730730
netrc_file_data = "machine 127.0.0.1 login {} password {}".format(
731731
auth.login,
732732
auth.password,
@@ -747,12 +747,12 @@ async def test_proxy_from_env_http_with_auth_from_netrc(
747747

748748

749749
async def test_proxy_from_env_http_without_auth_from_netrc(
750-
proxy_test_server, get_request, tmpdir, mocker
750+
proxy_test_server, get_request, tmp_path, mocker
751751
):
752752
url = "http://aiohttp.io/path"
753753
proxy = await proxy_test_server()
754754
auth = aiohttp.BasicAuth("user", "pass")
755-
netrc_file = tmpdir.join("test_netrc")
755+
netrc_file = tmp_path / "test_netrc"
756756
netrc_file_data = "machine 127.0.0.2 login {} password {}".format(
757757
auth.login,
758758
auth.password,
@@ -773,12 +773,12 @@ async def test_proxy_from_env_http_without_auth_from_netrc(
773773

774774

775775
async def test_proxy_from_env_http_without_auth_from_wrong_netrc(
776-
proxy_test_server, get_request, tmpdir, mocker
776+
proxy_test_server, get_request, tmp_path, mocker
777777
):
778778
url = "http://aiohttp.io/path"
779779
proxy = await proxy_test_server()
780780
auth = aiohttp.BasicAuth("user", "pass")
781-
netrc_file = tmpdir.join("test_netrc")
781+
netrc_file = tmp_path / "test_netrc"
782782
invalid_data = f"machine 127.0.0.1 {auth.login} pass {auth.password}"
783783
with open(str(netrc_file), "w") as f:
784784
f.write(invalid_data)

tests/test_web_functional.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ async def handler(request):
17861786
await resp.release()
17871787

17881788

1789-
async def test_response_with_bodypart_named(aiohttp_client, tmpdir) -> None:
1789+
async def test_response_with_bodypart_named(aiohttp_client, tmp_path) -> None:
17901790
async def handler(request):
17911791
reader = await request.multipart()
17921792
part = await reader.next()
@@ -1796,9 +1796,9 @@ async def handler(request):
17961796
app.router.add_post("/", handler)
17971797
client = await aiohttp_client(app)
17981798

1799-
f = tmpdir.join("foobar.txt")
1799+
f = tmp_path / "foobar.txt"
18001800
f.write_text("test", encoding="utf8")
1801-
with open(str(f), "rb") as fd:
1801+
with f.open("rb") as fd:
18021802
data = {"file": fd}
18031803
resp = await client.post("/", data=data)
18041804

tests/test_web_sendfile_functional.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -558,18 +558,18 @@ def test_static_route_path_existence_check() -> None:
558558
web.StaticResource("/", nodirectory)
559559

560560

561-
async def test_static_file_huge(aiohttp_client, tmpdir) -> None:
561+
async def test_static_file_huge(aiohttp_client, tmp_path) -> None:
562562
filename = "huge_data.unknown_mime_type"
563563

564564
# fill 20MB file
565-
with tmpdir.join(filename).open("wb") as f:
565+
with (tmp_path / filename).open("wb") as f:
566566
for i in range(1024 * 20):
567567
f.write((chr(i % 64 + 0x20) * 1024).encode())
568568

569-
file_st = os.stat(str(tmpdir.join(filename)))
569+
file_st = os.stat(str(tmp_path / filename))
570570

571571
app = web.Application()
572-
app.router.add_static("/static", str(tmpdir))
572+
app.router.add_static("/static", str(tmp_path))
573573
client = await aiohttp_client(app)
574574

575575
resp = await client.get("/static/" + filename)
@@ -579,7 +579,7 @@ async def test_static_file_huge(aiohttp_client, tmpdir) -> None:
579579
assert resp.headers.get("CONTENT-ENCODING") is None
580580
assert int(resp.headers.get("CONTENT-LENGTH")) == file_st.st_size
581581

582-
f = tmpdir.join(filename).open("rb")
582+
f = (tmp_path / filename).open("rb")
583583
off = 0
584584
cnt = 0
585585
while off < file_st.st_size:
@@ -988,11 +988,11 @@ async def handler(request):
988988
await client.close()
989989

990990

991-
async def test_static_file_huge_cancel(aiohttp_client, tmpdir) -> None:
991+
async def test_static_file_huge_cancel(aiohttp_client, tmp_path) -> None:
992992
filename = "huge_data.unknown_mime_type"
993993

994994
# fill 100MB file
995-
with tmpdir.join(filename).open("wb") as f:
995+
with (tmp_path / filename).open("wb") as f:
996996
for i in range(1024 * 20):
997997
f.write((chr(i % 64 + 0x20) * 1024).encode())
998998

@@ -1005,7 +1005,7 @@ async def handler(request):
10051005
tr = request.transport
10061006
sock = tr.get_extra_info("socket")
10071007
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
1008-
ret = web.FileResponse(pathlib.Path(str(tmpdir.join(filename))))
1008+
ret = web.FileResponse(pathlib.Path(str(tmp_path / filename)))
10091009
return ret
10101010

10111011
app = web.Application()
@@ -1029,11 +1029,11 @@ async def handler(request):
10291029
await client.close()
10301030

10311031

1032-
async def test_static_file_huge_error(aiohttp_client, tmpdir) -> None:
1032+
async def test_static_file_huge_error(aiohttp_client, tmp_path) -> None:
10331033
filename = "huge_data.unknown_mime_type"
10341034

10351035
# fill 20MB file
1036-
with tmpdir.join(filename).open("wb") as f:
1036+
with (tmp_path / filename).open("wb") as f:
10371037
f.seek(20 * 1024 * 1024)
10381038
f.write(b"1")
10391039

@@ -1042,7 +1042,7 @@ async def handler(request):
10421042
tr = request.transport
10431043
sock = tr.get_extra_info("socket")
10441044
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
1045-
ret = web.FileResponse(pathlib.Path(str(tmpdir.join(filename))))
1045+
ret = web.FileResponse(pathlib.Path(str(tmp_path / filename)))
10461046
return ret
10471047

10481048
app = web.Application()

tests/test_web_urldispatcher.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,13 @@ async def post(self) -> web.Response:
526526

527527

528528
async def test_static_absolute_url(
529-
aiohttp_client: AiohttpClient, tmpdir: pathlib.Path
529+
aiohttp_client: AiohttpClient, tmp_path: pathlib.Path
530530
) -> None:
531531
# requested url is an absolute name like
532532
# /static/\\machine_name\c$ or /static/D:\path
533533
# where the static dir is totally different
534534
app = web.Application()
535-
fname = tmpdir / "file.txt"
535+
fname = tmp_path / "file.txt"
536536
fname.write_text("sample text", "ascii")
537537
here = pathlib.Path(__file__).parent
538538
app.router.add_static("/static", here)

0 commit comments

Comments
 (0)