Skip to content

Commit b698943

Browse files
authored
Stop using the deprecated tmpdir fixture from pytest (#1396)
Use the newer tmp_path
1 parent 38888ab commit b698943

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) The pyOpenSSL developers
22
# See LICENSE for details.
33

4+
import pathlib
45
from tempfile import mktemp
56

67
import pytest
@@ -18,10 +19,10 @@ def pytest_report_header(config):
1819

1920

2021
@pytest.fixture
21-
def tmpfile(tmpdir):
22+
def tmpfile(tmp_path: pathlib.Path) -> bytes:
2223
"""
2324
Return UTF-8-encoded bytes of a path to a tmp file.
2425
2526
The file will be cleaned up after the test run.
2627
"""
27-
return mktemp(dir=tmpdir.dirname).encode("utf-8")
28+
return mktemp(dir=tmp_path).encode("utf-8")

tests/test_crypto.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
import base64
11+
import pathlib
1112
import sys
1213
import typing
1314
from datetime import datetime, timedelta, timezone
@@ -2477,9 +2478,11 @@ def test_load_locations_fails_when_all_args_are_none(self) -> None:
24772478
with pytest.raises(Error):
24782479
store.load_locations(None, None)
24792480

2480-
def test_load_locations_raises_error_on_failure(self, tmpdir) -> None:
2481-
invalid_ca_file = tmpdir.join("invalid.pem")
2482-
invalid_ca_file.write("This is not a certificate")
2481+
def test_load_locations_raises_error_on_failure(
2482+
self, tmp_path: pathlib.Path
2483+
) -> None:
2484+
invalid_ca_file = tmp_path / "invalid.pem"
2485+
invalid_ca_file.write_text("This is not a certificate")
24832486

24842487
store = X509Store()
24852488
with pytest.raises(Error):
@@ -3369,22 +3372,25 @@ def test_get_verified_chain_invalid_chain_no_root(self) -> None:
33693372
assert exc.value.certificate.get_subject().CN == "intermediate"
33703373

33713374
@pytest.fixture
3372-
def root_ca_file(self, tmpdir):
3373-
return self._create_ca_file(tmpdir, "root_ca_hash_dir", self.root_cert)
3375+
def root_ca_file(self, tmp_path: pathlib.Path) -> pathlib.Path:
3376+
return self._create_ca_file(
3377+
tmp_path, "root_ca_hash_dir", self.root_cert
3378+
)
33743379

33753380
@pytest.fixture
3376-
def intermediate_ca_file(self, tmpdir):
3381+
def intermediate_ca_file(self, tmp_path: pathlib.Path) -> pathlib.Path:
33773382
return self._create_ca_file(
3378-
tmpdir, "intermediate_ca_hash_dir", self.intermediate_cert
3383+
tmp_path, "intermediate_ca_hash_dir", self.intermediate_cert
33793384
)
33803385

33813386
@staticmethod
3382-
def _create_ca_file(base_path, hash_directory: str, cacert: X509):
3387+
def _create_ca_file(
3388+
base_path: pathlib.Path, hash_directory: str, cacert: X509
3389+
) -> pathlib.Path:
33833390
ca_hash = f"{cacert.subject_name_hash():08x}.0"
3384-
cafile = base_path.join(hash_directory, ca_hash)
3385-
cafile.write_binary(
3386-
dump_certificate(FILETYPE_PEM, cacert), ensure=True
3387-
)
3391+
cafile = base_path / hash_directory / ca_hash
3392+
cafile.parent.mkdir(parents=True, exist_ok=True)
3393+
cafile.write_bytes(dump_certificate(FILETYPE_PEM, cacert))
33883394
return cafile
33893395

33903396
def test_verify_with_ca_file_location(self, root_ca_file) -> None:
@@ -3396,7 +3402,7 @@ def test_verify_with_ca_file_location(self, root_ca_file) -> None:
33963402

33973403
def test_verify_with_ca_path_location(self, root_ca_file) -> None:
33983404
store = X509Store()
3399-
store.load_locations(None, str(root_ca_file.dirname))
3405+
store.load_locations(None, str(root_ca_file.parent))
34003406

34013407
store_ctx = X509StoreContext(store, self.intermediate_cert)
34023408
store_ctx.verify_certificate()
@@ -3406,7 +3412,7 @@ def test_verify_with_cafile_and_capath(
34063412
):
34073413
store = X509Store()
34083414
store.load_locations(
3409-
cafile=str(root_ca_file), capath=str(intermediate_ca_file.dirname)
3415+
cafile=str(root_ca_file), capath=str(intermediate_ca_file.parent)
34103416
)
34113417

