Skip to content

Commit

Permalink
add: auto convert absolute_path => relative (iterative#2975)
Browse files Browse the repository at this point in the history
* add: auto convert absolute_path => relative

- fixes iterative#2955

* use path_isin

* catch-all absolute paths in repo

* unneeded leftover

* revert realtive imports for later

* output: move abs2relpath from Base to Local

* output:local:fix parse_path

* remove path_info

* test:local versus base abs2relpath

* test:fix remaining abs2relpath test

* output: local path relative to wdir

* output: local: modify path before inheritance

* output: local: minor inheritance fix

* test: local: split output test

* test: local: positively explicit
  • Loading branch information
casperdcl authored Feb 10, 2020
1 parent 65ee446 commit 3d5b8ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions dvc/output/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class OutputLOCAL(OutputBase):
REMOTE = RemoteLOCAL
sep = os.sep

def __init__(self, stage, path, *args, **kwargs):
if stage and path_isin(path, stage.repo.root_dir):
path = relpath(path, stage.wdir)

super().__init__(stage, path, *args, **kwargs)

def _parse_path(self, remote, path):
parsed = urlparse(path)
if parsed.scheme == "remote":
Expand Down
2 changes: 1 addition & 1 deletion tests/func/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test(self):
self.assertEqual(ret, 0)

d = load_stage_file("bar.dvc")
self.assertEqual(d["outs"][0]["path"], bar)
self.assertEqual(d["outs"][0]["path"], self.BAR)


class TestCmdAdd(TestDvc):
Expand Down
23 changes: 19 additions & 4 deletions tests/unit/output/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,28 @@ def test_str_workdir_inside_repo(dvc):
assert os.path.join("some_folder", "path") == str(output)


def test_str_on_absolute_path(dvc):
def test_str_on_local_absolute_path(dvc):
stage = Stage(dvc)

path = os.path.abspath(os.path.join("path", "to", "file"))
output = OutputLOCAL(stage, path, cache=False)
rel_path = os.path.join("path", "to", "file")
abs_path = os.path.abspath(rel_path)
output = OutputLOCAL(stage, abs_path, cache=False)

assert path == str(output)
assert output.def_path == rel_path
assert output.path_info.fspath == abs_path
assert str(output) == rel_path


def test_str_on_external_absolute_path(dvc):
stage = Stage(dvc)

rel_path = os.path.join("..", "path", "to", "file")
abs_path = os.path.abspath(rel_path)
output = OutputLOCAL(stage, abs_path, cache=False)

assert output.def_path == abs_path
assert output.path_info.fspath == abs_path
assert str(output) == abs_path


class TestGetFilesNumber(TestDvc):
Expand Down

0 comments on commit 3d5b8ec

Please sign in to comment.