Skip to content

Commit

Permalink
Add test for output printed to stdout
Browse files Browse the repository at this point in the history
Unit-test for the `print_source` function used when the `--stdout`
option is applied. To test the behaviour with/without pygments the
importing of pygments was extracted to a function.
  • Loading branch information
magnunm committed Feb 6, 2022
1 parent 9259354 commit 66f38fb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ def print_source(new: TextDocument) -> None:
"""Print the reformatted Python source code"""
if sys.stdout.isatty():
try:
# pylint: disable=import-outside-toplevel
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers.python import PythonLexer
(
highlight,
TerminalFormatter, # pylint: disable=invalid-name
PythonLexer,
) = _import_pygments() # type: ignore
except ImportError:
print(new.string, end="")
else:
Expand All @@ -270,6 +271,21 @@ def print_source(new: TextDocument) -> None:
print(new.string, end="")


def _import_pygments(): # type: ignore
"""Import within a function to ease mocking the import in unit-tests.
Cannot be typed as it imports parts of its own return type.
"""
# pylint: disable=import-outside-toplevel
from pygments import highlight
from pygments.formatters import ( # pylint: disable=no-name-in-module
TerminalFormatter,
)
from pygments.lexers.python import PythonLexer

return highlight, TerminalFormatter, PythonLexer


def main(argv: List[str] = None) -> int:
"""Parse the command line and reformat and optionally lint each source file
Expand Down
46 changes: 45 additions & 1 deletion src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from textwrap import dedent
from types import SimpleNamespace
from unittest.mock import call, patch
from unittest.mock import Mock, call, patch

import pytest

Expand Down Expand Up @@ -778,6 +778,50 @@ def test_modify_file(tmp_path, new_content, expect):
assert result == expect


@pytest.mark.kwparametrize(
dict(
new_content=TextDocument(lines=['print("foo")']),
tty=False,
with_pygments=False,
expect=('print("foo")\n',),
),
dict(
new_content=TextDocument(lines=['print("foo")']),
tty=False,
with_pygments=True,
expect=('print("foo")\n',),
),
dict(
new_content=TextDocument(lines=['print("foo")']),
tty=True,
with_pygments=False,
expect=('print("foo")\n',),
),
dict(
new_content=TextDocument(lines=['print("foo")']),
tty=True,
with_pygments=True,
expect=(
'\x1b[36mprint\x1b[39;49;00m(\x1b[33m"\x1b[39;49;00mfoo'
+ '\x1b[33m"\x1b[39;49;00m)\n',
'\x1b[36mprint\x1b[39;49;00m(\x1b[33m"\x1b[39;49;00m\x1b[33mfoo'
+ '\x1b[39;49;00m\x1b[33m"\x1b[39;49;00m)\n',
),
),
)
def test_print_source(new_content, tty, with_pygments, expect, capsys):
"""Highlight is applied only if tty, final newline is handled correctly."""
with patch("sys.stdout.isatty", Mock(return_value=tty)), patch(
"darker.__main__._import_pygments",
Mock(
return_value=darker.__main__._import_pygments(),
side_effect=None if with_pygments else ImportError(),
),
):
darker.__main__.print_source(new_content)
assert capsys.readouterr().out in expect


def test_stdout_path_resolution(git_repo, capsys):
"""When using ``--stdout``, file paths are resolved correctly"""
git_repo.add({"src/menu.py": "print ( 'foo' )\n"})
Expand Down

0 comments on commit 66f38fb

Please sign in to comment.