Skip to content

Commit 8960063

Browse files
vaneseltineasvetlov
authored andcommitted
Replace all tmpdir fixtures with tmp_path (#3551) (#3955)
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 #3551 and should have no side effects.
1 parent 2c38ce2 commit 8960063

8 files changed

+29
-27
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
@@ -163,6 +163,7 @@ Martin Melka
163163
Martin Richard
164164
Mathias Fröjdman
165165
Mathieu Dugré
166+
Matt VanEseltine
166167
Matthieu Hauglustaine
167168
Matthieu Rigal
168169
Michael Ihnatenko

tests/test_client_request.py

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

657657

658-
async def test_pass_falsy_data_file(loop, tmpdir) -> None:
659-
testfile = tmpdir.join('tmpfile').open('w+b')
658+
async def test_pass_falsy_data_file(loop, tmp_path) -> None:
659+
testfile = (tmp_path / 'tmpfile').open('w+b')
660660
testfile.write(b'data')
661661
testfile.seek(0)
662662
skip = frozenset([hdrs.CONTENT_TYPE])

tests/test_proxy_functional.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,11 @@ async def test_proxy_from_env_http_with_auth(proxy_test_server,
530530

531531

532532
async def test_proxy_from_env_http_with_auth_from_netrc(
533-
proxy_test_server, get_request, tmpdir, mocker):
533+
proxy_test_server, get_request, tmp_path, mocker):
534534
url = 'http://aiohttp.io/path'
535535
proxy = await proxy_test_server()
536536
auth = aiohttp.BasicAuth('user', 'pass')
537-
netrc_file = tmpdir.join('test_netrc')
537+
netrc_file = tmp_path / 'test_netrc'
538538
netrc_file_data = 'machine 127.0.0.1 login %s password %s' % (
539539
auth.login, auth.password)
540540
with open(str(netrc_file), 'w') as f:
@@ -552,11 +552,11 @@ async def test_proxy_from_env_http_with_auth_from_netrc(
552552

553553

554554
async def test_proxy_from_env_http_without_auth_from_netrc(
555-
proxy_test_server, get_request, tmpdir, mocker):
555+
proxy_test_server, get_request, tmp_path, mocker):
556556
url = 'http://aiohttp.io/path'
557557
proxy = await proxy_test_server()
558558
auth = aiohttp.BasicAuth('user', 'pass')
559-
netrc_file = tmpdir.join('test_netrc')
559+
netrc_file = tmp_path / 'test_netrc'
560560
netrc_file_data = 'machine 127.0.0.2 login %s password %s' % (
561561
auth.login, auth.password)
562562
with open(str(netrc_file), 'w') as f:
@@ -574,11 +574,11 @@ async def test_proxy_from_env_http_without_auth_from_netrc(
574574

575575

576576
async def test_proxy_from_env_http_without_auth_from_wrong_netrc(
577-
proxy_test_server, get_request, tmpdir, mocker):
577+
proxy_test_server, get_request, tmp_path, mocker):
578578
url = 'http://aiohttp.io/path'
579579
proxy = await proxy_test_server()
580580
auth = aiohttp.BasicAuth('user', 'pass')
581-
netrc_file = tmpdir.join('test_netrc')
581+
netrc_file = tmp_path / 'test_netrc'
582582
invalid_data = 'machine 127.0.0.1 %s pass %s' % (
583583
auth.login, auth.password)
584584
with open(str(netrc_file), 'w') as f:

tests/test_run_app.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ def test_run_app_custom_backlog_unix(patched_loop) -> None:
353353

354354

355355
@skip_if_no_unix_socks
356-
def test_run_app_http_unix_socket(patched_loop, tmpdir) -> None:
356+
def test_run_app_http_unix_socket(patched_loop, tmp_path) -> None:
357357
app = web.Application()
358358

359-
sock_path = str(tmpdir / 'socket.sock')
359+
sock_path = str(tmp_path / 'socket.sock')
360360
printer = mock.Mock(wraps=stopper(patched_loop))
361361
web.run_app(app, path=sock_path, print=printer)
362362

@@ -366,10 +366,10 @@ def test_run_app_http_unix_socket(patched_loop, tmpdir) -> None:
366366

367367

368368
@skip_if_no_unix_socks
369-
def test_run_app_https_unix_socket(patched_loop, tmpdir) -> None:
369+
def test_run_app_https_unix_socket(patched_loop, tmp_path) -> None:
370370
app = web.Application()
371371

372-
sock_path = str(tmpdir / 'socket.sock')
372+
sock_path = str(tmp_path / 'socket.sock')
373373
ssl_context = ssl.create_default_context()
374374
printer = mock.Mock(wraps=stopper(patched_loop))
375375
web.run_app(app, path=sock_path, ssl_context=ssl_context, print=printer)

tests/test_web_functional.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ async def handler(request):
15601560
{'name': 'file', 'filename': 'file', 'filename*': 'file'})
15611561

