MCPcopy Create free account
hub / github.com/pytest-dev/pytest / bestrelpath

Function bestrelpath

src/_pytest/pathlib.py:680–706  ·  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

678
679
680def bestrelpath(directory: Path, dest: Path) -> str:
681 """Return a string which is a relative path from directory to dest such
682 that directory/bestrelpath == dest.
683
684 The paths must be either both absolute or both relative.
685
686 If no such path can be determined, returns dest.
687 """
688 assert isinstance(directory, Path)
689 assert isinstance(dest, Path)
690 if dest == directory:
691 return os.curdir
692 # Find the longest common directory.
693 base = commonpath(directory, dest)
694 # Can be the case on Windows for two absolute paths on different drives.
695 # Can be the case for two relative paths without common prefix.
696 # Can be the case for a relative path and an absolute path.
697 if not base:
698 return str(dest)
699 reldirectory = directory.relative_to(base)
700 reldest = dest.relative_to(base)
701 return os.path.join(
702 # Back from directory to base.
703 *([os.pardir] * len(reldirectory.parts)),
704 # Forward from base to dest.
705 *reldest.parts,
706 )
707
708
709# Originates from py. path.local.copy(), with siginficant trims and adjustments.

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
_factorytracebackMethod · 0.90
__missing__Method · 0.90
_makepathMethod · 0.90
cwd_relative_nodeidMethod · 0.90

Calls 1

commonpathFunction · 0.85

Tested by 3

getpathnodeMethod · 0.72
test_bestrelpathFunction · 0.72