From 366d582cdb2ac64f77c2f07bb99a275fae264355 Mon Sep 17 00:00:00 2001 From: Puneetha Pai Date: Thu, 20 Aug 2020 12:09:33 +0530 Subject: [PATCH 1/3] diff: Add tests to hash formatting function --- dvc/command/diff.py | 11 +++++----- tests/unit/command/test_diff.py | 39 ++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/dvc/command/diff.py b/dvc/command/diff.py index a0f8b3419e..6f6113b108 100644 --- a/dvc/command/diff.py +++ b/dvc/command/diff.py @@ -11,6 +11,12 @@ logger = logging.getLogger(__name__) +def _digest(checksum): + if isinstance(checksum, str): + return checksum[0:8] + return "{}..{}".format(checksum["old"][0:8], checksum["new"][0:8]) + + def _show_md(diff): from dvc.utils.diff import table @@ -55,11 +61,6 @@ def _format(diff): At the bottom, include a summary with the number of files per state. """ - def _digest(checksum): - if isinstance(checksum, str): - return checksum[0:8] - return "{}..{}".format(checksum["old"][0:8], checksum["new"][0:8]) - colors = { "added": colorama.Fore.GREEN, "modified": colorama.Fore.YELLOW, diff --git a/tests/unit/command/test_diff.py b/tests/unit/command/test_diff.py index 529cbe37ad..9401e3a51c 100644 --- a/tests/unit/command/test_diff.py +++ b/tests/unit/command/test_diff.py @@ -2,8 +2,22 @@ import logging import os +import pytest + from dvc.cli import parse_args -from dvc.command.diff import _show_md +from dvc.command.diff import _digest, _show_md + + +@pytest.mark.parametrize( + "checksum, expected", + [ + ("wxyz1234pq", "wxyz1234"), + (dict(old="1234567890", new="0987654321"), "12345678..09876543"), + ], + ids=["str", "dict"], +) +def test_digest(checksum, expected): + assert expected == _digest(checksum) def test_default(mocker, caplog): @@ -118,6 +132,29 @@ def test_show_md_empty(): def test_show_md(): + diff = { + "deleted": [ + {"path": "zoo"}, + {"path": os.path.join("data", "")}, + {"path": os.path.join("data", "foo")}, + {"path": os.path.join("data", "bar")}, + ], + "modified": [{"path": "file"}], + "added": [{"path": "file"}], + } + assert _show_md(diff) == ( + "| Status | Path |\n" + "|----------|----------|\n" + "| added | file |\n" + "| deleted | data{sep} |\n" + "| deleted | data{sep}bar |\n" + "| deleted | data{sep}foo |\n" + "| deleted | zoo |\n" + "| modified | file |\n" + ).format(sep=os.path.sep) + + +def ignore_test_show_md_with_hash(): diff = { "deleted": [ {"path": "zoo", "hash": "22222"}, From 6bb11c58edb58ef7002b882e02e1d1e39e8a629e Mon Sep 17 00:00:00 2001 From: Puneetha Pai Date: Thu, 20 Aug 2020 15:55:40 +0530 Subject: [PATCH 2/3] diff: Add test for show_md with hash --- dvc/command/diff.py | 18 ++++++++++++------ tests/unit/command/test_diff.py | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/dvc/command/diff.py b/dvc/command/diff.py index 6f6113b108..f8cd6927a6 100644 --- a/dvc/command/diff.py +++ b/dvc/command/diff.py @@ -17,19 +17,25 @@ def _digest(checksum): return "{}..{}".format(checksum["old"][0:8], checksum["new"][0:8]) -def _show_md(diff): +def _show_md(diff, show_hash=False): from dvc.utils.diff import table + header = ["Status", "Hash", "Path"] if show_hash else ["Status", "Path"] rows = [] for status in ["added", "deleted", "modified"]: entries = diff.get(status, []) if not entries: continue - paths = sorted(entry["path"] for entry in entries) - for path in paths: - rows.append([status, path]) - - return table(["Status", "Path"], rows, True) + sorted_entries = sorted(entries, key=lambda entry: entry["path"]) + for entry in sorted_entries: + path = entry["path"] + if show_hash: + check_sum = _digest(entry.get("hash", "")) + rows.append([status, check_sum, path]) + else: + rows.append([status, path]) + + return table(header, rows, True) class CmdDiff(CmdBase): diff --git a/tests/unit/command/test_diff.py b/tests/unit/command/test_diff.py index 9401e3a51c..a4c8e55a7c 100644 --- a/tests/unit/command/test_diff.py +++ b/tests/unit/command/test_diff.py @@ -154,7 +154,7 @@ def test_show_md(): ).format(sep=os.path.sep) -def ignore_test_show_md_with_hash(): +def test_show_md_with_hash(): diff = { "deleted": [ {"path": "zoo", "hash": "22222"}, @@ -167,13 +167,13 @@ def ignore_test_show_md_with_hash(): ], "added": [{"path": "file", "hash": "00000000"}], } - assert _show_md(diff) == ( - "| Status | Path |\n" - "|----------|----------|\n" - "| added | file |\n" - "| deleted | data{sep} |\n" - "| deleted | data{sep}bar |\n" - "| deleted | data{sep}foo |\n" - "| deleted | zoo |\n" - "| modified | file |\n" + assert _show_md(diff, show_hash=True) == ( + "| Status | Hash | Path |\n" + "|----------|--------------------|----------|\n" + "| added | 00000000 | file |\n" + "| deleted | XXXXXXXX | data{sep} |\n" + "| deleted | 00000000 | data{sep}bar |\n" + "| deleted | 11111111 | data{sep}foo |\n" + "| deleted | 22222 | zoo |\n" + "| modified | AAAAAAAA..BBBBBBBB | file |\n" ).format(sep=os.path.sep) From 656318d19cb0265fa5934d1a4975210d161f6916 Mon Sep 17 00:00:00 2001 From: Puneetha Pai Date: Thu, 20 Aug 2020 21:08:20 +0530 Subject: [PATCH 3/3] diff: Add stub test for _show_md from command --- dvc/command/diff.py | 5 +++-- tests/unit/command/test_diff.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/dvc/command/diff.py b/dvc/command/diff.py index f8cd6927a6..380a67e551 100644 --- a/dvc/command/diff.py +++ b/dvc/command/diff.py @@ -118,7 +118,8 @@ def run(self): try: diff = self.repo.diff(self.args.a_rev, self.args.b_rev) - if not self.args.show_hash: + show_hash = self.args.show_hash + if not show_hash: for _, entries in diff.items(): for entry in entries: del entry["hash"] @@ -126,7 +127,7 @@ def run(self): if self.args.show_json: logger.info(json.dumps(diff)) elif self.args.show_md: - logger.info(_show_md(diff)) + logger.info(_show_md(diff, show_hash)) elif diff: logger.info(self._format(diff)) diff --git a/tests/unit/command/test_diff.py b/tests/unit/command/test_diff.py index a4c8e55a7c..e1f050a452 100644 --- a/tests/unit/command/test_diff.py +++ b/tests/unit/command/test_diff.py @@ -2,6 +2,7 @@ import logging import os +import mock import pytest from dvc.cli import parse_args @@ -104,6 +105,21 @@ def test_show_json_and_hash(mocker, caplog): assert '"modified": []' in caplog.text +@pytest.mark.parametrize("show_hash", [None, True, False]) +@mock.patch("dvc.command.diff._show_md") +def test_diff_show_md_and_hash(mock_show_md, mocker, caplog, show_hash): + options = ["diff", "--show-md"] + (["--show-hash"] if show_hash else []) + args = parse_args(options) + cmd = args.func(args) + + diff = {} + show_hash = show_hash if show_hash else False + mocker.patch("dvc.repo.Repo.diff", return_value=diff) + + assert 0 == cmd.run() + mock_show_md.assert_called_once_with(diff, show_hash) + + def test_no_changes(mocker, caplog): args = parse_args(["diff", "--show-json"]) cmd = args.func(args)