make pull --run-cache work with local remote having relative path (#4670)

* Fixes #4637

* use PathInfo instead of Path

* catch ValueError with PathInfo.relative_to and fallback to dvc.utils.relpath

* cleanup

* add test

Co-authored-by: Peter Rowlands <[email protected]>
This commit is contained in:
Musa Atlıhan
2020-10-19 17:53:01 +09:00
committed by GitHub
co-authored by Peter Rowlands
parent 699602f4d0
commit 4b6ebc7527
2 changed files with 26 additions and 0 deletions
+11
View File
@@ -75,6 +75,17 @@ class PathInfo(pathlib.PurePath, _BasePath):
n = len(other._cparts)
return len(self._cparts) > n and self._cparts[:n] == other._cparts
def relative_to(self, other): # pylint: disable=arguments-differ
# pathlib relative_to raises exception when one path is not a direct
# descendant of the other when os.path.relpath would return abspath.
# For DVC PathInfo we only need the relpath behavior.
# See: https://bugs.python.org/issue40358
try:
path = super().relative_to(other)
except ValueError:
path = relpath(self, other)
return self.__class__(path)
class WindowsPathInfo(PathInfo, pathlib.PureWindowsPath):
pass
+15
View File
@@ -1,4 +1,5 @@
import copy
import os
import pathlib
import pytest
@@ -102,3 +103,17 @@ def test_url_replace_path():
assert u.params == new_u.params
assert u.query == new_u.query
assert u.fragment == new_u.fragment
@pytest.mark.parametrize(
"p1, p2",
[
("/foo/bar", "/baz"),
("/baz", "/foo/bar"),
("/foo/bar", "../baz"),
("../baz", "/foo/bar"),
],
)
def test_relative_to_different_subpath(p1, p2):
expected = PathInfo(os.path.relpath(p1, p2))
assert PathInfo(p1).relative_to(PathInfo(p2)) == expected