generated from jCloud/repository-template
Add function to get the path segments of a path relative to another
This commit is contained in:
@@ -24,3 +24,4 @@
|
|||||||
- Add feature to parse Python docstrings
|
- Add feature to parse Python docstrings
|
||||||
- Add function to ensure a non-empty string.
|
- Add function to ensure a non-empty string.
|
||||||
- Add Python definition documentation generator
|
- Add Python definition documentation generator
|
||||||
|
- Add function to get the path segments of a path relative to another
|
||||||
@@ -152,3 +152,24 @@ def non_empty_str(value: Union[str, None]) -> Union[str, None]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def get_relative_path_segments(path: pathlib.Path, root: pathlib.Path) -> list[str]:
|
||||||
|
'''
|
||||||
|
Returns the path segments relative to ``root``.
|
||||||
|
|
||||||
|
:param path: The path.
|
||||||
|
:type path: pathlib.Path
|
||||||
|
:param root: The root path.
|
||||||
|
:type root: pathlib.Path
|
||||||
|
|
||||||
|
:return: The path segments relative to ``root``.
|
||||||
|
:rtype: list[str]
|
||||||
|
'''
|
||||||
|
|
||||||
|
return list(
|
||||||
|
path.resolve()
|
||||||
|
.relative_to(
|
||||||
|
root.resolve()
|
||||||
|
)
|
||||||
|
.parts
|
||||||
|
)
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
from src.jcloud_docsgen.utils import get_relative_path_segments
|
||||||
|
import pytest
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('path,root,expected', [
|
||||||
|
(pathlib.Path('/'), pathlib.Path('/'), []),
|
||||||
|
(pathlib.Path('/a'), pathlib.Path('/'), ['a']),
|
||||||
|
(pathlib.Path('/a/'), pathlib.Path('/'), ['a']),
|
||||||
|
(pathlib.Path('/a'), pathlib.Path('/a'), []),
|
||||||
|
(pathlib.Path('/a/'), pathlib.Path('/a'), []),
|
||||||
|
(pathlib.Path('/a'), pathlib.Path('/a/'), []),
|
||||||
|
(pathlib.Path('/a/'), pathlib.Path('/a/'), []),
|
||||||
|
(pathlib.Path('/a/b'), pathlib.Path('/'), ['a', 'b']),
|
||||||
|
(pathlib.Path('/a/b/'), pathlib.Path('/'), ['a', 'b']),
|
||||||
|
(pathlib.Path('/a/b'), pathlib.Path('/a'), ['b']),
|
||||||
|
(pathlib.Path('/a/b/'), pathlib.Path('/a'), ['b']),
|
||||||
|
(pathlib.Path('/a/b'), pathlib.Path('/a/'), ['b']),
|
||||||
|
(pathlib.Path('/a/b/'), pathlib.Path('/a/'), ['b']),
|
||||||
|
(pathlib.Path('/a/b'), pathlib.Path('/a/b'), []),
|
||||||
|
(pathlib.Path('/a/b/'), pathlib.Path('/a/b'), []),
|
||||||
|
(pathlib.Path('/a/b'), pathlib.Path('/a/b/'), []),
|
||||||
|
(pathlib.Path('/a/b/'), pathlib.Path('/a/b/'), []),
|
||||||
|
|
||||||
|
(pathlib.Path('./'), pathlib.Path('./'), []),
|
||||||
|
(pathlib.Path('./a'), pathlib.Path('./'), ['a']),
|
||||||
|
(pathlib.Path('./a/'), pathlib.Path('./'), ['a']),
|
||||||
|
(pathlib.Path('./a'), pathlib.Path('./a'), []),
|
||||||
|
(pathlib.Path('./a/'), pathlib.Path('./a'), []),
|
||||||
|
(pathlib.Path('./a'), pathlib.Path('./a/'), []),
|
||||||
|
(pathlib.Path('./a/'), pathlib.Path('./a/'), []),
|
||||||
|
(pathlib.Path('./a/b'), pathlib.Path('./'), ['a', 'b']),
|
||||||
|
(pathlib.Path('./a/b/'), pathlib.Path('./'), ['a', 'b']),
|
||||||
|
(pathlib.Path('./a/b'), pathlib.Path('./a'), ['b']),
|
||||||
|
(pathlib.Path('./a/b/'), pathlib.Path('./a'), ['b']),
|
||||||
|
(pathlib.Path('./a/b'), pathlib.Path('./a/'), ['b']),
|
||||||
|
(pathlib.Path('./a/b/'), pathlib.Path('./a/'), ['b']),
|
||||||
|
(pathlib.Path('./a/b'), pathlib.Path('./a/b'), []),
|
||||||
|
(pathlib.Path('./a/b/'), pathlib.Path('./a/b'), []),
|
||||||
|
(pathlib.Path('./a/b'), pathlib.Path('./a/b/'), []),
|
||||||
|
(pathlib.Path('./a/b/'), pathlib.Path('./a/b/'), []),
|
||||||
|
|
||||||
|
(pathlib.Path(''), pathlib.Path(''), []),
|
||||||
|
(pathlib.Path('a'), pathlib.Path(''), ['a']),
|
||||||
|
(pathlib.Path('a/'), pathlib.Path(''), ['a']),
|
||||||
|
(pathlib.Path('a'), pathlib.Path('a'), []),
|
||||||
|
(pathlib.Path('a/'), pathlib.Path('a'), []),
|
||||||
|
(pathlib.Path('a'), pathlib.Path('a/'), []),
|
||||||
|
(pathlib.Path('a/'), pathlib.Path('a/'), []),
|
||||||
|
(pathlib.Path('a/b'), pathlib.Path(''), ['a', 'b']),
|
||||||
|
(pathlib.Path('a/b/'), pathlib.Path(''), ['a', 'b']),
|
||||||
|
(pathlib.Path('a/b'), pathlib.Path('a'), ['b']),
|
||||||
|
(pathlib.Path('a/b/'), pathlib.Path('a'), ['b']),
|
||||||
|
(pathlib.Path('a/b'), pathlib.Path('a/'), ['b']),
|
||||||
|
(pathlib.Path('a/b/'), pathlib.Path('a/'), ['b']),
|
||||||
|
(pathlib.Path('a/b'), pathlib.Path('a/b'), []),
|
||||||
|
(pathlib.Path('a/b/'), pathlib.Path('a/b'), []),
|
||||||
|
(pathlib.Path('a/b'), pathlib.Path('a/b/'), []),
|
||||||
|
(pathlib.Path('a/b/'), pathlib.Path('a/b/'), []),
|
||||||
|
|
||||||
|
])
|
||||||
|
def test_get_relative_path_segments(path, root, expected):
|
||||||
|
assert get_relative_path_segments(path, root) == expected
|
||||||
Reference in New Issue
Block a user