ui: migrating rest to the ui.write (#6176)

* complete migration to ui.write

* fix small bugs
This commit is contained in:
Saugat Pachhai
2021-06-21 22:14:59 +03:00
committed by GitHub
parent f2998630a3
commit 476af100de
29 changed files with 231 additions and 230 deletions
+36 -41
View File
@@ -1,5 +1,4 @@
import collections
import logging
import os
import mock
@@ -21,7 +20,7 @@ def test_digest(checksum, expected):
assert expected == _digest(checksum)
def test_default(mocker, caplog):
def test_default(mocker, capsys):
args = parse_args(["diff"])
cmd = args.func(args)
diff = {
@@ -51,10 +50,10 @@ def test_default(mocker, caplog):
"\n"
"files summary: 1 added, 0 deleted, "
"1 renamed, 0 modified, 0 not in cache"
).format(sep=os.path.sep) in caplog.text
).format(sep=os.path.sep) in capsys.readouterr()[0]
def test_show_hash(mocker, caplog):
def test_show_hash(mocker, capsys):
args = parse_args(["diff", "--show-hash"])
cmd = args.func(args)
diff = {
@@ -81,6 +80,8 @@ def test_show_hash(mocker, caplog):
}
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == cmd.run()
out, _ = capsys.readouterr()
assert (
"Deleted:\n"
" XXXXXXXX " + os.path.join("data", "") + "\n"
@@ -100,10 +101,10 @@ def test_show_hash(mocker, caplog):
"\n"
"files summary: 0 added, 2 deleted, "
"1 renamed, 2 modified, 0 not in cache"
) in caplog.text
) in out
def test_show_json(mocker, caplog):
def test_show_json(mocker, capsys):
args = parse_args(["diff", "--show-json"])
cmd = args.func(args)
diff = {
@@ -118,13 +119,14 @@ def test_show_json(mocker, caplog):
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == cmd.run()
assert '"added": [{"path": "file1"}, {"path": "file2"}]' in caplog.text
assert '"deleted": []' in caplog.text
assert '"modified": []' in caplog.text
assert '"not in cache": []' in caplog.text
out, _ = capsys.readouterr()
assert '"added": [{"path": "file1"}, {"path": "file2"}]' in out
assert '"deleted": []' in out
assert '"modified": []' in out
assert '"not in cache": []' in out
def test_show_json_and_hash(mocker, caplog):
def test_show_json_and_hash(mocker, capsys):
args = parse_args(["diff", "--show-json", "--show-hash"])
cmd = args.func(args)
@@ -147,20 +149,21 @@ def test_show_json_and_hash(mocker, caplog):
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == cmd.run()
out, _ = capsys.readouterr()
assert (
'"added": [{"path": "file1", "hash": "11111111"}, '
'{"path": "file2", "hash": "22222222"}]' in caplog.text
'{"path": "file2", "hash": "22222222"}]' in out
)
assert '"deleted": []' in caplog.text
assert '"modified": []' in caplog.text
assert '"deleted": []' in out
assert '"modified": []' in out
assert (
'"renamed": [{"path": {"old": "file_old", '
'"new": "file_new"}, "hash": "11111111"}]' in caplog.text
'"new": "file_new"}, "hash": "11111111"}]' in out
)
assert '"not in cache": []' in caplog.text
assert '"not in cache": []' in out
def test_show_json_hide_missing(mocker, caplog):
def test_show_json_hide_missing(mocker, capsys):
args = parse_args(["diff", "--show-json", "--hide-missing"])
cmd = args.func(args)
diff = {
@@ -181,19 +184,17 @@ def test_show_json_hide_missing(mocker, caplog):
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == cmd.run()
assert '"added": [{"path": "file1"}, {"path": "file2"}]' in caplog.text
assert '"deleted": []' in caplog.text
assert (
'"renamed": [{"path": {"old": "file_old", "new": "file_new"}'
in caplog.text
)
assert '"modified": []' in caplog.text
assert '"not in cache": []' not in caplog.text
out, _ = capsys.readouterr()
assert '"added": [{"path": "file1"}, {"path": "file2"}]' in out
assert '"deleted": []' in out
assert '"renamed": [{"path": {"old": "file_old", "new": "file_new"}' in out
assert '"modified": []' in out
assert '"not in cache": []' not in out
@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):
def test_diff_show_md_and_hash(mock_show_md, mocker, show_hash):
options = ["diff", "--show-md"] + (["--show-hash"] if show_hash else [])
args = parse_args(options)
cmd = args.func(args)
@@ -206,27 +207,20 @@ def test_diff_show_md_and_hash(mock_show_md, mocker, caplog, show_hash):
mock_show_md.assert_called_once_with(diff, show_hash, False)
def test_no_changes(mocker, caplog):
def test_no_changes(mocker, capsys):
args = parse_args(["diff", "--show-json"])
cmd = args.func(args)
mocker.patch("dvc.repo.Repo.diff", return_value={})
def info():
return [
msg
for name, level, msg in caplog.record_tuples
if name.startswith("dvc") and level == logging.INFO
]
assert 0 == cmd.run()
assert ["{}"] == info()
caplog.clear()
out, _ = capsys.readouterr()
assert "{}" in out
args = parse_args(["diff"])
cmd = args.func(args)
assert 0 == cmd.run()
assert not info()
out, _ = capsys.readouterr()
assert not out
def test_show_md(capsys):
@@ -330,7 +324,7 @@ def test_show_md_hide_missing(capsys):
).format(sep=os.path.sep)
def test_hide_missing(mocker, caplog):
def test_hide_missing(mocker, capsys):
args = parse_args(["diff", "--hide-missing"])
cmd = args.func(args)
diff = {
@@ -348,6 +342,7 @@ def test_hide_missing(mocker, caplog):
mocker.patch("dvc.repo.Repo.diff", return_value=diff)
assert 0 == cmd.run()
out, _ = capsys.readouterr()
assert (
"Added:\n"
" file\n"
@@ -356,5 +351,5 @@ def test_hide_missing(mocker, caplog):
" file_old -> file_new\n"
"\n"
"files summary: 1 added, 0 deleted, 1 renamed, 0 modified"
) in caplog.text
assert "not in cache" not in caplog.text
) in out
assert "not in cache" not in out
+3 -2
View File
@@ -28,7 +28,7 @@ def test_get(mocker):
)
def test_get_url(mocker, caplog):
def test_get_url(mocker, capsys):
cli_args = parse_args(
["get", "repo_url", "src", "--rev", "version", "--show-url"]
)
@@ -38,6 +38,7 @@ def test_get_url(mocker, caplog):
m = mocker.patch("dvc.api.get_url", return_value="resource_url")
assert cmd.run() == 0
assert "resource_url" in caplog.text
out, _ = capsys.readouterr()
assert "resource_url" in out
m.assert_called_once_with("src", repo="repo_url", rev="version")
+6 -7
View File
@@ -50,7 +50,7 @@ def test_metrics_diff(dvc, mocker, capsys):
)
def test_metrics_diff_json(dvc, mocker, caplog, capsys):
def test_metrics_diff_json(dvc, mocker, capsys):
cli_args = parse_args(
[
"metrics",
@@ -79,6 +79,7 @@ def test_metrics_diff_json(dvc, mocker, caplog, capsys):
show_diff_mock = mocker.patch("dvc.compare.show_diff")
assert cmd.run() == 0
out, _ = capsys.readouterr()
metrics_diff.assert_called_once_with(
cmd.repo,
targets=["target1", "target2"],
@@ -88,8 +89,7 @@ def test_metrics_diff_json(dvc, mocker, caplog, capsys):
all=True,
)
show_diff_mock.assert_not_called()
assert json.dumps(diff) in caplog.text
assert capsys.readouterr() == ("", "")
assert json.dumps(diff) in out
def test_metrics_show(dvc, mocker):
@@ -134,7 +134,7 @@ def test_metrics_show(dvc, mocker):
)
def test_metrics_show_json(dvc, mocker, caplog, capsys):
def test_metrics_show_json(dvc, mocker, capsys):
cli_args = parse_args(
[
"metrics",
@@ -161,7 +161,7 @@ def test_metrics_show_json(dvc, mocker, caplog, capsys):
show_metrics_mock = mocker.patch("dvc.compare.show_metrics")
assert cmd.run() == 0
out, _ = capsys.readouterr()
metrics_show.assert_called_once_with(
cmd.repo,
["target1", "target2"],
@@ -171,5 +171,4 @@ def test_metrics_show_json(dvc, mocker, caplog, capsys):
all_commits=True,
)
show_metrics_mock.assert_not_called()
assert json.dumps(d) in caplog.text
assert capsys.readouterr() == ("", "")
assert json.dumps(d) in out
+4 -8
View File
@@ -1,5 +1,3 @@
import logging
from dvc.cli import parse_args
from dvc.command.params import CmdParamsDiff
@@ -57,7 +55,7 @@ def test_params_diff_from_cli(dvc, mocker):
)
def test_params_diff_show_json(dvc, mocker, caplog, capsys):
def test_params_diff_show_json(dvc, mocker, capsys):
cli_args = parse_args(
["params", "diff", "HEAD~10", "HEAD~1", "--show-json"]
)
@@ -67,9 +65,7 @@ def test_params_diff_show_json(dvc, mocker, caplog, capsys):
)
show_diff_mock = mocker.patch("dvc.compare.show_diff")
with caplog.at_level(logging.INFO, logger="dvc"):
assert cmd.run() == 0
assert '{"params.yaml": {"a": "b"}}\n' in caplog.text
assert cmd.run() == 0
out, _ = capsys.readouterr()
assert '{"params.yaml": {"a": "b"}}\n' in out
show_diff_mock.assert_not_called()
assert capsys.readouterr() == ("", "")
+16 -12
View File
@@ -1,4 +1,3 @@
import logging
import os
import posixpath
@@ -91,7 +90,7 @@ def test_plots_show_vega(dvc, mocker):
)
def test_plots_diff_vega(dvc, mocker, caplog):
def test_plots_diff_vega(dvc, mocker, capsys):
cli_args = parse_args(
[
"plots",
@@ -108,10 +107,12 @@ def test_plots_diff_vega(dvc, mocker, caplog):
"dvc.repo.plots.diff.diff", return_value={"plots.csv": "plothtml"}
)
assert cmd.run() == 0
assert "plothtml" in caplog.text
out, _ = capsys.readouterr()
assert "plothtml" in out
def test_plots_diff_open(tmp_dir, dvc, mocker, caplog):
def test_plots_diff_open(tmp_dir, dvc, mocker, capsys):
mocked_open = mocker.patch("webbrowser.open", return_value=True)
cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"])
cmd = cli_args.func(cli_args)
@@ -123,10 +124,12 @@ def test_plots_diff_open(tmp_dir, dvc, mocker, caplog):
mocked_open.assert_called_once_with("plots.html")
expected_url = posixpath.join(tmp_dir.as_uri(), "plots.html")
assert expected_url in caplog.text
out, _ = capsys.readouterr()
assert expected_url in out
def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog):
def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys):
mocked_open = mocker.patch("webbrowser.open", return_value=False)
cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"])
cmd = cli_args.func(cli_args)
@@ -140,10 +143,9 @@ def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog):
error_message = "Failed to open. Please try opening it manually."
expected_url = posixpath.join(tmp_dir.as_uri(), "plots.html")
assert caplog.record_tuples == [
("dvc.command.plots", logging.INFO, expected_url),
("dvc.command.plots", logging.ERROR, error_message),
]
out, err = capsys.readouterr()
assert expected_url in out
assert error_message in err
@pytest.mark.parametrize(
@@ -155,7 +157,7 @@ def test_plots_diff_open_failed(tmp_dir, dvc, mocker, caplog):
ids=["quote", "resolve"],
)
def test_plots_path_is_quoted_and_resolved_properly(
tmp_dir, dvc, mocker, caplog, output, expected_url_path
tmp_dir, dvc, mocker, capsys, output, expected_url_path
):
cli_args = parse_args(
["plots", "diff", "--targets", "datafile", "--out", output]
@@ -167,4 +169,6 @@ def test_plots_path_is_quoted_and_resolved_properly(
assert cmd.run() == 0
expected_url = posixpath.join(tmp_dir.as_uri(), expected_url_path)
assert expected_url in caplog.text
out, _ = capsys.readouterr()
assert expected_url in out
+4 -3
View File
@@ -45,16 +45,17 @@ def test_cloud_status(tmp_dir, dvc, mocker):
@pytest.mark.parametrize("status", [{}, {"a": "b", "c": [1, 2, 3]}, [1, 2, 3]])
def test_status_show_json(dvc, mocker, caplog, status):
def test_status_show_json(dvc, mocker, capsys, 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)]
out, _ = capsys.readouterr()
assert json.dumps(status) in out
@pytest.mark.parametrize(