Skip to content
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

Return written path in Blob.write_to_path #152

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/152.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return written path in :attr:`Blob.write_to_path` to simplify the usage of this method.
14 changes: 7 additions & 7 deletions tests/test_trustme.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
import ssl
import socket
import threading
import datetime
from concurrent.futures import ThreadPoolExecutor

Expand Down Expand Up @@ -197,22 +196,22 @@ def test_blob(tmpdir):

# write_to_path

b.write_to_path(str(tmpdir / "test1"))
with (tmpdir / "test1").open("rb") as f:
path = b.write_to_path(str(tmpdir / "test1"))
with open(path, "rb") as f:
assert f.read() == test_data

# append=False overwrites
with (tmpdir / "test2").open("wb") as f:
f.write(b"asdf")
b.write_to_path(str(tmpdir / "test2"))
with (tmpdir / "test2").open("rb") as f:
path = b.write_to_path(str(tmpdir / "test2"))
with open(path, "rb") as f:
assert f.read() == test_data

# append=True appends
with (tmpdir / "test2").open("wb") as f:
f.write(b"asdf")
b.write_to_path(str(tmpdir / "test2"), append=True)
with (tmpdir / "test2").open("rb") as f:
path = b.write_to_path(str(tmpdir / "test2"), append=True)
with open(path, "rb") as f:
assert f.read() == b"asdf" + test_data

# tempfile
Expand All @@ -222,6 +221,7 @@ def test_blob(tmpdir):
with open(path, "rb") as f:
assert f.read() == test_data


def test_ca_from_pem(tmpdir):
ca1 = trustme.CA()
ca2 = trustme.CA.from_pem(ca1.cert_pem.bytes(), ca1.private_key_pem.bytes())
Expand Down
3 changes: 3 additions & 0 deletions trustme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def write_to_path(self, path, append=False):
path (str): The path to write to.
append (bool): If False (the default), replace any existing file
with the given name. If True, append to any existing file.
Returns:
The written path.

"""
if append:
Expand All @@ -160,6 +162,7 @@ def write_to_path(self, path, append=False):
mode = "wb"
with open(path, mode) as f:
f.write(self._data)
return path

@contextmanager
def tempfile(self, dir=None):
Expand Down