Skip to content

Commit

Permalink
style,test: reformat test_main* modules
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Aug 9, 2024
1 parent 3e002b4 commit ff6333e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import darker.__main__
import darker.import_sorting
from darker.git import EditedLinenumsDiffer
from darker.tests.examples import A_PY, A_PY_BLACK, A_PY_BLACK_FLYNT, A_PY_BLACK_ISORT
from darker.tests.test_fstring import FLYNTED_SOURCE, MODIFIED_SOURCE, ORIGINAL_SOURCE
from darker.tests.examples import A_PY, A_PY_BLACK, A_PY_BLACK_ISORT, A_PY_BLACK_FLYNT
from darkgraylib.git import RevisionRange
from darkgraylib.testtools.highlighting_helpers import BLUE, CYAN, RESET, WHITE, YELLOW
from darkgraylib.utils import TextDocument, joinlines
Expand Down
54 changes: 31 additions & 23 deletions src/darker/tests/test_main_format_edited_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import re
from io import BytesIO
from pathlib import Path
from unittest.mock import patch, Mock
from unittest.mock import Mock, patch

import pytest

import darker.__main__
import darker.verification
from darker.config import Exclusions
from darker.tests.examples import A_PY, A_PY_BLACK, A_PY_BLACK_ISORT, A_PY_BLACK_FLYNT
from darker.tests.examples import A_PY, A_PY_BLACK, A_PY_BLACK_FLYNT, A_PY_BLACK_ISORT
from darker.verification import NotEquivalentError
from darkgraylib.git import RevisionRange, WORKTREE
from darkgraylib.git import WORKTREE, RevisionRange
from darkgraylib.utils import TextDocument, joinlines

A_PY_ISORT = ["import os", "import sys", "", "print( '{}'.format('42'))", ""]
Expand Down Expand Up @@ -66,9 +66,15 @@
)
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
def test_format_edited_parts(
git_repo, black_config, black_exclude, isort_exclude, flynt_exclude, newline, expect
git_repo,
black_config,
black_exclude,
isort_exclude,
flynt_exclude,
newline,
expect,
):
"""Correct reformatting and import sorting changes are produced
"""Correct reformatting and import sorting changes are produced.
Black reformatting is done even if a file is excluded in Black configuration.
File exclusion is done in Darker before calling
Expand Down Expand Up @@ -113,7 +119,7 @@ def test_format_edited_parts(
"a.py",
("print('a.py HEAD' )", "#", "print( 'a.py STDIN')"),
("print('a.py HEAD' )", "#", 'print("a.py STDIN")'),
)
),
],
),
dict(
Expand All @@ -124,7 +130,7 @@ def test_format_edited_parts(
"a.py",
("print('a.py :WORKTREE:' )", "#", "print( 'a.py STDIN')"),
("print('a.py :WORKTREE:' )", "#", 'print("a.py STDIN")'),
)
),
],
),
dict(
Expand All @@ -135,13 +141,13 @@ def test_format_edited_parts(
"a.py",
("print('a.py :WORKTREE:' )", "#", "print( 'a.py HEAD')"),
('print("a.py :WORKTREE:")', "#", "print( 'a.py HEAD')"),
)
),
],
),
)
@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["unix", "windows"])
def test_format_edited_parts_stdin(git_repo, newline, rev1, rev2, expect):
"""`format_edited_parts` with ``--stdin-filename``"""
"""`format_edited_parts` with ``--stdin-filename``."""
n = newline # pylint: disable=invalid-name
paths = git_repo.add(
{
Expand All @@ -151,10 +157,10 @@ def test_format_edited_parts_stdin(git_repo, newline, rev1, rev2, expect):
commit="Initial commit",
)
paths["a.py"].write_bytes(
f"print('a.py :WORKTREE:' ){n}#{n}print( 'a.py HEAD'){n}".encode("ascii")
f"print('a.py :WORKTREE:' ){n}#{n}print( 'a.py HEAD'){n}".encode("ascii"),
)
paths["b.py"].write_bytes(
f"print('b.py HEAD' ){n}#{n}print( 'b.py WORKTREE'){n}".encode("ascii")
f"print('b.py HEAD' ){n}#{n}print( 'b.py WORKTREE'){n}".encode("ascii"),
)
stdin = f"print('a.py {rev1}' ){n}#{n}print( 'a.py STDIN'){n}".encode("ascii")
with patch.object(
Expand All @@ -172,7 +178,7 @@ def test_format_edited_parts_stdin(git_repo, newline, rev1, rev2, expect):
RevisionRange(rev1, rev2),
{},
report_unmodified=False,
)
),
)

expect = [
Expand All @@ -183,7 +189,7 @@ def test_format_edited_parts_stdin(git_repo, newline, rev1, rev2, expect):


def test_format_edited_parts_all_unchanged(git_repo, monkeypatch):
"""``format_edited_parts()`` yields nothing if no reformatting was needed"""
"""``format_edited_parts()`` yields nothing if no reformatting was needed."""
monkeypatch.chdir(git_repo.root)
paths = git_repo.add({"a.py": "pass\n", "b.py": "pass\n"}, commit="Initial commit")
paths["a.py"].write_bytes(b'"properly"\n"formatted"\n')
Expand All @@ -197,19 +203,21 @@ def test_format_edited_parts_all_unchanged(git_repo, monkeypatch):
RevisionRange("HEAD", ":WORKTREE:"),
{},
report_unmodified=False,
)
),
)

