From 047bcd6ffb085ce2f45a1c44e7879aa8a17431ee Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Tue, 9 Jun 2020 23:47:51 +0300 Subject: [PATCH] status: introduce --show-json Fixes #1406 --- dvc/command/data_sync.py | 6 ++++++ dvc/command/status.py | 15 ++++++++----- tests/unit/command/test_status.py | 35 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/dvc/command/data_sync.py b/dvc/command/data_sync.py index baee9343ae..bf384befa3 100644 --- a/dvc/command/data_sync.py +++ b/dvc/command/data_sync.py @@ -363,5 +363,11 @@ def add_parser(subparsers, _parent_parser): default=False, help="Show status of all stages in the specified directory.", ) + status_parser.add_argument( + "--show-json", + action="store_true", + default=False, + help="Show status in JSON format.", + ) status_parser.set_defaults(func=CmdDataStatus) diff --git a/dvc/command/status.py b/dvc/command/status.py index 08d921bd66..97e51513f3 100644 --- a/dvc/command/status.py +++ b/dvc/command/status.py @@ -50,11 +50,16 @@ def run(self): with_deps=self.args.with_deps, recursive=self.args.recursive, ) - if st: - if self.args.quiet: - return 1 - else: - self._show(st, indent) + + if self.args.quiet: + return bool(st) + + if self.args.show_json: + import json + + logger.info(json.dumps(st)) + elif st: + self._show(st, indent) else: logger.info(self.UP_TO_DATE_MSG) diff --git a/tests/unit/command/test_status.py b/tests/unit/command/test_status.py index 6ac7c6970d..f2e1c57460 100644 --- a/tests/unit/command/test_status.py +++ b/tests/unit/command/test_status.py @@ -1,3 +1,7 @@ +import json + +import pytest + from dvc.cli import parse_args from dvc.command.status import CmdDataStatus @@ -38,3 +42,34 @@ def test_cloud_status(mocker): with_deps=True, recursive=True, ) + + +@pytest.mark.parametrize("status", [{}, {"a": "b", "c": [1, 2, 3]}, [1, 2, 3]]) +def test_status_show_json(mocker, caplog, status): + cli_args = parse_args(["status", "--show-json"]) + assert cli_args.func == CmdDataStatus + + cmd = cli_args.func(cli_args) + + mocker.patch.object(cmd.repo, "status", autospec=True, return_value=status) + caplog.clear() + assert cmd.run() == 0 + assert caplog.messages == [json.dumps(status)] + + +@pytest.mark.parametrize( + "status, ret", [({}, 0), ({"a": "b", "c": [1, 2, 3]}, 1), ([1, 2, 3], 1)] +) +def test_status_quiet(mocker, caplog, capsys, status, ret): + cli_args = parse_args(["status", "-q"]) + assert cli_args.func == CmdDataStatus + + cmd = cli_args.func(cli_args) + + mocker.patch.object(cmd.repo, "status", autospec=True, return_value=status) + caplog.clear() + assert cmd.run() == ret + assert not caplog.messages + captured = capsys.readouterr() + assert not captured.err + assert not captured.out