Assert TempDirectory.path has not been cleaned up on access

This commit is contained in:
Chris Hunt 2019-09-23 20:00:01 -04:00
parent 7ea1fcdb13
commit e6bde63620
2 changed files with 27 additions and 4 deletions

View File

@ -54,12 +54,16 @@ class TempDirectory(object):
path = self._create(kind)
self._path = path
self._deleted = False
self.delete = delete
self.kind = kind
@property
def path(self):
# type: () -> str
assert not self._deleted, (
"Attempted to access deleted path: {}".format(self._path)
)
return self._path
def __repr__(self):
@ -88,8 +92,9 @@ class TempDirectory(object):
def cleanup(self):
"""Remove the temporary directory created and reset state
"""
if os.path.exists(self.path):
rmtree(self.path)
self._deleted = True
if os.path.exists(self._path):
rmtree(self._path)
class AdjacentTempDirectory(TempDirectory):

View File

@ -55,7 +55,26 @@ def test_deletes_readonly_files():
create_file(tmp_dir.path, "subfolder", "readonly-file")
readonly_file(tmp_dir.path, "subfolder", "readonly-file")
assert not os.path.exists(tmp_dir.path)
def test_path_access_after_context_raises():
with TempDirectory() as tmp_dir:
path = tmp_dir.path
with pytest.raises(AssertionError) as e:
_ = tmp_dir.path
assert path in str(e.value)
def test_path_access_after_clean_raises():
tmp_dir = TempDirectory()
path = tmp_dir.path
tmp_dir.cleanup()
with pytest.raises(AssertionError) as e:
_ = tmp_dir.path
assert path in str(e.value)
def test_create_and_cleanup_work():
@ -66,7 +85,6 @@ def test_create_and_cleanup_work():
assert os.path.exists(created_path)
tmp_dir.cleanup()
assert tmp_dir.path is not None
assert not os.path.exists(created_path)