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):