mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-12 15:28:21 +01:00
diff: use ui.table for --show-md (#5887)
This commit is contained in:
+3
-3
@@ -18,9 +18,9 @@ def _digest(checksum):
|
||||
|
||||
|
||||
def _show_md(diff, show_hash=False, hide_missing=False):
|
||||
from dvc.utils.diff import table
|
||||
from dvc.ui import ui
|
||||
|
||||
header = ["Status", "Hash", "Path"] if show_hash else ["Status", "Path"]
|
||||
headers = ["Status", "Hash", "Path"] if show_hash else ["Status", "Path"]
|
||||
rows = []
|
||||
statuses = ["added", "deleted", "renamed", "modified"]
|
||||
if not hide_missing:
|
||||
@@ -39,7 +39,7 @@ def _show_md(diff, show_hash=False, hide_missing=False):
|
||||
else:
|
||||
rows.append([status, path])
|
||||
|
||||
return table(header, rows, True)
|
||||
ui.table(rows, headers=headers, markdown=True)
|
||||
|
||||
|
||||
class CmdDiff(CmdBase):
|
||||
|
||||
@@ -85,28 +85,6 @@ def diff(old, new, with_unchanged=False):
|
||||
return dict(res)
|
||||
|
||||
|
||||
def table(header, rows, markdown=False):
|
||||
from tabulate import tabulate
|
||||
|
||||
if not rows and not markdown:
|
||||
return ""
|
||||
|
||||
ret = tabulate(
|
||||
rows,
|
||||
header,
|
||||
tablefmt="github" if markdown else "plain",
|
||||
disable_numparse=True,
|
||||
# None will be shown as "" by default, overriding
|
||||
missingval="—",
|
||||
)
|
||||
|
||||
if markdown:
|
||||
# NOTE: md table is incomplete without the trailing newline
|
||||
ret += "\n"
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def format_dict(d):
|
||||
ret = {}
|
||||
for key, val in d.items():
|
||||
|
||||
@@ -229,11 +229,7 @@ def test_no_changes(mocker, caplog):
|
||||
assert not info()
|
||||
|
||||
|
||||
def test_show_md_empty():
|
||||
assert _show_md({}) == ("| Status | Path |\n|----------|--------|\n")
|
||||
|
||||
|
||||
def test_show_md():
|
||||
def test_show_md(capsys):
|
||||
diff = {
|
||||
"deleted": [
|
||||
{"path": "zoo"},
|
||||
@@ -246,7 +242,10 @@ def test_show_md():
|
||||
"renamed": [{"path": {"old": "file_old", "new": "file_new"}}],
|
||||
"not in cache": [{"path": "file2"}],
|
||||
}
|
||||
assert _show_md(diff) == (
|
||||
|
||||
_show_md(diff)
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == (
|
||||
"| Status | Path |\n"
|
||||
"|--------------|----------------------|\n"
|
||||
"| added | file |\n"
|
||||
@@ -257,10 +256,11 @@ def test_show_md():
|
||||
"| renamed | file_old -> file_new |\n"
|
||||
"| modified | file |\n"
|
||||
"| not in cache | file2 |\n"
|
||||
"\n"
|
||||
).format(sep=os.path.sep)
|
||||
|
||||
|
||||
def test_show_md_with_hash():
|
||||
def test_show_md_with_hash(capsys):
|
||||
diff = {
|
||||
"deleted": [
|
||||
{"path": "zoo", "hash": "22222"},
|
||||
@@ -280,7 +280,11 @@ def test_show_md_with_hash():
|
||||
],
|
||||
"not in cache": [{"path": "file2", "hash": "12345678"}],
|
||||
}
|
||||
assert _show_md(diff, show_hash=True) == (
|
||||
|
||||
_show_md(diff, show_hash=True)
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == (
|
||||
"| Status | Hash | Path |\n"
|
||||
"|--------------|--------------------|----------------------|\n"
|
||||
"| added | 00000000 | file |\n"
|
||||
@@ -291,10 +295,11 @@ def test_show_md_with_hash():
|
||||
"| renamed | 11111111 | file_old -> file_new |\n"
|
||||
"| modified | AAAAAAAA..BBBBBBBB | file |\n"
|
||||
"| not in cache | 12345678 | file2 |\n"
|
||||
"\n"
|
||||
).format(sep=os.path.sep)
|
||||
|
||||
|
||||
def test_show_md_hide_missing():
|
||||
def test_show_md_hide_missing(capsys):
|
||||
diff = {
|
||||
"deleted": [
|
||||
{"path": "zoo"},
|
||||
@@ -307,7 +312,11 @@ def test_show_md_hide_missing():
|
||||
"renamed": [{"path": {"old": "file_old", "new": "file_new"}}],
|
||||
"not in cache": [{"path": "file2"}],
|
||||
}
|
||||
assert _show_md(diff, hide_missing=True) == (
|
||||
|
||||
_show_md(diff, hide_missing=True)
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert out == (
|
||||
"| Status | Path |\n"
|
||||
"|----------|----------------------|\n"
|
||||
"| added | file |\n"
|
||||
@@ -317,6 +326,7 @@ def test_show_md_hide_missing():
|
||||
"| deleted | data{sep}bar |\n"
|
||||
"| renamed | file_old -> file_new |\n"
|
||||
"| modified | file |\n"
|
||||
"\n"
|
||||
).format(sep=os.path.sep)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user