Add subclass of pathlib.Path for existing directories

This commit is contained in:
2026-04-05 20:15:33 +02:00
parent 00fbd89746
commit d79a22e98f
2 changed files with 64 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
# Copyright 2026 jCloud Services GbR
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
The utilities for jcloud_docsgen
'''
import pathlib
import os
import errno
class ExistingDirectory(pathlib.Path):
'''
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
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.isdir(path):
raise NotADirectoryError(errno.ENOTDIR, 'not a directory', path)
super().__init__(*(path, *args), **kwargs)
@@ -0,0 +1,28 @@
from src.jcloud_docsgen._utils import ExistingDirectory
import pytest
import pathlib
@pytest.mark.parametrize('path,expected', [
('tests/unit/_utils/test_directory/', 'tests/unit/_utils/test_directory'),
('tests/unit/_utils/test_directory', 'tests/unit/_utils/test_directory'),
('tests/unit/_utils/', 'tests/unit/_utils'),
('tests/unit/_utils', 'tests/unit/_utils'),
('/', '/'),
('./', '.'),
('.', '.'),
('../', '..'),
('..', '..'),
])
def test_ExistingDirectory(path, expected):
assert ExistingDirectory(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/\''),
('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: \'\''),
('README.md', NotADirectoryError, '[Errno 20] not a directory: \'README.md\''),
])
def test_ExistingDirectory_exceptions(path, expected_exception, expected_exception_message):
with pytest.raises(expected_exception) as exc_info:
ExistingDirectory(path)
assert str(exc_info.value) == expected_exception_message