From c6f49bd23d172a9831fc4b2e0515ff3f59ca7787 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Fri, 14 May 2021 10:19:55 +0545 Subject: [PATCH] ls: use ui.write (#6004) --- dvc/command/ls/__init__.py | 8 ++-- tests/func/test_ls.py | 30 --------------- tests/unit/command/ls/test_ls.py | 49 ++++++++++++++++++++++--- tests/unit/command/ls/test_ls_colors.py | 15 ++++++++ 4 files changed, 63 insertions(+), 39 deletions(-) diff --git a/dvc/command/ls/__init__.py b/dvc/command/ls/__init__.py index ff18ddec0..a44877f03 100644 --- a/dvc/command/ls/__init__.py +++ b/dvc/command/ls/__init__.py @@ -1,11 +1,11 @@ import argparse import logging -import sys from dvc.command import completion from dvc.command.base import CmdBaseNoRepo, append_doc_link from dvc.command.ls.ls_colors import LsColors from dvc.exceptions import DvcException +from dvc.ui import ui logger = logging.getLogger(__name__) @@ -37,10 +37,10 @@ class CmdList(CmdBaseNoRepo): if self.args.show_json: import json - logger.info(json.dumps(entries)) + ui.write(json.dumps(entries)) elif entries: - entries = _prettify(entries, sys.stdout.isatty()) - logger.info("\n".join(entries)) + entries = _prettify(entries, with_color=True) + ui.write("\n".join(entries)) return 0 except DvcException: logger.exception(f"failed to list '{self.args.url}'") diff --git a/tests/func/test_ls.py b/tests/func/test_ls.py index 4ccaf62e9..812e4fbbc 100644 --- a/tests/func/test_ls.py +++ b/tests/func/test_ls.py @@ -73,36 +73,6 @@ def test_ls_repo(tmp_dir, dvc, scm): ) -def test_ls_repo_with_color(tmp_dir, dvc, scm, mocker, monkeypatch, caplog): - import logging - - from dvc.cli import parse_args - - tmp_dir.scm_gen(FS_STRUCTURE, commit="init") - tmp_dir.dvc_gen(DVC_STRUCTURE, commit="dvc") - - monkeypatch.setenv("LS_COLORS", "rs=0:di=01;34:*.xml=01;31:*.dvc=01;33:") - cli_args = parse_args(["list", os.fspath(tmp_dir)]) - cmd = cli_args.func(cli_args) - - caplog.clear() - mocker.patch("sys.stdout.isatty", return_value=True) - with caplog.at_level(logging.INFO, logger="dvc.command.ls"): - assert cmd.run() == 0 - - assert caplog.records[-1].msg == "\n".join( - [ - ".dvcignore", - ".gitignore", - "README.md", - "\x1b[01;34mdata\x1b[0m", - "\x1b[01;34mmodel\x1b[0m", - "\x1b[01;31mstructure.xml\x1b[0m", - "\x1b[01;33mstructure.xml.dvc\x1b[0m", - ] - ) - - def test_ls_repo_recursive(tmp_dir, dvc, scm): tmp_dir.scm_gen(FS_STRUCTURE, commit="init") tmp_dir.dvc_gen(DVC_STRUCTURE, commit="dvc") diff --git a/tests/unit/command/ls/test_ls.py b/tests/unit/command/ls/test_ls.py index c6be019de..c690d0ff5 100644 --- a/tests/unit/command/ls/test_ls.py +++ b/tests/unit/command/ls/test_ls.py @@ -1,5 +1,4 @@ import json -import logging from dvc.cli import parse_args from dvc.command.ls import CmdList @@ -57,7 +56,7 @@ def test_list_outputs_only(mocker): ) -def test_show_json(mocker, caplog): +def test_show_json(mocker, capsys): cli_args = parse_args(["list", "local_dir", "--show-json"]) assert cli_args.func == CmdList @@ -66,7 +65,47 @@ def test_show_json(mocker, caplog): result = [{"key": "val"}] mocker.patch("dvc.repo.Repo.ls", return_value=result) - with caplog.at_level(logging.INFO, "dvc"): - assert cmd.run() == 0 + assert cmd.run() == 0 + out, _ = capsys.readouterr() + assert json.dumps(result) in out - assert json.dumps(result) in caplog.messages + +def test_show_colors(mocker, capsys, monkeypatch): + cli_args = parse_args(["list", "local_dir"]) + assert cli_args.func == CmdList + cmd = cli_args.func(cli_args) + + monkeypatch.setenv( + "LS_COLORS", "ex=01;32:rs=0:di=01;34:*.xml=01;31:*.dvc=01;33:" + ) + result = [ + {"isdir": False, "isexec": 0, "isout": False, "path": ".dvcignore"}, + {"isdir": False, "isexec": 0, "isout": False, "path": ".gitignore"}, + {"isdir": False, "isexec": 0, "isout": False, "path": "README.md"}, + {"isdir": True, "isexec": 0, "isout": True, "path": "data"}, + {"isdir": False, "isexec": 0, "isout": True, "path": "structure.xml"}, + { + "isdir": False, + "isexec": 0, + "isout": False, + "path": "structure.xml.dvc", + }, + {"isdir": True, "isexec": 0, "isout": False, "path": "src"}, + {"isdir": False, "isexec": 1, "isout": False, "path": "run.sh"}, + ] + mocker.patch("dvc.repo.Repo.ls", return_value=result) + + assert cmd.run() == 0 + out, _ = capsys.readouterr() + entries = out.splitlines() + + assert entries == [ + ".dvcignore", + ".gitignore", + "README.md", + "\x1b[01;34mdata\x1b[0m", + "\x1b[01;31mstructure.xml\x1b[0m", + "\x1b[01;33mstructure.xml.dvc\x1b[0m", + "\x1b[01;34msrc\x1b[0m", + "\x1b[01;32mrun.sh\x1b[0m", + ] diff --git a/tests/unit/command/ls/test_ls_colors.py b/tests/unit/command/ls/test_ls_colors.py index 7d53a4de7..32c840594 100644 --- a/tests/unit/command/ls/test_ls_colors.py +++ b/tests/unit/command/ls/test_ls_colors.py @@ -56,3 +56,18 @@ def test_ls_colors_exec(): def test_ls_colors_ext(): ls_colors = LsColors(LsColors.default + ":*.xml=01;33") assert colorize(ls_colors)("file.xml") == "\x1b[01;33mfile.xml\x1b[0m" + + +def test_ls_repo_with_custom_color_env_defined(monkeypatch): + monkeypatch.setenv("LS_COLORS", "rs=0:di=01;34:*.xml=01;31:*.dvc=01;33:") + ls_colors = LsColors() + colorizer = colorize(ls_colors) + + assert colorizer(".dvcignore") == ".dvcignore" + assert colorizer(".gitignore") == ".gitignore" + assert colorizer("README.md") == "README.md" + assert colorizer("data", "d") == "\x1b[01;34mdata\x1b[0m" + assert colorizer("structure.xml") == "\x1b[01;31mstructure.xml\x1b[0m" + assert ( + colorizer("structure.xml.dvc") == "\x1b[01;33mstructure.xml.dvc\x1b[0m" + )