for step, path in enumerate([other] + list(other.parents)):
if self.is_relative_to(path):
break
+ elif not walk_up:
+ raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
+ elif path.name == '..':
+ raise ValueError(f"'..' segment in {str(other)!r} cannot be walked")
else:
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
- if step and not walk_up:
- raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
parts = ['..'] * step + self._tail[len(path._tail):]
return self.with_segments(*parts)
self.assertRaises(ValueError, p.relative_to, P('a/b/c'))
self.assertRaises(ValueError, p.relative_to, P('a/c'))
self.assertRaises(ValueError, p.relative_to, P('/a'))
+ self.assertRaises(ValueError, p.relative_to, P("../a"))
+ self.assertRaises(ValueError, p.relative_to, P("a/.."))
+ self.assertRaises(ValueError, p.relative_to, P("/a/.."))
self.assertRaises(ValueError, p.relative_to, P('/'), walk_up=True)
self.assertRaises(ValueError, p.relative_to, P('/a'), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
p = P('/a/b')
self.assertEqual(p.relative_to(P('/')), P('a/b'))
self.assertEqual(p.relative_to('/'), P('a/b'))
self.assertRaises(ValueError, p.relative_to, P())
self.assertRaises(ValueError, p.relative_to, '')
self.assertRaises(ValueError, p.relative_to, P('a'))
+ self.assertRaises(ValueError, p.relative_to, P("../a"))
+ self.assertRaises(ValueError, p.relative_to, P("a/.."))
+ self.assertRaises(ValueError, p.relative_to, P("/a/.."))
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("../a"), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("a/.."), walk_up=True)
+ self.assertRaises(ValueError, p.relative_to, P("/a/.."), walk_up=True)
def test_is_relative_to_common(self):
P = self.cls