From 6f9dac280f24d1c1a5d843e74fc47f5fbffe2c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Thu, 28 Mar 2019 15:45:47 +0100 Subject: [PATCH] stage: remove outs upon import (#1796) * stage: remove outs upon import * Update dvc/remote/local.py Co-Authored-By: pared --- dvc/remote/local.py | 6 +++++- dvc/stage.py | 2 +- tests/test_import.py | 23 ++++++++++++++++++++++- tests/test_run.py | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/dvc/remote/local.py b/dvc/remote/local.py index adb847a04..b59e64109 100644 --- a/dvc/remote/local.py +++ b/dvc/remote/local.py @@ -755,7 +755,11 @@ class RemoteLOCAL(RemoteBase): # would get only the part of file. So, at first, the file should be # copied with the temporary name, and then original file should be # replaced by new. - copyfile(path, tmp) + copyfile( + path, + tmp, + name="Unprotecting '{}'".format(os.path.relpath(path)), + ) remove(path) os.rename(tmp, path) diff --git a/dvc/stage.py b/dvc/stage.py index cec811136..913b21e0a 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -730,7 +730,7 @@ class Stage(object): raise StageCmdFailedError(self) def run(self, dry=False, resume=False, no_commit=False, force=False): - if self.cmd and not self.locked and not dry: + if (self.cmd or self.is_import) and not self.locked and not dry: self.remove_outs(ignore_remove=False, force=False) if self.locked: diff --git a/tests/test_import.py b/tests/test_import.py index fa8d67ccf..82ebb3d66 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -1,3 +1,5 @@ +import dvc + from dvc.utils.compat import str import os @@ -9,7 +11,7 @@ from dvc.exceptions import DvcException from dvc.main import main from mock import patch, mock_open, call from tests.basic_env import TestDvc -from tests.utils import reset_logger_error_output +from tests.utils import reset_logger_error_output, spy from tests.utils.httpd import StaticFileServer from tests.utils.logger import MockLoggerHandlers, ConsoleFontColorsRemover @@ -137,3 +139,22 @@ class TestShouldNotResumeDownload(TestInterruptedDownload): m_handle = m() expected_calls = [call(b"f"), call(b"o"), call(b"o")] m_handle.write.assert_has_calls(expected_calls, any_order=False) + + +class TestShouldRemoveOutsBeforeImport(TestDvc): + def setUp(self): + super(TestShouldRemoveOutsBeforeImport, self).setUp() + tmp_dir = self.mkdtemp() + self.external_source = os.path.join(tmp_dir, "file") + with open(self.external_source, "w") as fobj: + fobj.write("content") + + def test(self): + remove_outs_call_counter = spy(dvc.stage.Stage.remove_outs) + with patch.object( + dvc.stage.Stage, "remove_outs", remove_outs_call_counter + ): + ret = main(["import", self.external_source]) + self.assertEqual(0, ret) + + self.assertEqual(1, remove_outs_call_counter.mock.call_count) diff --git a/tests/test_run.py b/tests/test_run.py index 448e3803a..26a194541 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -864,7 +864,7 @@ class TestShouldRaiseOnOverlappingOutputPaths(TestDvc): class TestRerunWithSameOutputs(TestDvc): def _read_content_only(self, path): with open(path, "r") as fobj: - return [line.rstrip() for line in fobj.readlines()] + return [line.rstrip() for line in fobj] @property def _outs_command(self):