From 4c5cb7afaed9413a39e15664a729c6dffafb0c18 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 29 Mar 2019 12:56:37 +0200 Subject: [PATCH] checkout: reorganize "`git checkout` hint" exception Signed-off-by: Ruslan Kuprieiev --- dvc/exceptions.py | 7 +++++++ dvc/repo/checkout.py | 6 ++---- tests/test_checkout.py | 29 ++++++++--------------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/dvc/exceptions.py b/dvc/exceptions.py index 57410a892..92bdb9169 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -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 + ) diff --git a/dvc/repo/checkout.py b/dvc/repo/checkout.py index 883f21c4c..d5365a893 100644 --- a/dvc/repo/checkout.py +++ b/dvc/repo/checkout.py @@ -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) diff --git a/tests/test_checkout.py b/tests/test_checkout.py index a07d24bc6..852223da1 100644 --- a/tests/test_checkout.py +++ b/tests/test_checkout.py @@ -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):