Skip to content

Commit

Permalink
Support for TempDirectory to wrap existing directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjw296 committed Jul 6, 2022
1 parent 829bc76 commit 8dc39cf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ def sybil_teardown(namespace):
],
patterns=['*.txt', '*.py'],
setup=sybil_setup, teardown=sybil_teardown,
fixtures=['tmp_path'],
).pytest()
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
intersphinx_mapping = {
'https://docs.python.org/3/': None,
'https://django.readthedocs.io/en/latest/': None,
'https://docs.pytest.org/en/latest/': None,
'https://sybil.readthedocs.io/en/latest/': None,
'https://zopecomponent.readthedocs.io/en/latest/': None,
}
Expand Down
19 changes: 19 additions & 0 deletions docs/files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ you can easily clean them all up:

>>> TempDirectory.cleanup_all()


Wrapping an existing temporary directory
----------------------------------------

If you're using a testing framework that already provides a temporary directory,
such as pytest's :ref:`tmp_path <tmp_path>` or :ref:`tmpdir <tmpdir>`, but wish to make use of
the :class:`TempDirectory` API for creating content or making assertions, then you can wrap the
existing object as follows:

>>> with TempDirectory(tmp_path) as d:
... d.write('some/path.txt', b'some text')
... d.compare(expected=('some/', 'some/path.txt'))
'...'

When doing this, :class:`TempDirectory` will not remove the directory it is wrapping:

>>> tmp_path.exists()
True

Features of a temporary directory
---------------------------------

Expand Down
6 changes: 3 additions & 3 deletions testfixtures/tempdirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class TempDirectory:
#: The absolute path of the :class:`TempDirectory` on disk
path = None

def __init__(self, ignore=(), create=True, path=None, encoding=None):
def __init__(self, path=None, *, ignore=(), create=None, encoding=None):
self.ignore = []
for regex in ignore:
self.ignore.append(compile(regex))
self.path = path
self.path = str(path) if path else None
self.encoding = encoding
self.dont_remove = bool(path)
if create:
if create or (path is None and create is None):
self.create()

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions testfixtures/tests/test_tempdirectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,15 @@ def test_override_default_encoding(self):
with TempDirectory(encoding='ascii') as d:
d.write('test.txt', decoded, encoding='utf-8')
compare(d.read('test.txt', encoding='utf-8'), expected=decoded)


def test_wrap_path(tmp_path: Path):
with TempDirectory(tmp_path) as d:
assert d.path == str(tmp_path)
assert tmp_path.exists()


def test_wrap_local(tmpdir: local):
with TempDirectory(tmpdir) as d:
assert d.path == str(tmpdir)
assert tmpdir.exists()

0 comments on commit 8dc39cf

Please sign in to comment.