MCPcopy
hub / github.com/pytest-dev/pytest / bestrelpath

Function bestrelpath

src/_pytest/pathlib.py:1010–1036  ·  view source on GitHub ↗

Return a string which is a relative path from directory to dest such that directory/bestrelpath == dest. The paths must be either both absolute or both relative. If no such path can be determined, returns dest.

(directory: Path, dest: Path)

Source from the content-addressed store, hash-verified

1008
1009
1010def bestrelpath(directory: Path, dest: Path) -> str:
1011 """Return a string which is a relative path from directory to dest such
1012 that directory/bestrelpath == dest.
1013
1014 The paths must be either both absolute or both relative.
1015
1016 If no such path can be determined, returns dest.
1017 """
1018 assert isinstance(directory, Path)
1019 assert isinstance(dest, Path)
1020 if dest == directory:
1021 return os.curdir
1022 # Find the longest common directory.
1023 base = commonpath(directory, dest)
1024 # Can be the case on Windows for two absolute paths on different drives.
1025 # Can be the case for two relative paths without common prefix.
1026 # Can be the case for a relative path and an absolute path.
1027 if not base:
1028 return str(dest)
1029 reldirectory = directory.relative_to(base)
1030 reldest = dest.relative_to(base)
1031 return os.path.join(
1032 # Back from directory to base.
1033 *([os.pardir] * len(reldirectory.parts)),
1034 # Forward from base to dest.
1035 *reldest.parts,
1036 )
1037
1038
1039def safe_exists(p: Path) -> bool:

Callers 14

getpathnodeMethod · 0.90
get_locationMethod · 0.90
write_fspath_resultMethod · 0.90
pytest_report_headerMethod · 0.90
_locationlineMethod · 0.90
_folded_skipsFunction · 0.90
_pretty_fixture_pathFunction · 0.90
get_best_relpathFunction · 0.90
__missing__Method · 0.90
_makepathMethod · 0.90
cwd_relative_nodeidMethod · 0.90

Calls 2

commonpathFunction · 0.85
joinMethod · 0.80

Tested by 3

getpathnodeMethod · 0.72
test_bestrelpathFunction · 0.72