diff: add CLI output

This commit is contained in:
Mr. Outis
2020-01-26 15:12:30 -06:00
parent a4ade13c0a
commit bd1adf2c23
3 changed files with 99 additions and 10 deletions
+46
View File
@@ -2,6 +2,8 @@ import argparse
import json
import logging
import colorama
from dvc.command.base import CmdBase, append_doc_link
from dvc.exceptions import DvcException
@@ -10,6 +12,48 @@ logger = logging.getLogger(__name__)
class CmdDiff(CmdBase):
@staticmethod
def _format(diff):
"""
Given a diff structure, generate a string of filenames separated
by new lines and grouped together by their state.
A group's header is colored and its entries are sorted to enhance
readability, for example:
Added:
another_file.txt
backup.tar
dir/
dir/1
If a group has no entries, it won't be included in the result.
"""
colors = {
"added": colorama.Fore.GREEN,
"modified": colorama.Fore.YELLOW,
"deleted": colorama.Fore.RED,
}
groups = []
for key, values in diff.items():
if not values:
continue
entries = sorted(" " + entry["filename"] for entry in values)
groups.append(
"{color}{header}{nc}:\n{entries}".format(
color=colors[key],
header=key.capitalize(),
nc=colorama.Fore.RESET,
entries="\n".join(entries),
)
)
return "\n\n".join(groups)
def run(self):
try:
diff = self.repo.diff(
@@ -22,6 +66,8 @@ class CmdDiff(CmdBase):
print(json.dumps(diff))
return 0
print(self._format(diff))
except DvcException:
logger.exception("failed to get 'diff {}'")
return 1
+2 -2
View File
@@ -10,7 +10,7 @@ Diffable.__eq__ = lambda self, other: self.filename == other.filename
Diffable.__hash__ = lambda self: hash(self.filename)
def diffables_from_output(output):
def _diffables_from_output(output):
"""
Transform an output into a list of Diffable objects so we can
compare them lately.
@@ -61,7 +61,7 @@ def diff(self, a_ref="HEAD", b_ref=None, *, target=None):
diffable
for stage in self.stages
for out in stage.outs
for diffable in diffables_from_output(out)
for diffable in _diffables_from_output(out)
if not target or target == str(out)
)
+51 -8
View File
@@ -3,6 +3,8 @@ import json
import os
import pytest
import colorama
from dvc.main import main
@@ -176,17 +178,16 @@ def test_directories(tmp_dir, scm, dvc):
}
def test_cli(tmp_dir, scm, dvc):
tmp_dir.dvc_gen("file", "text")
# main(["diff"])
pytest.skip("TODO: define output structure")
def test_json(tmp_dir, scm, dvc, capsys):
def test_json(tmp_dir, scm, dvc, mocker, capsys):
assert 0 == main(["diff", "--json"])
assert not capsys.readouterr().out
tmp_dir.dvc_gen("file", "text")
diff = {
"added": [{"filename": "file", "checksum": digest("text")}],
"deleted": [],
"modified": [],
}
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == main(["diff", "--json"])
assert (
json.dumps(
@@ -198,3 +199,45 @@ def test_json(tmp_dir, scm, dvc, capsys):
)
in capsys.readouterr().out
)
def test_cli(tmp_dir, scm, dvc, mocker, capsys):
assert 0 == main(["diff"])
assert not capsys.readouterr().out
diff = {
"added": [{"filename": "file", "checksum": digest("text")}],
"deleted": [],
"modified": [],
}
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == main(["diff"])
assert (
"{green}Added{nc}:\n"
" file\n".format(green=colorama.Fore.GREEN, nc=colorama.Fore.RESET)
== capsys.readouterr().out
)
diff = {
"added": [],
"deleted": [{"filename": "file", "checksum": digest("text")}],
"modified": [
{"filename": "foo", "checksum": digest("foo")},
{"filename": "bar", "checksum": digest("bar")},
],
}
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == main(["diff"])
assert (
"{red}Deleted{nc}:\n"
" file\n"
"\n"
"{yellow}Modified{nc}:\n"
" bar\n"
" foo\n".format(
red=colorama.Fore.RED,
yellow=colorama.Fore.YELLOW,
nc=colorama.Fore.RESET,
)
== capsys.readouterr().out
)