mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
ls: use ui.write (#6004)
This commit is contained in:
@@ -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}'")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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",
|
||||
]
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user