From a4b2d38af3e78896495a7be4635a892ba86bf7a9 Mon Sep 17 00:00:00 2001 From: dberenbaum Date: Fri, 18 Feb 2022 10:44:09 -0500 Subject: [PATCH] diff: fix missing key bug (#6720) --- dvc/commands/diff.py | 2 +- tests/unit/command/test_diff.py | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/dvc/commands/diff.py b/dvc/commands/diff.py index 939ee11898..98ff9c2b30 100644 --- a/dvc/commands/diff.py +++ b/dvc/commands/diff.py @@ -128,7 +128,7 @@ def run(self): show_hash = self.args.show_hash hide_missing = self.args.b_rev or self.args.hide_missing if hide_missing: - del diff["not in cache"] + diff.pop("not in cache", None) for key, entries in diff.items(): entries = sorted( diff --git a/tests/unit/command/test_diff.py b/tests/unit/command/test_diff.py index fc389073cb..c1c44c0c1d 100644 --- a/tests/unit/command/test_diff.py +++ b/tests/unit/command/test_diff.py @@ -204,20 +204,31 @@ def test_diff_show_markdown_and_hash(mocker, show_hash): mock_show_markdown.assert_called_once_with(diff, show_hash, False) -def test_no_changes(mocker, capsys): - args = parse_args(["diff", "--json"]) +@pytest.mark.parametrize( + "opts", + ( + [], + ["a_rev", "b_rev"], + ["--targets", "."], + ["--hide-missing"], + ), +) +@pytest.mark.parametrize( + "show, expected", + ( + ([], ""), + (["--json"], "{}"), + (["--md"], "| Status | Path |\n|----------|--------|"), + ), +) +def test_no_changes(mocker, capsys, opts, show, expected): + args = parse_args(["diff", *opts, *show]) cmd = args.func(args) mocker.patch("dvc.repo.Repo.diff", return_value={}) assert 0 == cmd.run() out, _ = capsys.readouterr() - assert "{}" in out - - args = parse_args(["diff"]) - cmd = args.func(args) - assert 0 == cmd.run() - out, _ = capsys.readouterr() - assert not out + assert expected == out.strip() def test_show_markdown(capsys):