15621562

1563-
async def test_response_with_bodypart_named(aiohttp_client, tmpdir) -> None:
1563+
async def test_response_with_bodypart_named(aiohttp_client, tmp_path) -> None:
15641564

15651565
async def handler(request):
15661566
reader = await request.multipart()
@@ -1571,7 +1571,7 @@ async def handler(request):
15711571
app.router.add_post('/', handler)
15721572
client = await aiohttp_client(app)
15731573

1574-
f = tmpdir.join('foobar.txt')
1574+
f = tmp_path / 'foobar.txt'
15751575
f.write_text('test', encoding='utf8')
15761576
data = {'file': open(str(f), 'rb')}
15771577
resp = await client.post('/', data=data)

tests/test_web_sendfile_functional.py

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

321321

322-
async def test_static_file_huge(aiohttp_client, tmpdir) -> None:
322+
async def test_static_file_huge(aiohttp_client, tmp_path) -> None:
323323
filename = 'huge_data.unknown_mime_type'
324324

325325
# fill 20MB file
326-
with tmpdir.join(filename).open('w') as f:
326+
with (tmp_path / filename).open('w') as f:
327327
for i in range(1024*20):
328328
f.write(chr(i % 64 + 0x20) * 1024)
329329

330-
file_st = os.stat(str(tmpdir.join(filename)))
330+
file_st = os.stat(str((tmp_path / filename)))
331331

332332
app = web.Application()
333-
app.router.add_static('/static', str(tmpdir))
333+
app.router.add_static('/static', str(tmp_path))
334334
client = await aiohttp_client(app)
335335

336336
resp = await client.get('/static/'+filename)
@@ -340,7 +340,7 @@ async def test_static_file_huge(aiohttp_client, tmpdir) -> None:
340340
assert resp.headers.get('CONTENT-ENCODING') is None
341341
assert int(resp.headers.get('CONTENT-LENGTH')) == file_st.st_size
342342

343-
f = tmpdir.join(filename).open('rb')
343+
f = (tmp_path / filename).open('rb')
344344
off = 0
345345
cnt = 0
346346
while off < file_st.st_size:
@@ -751,11 +751,11 @@ async def handler(request):
751751
await resp.release()
752752

753753

754-
async def test_static_file_huge_cancel(aiohttp_client, tmpdir) -> None:
754+
async def test_static_file_huge_cancel(aiohttp_client, tmp_path) -> None:
755755
filename = 'huge_data.unknown_mime_type'
756756

757757
# fill 100MB file
758-
with tmpdir.join(filename).open('w') as f:
758+
with (tmp_path / filename).open('w') as f:
759759
for i in range(1024*20):
760760
f.write(chr(i % 64 + 0x20) * 1024)
761761

@@ -768,7 +768,7 @@ async def handler(request):
768768
tr = request.transport
769769
sock = tr.get_extra_info('socket')
770770
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
771-
ret = web.FileResponse(pathlib.Path(str(tmpdir.join(filename))))
771+
ret = web.FileResponse(pathlib.Path(str((tmp_path / filename))))
772772
return ret
773773

774774
app = web.Application()
@@ -789,11 +789,11 @@ async def handler(request):
789789
assert len(data) < 1024 * 1024 * 20
790790

791791

792-
async def test_static_file_huge_error(aiohttp_client, tmpdir) -> None:
792+
async def test_static_file_huge_error(aiohttp_client, tmp_path) -> None:
793793
filename = 'huge_data.unknown_mime_type'
794794

795795
# fill 20MB file
796-
with tmpdir.join(filename).open('wb') as f:
796+
with (tmp_path / filename).open('wb') as f:
797797
f.seek(20*1024*1024)
798798
f.write(b'1')
799799

@@ -802,7 +802,7 @@ async def handler(request):
802802
tr = request.transport
803803
sock = tr.get_extra_info('socket')
804804
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
805-
ret = web.FileResponse(pathlib.Path(str(tmpdir.join(filename))))
805+
ret = web.FileResponse(pathlib.Path(str((tmp_path / filename))))
806806
return ret
807807

808808
app = web.Application()

tests/test_web_urldispatcher.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,12 @@ async def post(self):
438438
await r.release()
439439

440440

441-
async def test_static_absolute_url(aiohttp_client, tmpdir) -> None:
441+
async def test_static_absolute_url(aiohttp_client, tmp_path) -> None:
442442
# requested url is an absolute name like
443443
# /static/\\machine_name\c$ or /static/D:\path
444444
# where the static dir is totally different
445445
app = web.Application()
446-
fname = tmpdir / 'file.txt'
446+
fname = tmp_path / 'file.txt'
447447
fname.write_text('sample text', 'ascii')
448448
here = pathlib.Path(__file__).parent
449449
app.router.add_static('/static', here)

0 commit comments

Comments
 (0)