34123418
store_ctx = X509StoreContext(store, self.intermediate_server_cert)
@@ -3422,9 +3428,11 @@ def test_verify_with_multiple_ca_files(
34223428
store_ctx = X509StoreContext(store, self.intermediate_server_cert)
34233429
store_ctx.verify_certificate()
34243430

3425-
def test_verify_failure_with_empty_ca_directory(self, tmpdir) -> None:
3431+
def test_verify_failure_with_empty_ca_directory(
3432+
self, tmp_path: pathlib.Path
3433+
) -> None:
34263434
store = X509Store()
3427-
store.load_locations(None, str(tmpdir))
3435+
store.load_locations(None, str(tmp_path))
34283436

34293437
store_ctx = X509StoreContext(store, self.intermediate_cert)
34303438
with pytest.raises(X509StoreContextError) as exc:

tests/test_ssl.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import datetime
1111
import gc
12+
import pathlib
1213
import select
1314
import sys
1415
import time
@@ -466,7 +467,7 @@ def test_SSLeay_version(self) -> None:
466467

467468

468469
@pytest.fixture
469-
def ca_file(tmpdir):
470+
def ca_file(tmp_path: pathlib.Path) -> bytes:
470471
"""
471472
Create a valid PEM file with CA certificates and return the path.
472473
"""
@@ -492,8 +493,8 @@ def ca_file(tmpdir):
492493

493494
certificate = builder.sign(private_key=key, algorithm=hashes.SHA256())
494495

495-
ca_file = tmpdir.join("test.pem")
496-
ca_file.write_binary(
496+
ca_file = tmp_path / "test.pem"
497+
ca_file.write_bytes(
497498
certificate.public_bytes(
498499
encoding=serialization.Encoding.PEM,
499500
)
@@ -568,12 +569,14 @@ def test_load_client_ca(self, context, ca_file) -> None:
568569
"""
569570
context.load_client_ca(ca_file)
570571

571-
def test_load_client_ca_invalid(self, context, tmpdir) -> None:
572+
def test_load_client_ca_invalid(
573+
self, context, tmp_path: pathlib.Path
574+
) -> None:
572575
"""
573576
`Context.load_client_ca` raises an Error if the ca file is invalid.
574577
"""
575-
ca_file = tmpdir.join("test.pem")
576-
ca_file.write("")
578+
ca_file = tmp_path / "test.pem"
579+
ca_file.write_text("")
577580

578581
with pytest.raises(Error) as e:
579582
context.load_client_ca(str(ca_file).encode("ascii"))
@@ -1511,7 +1514,7 @@ def test_set_verify_default_callback(self, mode) -> None:
15111514
else:
15121515
self._handshake_test(serverContext, clientContext)
15131516

1514-
def test_add_extra_chain_cert(self, tmpdir) -> None:
1517+
def test_add_extra_chain_cert(self, tmp_path: pathlib.Path) -> None:
15151518
"""
15161519
`Context.add_extra_chain_cert` accepts an `X509`
15171520
instance to add to the certificate chain.
@@ -1533,11 +1536,11 @@ def test_add_extra_chain_cert(self, tmpdir) -> None:
15331536
(icert, "i.pem"),
15341537
(scert, "s.pem"),
15351538
]:
1536-
with tmpdir.join(name).open("w") as f:
1539+
with (tmp_path / name).open("w") as f:
15371540
f.write(dump_certificate(FILETYPE_PEM, cert).decode("ascii"))
15381541

15391542
for key, name in [(cakey, "ca.key"), (ikey, "i.key"), (skey, "s.key")]:
1540-
with tmpdir.join(name).open("w") as f:
1543+
with (tmp_path / name).open("w") as f:
15411544
f.write(dump_privatekey(FILETYPE_PEM, key).decode("ascii"))
15421545

15431546
# Create the server context
@@ -1552,7 +1555,7 @@ def test_add_extra_chain_cert(self, tmpdir) -> None:
15521555
clientContext.set_verify(
15531556
VERIFY_PEER | VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb
15541557
)
1555-
clientContext.load_verify_locations(str(tmpdir.join("ca.pem")))
1558+
clientContext.load_verify_locations(str(tmp_path / "ca.pem"))
15561559

15571560
# Try it out.
15581561
self._handshake_test(serverContext, clientContext)

0 commit comments

Comments
 (0)