Skip to content

Commit

Permalink
Print all logs to sys.stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
Jairo Llopis authored and yajo committed Aug 17, 2020
1 parent 307fd8a commit 2cf5be0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
38 changes: 29 additions & 9 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def copy(
copy_local(conf=conf)
except Exception:
if conf.cleanup_on_error and not do_diff_update:
print("Something went wrong. Removing destination folder.")
print(
colors.warn | "Something went wrong. Removing destination folder.",
file=sys.stderr,
)
shutil.rmtree(conf.dst_path, ignore_errors=True)
raise
finally:
Expand Down Expand Up @@ -269,7 +272,8 @@ def update_diff(conf: ConfigData) -> None:
except ProcessExecutionError:
print(
colors.warn
| "Make sure Git >= 2.24 is installed to improve updates."
| "Make sure Git >= 2.24 is installed to improve updates.",
file=sys.stderr,
)
diff = diff_cmd("--inter-hunk-context=0")
# Run pre-migration tasks
Expand Down Expand Up @@ -354,13 +358,19 @@ def render_folder(rel_folder: Path, conf: ConfigData) -> None:
return

if dst_path.exists():
printf("identical", rel_path, style=Style.IGNORE, quiet=conf.quiet)
printf(
"identical",
rel_path,
style=Style.IGNORE,
quiet=conf.quiet,
file_=sys.stderr,
)
return

if not conf.pretend:
make_folder(dst_path)

printf("create", rel_path, style=Style.OK, quiet=conf.quiet)
printf("create", rel_path, style=Style.OK, quiet=conf.quiet, file_=sys.stderr)


def render_file(
Expand All @@ -386,15 +396,25 @@ def render_file(
dst_path = conf.dst_path / rel_path

if not dst_path.exists():
printf("create", rel_path, style=Style.OK, quiet=conf.quiet)
printf("create", rel_path, style=Style.OK, quiet=conf.quiet, file_=sys.stderr)
elif files_are_identical(src_path, dst_path, content):
printf("identical", rel_path, style=Style.IGNORE, quiet=conf.quiet)
printf(
"identical",
rel_path,
style=Style.IGNORE,
quiet=conf.quiet,
file_=sys.stderr,
)
return
elif must_skip(rel_path) or not overwrite_file(conf, dst_path, rel_path):
printf("skip", rel_path, style=Style.WARNING, quiet=conf.quiet)
printf(
"skip", rel_path, style=Style.WARNING, quiet=conf.quiet, file_=sys.stderr
)
return
else:
printf("force", rel_path, style=Style.WARNING, quiet=conf.quiet)
printf(
"force", rel_path, style=Style.WARNING, quiet=conf.quiet, file_=sys.stderr
)

if conf.pretend:
pass
Expand Down Expand Up @@ -432,7 +452,7 @@ def overwrite_file(conf: ConfigData, dst_path: Path, rel_path: Path) -> bool:
True if the overwrite was forced or the user answered yes,
False if skipped by configuration or if the user answered no.
"""
printf("conflict", rel_path, style=Style.DANGER, quiet=conf.quiet)
printf("conflict", rel_path, style=Style.DANGER, quiet=conf.quiet, file_=sys.stderr)
if conf.force:
return True
if conf.skip:
Expand Down
16 changes: 9 additions & 7 deletions copier/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import errno
import os
import shutil
import sys
import unicodedata
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, TextIO, Union

import colorama
import pathspec
Expand Down Expand Up @@ -53,6 +54,7 @@ def printf(
style: Optional[IntSeq] = None,
indent: int = 10,
quiet: Union[bool, StrictBool] = False,
file_: TextIO = sys.stdout,
) -> Optional[str]:
if quiet:
return None # HACK: Satisfy MyPy
Expand All @@ -62,19 +64,19 @@ def printf(
return action + _msg

out = style + [action] + Style.RESET + [INDENT, _msg] # type: ignore
print(*out, sep="")
print(*out, sep="", file=file_)
return None # HACK: Satisfy MyPy


def printf_exception(
e: Exception, action: str, msg: str = "", indent: int = 0, quiet: bool = False
) -> None:
if not quiet:
print("")
printf(action, msg=msg, style=Style.DANGER, indent=indent)
print(HLINE)
print(e)
print(HLINE)
print("", file=sys.stderr)
printf(action, msg=msg, style=Style.DANGER, indent=indent, file_=sys.stderr)
print(HLINE, file=sys.stderr)
print(e, file=sys.stderr)
print(HLINE, file=sys.stderr)


def required(value: T, **kwargs: Any) -> T:
Expand Down
7 changes: 5 additions & 2 deletions copier/vcs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Utilities related to VCS."""

import re
import sys
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -75,7 +75,10 @@ def checkout_latest_tag(local_repo: StrOrPath, use_prereleases: OptBool = False)
try:
latest_tag = str(sorted_tags[0])
except IndexError:
print(colors.warn | "No git tags found in template; using HEAD as ref")
print(
colors.warn | "No git tags found in template; using HEAD as ref",
file=sys.stderr,
)
latest_tag = "HEAD"
git("checkout", "--force", latest_tag)
git("submodule", "update", "--checkout", "--init", "--recursive", "--force")
Expand Down
16 changes: 8 additions & 8 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def test_invalid_yaml(capsys):
conf_path = Path("tests", "demo_invalid", "copier.yml")
with pytest.raises(InvalidConfigFileError):
load_yaml_data(conf_path)
out, _ = capsys.readouterr()
assert "INVALID CONFIG FILE" in out
assert str(conf_path) in out
_, err = capsys.readouterr()
assert "INVALID CONFIG FILE" in err
assert str(conf_path) in err


@pytest.mark.parametrize(
"conf_path,flags,check_out",
"conf_path,flags,check_err",
(
("tests/demo_invalid", {"_warning": False}, lambda x: "INVALID" in x),
("tests/demo_invalid", {"quiet": True}, lambda x: x == ""),
Expand All @@ -63,12 +63,12 @@ def test_invalid_yaml(capsys):
("tests/demo_transclude_invalid_multi/demo", {}, None),
),
)
def test_invalid_config_data(conf_path, flags, check_out, capsys):
def test_invalid_config_data(conf_path, flags, check_err, capsys):
with pytest.raises(InvalidConfigFileError):
load_config_data(conf_path, **flags)
if check_out:
out, _ = capsys.readouterr()
assert check_out(out)
if check_err:
_, err = capsys.readouterr()
assert check_err(err)


def test_config_data_empty():
Expand Down
43 changes: 22 additions & 21 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,44 @@

def test_output(capsys, tmp_path):
render(tmp_path, quiet=False)
out, _ = capsys.readouterr()
assert re.search(r"create[^\s]* config\.py", out)
assert re.search(r"create[^\s]* pyproject\.toml", out)
assert re.search(r"create[^\s]* doc[/\\]images[/\\]nslogo\.gif", out)
_, err = capsys.readouterr()
assert re.search(r"create[^\s]* config\.py", err)
assert re.search(r"create[^\s]* pyproject\.toml", err)
assert re.search(r"create[^\s]* doc[/\\]images[/\\]nslogo\.gif", err)


def test_output_pretend(capsys, tmp_path):
render(tmp_path, quiet=False, pretend=True)
out, _ = capsys.readouterr()
assert re.search(r"create[^\s]* config\.py", out)
assert re.search(r"create[^\s]* pyproject\.toml", out)
assert re.search(r"create[^\s]* doc[/\\]images[/\\]nslogo\.gif", out)
_, err = capsys.readouterr()
assert re.search(r"create[^\s]* config\.py", err)
assert re.search(r"create[^\s]* pyproject\.toml", err)
assert re.search(r"create[^\s]* doc[/\\]images[/\\]nslogo\.gif", err)


def test_output_force(capsys, tmp_path):
render(tmp_path)
out, _ = capsys.readouterr()
capsys.readouterr()
render(tmp_path, quiet=False, force=True)
out, _ = capsys.readouterr()
assert re.search(r"conflict[^\s]* config\.py", out)
assert re.search(r"force[^\s]* config\.py", out)
assert re.search(r"identical[^\s]* pyproject\.toml", out)
assert re.search(r"identical[^\s]* doc[/\\]images[/\\]nslogo\.gif", out)
_, err = capsys.readouterr()
assert re.search(r"conflict[^\s]* config\.py", err)
assert re.search(r"force[^\s]* config\.py", err)
assert re.search(r"identical[^\s]* pyproject\.toml", err)
assert re.search(r"identical[^\s]* doc[/\\]images[/\\]nslogo\.gif", err)


def test_output_skip(capsys, tmp_path):
render(tmp_path)
out, _ = capsys.readouterr()
capsys.readouterr()
render(tmp_path, quiet=False, skip=True)
out, _ = capsys.readouterr()
assert re.search(r"conflict[^\s]* config\.py", out)
assert re.search(r"skip[^\s]* config\.py", out)
assert re.search(r"identical[^\s]* pyproject\.toml", out)
assert re.search(r"identical[^\s]* doc[/\\]images[/\\]nslogo\.gif", out)
_, err = capsys.readouterr()
assert re.search(r"conflict[^\s]* config\.py", err)
assert re.search(r"skip[^\s]* config\.py", err)
assert re.search(r"identical[^\s]* pyproject\.toml", err)
assert re.search(r"identical[^\s]* doc[/\\]images[/\\]nslogo\.gif", err)


def test_output_quiet(capsys, tmp_path):
render(tmp_path, quiet=True)
out, _ = capsys.readouterr()
out, err = capsys.readouterr()
assert out == ""
assert err == ""

0 comments on commit 2cf5be0

Please sign in to comment.