From 3d5b8ecdd52263d2ff6b806aca19a6e6264d4a14 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 10 Feb 2020 16:35:24 +0000 Subject: [PATCH] add: auto convert absolute_path => relative (#2975) * add: auto convert absolute_path => relative - fixes #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 --- dvc/output/local.py | 6 ++++++ tests/func/test_add.py | 2 +- tests/unit/output/test_local.py | 23 +++++++++++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/dvc/output/local.py b/dvc/output/local.py index 0e1b72dc97..6e4123d1e3 100644 --- a/dvc/output/local.py +++ b/dvc/output/local.py @@ -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": diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 1ce44a0b0b..002722f52b 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -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): diff --git a/tests/unit/output/test_local.py b/tests/unit/output/test_local.py index 50059e6eae..ef029d2940 100644 --- a/tests/unit/output/test_local.py +++ b/tests/unit/output/test_local.py @@ -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):