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

Method bestrelpath

src/_pytest/_py/path.py:454–478  ·  view source on GitHub ↗

Return a string which is a relative path from self (assumed to be a directory) to dest such that self.join(bestrelpath) == dest and if not such path can be determined return dest.

(self, dest)

Source from the content-addressed store, hash-verified

452 return self.ensure(*args, dir=True)
453
454 def bestrelpath(self, dest):
455 """Return a string which is a relative path from self
456 (assumed to be a directory) to dest such that
457 self.join(bestrelpath) == dest and if not such
458 path can be determined return dest.
459 """
460 try:
461 if self == dest:
462 return os.curdir
463 base = self.common(dest)
464 if not base: # can be the case on windows
465 return str(dest)
466 self2base = self.relto(base)
467 reldest = dest.relto(base)
468 if self2base:
469 n = self2base.count(self.sep) + 1
470 else:
471 n = 0
472 lst = [os.pardir] * n
473 if reldest:
474 lst.append(reldest)
475 target = dest.sep.join(lst)
476 return target
477 except AttributeError:
478 return str(dest)
479
480 def exists(self):
481 return self.check()

Callers 1

test_bestrelpathMethod · 0.80

Calls 5

commonMethod · 0.95
reltoMethod · 0.95
appendMethod · 0.80
joinMethod · 0.80
reltoMethod · 0.45

Tested by 1

test_bestrelpathMethod · 0.64