generated from jCloud/repository-template
Add class for existing files
This commit is contained in:
@@ -15,3 +15,4 @@
|
||||
- Add feature to compare `core.python.namespaces.PythonPackageNamespace` instances or `core.python.namespaces.PythonModuleNamespace` instances
|
||||
- Add string representation for python namespace objects
|
||||
- Add `PythonDocumentationGenerator` method to collect all namespaces
|
||||
- Add class for existing files
|
||||
@@ -24,7 +24,7 @@ import types
|
||||
|
||||
class ExistingDirectory(pathlib.Path):
|
||||
'''
|
||||
pathlib.Path subclass that is a directory
|
||||
pathlib.Path subclass that is a directory.
|
||||
|
||||
ExistingDirectory represents a filesystem path but it also checks
|
||||
whether the path exists and is not a file but a directory. It
|
||||
@@ -37,6 +37,21 @@ class ExistingDirectory(pathlib.Path):
|
||||
raise NotADirectoryError(errno.ENOTDIR, 'not a directory', path)
|
||||
super().__init__(*(path, *args), **kwargs)
|
||||
|
||||
class ExistingFile(pathlib.Path):
|
||||
'''
|
||||
pathlib.Path subclass that is a file.
|
||||
|
||||
ExistingFile represents a filesystem path but it also checks whether
|
||||
the path exists and is not a directory but a file. It inherits from
|
||||
pathlib.Path.
|
||||
'''
|
||||
def __init__(self, path, *args, **kwargs) -> None:
|
||||
if not os.path.exists(path):
|
||||
raise FileNotFoundError(errno.ENOENT, 'no such file or directory', path)
|
||||
if not os.path.isfile(path):
|
||||
raise IsADirectoryError(errno.EISDIR, 'is a directory', path)
|
||||
super().__init__(*(path, *args), **kwargs)
|
||||
|
||||
def _quote(string: str, quotation_mark: str) -> str:
|
||||
'''
|
||||
Quotes a string.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from src.jcloud_docsgen.utils import ExistingFile
|
||||
import pytest
|
||||
import pathlib
|
||||
|
||||
@pytest.mark.parametrize('path,expected', [
|
||||
('tests/unit/utils/test_file', 'tests/unit/utils/test_file'),
|
||||
('tests/unit/utils/test_symlink', 'tests/unit/utils/test_symlink'),
|
||||
])
|
||||
def test_ExistingDirectory(path, expected):
|
||||
assert ExistingFile(path).as_posix() == expected
|
||||
|
||||
@pytest.mark.parametrize('path,expected_exception,expected_exception_message', [
|
||||
('tests/unit/utils/does_not_exist', FileNotFoundError, '[Errno 2] no such file or directory: \'tests/unit/utils/does_not_exist\''),
|
||||
('', FileNotFoundError, '[Errno 2] no such file or directory: \'\''),
|
||||
('tests/unit/utils/test_directory', IsADirectoryError, '[Errno 21] is a directory: \'tests/unit/utils/test_directory\''),
|
||||
('.', IsADirectoryError, '[Errno 21] is a directory: \'.\''),
|
||||
('./', IsADirectoryError, '[Errno 21] is a directory: \'./\''),
|
||||
])
|
||||
def test_ExistingDirectory_exceptions(path, expected_exception, expected_exception_message):
|
||||
with pytest.raises(expected_exception) as exc_info:
|
||||
ExistingFile(path)
|
||||
assert str(exc_info.value) == expected_exception_message
|
||||
Reference in New Issue
Block a user