Skip to content

Commit

Permalink
[PR #3955/8960063e backport][3.10] Replace all tmpdir fixtures with t…
Browse files Browse the repository at this point in the history
…mp_path (#3551) (#8075)

**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 <vaneseltine@gmail.com>
  • Loading branch information
webknjaz and vaneseltine committed Jan 28, 2024
1 parent a67fb10 commit bb59070
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGES/3551.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace all tmpdir fixtures with tmp_path in test suite.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Martin Richard
Martin Sucha
Mathias Fröjdman
Mathieu Dugré
Matt VanEseltine
Matthias Marquardt
Matthieu Hauglustaine
Matthieu Rigal
Expand Down
4 changes: 2 additions & 2 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ async def test_pass_falsy_data(loop) -> None:
await req.close()


async def test_pass_falsy_data_file(loop, tmpdir) -> None:
testfile = tmpdir.join("tmpfile").open("w+b")
async def test_pass_falsy_data_file(loop, tmp_path) -> None:
testfile = (tmp_path / "tmpfile").open("w+b")
testfile.write(b"data")
testfile.seek(0)
skip = frozenset([hdrs.CONTENT_TYPE])
Expand Down
12 changes: 6 additions & 6 deletions tests/test_proxy_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ async def test_proxy_from_env_http_with_auth(proxy_test_server, get_request, moc


async def test_proxy_from_env_http_with_auth_from_netrc(
proxy_test_server, get_request, tmpdir, mocker
proxy_test_server, get_request, tmp_path, mocker
):
url = "http://aiohttp.io/path"
proxy = await proxy_test_server()
auth = aiohttp.BasicAuth("user", "pass")
netrc_file = tmpdir.join("test_netrc")
netrc_file = tmp_path / "test_netrc"
netrc_file_data = "machine 127.0.0.1 login {} password {}".format(
auth.login,
auth.password,
Expand All @@ -747,12 +747,12 @@ async def test_proxy_from_env_http_with_auth_from_netrc(


async def test_proxy_from_env_http_without_auth_from_netrc(
proxy_test_server, get_request, tmpdir, mocker
proxy_test_server, get_request, tmp_path, mocker
):
url = "http://aiohttp.io/path"
proxy = await proxy_test_server()
auth = aiohttp.BasicAuth("user", "pass")
netrc_file = tmpdir.join("test_netrc")
netrc_file = tmp_path / "test_netrc"
netrc_file_data = "machine 127.0.0.2 login {} password {}".format(
auth.login,
auth.password,
Expand All @@ -773,12 +773,12 @@ async def test_proxy_from_env_http_without_auth_from_netrc(


async def test_proxy_from_env_http_without_auth_from_wrong_netrc(
proxy_test_server, get_request, tmpdir, mocker
proxy_test_server, get_request, tmp_path, mocker
):
url = "http://aiohttp.io/path"
proxy = await proxy_test_server()
auth = aiohttp.BasicAuth("user", "pass")
netrc_file = tmpdir.join("test_netrc")
netrc_file = tmp_path / "test_netrc"
invalid_data = f"machine 127.0.0.1 {auth.login} pass {auth.password}"
with open(str(netrc_file), "w") as f:
f.write(invalid_data)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ async def handler(request):
await resp.release()


async def test_response_with_bodypart_named(aiohttp_client, tmpdir) -> None:
async def test_response_with_bodypart_named(aiohttp_client, tmp_path) -> None:
async def handler(request):
reader = await request.multipart()
part = await reader.next()
Expand All @@ -1796,9 +1796,9 @@ async def handler(request):
app.router.add_post("/", handler)
client = await aiohttp_client(app)

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

Expand Down
22 changes: 11 additions & 11 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,18 @@ def test_static_route_path_existence_check() -> None:
web.StaticResource("/", nodirectory)


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

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

file_st = os.stat(str(tmpdir.join(filename)))
file_st = os.stat(str(tmp_path / filename))

app = web.Application()
app.router.add_static("/static", str(tmpdir))
app.router.add_static("/static", str(tmp_path))
client = await aiohttp_client(app)

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

f = tmpdir.join(filename).open("rb")
f = (tmp_path / filename).open("rb")
off = 0
cnt = 0
while off < file_st.st_size:
Expand Down Expand Up @@ -988,11 +988,11 @@ async def handler(request):
await client.close()


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

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

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

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


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

# fill 20MB file
with tmpdir.join(filename).open("wb") as f:
with (tmp_path / filename).open("wb") as f:
f.seek(20 * 1024 * 1024)
f.write(b"1")

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

app = web.Application()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,13 @@ async def post(self) -> web.Response:


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

0 comments on commit bb59070

Please sign in to comment.