Skip to content

Commit

Permalink
dvc: fix --quiet mode
Browse files Browse the repository at this point in the history
Part of the issue is a bug from iterative#3806

Kudos to @karajan1001 for finding this bug while working on iterative#4282
  • Loading branch information
efiop committed Jul 30, 2020
1 parent 2f48bb4 commit a02c191
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
40 changes: 21 additions & 19 deletions dvc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
logger = logging.getLogger("dvc")


def main(argv=None):
def main(argv=None): # noqa: C901
"""Run dvc CLI command.
Args:
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
38 changes: 22 additions & 16 deletions tests/func/test_status.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit a02c191

Please sign in to comment.