assert result == []


def test_format_edited_parts_ast_changed(git_repo, caplog):
"""``darker.__main__.format_edited_parts()`` when reformatting changes the AST"""
"""``darker.__main__.format_edited_parts()`` when reformatting changes the AST."""
caplog.set_level(logging.DEBUG, logger="darker.__main__")
paths = git_repo.add({"a.py": "1\n2\n3\n4\n5\n6\n7\n8\n"}, commit="Initial commit")
paths["a.py"].write_bytes(b"8\n7\n6\n5\n4\n3\n2\n1\n")
mock_ctx = patch.object(
darker.verification.ASTVerifier, "is_equivalent_to_baseline", return_value=False
darker.verification.ASTVerifier,
"is_equivalent_to_baseline",
return_value=False,
)
with mock_ctx, pytest.raises(NotEquivalentError):
_ = list(
Expand All @@ -220,7 +228,7 @@ def test_format_edited_parts_ast_changed(git_repo, caplog):
RevisionRange("HEAD", ":WORKTREE:"),
black_config={},
report_unmodified=False,
)
),
)
a_py = str(paths["a.py"])
main = "darker.__main__:__main__.py"
Expand All @@ -241,7 +249,7 @@ def test_format_edited_parts_ast_changed(git_repo, caplog):


def test_format_edited_parts_isort_on_already_formatted(git_repo):
"""An already correctly formatted file after ``isort`` is simply skipped"""
"""An already correctly formatted file after ``isort`` is simply skipped."""
before = [
"import a",
"import b",
Expand Down Expand Up @@ -275,7 +283,7 @@ def test_format_edited_parts_isort_on_already_formatted(git_repo):
dict(rev1="HEAD", rev2=WORKTREE, expect=[(":WORKTREE:", "reformatted")]),
)
def test_format_edited_parts_historical(git_repo, rev1, rev2, expect):
"""``format_edited_parts()`` is correct for different commit pairs"""
"""``format_edited_parts()`` is correct for different commit pairs."""
a_py = {
"HEAD^": TextDocument.from_lines(
[
Expand All @@ -284,28 +292,28 @@ def test_format_edited_parts_historical(git_repo, rev1, rev2, expect):
"",
"a.foo()",
"bar()",
]
],
),
"HEAD": TextDocument.from_lines(
[
"from b import bar, foo",
"",
"bar()",
]
],
),
":WORKTREE:": TextDocument.from_lines(
[
"from b import foo, bar",
"",
"bar( )",
]
],
),
"reformatted": TextDocument.from_lines(
[
"from b import bar, foo",
"",
"bar()",
]
],
),
}
paths = git_repo.add({"a.py": a_py["HEAD^"].string}, commit="Initial commit")
Expand Down
20 changes: 13 additions & 7 deletions src/darker/tests/test_main_isort.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for the ``--isort`` option of the ``darker`` command-line interface"""
"""Tests for the ``--isort`` option of the ``darker`` command-line interface."""

# pylint: disable=redefined-outer-name,unused-argument,use-dict-literal

Expand All @@ -22,7 +22,9 @@ def test_isort_option_without_isort(git_repo): # noqa: ARG001
# The `git_repo` fixture ensures test is not run in the Darker repository clone in
# CI builds. It helps avoid a NixOS test issue.
with isort_present(present=False), patch.object(
darker.__main__, "isort", None
darker.__main__,
"isort",
None,
), pytest.raises(MissingPackageError) as exc_info:

darker.__main__.main(["--isort", "."])
Expand All @@ -35,7 +37,7 @@ def test_isort_option_without_isort(git_repo): # noqa: ARG001

@pytest.fixture()
def run_isort(git_repo, monkeypatch, caplog, request):
"""Fixture for running Darker with requested arguments and a patched `isort`
"""Fixture for running Darker with requested arguments and a patched `isort`.
Provides an `run_isort.isort_code` mock object which allows checking whether and how
the `isort.code()` function was called.
Expand All @@ -48,7 +50,9 @@ def run_isort(git_repo, monkeypatch, caplog, request):
isorted_code = "import os; import sys;"
blacken_code = "import os\nimport sys\n"
patch_run_black_ctx = patch.object(
darker.__main__, "run_black", return_value=TextDocument(blacken_code)
darker.__main__,
"run_black",
return_value=TextDocument(blacken_code),
)
with patch_run_black_ctx, patch("darker.import_sorting.isort_code") as isort_code:
isort_code.return_value = isorted_code
Expand All @@ -61,7 +65,7 @@ def run_isort(git_repo, monkeypatch, caplog, request):


def test_isort_option_with_isort(run_isort):
"""Doesn't prompt to install ``isort`` if it's already installed"""
"""Doesn't prompt to install ``isort`` if it's already installed."""
assert "Please run" not in run_isort.caplog.text


Expand All @@ -71,7 +75,9 @@ def test_isort_option_with_isort(run_isort):
indirect=["run_isort"],
)
def test_isort_option_with_isort_calls_sortimports(run_isort, isort_args):
"""Relevant config options are passed from command line to ``isort``"""
"""Relevant config options are passed from command line to ``isort``."""
run_isort.isort_code.assert_called_once_with(
code="changed", settings_path=str(run_isort.root), **isort_args
code="changed",
settings_path=str(run_isort.root),
**isort_args,
)

0 comments on commit ff6333e

Please sign in to comment.