Skip to content

Commit

Permalink
🩹 Fix wrong paths in result.yml when using relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Dec 18, 2022
1 parent e5f083b commit 2f1643b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions glotaran/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def chdir_context(folder_path: StrOrPath) -> Generator[Path, None, None]:
def relative_posix_path(source_path: StrOrPath, base_path: StrOrPath | None = None) -> str:
"""Ensure that ``source_path`` is a posix path, relative to ``base_path`` if defined.
For ``source_path`` to be converted to a relative path it either needs to a an absolute path or
``base_path`` needs to be a parent directory of ``source_path``.
On Windows if ``source_path`` and ``base_path`` are on different drives, it will return
the absolute posix path to the file.
Expand All @@ -234,17 +236,20 @@ def relative_posix_path(source_path: StrOrPath, base_path: StrOrPath | None = No
source_path : StrOrPath
Path which should be converted to a relative posix path.
base_path : StrOrPath, optional
Base path the resulting path string should be relative to., by default None
Base path the resulting path string should be relative to. Defaults to ``None``.
Returns
-------
str
``source_path`` as posix path relative to ``base_path`` if defined.
"""
source_path = Path(source_path).as_posix()
if base_path is not None and os.path.isabs(source_path):
source_path = Path(source_path)
if base_path is not None and (
source_path.is_absolute() or Path(base_path).resolve() in source_path.resolve().parents
):
with contextlib.suppress(ValueError):
source_path = os.path.relpath(source_path, Path(base_path).as_posix())
source_path = os.path.relpath(source_path.as_posix(), Path(base_path).as_posix())

return Path(source_path).as_posix()


Expand Down
8 changes: 8 additions & 0 deletions glotaran/utils/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def test_relative_posix_path(tmp_path: Path, rel_file_path: str):

assert rel_result_no_common == f"../{rel_file_path}"

result_folder = tmp_path / "results"
with chdir_context(result_folder):
original_rel_path = relative_posix_path(rel_file_path, result_folder)
assert original_rel_path == rel_file_path

original_rel_path = relative_posix_path(rel_file_path, "not_a_parent")
assert original_rel_path == rel_file_path


def test_chdir_context(tmp_path: Path):
"""Original Path is restored even after exception is thrown."""
Expand Down

0 comments on commit 2f1643b

Please sign in to comment.