diff --git a/dvc/analytics.py b/dvc/analytics.py index 1ae02f60a..a8969b2ef 100644 --- a/dvc/analytics.py +++ b/dvc/analytics.py @@ -164,4 +164,6 @@ def _find_or_create_user_id(): return user_id except LockError: - logger.debug("Failed to acquire {lockfile}".format(lockfile=lockfile)) + logger.debug( + "Failed to acquire '{lockfile}'".format(lockfile=lockfile) + ) diff --git a/dvc/command/remove.py b/dvc/command/remove.py index 179d373f6..b4b47f98f 100644 --- a/dvc/command/remove.py +++ b/dvc/command/remove.py @@ -20,7 +20,7 @@ class CmdRemove(CmdBase): if self.args.force: return False - msg = "Are you sure you want to remove {} with its outputs?".format( + msg = "Are you sure you want to remove '{}' with its outputs?".format( target ) @@ -38,7 +38,7 @@ class CmdRemove(CmdBase): outs_only = self._is_outs_only(target) self.repo.remove(target, outs_only=outs_only) except DvcException: - logger.exception("failed to remove {}".format(target)) + logger.exception("failed to remove '{}'".format(target)) return 1 return 0 diff --git a/dvc/config.py b/dvc/config.py index 0854ae930..5c71da3d0 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -81,7 +81,7 @@ def Choices(*choices): *choices: pass allowed values as arguments, or pass a list or tuple as a single argument """ - return Any(*choices, msg="expected one of {}".format(",".join(choices))) + return Any(*choices, msg="expected one of {}".format(", ".join(choices))) class Config(object): # pylint: disable=too-many-instance-attributes diff --git a/dvc/remote/base.py b/dvc/remote/base.py index 57a1846a6..0f66b27ab 100644 --- a/dvc/remote/base.py +++ b/dvc/remote/base.py @@ -58,7 +58,7 @@ class RemoteCmdError(DvcException): class RemoteActionNotImplemented(DvcException): def __init__(self, action, scheme): - m = "{} is not supported by {} remote".format(action, scheme) + m = "{} is not supported for {} remotes".format(action, scheme) super(RemoteActionNotImplemented, self).__init__(m) diff --git a/dvc/remote/oss.py b/dvc/remote/oss.py index 9445fd0b2..0abf1a92e 100644 --- a/dvc/remote/oss.py +++ b/dvc/remote/oss.py @@ -69,9 +69,9 @@ class RemoteOSS(RemoteBASE): def oss_service(self): import oss2 - logger.debug("URL {}".format(self.path_info)) - logger.debug("key id {}".format(self.key_id)) - logger.debug("key secret {}".format(self.key_secret)) + logger.debug("URL: {}".format(self.path_info)) + logger.debug("key id: {}".format(self.key_id)) + logger.debug("key secret: {}".format(self.key_secret)) auth = oss2.Auth(self.key_id, self.key_secret) bucket = oss2.Bucket(auth, self.endpoint, self.path_info.bucket) diff --git a/dvc/repo/add.py b/dvc/repo/add.py index 85486c1f7..8fe180d41 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -37,7 +37,7 @@ def add(repo, targets, recursive=False, no_commit=False, fname=None): "You are adding a large directory '{target}' recursively," " consider tracking it as a whole instead.\n" "{purple}HINT:{nc} Remove the generated DVC-file and then" - " run {cyan}dvc add {target}{nc}".format( + " run `{cyan}dvc add {target}{nc}`".format( purple=colorama.Fore.MAGENTA, cyan=colorama.Fore.CYAN, nc=colorama.Style.RESET_ALL, diff --git a/dvc/repo/gc.py b/dvc/repo/gc.py index 8313f8feb..85d681b08 100644 --- a/dvc/repo/gc.py +++ b/dvc/repo/gc.py @@ -12,7 +12,7 @@ logger = logging.getLogger(__name__) def _do_gc(typ, func, clist): removed = func(clist) if not removed: - logger.info("No unused {} cache to remove.".format(typ)) + logger.info("No unused '{}' cache to remove.".format(typ)) @locked diff --git a/dvc/stage.py b/dvc/stage.py index 236a5f2d3..7aafa5d75 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -116,7 +116,7 @@ class MissingDep(DvcException): else: dep = "dependency" - msg = "missing {}: {}".format(dep, ", ".join(map(str, deps))) + msg = "missing '{}': {}".format(dep, ", ".join(map(str, deps))) super(MissingDep, self).__init__(msg) @@ -128,7 +128,7 @@ class MissingDataSource(DvcException): if len(missing_files) > 1: source += "s" - msg = "missing data {}: {}".format(source, ", ".join(missing_files)) + msg = "missing data '{}': {}".format(source, ", ".join(missing_files)) super(MissingDataSource, self).__init__(msg) diff --git a/dvc/updater.py b/dvc/updater.py index 661221926..4c83084a6 100644 --- a/dvc/updater.py +++ b/dvc/updater.py @@ -124,12 +124,13 @@ class Updater(object): # pragma: no cover def _get_update_instructions(self): instructions = { - "pip": "Run {yellow}pip{reset} install dvc {blue}--upgrade{reset}", - "rpm": "Run {yellow}yum{reset} update dvc", - "brew": "Run {yellow}brew{reset} upgrade dvc", + "pip": "Run `{yellow}pip{reset} install dvc " + "{blue}--upgrade{reset}`", + "rpm": "Run `{yellow}yum{reset} update dvc`", + "brew": "Run `{yellow}brew{reset} upgrade dvc`", "deb": ( - "Run {yellow}apt-get{reset} install" - " {blue}--only-upgrade{reset} dvc" + "Run `{yellow}apt-get{reset} install" + " {blue}--only-upgrade{reset} dvc`" ), "binary": ( "To upgrade follow these steps:\n" @@ -137,12 +138,11 @@ class Updater(object): # pragma: no cover "2. Go to {blue}https://dvc.org{reset}\n" "3. Download and install new binary" ), - "conda": "Run {yellow}conda{reset} update dvc", - "choco": "Run {yellow}choco{reset} upgrade dvc", + "conda": "Run `{yellow}conda{reset} update dvc`", + "choco": "Run `{yellow}choco{reset} upgrade dvc`", None: ( - "Find the latest release at\n{blue}" - "https://github.com/iterative/dvc/releases/latest" - "{reset}" + "Find the latest release at\n" + "{blue}https://github.com/iterative/dvc/releases/latest{reset}" ), } diff --git a/dvc/utils/__init__.py b/dvc/utils/__init__.py index a1d15aa0b..92fd34a8a 100644 --- a/dvc/utils/__init__.py +++ b/dvc/utils/__init__.py @@ -50,7 +50,9 @@ def file_md5(fname): no_progress_bar = True if size >= LARGE_FILE_SIZE: no_progress_bar = False - msg = "Computing md5 for a large file {}. This is only done once." + msg = ( + "Computing md5 for a large file '{}'. This is only done once." + ) logger.info(msg.format(relpath(fname))) name = relpath(fname) diff --git a/scripts/build_posix.sh b/scripts/build_posix.sh index da89bd757..cfe1bf611 100755 --- a/scripts/build_posix.sh +++ b/scripts/build_posix.sh @@ -87,6 +87,9 @@ install_dependencies() fi gem install --no-document fpm + + # https://github.com/iterative/dvc/issues/2970 + gem uninstall -i /Users/travis/.rvm/gems/ruby-2.4.3@global rubygems-bundler print_info "Upgrading pip..." pip install --upgrade pip diff --git a/scripts/ci/before_install.sh b/scripts/ci/before_install.sh index bf5c9fa78..2240f9cd0 100644 --- a/scripts/ci/before_install.sh +++ b/scripts/ci/before_install.sh @@ -45,10 +45,10 @@ fi if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then if [[ -n "$PY2" ]]; then - choco install python2 + $scriptdir/retry.sh choco install python2 echo 'PATH="/c/Python27:/c/Python27/Scripts:$PATH"' >> env.sh else - choco install python --version 3.7.5 + $scriptdir/retry.sh choco install python --version 3.7.5 echo 'PATH="/c/Python37:/c/Python37/Scripts:$PATH"' >> env.sh fi elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 1d99ed4c1..3c3b21519 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -92,7 +92,7 @@ class TestAddCmdDirectoryRecursive(TestDvc): "You are adding a large directory 'large-dir' recursively," " consider tracking it as a whole instead.\n" "{purple}HINT:{nc} Remove the generated DVC-file and then" - " run {cyan}dvc add large-dir{nc}".format( + " run `{cyan}dvc add large-dir{nc}`".format( purple=colorama.Fore.MAGENTA, cyan=colorama.Fore.CYAN, nc=colorama.Style.RESET_ALL, diff --git a/tests/func/test_destroy.py b/tests/func/test_destroy.py deleted file mode 100644 index 3535c9d7a..000000000 --- a/tests/func/test_destroy.py +++ /dev/null @@ -1,28 +0,0 @@ -import os - -from dvc.system import System - - -def test_destroy(repo_dir, dvc_repo): - # NOTE: using symlink to ensure that data was unprotected after `destroy` - dvc_repo.config.set("cache", "type", "symlink") - - foo_stage, = dvc_repo.add(repo_dir.FOO) - data_dir_stage, = dvc_repo.add(repo_dir.DATA_DIR) - - dvc_repo.destroy() - - assert not os.path.exists(dvc_repo.dvc_dir) - assert not os.path.exists(foo_stage.path) - assert not os.path.exists(data_dir_stage.path) - - assert os.path.isfile(repo_dir.FOO) - assert os.path.isdir(repo_dir.DATA_DIR) - assert os.path.isfile(repo_dir.DATA) - assert os.path.isdir(repo_dir.DATA_SUB_DIR) - assert os.path.isfile(repo_dir.DATA_SUB) - - assert not System.is_symlink(repo_dir.FOO) - assert not System.is_symlink(repo_dir.DATA_DIR) - assert not System.is_symlink(repo_dir.DATA) - assert not System.is_symlink(repo_dir.DATA_SUB) diff --git a/tests/func/test_repo.py b/tests/func/test_repo.py index 2bf2340b0..bb278d273 100644 --- a/tests/func/test_repo.py +++ b/tests/func/test_repo.py @@ -1,71 +1,73 @@ -from dvc.ignore import DvcIgnore -from dvc.main import main +import os + +from dvc.scm.git.tree import GitTree +from dvc.cache import Cache from dvc.repo import Repo -from dvc.scm.git import GitTree -from dvc.scm.tree import WorkingTree -from dvc.stage import Stage -from tests.basic_env import TestDvcGit +from dvc.system import System +from dvc.utils.compat import fspath -class TestCollect(TestDvcGit): - def setUp(self): - super(TestCollect, self).setUp() - self.dvc.add(self.FOO) - self.dvc.run( - deps=[self.FOO], - outs=[self.BAR], - cmd="python code.py {} {}".format(self.FOO, self.BAR), +def test_destroy(tmp_dir, dvc): + dvc.config.set("cache", "type", "symlink") + dvc.cache = Cache(dvc) + + tmp_dir.dvc_gen("file", "text") + tmp_dir.dvc_gen({"dir": {"file": "lorem", "subdir/file": "ipsum"}}) + + dvc.destroy() + + # Remove all the files related to DVC + for path in [".dvc", "file.dvc", "dir.dvc"]: + assert not (tmp_dir / path).exists() + + # Leave the rest of the files + for path in ["file", "dir/file", "dir/subdir/file"]: + assert (tmp_dir / path).is_file() + + # Make sure that data was unprotected after `destroy` + for path in ["file", "dir", "dir/file", "dir/subdir", "dir/subdir/file"]: + assert not System.is_symlink(fspath(tmp_dir / path)) + + +def test_collect(tmp_dir, scm, dvc, run_copy): + def collect_outs(*args, **kwargs): + return set( + str(out.path_info) + for stage in dvc.collect(*args, **kwargs) + for out in stage.outs ) - self.dvc.scm.add([".gitignore", self.FOO + ".dvc", self.BAR + ".dvc"]) - self.dvc.scm.commit("foo.dvc and bar.dvc") - self.dvc.scm.checkout("new_branch", True) - self.dvc.run( - deps=[self.BAR], - outs=["buzz"], - cmd="python code.py {} {}".format(self.BAR, "buzz"), - ) - self.dvc.scm.add([".gitignore", "buzz.dvc"]) - self.dvc.scm.commit("add buzz") - self.dvc.scm.checkout("master") - def _check(self, branch, target, with_deps, expected): - if branch: - self.dvc.tree = GitTree(self.dvc.scm.repo, branch) - else: - self.dvc.tree = WorkingTree() - result = self.dvc.collect(target + ".dvc", with_deps=with_deps) - self.assertEqual([[str(j) for j in i.outs] for i in result], expected) - return result + tmp_dir.dvc_gen("foo", "foo") + run_copy("foo", "bar") + scm.add([".gitignore", "foo.dvc", "bar.dvc"]) + scm.commit("Add foo and bar") - def test(self): - self._check("", self.BAR, True, [[self.FOO], [self.BAR]]) - self._check("master", self.BAR, True, [[self.FOO], [self.BAR]]) - self._check( - "new_branch", "buzz", True, [[self.FOO], [self.BAR], ["buzz"]] - ) - result = self._check("new_branch", "buzz", False, [["buzz"]]) - self.assertEqual([str(i) for i in result[0].deps], ["bar"]) + scm.checkout("new-branch", create_new=True) + + run_copy("bar", "buzz") + scm.add([".gitignore", "buzz.dvc"]) + scm.commit("Add buzz") + + assert collect_outs("bar.dvc", with_deps=True) == {"foo", "bar"} + + dvc.tree = GitTree(scm.repo, "new-branch") + + assert collect_outs("buzz.dvc", with_deps=True) == {"foo", "bar", "buzz"} + assert collect_outs("buzz.dvc", with_deps=False) == {"buzz"} -class TestIgnore(TestDvcGit): - def _stage_name(self, file): - return file + Stage.STAGE_FILE_SUFFIX +def test_stages(tmp_dir, dvc): + def stages(): + return set(stage.relpath for stage in Repo(fspath(tmp_dir)).stages) - def test_should_not_gather_stage_files_from_ignored_dir(self): - ret = main(["add", self.FOO, self.BAR, self.DATA, self.DATA_SUB]) - self.assertEqual(0, ret) + tmp_dir.dvc_gen({"file": "a", "dir/file": "b", "dir/subdir/file": "c"}) - stages = self.dvc.stages - self.assertEqual(4, len(stages)) + assert stages() == { + "file.dvc", + os.path.join("dir", "file.dvc"), + os.path.join("dir", "subdir", "file.dvc"), + } - self.create(DvcIgnore.DVCIGNORE_FILE, self.DATA_DIR) + tmp_dir.gen(".dvcignore", "dir") - self.dvc = Repo(self.dvc.root_dir) - stages = self.dvc.stages - self.assertEqual(2, len(stages)) - - stagenames = [s.relpath for s in stages] - self.assertIn(self._stage_name(self.FOO), stagenames) - self.assertIn(self._stage_name(self.BAR), stagenames) - self.assertNotIn(self._stage_name(self.DATA), stagenames) - self.assertNotIn(self._stage_name(self.DATA_SUB), stagenames) + assert stages() == {"file.dvc"} diff --git a/tests/func/test_updater.py b/tests/func/test_updater.py deleted file mode 100644 index 4045c0ca6..000000000 --- a/tests/func/test_updater.py +++ /dev/null @@ -1,52 +0,0 @@ -import os - -import mock -import pytest - -from dvc.updater import Updater - - -@pytest.fixture -def updater(dvc_repo): - return Updater(dvc_repo.dvc_dir) - - -def test_updater(updater): - # NOTE: only test on travis CRON to avoid generating too much logs - travis = os.getenv("TRAVIS") == "true" - if not travis: - return - - cron = os.getenv("TRAVIS_EVENT_TYPE") == "cron" - if not cron: - return - - env = os.environ.copy() - if env.get("CI"): - del env["CI"] - - with mock.patch.dict(os.environ, env): - updater.check() - updater.check() - updater.check() - - -def test_check_version_newer(updater): - updater.latest = "0.20.8" - updater.current = "0.21.0" - - assert not updater._is_outdated() - - -def test_check_version_equal(updater): - updater.latest = "0.20.8" - updater.current = "0.20.8" - - assert not updater._is_outdated() - - -def test_check_version_outdated(updater): - updater.latest = "0.21.0" - updater.current = "0.20.8" - - assert updater._is_outdated() diff --git a/tests/unit/test_repo.py b/tests/unit/test_repo.py index ad7426154..0d7577bd4 100644 --- a/tests/unit/test_repo.py +++ b/tests/unit/test_repo.py @@ -1,13 +1,6 @@ import os -from tests.basic_env import TestDvc - -class TestIsDvcInternal(TestDvc): - def test_return_false_on_non_dvc_file(self): - path = os.path.join("path", "to-non-.dvc", "file") - self.assertFalse(self.dvc.is_dvc_internal(path)) - - def test_return_true_on_dvc_internal_file(self): - path = os.path.join("path", "to", ".dvc", "file") - self.assertTrue(path) +def test_is_dvc_internal(dvc): + assert dvc.is_dvc_internal(os.path.join("path", "to", ".dvc", "file")) + assert not dvc.is_dvc_internal(os.path.join("path", "to-non-.dvc", "file")) diff --git a/tests/unit/test_updater.py b/tests/unit/test_updater.py index c8a0ee2de..f0e350528 100644 --- a/tests/unit/test_updater.py +++ b/tests/unit/test_updater.py @@ -1,40 +1,61 @@ import json import os +import mock +import pytest from dvc import __version__ from dvc.updater import Updater -class MockResponse(object): - def __init__(self, json_data, status_code): - self.json_data = json_data - self.status_code = status_code - - def json(self): - return self.json_data +@pytest.fixture +def updater(dvc): + return Updater(dvc.dvc_dir) -def mocked_requests_get(*args, **kwargs): - class MockResponse: - def __init__(self, json_data, status_code): - self.json_data = json_data - self.status_code = status_code +@mock.patch("requests.get") +def test_fetch(mock_get, updater): + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = {"version": __version__} - def json(self): - return self.json_data - - return MockResponse({"version": __version__}, 200) - - -def test_fetch(dvc_repo, mocker): - updater = Updater(dvc_repo.dvc_dir) assert not os.path.exists(updater.updater_file) - mock_get = mocker.patch("requests.get", side_effect=mocked_requests_get) updater.fetch(detach=False) - mock_get.assert_called_once_with(Updater.URL, timeout=Updater.TIMEOUT_GET) + mock_get.assert_called_once_with(Updater.URL, timeout=Updater.TIMEOUT_GET) assert os.path.isfile(updater.updater_file) + with open(updater.updater_file, "r") as fobj: info = json.load(fobj) + assert info["version"] == __version__ + + +@pytest.mark.parametrize( + "latest, current, result", + [ + ("0.20.8", "0.21.0", False), + ("0.20.8", "0.20.8", False), + ("0.20.8", "0.19.0", True), + ], +) +def test_is_outdated(latest, current, result, updater): + updater.latest = latest + updater.current = current + + assert updater._is_outdated() == result + + +@pytest.mark.skipif( + os.getenv("TRAVIS_EVENT_TYPE") != "cron", + reason="Only run on travis CRON to avoid generating too much logs", +) +@mock.patch("dvc.updater.Updater._check") +def test_check(mock_check, updater, monkeypatch): + monkeypatch.delenv("CI", None) + monkeypatch.setenv("DVC_TEST", False) + + updater.check() + updater.check() + updater.check() + + assert mock_check.call_count == 3