diff --git a/dvc/repo/move.py b/dvc/repo/move.py index 6b1e0a6f6..46b4fc98b 100644 --- a/dvc/repo/move.py +++ b/dvc/repo/move.py @@ -52,7 +52,9 @@ def move(self, from_path, to_path): os.path.basename(to_path) + Stage.STAGE_FILE_SUFFIX, ) - stage.cwd = os.path.join(self.root_dir, os.path.dirname(to_path)) + stage.cwd = os.path.abspath( + os.path.join(os.curdir, os.path.dirname(to_path)) + ) to_out = Output.loads_from( stage, [os.path.basename(to_path)], out.cache, out.metric diff --git a/dvc/utils/compat.py b/dvc/utils/compat.py index da16b3afc..c72871dec 100644 --- a/dvc/utils/compat.py +++ b/dvc/utils/compat.py @@ -2,6 +2,7 @@ import sys import os +import errno # Syntax sugar. @@ -42,8 +43,9 @@ def _makedirs(name, mode=0o777, exist_ok=False): if head and tail and not os.path.exists(head): try: _makedirs(head, exist_ok=exist_ok) - except FileExistsError: - pass + except OSError as e: + if e.errno == errno.EEXIST: + pass cdir = os.curdir if isinstance(tail, bytes): cdir = bytes(os.curdir, "ASCII") diff --git a/tests/test_move.py b/tests/test_move.py index cff338801..297400ae1 100644 --- a/tests/test_move.py +++ b/tests/test_move.py @@ -6,7 +6,7 @@ from dvc.stage import Stage from tests.basic_env import TestDvc from tests.test_repro import TestRepro -from tests.utils import load_stage_file +from tests.utils import load_stage_file, cd class TestMove(TestDvc): @@ -168,3 +168,17 @@ class TestMoveFileBetweenDirectories(TestDvc): self.assertEqual( os.path.basename(self.DATA), new_stage_file["outs"][0]["path"] ) + + +class TestMoveFileInsideDirectory(TestDvc): + def test(self): + ret = main(["add", "data_dir/data"]) + self.assertEqual(ret, 0) + + with cd("data_dir"): + ret = main(["move", "data", "data.txt"]) + self.assertEqual(ret, 0) + + self.assertFalse(os.path.exists("data_dir/data")) + self.assertTrue(os.path.exists("data_dir/data.txt")) + self.assertTrue(os.path.exists("data_dir/data.txt.dvc")) diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index d4e2f91e1..d39ef5d9a 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -1,5 +1,7 @@ import yaml +import os from mock import MagicMock +from contextlib import contextmanager def spy(method_to_decorate): @@ -16,3 +18,13 @@ def spy(method_to_decorate): def load_stage_file(path): with open(path, "r") as fobj: return yaml.safe_load(fobj) + + +@contextmanager +def cd(newdir): + prevdir = os.getcwd() + os.chdir(os.path.expanduser(newdir)) + try: + yield + finally: + os.chdir(prevdir)