From a02c19102e0a95d2e27c9bc3a5c6f9ab0ed94956 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 30 Jul 2020 02:50:22 +0300 Subject: [PATCH] dvc: fix --quiet mode Part of the issue is a bug from #3806 Kudos to @karajan1001 for finding this bug while working on #4282 --- dvc/main.py | 40 ++++++++++++++++++++------------------- tests/func/test_status.py | 38 +++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/dvc/main.py b/dvc/main.py index dc737aa5f5..fc1450e536 100644 --- a/dvc/main.py +++ b/dvc/main.py @@ -21,7 +21,7 @@ logger = logging.getLogger("dvc") -def main(argv=None): +def main(argv=None): # noqa: C901 """Run dvc CLI command. Args: @@ -37,16 +37,17 @@ def main(argv=None): try: args = parse_args(argv) - verbosity = args.verbose - args.quiet - if verbosity: - logger.setLevel( - { - -2: logging.CRITICAL, - -1: logging.ERROR, - 1: logging.DEBUG, - 2: logging.TRACE, - }[max(-2, min(verbosity, 2))] - ) + level = None + if args.quiet: + level = logging.CRITICAL + elif args.verbose == 1: + level = logging.DEBUG + elif args.verbose > 1: + level = logging.TRACE + + if level is not None: + logger.setLevel(level) + logger.trace(args) cmd = args.func(args) @@ -81,6 +82,15 @@ def main(argv=None): except Exception: # noqa, pylint: disable=broad-except logger.exception("unexpected error") ret = 255 + + try: + if ret != 0: + logger.info(FOOTER) + + if analytics.is_enabled(): + analytics.collect_and_send_report(args, ret) + + return ret finally: logger.setLevel(outerLogLevel) @@ -92,11 +102,3 @@ def main(argv=None): # Remove cached repos in the end of the call, these are anonymous # so won't be reused by any other subsequent run anyway. clean_repos() - - if ret != 0: - logger.info(FOOTER) - - if analytics.is_enabled(): - analytics.collect_and_send_report(args, ret) - - return ret diff --git a/tests/func/test_status.py b/tests/func/test_status.py index c79c361ada..557f96719c 100644 --- a/tests/func/test_status.py +++ b/tests/func/test_status.py @@ -1,28 +1,34 @@ import os -from mock import patch - from dvc.main import main -from tests.basic_env import TestDvc -class TestStatus(TestDvc): - def test_quiet(self): - self.dvc.add(self.FOO) +def test_quiet(tmp_dir, dvc, capsys): + tmp_dir.dvc_gen("foo", "foo") + + # clear + capsys.readouterr() + + assert main(["status", "--quiet"]) == 0 + out_err = capsys.readouterr() + assert not out_err.out + assert not out_err.err + + tmp_dir.gen("foo", "barr") - ret = main(["status", "--quiet"]) - self.assertEqual(ret, 0) + assert main(["status", "--quiet"]) == 1 + out_err = capsys.readouterr() + assert not out_err.out + assert not out_err.err - os.remove(self.FOO) - os.rename(self.BAR, self.FOO) - ret = main(["status", "--quiet"]) - self.assertEqual(ret, 1) +def test_implied_cloud(dvc, mocker): + mock_status = mocker.patch( + "dvc.repo.status._cloud_status", return_value=True + ) - @patch("dvc.repo.status._cloud_status", return_value=True) - def test_implied_cloud(self, mock_status): - main(["status", "--remote", "something"]) - mock_status.assert_called() + main(["status", "--remote", "something"]) + assert mock_status.called def test_status_non_dvc_repo_import(tmp_dir, dvc, git_dir):