Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dvc/command/data_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 10 additions & 5 deletions dvc/command/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were printing this even in quiet mode before smh...


Expand Down
35 changes: 35 additions & 0 deletions tests/unit/command/test_status.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import json

import pytest

from dvc.cli import parse_args
from dvc.command.status import CmdDataStatus

Expand Down Expand Up @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just paranoid about us finally migrating to using print in CLI and this test becoming false-positive.

assert not captured.err
assert not captured.out