checkout: reorganize "git checkout hint" exception

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2019-03-29 13:44:32 +02:00
parent c03b1c1693
commit 4c5cb7afae
3 changed files with 17 additions and 25 deletions
+7
View File
@@ -248,3 +248,10 @@ class TargetNotDirectoryError(DvcException):
super(TargetNotDirectoryError, self).__init__(
"Target: {} is not a directory".format(path)
)
class CheckoutErrorSuggestGit(DvcException):
def __init__(self, target, cause):
super(CheckoutErrorSuggestGit, self).__init__(
"Did you mean 'git checkout {}'?".format(target), cause=cause
)
+2 -4
View File
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
import dvc.logger as logger
from dvc.exceptions import DvcException
from dvc.exceptions import CheckoutErrorSuggestGit
from dvc.progress import ProgressCallback
@@ -34,9 +34,7 @@ def checkout(self, target=None, with_deps=False, force=False, recursive=False):
except (StageFileDoesNotExistError, StageFileBadNameError) as exc:
if not target:
raise
raise DvcException(
str(exc) + " Did you mean 'git checkout {}'?".format(target)
)
raise CheckoutErrorSuggestGit(target, exc)
with self.state:
_cleanup_unused_links(self, all_stages)
+8 -21
View File
@@ -15,12 +15,13 @@ from dvc.system import System
from dvc.utils import walk_files
from tests.basic_env import TestDvc
from tests.test_repro import TestRepro
from dvc.stage import Stage
from dvc.stage import Stage, StageFileBadNameError, StageFileDoesNotExistError
from dvc.remote.local import RemoteLOCAL
from dvc.exceptions import (
DvcException,
ConfirmRemoveError,
TargetNotDirectoryError,
CheckoutErrorSuggestGit,
)
from mock import patch
@@ -385,30 +386,16 @@ class TestCheckoutSuggestGit(TestRepro):
try:
self.dvc.checkout(target="gitbranch")
except DvcException as exc:
msg = str(exc)
self.assertEqual(
msg,
(
"'gitbranch' does not exist. "
"Did you mean 'git checkout gitbranch'?"
),
)
self.assertIsInstance(exc, CheckoutErrorSuggestGit)
self.assertIsInstance(exc.cause, StageFileDoesNotExistError)
self.assertIsNone(exc.cause.cause)
try:
self.dvc.checkout(target=self.FOO)
except DvcException as exc:
msg = str(exc)
self.assertEqual(
msg,
(
"bad stage filename '{}'."
" Stage files should be named 'Dvcfile'"
" or have a '.dvc' suffix (e.g. '{}.dvc')."
" Did you mean 'git checkout {}'?".format(
self.FOO, self.FOO, self.FOO
)
),
)
self.assertIsInstance(exc, CheckoutErrorSuggestGit)
self.assertIsInstance(exc.cause, StageFileBadNameError)
self.assertIsNone(exc.cause.cause)
class TestCheckoutShouldHaveSelfClearingProgressBar(TestDvc):