diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c80ed5e..3056253 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -14,4 +14,5 @@ - Add feature to get the names of all sub-namespaces of a `core.python.namespaces.PythonPackageNamespace` - 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 \ No newline at end of file +- Add `PythonDocumentationGenerator` method to collect all namespaces +- Add class for existing files \ No newline at end of file diff --git a/src/jcloud_docsgen/utils.py b/src/jcloud_docsgen/utils.py index afb76d3..75e54b5 100644 --- a/src/jcloud_docsgen/utils.py +++ b/src/jcloud_docsgen/utils.py @@ -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. diff --git a/tests/unit/utils/test_ExistingFile.py b/tests/unit/utils/test_ExistingFile.py new file mode 100644 index 0000000..2cc0749 --- /dev/null +++ b/tests/unit/utils/test_ExistingFile.py @@ -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 \ No newline at end of file diff --git a/tests/unit/utils/test_file b/tests/unit/utils/test_file new file mode 100644 index 0000000..e69de29