diff --git a/dvc/exceptions.py b/dvc/exceptions.py index bca505c1a..111f26b92 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -325,3 +325,7 @@ class CheckoutError(DvcException): "forget to fetch?".format("\n".join(targets)) ) super(CheckoutError, self).__init__(m) + + +class CollectCacheError(DvcException): + pass diff --git a/dvc/output/base.py b/dvc/output/base.py index 699ceb3d3..f940d3f52 100644 --- a/dvc/output/base.py +++ b/dvc/output/base.py @@ -7,7 +7,7 @@ from schema import Or, Optional from dvc.cache import NamedCache import dvc.prompt as prompt -from dvc.exceptions import DvcException +from dvc.exceptions import DvcException, CollectCacheError from dvc.utils.compat import str, urlparse from dvc.remote.base import RemoteBASE @@ -353,7 +353,7 @@ class OutputBase(object): nc.add(self.scheme, 'd3b07384d113', 'directory/bar') """ - ret = [] + cache = NamedCache() if self.cache.changed_cache_file(self.checksum): try: @@ -373,14 +373,13 @@ class OutputBase(object): "Would you like to continue? Use '-f' to force." ) if not force and not prompt.confirm(msg.format(self.path_info)): - raise DvcException( + raise CollectCacheError( "unable to fully collect used cache" " without cache for directory '{}'".format(self) ) else: - return ret + return cache - cache = NamedCache() for entry in self.dir_cache: checksum = entry[self.remote.PARAM_CHECKSUM] path_info = self.path_info / entry[self.remote.PARAM_RELPATH] diff --git a/tests/func/test_gc.py b/tests/func/test_gc.py index d9a558617..efcf29a7b 100644 --- a/tests/func/test_gc.py +++ b/tests/func/test_gc.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals import os + +import pytest import configobj from git import Repo @@ -7,6 +9,7 @@ from git import Repo from dvc.utils.compat import pathlib from dvc.main import main from dvc.repo import Repo as DvcRepo +from dvc.exceptions import CollectCacheError from tests.basic_env import TestDvcGit from tests.basic_env import TestDir @@ -208,3 +211,20 @@ def test_all_commits(git, dvc_repo): def _count_files(path): return sum(len(files) for _, _, files in os.walk(path)) + + +def test_gc_no_dir_cache(repo_dir, dvc_repo): + dvc_repo.add(repo_dir.FOO) + dvc_repo.add(repo_dir.BAR) + dir_stage, = dvc_repo.add(repo_dir.DATA_DIR) + + os.unlink(dir_stage.outs[0].cache_path) + + with pytest.raises(CollectCacheError): + dvc_repo.gc() + + assert _count_files(dvc_repo.cache.local.cache_dir) == 4 + + dvc_repo.gc(force=True) + + assert _count_files(dvc_repo.cache.local.cache_dir) == 2