Skip to content

Commit

Permalink
Merge pull request #515 from jedie/jedie-patch-1
Browse files Browse the repository at this point in the history
Bugfix black .gitignore usage
  • Loading branch information
akaihola authored Feb 26, 2024
2 parents 8bf0b6b + 4363e56 commit 277b6b3
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 4 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ test =
flynt>=0.76,<0.78
isort>=5.0.1
mypy>=0.990
pathspec # to test `gen_python_files` in `test_black_diff.py`
pip-requirements-parser
pygments
pytest>=6.2.0
Expand Down
4 changes: 3 additions & 1 deletion src/darker/black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ def filter_python_files(
kwargs = {"verbose": False, "quiet": False} if "verbose" in sig.parameters else {}
# `gitignore=` was replaced with `gitignore_dict=` in black==22.10.1.dev19+gffaaf48
for param in sig.parameters:
if param.startswith("gitignore"):
if param == "gitignore":
kwargs[param] = None # type: ignore[assignment]
elif param == "gitignore_dict":
kwargs[param] = {} # type: ignore[assignment]
absolute_paths = {p.resolve() for p in paths}
directories = {p for p in absolute_paths if p.is_dir()}
files = {p for p in absolute_paths if p not in directories}
Expand Down
127 changes: 124 additions & 3 deletions src/darker/tests/test_black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import ANY, patch
from typing import Dict, Iterable, Iterator, Optional, Pattern, TYPE_CHECKING
from unittest.mock import ANY, Mock, call, patch

import pytest
import regex

from black import Mode, TargetVersion
from black import Mode, Report, TargetVersion
from pathspec import PathSpec

from darker import black_diff
from darker.black_diff import (
Expand Down Expand Up @@ -205,6 +206,126 @@ def test_filter_python_files( # pylint: disable=too-many-arguments
assert result == expect_paths


def make_mock_gen_python_files_black_21_7b1_dev8():
"""Create `gen_python_files` mock for Black 21.7b1.dev8+ge76adbe
Also record the call made to the mock function for test verification.
This revision didn't yet have the `verbose` and `quiet` parameters.
"""
calls = Mock()

def gen_python_files(
paths: Iterable[Path],

Check failure on line 220 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L220

Unused argument 'paths' (unused-argument, W0613)
root: Path,

Check failure on line 221 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L221

Unused argument 'root' (unused-argument, W0613)
include: Pattern[str],

Check failure on line 222 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L222

Unused argument 'include' (unused-argument, W0613)
exclude: Pattern[str],

Check failure on line 223 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L223

Unused argument 'exclude' (unused-argument, W0613)
extend_exclude: Optional[Pattern[str]],

Check failure on line 224 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L224

Unused argument 'extend_exclude' (unused-argument, W0613)
force_exclude: Optional[Pattern[str]],

Check failure on line 225 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L225

Unused argument 'force_exclude' (unused-argument, W0613)
report: Report,

Check failure on line 226 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L226

Unused argument 'report' (unused-argument, W0613)
gitignore: Optional[PathSpec],
) -> Iterator[Path]:
calls.gen_python_files = call(gitignore=gitignore)
for _ in []:
yield Path()

return gen_python_files, calls


def make_mock_gen_python_files_black_21_7b1_dev9():
"""Create `gen_python_files` mock for Black 21.7b1.dev9+gb1d0601
Also record the call made to the mock function for test verification.
This revision added `verbose` and `quiet` parameters to `gen_python_files`.
"""
calls = Mock()

def gen_python_files(
paths: Iterable[Path],

Check failure on line 247 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L247

Unused argument 'paths' (unused-argument, W0613)
root: Path,

Check failure on line 248 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L248

Unused argument 'root' (unused-argument, W0613)
include: Pattern[str],

Check failure on line 249 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L249

Unused argument 'include' (unused-argument, W0613)
exclude: Pattern[str],

Check failure on line 250 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L250

Unused argument 'exclude' (unused-argument, W0613)
extend_exclude: Optional[Pattern[str]],

Check failure on line 251 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L251

Unused argument 'extend_exclude' (unused-argument, W0613)
force_exclude: Optional[Pattern[str]],

Check failure on line 252 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L252

Unused argument 'force_exclude' (unused-argument, W0613)
report: Report,

Check failure on line 253 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L253

Unused argument 'report' (unused-argument, W0613)
gitignore: Optional[PathSpec],
*,
verbose: bool,
quiet: bool,
) -> Iterator[Path]:
calls.gen_python_files = call(
gitignore=gitignore,
verbose=verbose,
quiet=quiet,
)
for _ in []:
yield Path()

return gen_python_files, calls


def make_mock_gen_python_files_black_22_10_1_dev19():
"""Create `gen_python_files` mock for Black 22.10.1.dev19+gffaaf48
Also record the call made to the mock function for test verification.
This revision renamed the `gitignore` parameter to `gitignore_dict`.
"""
calls = Mock()

def gen_python_files(
paths: Iterable[Path],

Check failure on line 281 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L281

Unused argument 'paths' (unused-argument, W0613)
root: Path,

Check failure on line 282 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L282

Unused argument 'root' (unused-argument, W0613)
include: Pattern[str],

Check failure on line 283 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L283

Unused argument 'include' (unused-argument, W0613)
exclude: Pattern[str],

Check failure on line 284 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L284

Unused argument 'exclude' (unused-argument, W0613)
extend_exclude: Optional[Pattern[str]],

Check failure on line 285 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L285

Unused argument 'extend_exclude' (unused-argument, W0613)
force_exclude: Optional[Pattern[str]],

Check failure on line 286 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L286

Unused argument 'force_exclude' (unused-argument, W0613)
report: Report,

Check failure on line 287 in src/darker/tests/test_black_diff.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/tests/test_black_diff.py#L287

Unused argument 'report' (unused-argument, W0613)
gitignore_dict: Optional[Dict[Path, PathSpec]],
*,
verbose: bool,
quiet: bool,
) -> Iterator[Path]:
calls.gen_python_files = call(
gitignore_dict=gitignore_dict,
verbose=verbose,
quiet=quiet,
)
for _ in []:
yield Path()

return gen_python_files, calls


@pytest.mark.kwparametrize(
dict(
make_mock=make_mock_gen_python_files_black_21_7b1_dev8,
expect={"gitignore": None},
),
dict(
make_mock=make_mock_gen_python_files_black_21_7b1_dev9,
expect={"gitignore": None, "verbose": False, "quiet": False},
),
dict(
make_mock=make_mock_gen_python_files_black_22_10_1_dev19,
expect={"gitignore_dict": {}, "verbose": False, "quiet": False},
),
)
def test_filter_python_files_gitignore(make_mock, tmp_path, expect):
"""`filter_python_files` uses per-Black-version params to `gen_python_files`"""
gen_python_files, calls = make_mock()
with patch.object(black_diff, "gen_python_files", gen_python_files):
# end of test setup

_ = filter_python_files(set(), tmp_path, BlackConfig())

assert calls.gen_python_files.kwargs == expect


@pytest.mark.parametrize("encoding", ["utf-8", "iso-8859-1"])
@pytest.mark.parametrize("newline", ["\n", "\r\n"])
def test_run_black(encoding, newline):
Expand Down

0 comments on commit 277b6b3

Please sign in to comment.