Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only apply Black excludes to directories (Sourcery refactored) #218

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals

"""
src = root / relative_path
rev1_relative_path = _get_rev1_path(relative_path)
edited_linenums_differ = EditedLinenumsDiffer(root, revrange)

# 4. run black
Expand Down Expand Up @@ -156,7 +157,7 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
# 2. diff the given revision and worktree for the file
# 3. extract line numbers in the edited to-file for changed lines
edited_linenums = edited_linenums_differ.revision_vs_lines(
relative_path, rev2_isorted, context_lines
rev1_relative_path, rev2_isorted, context_lines
)
if enable_isort and not edited_linenums and rev2_isorted == rev2_content:
logger.debug("No changes in %s after isort", src)
Expand Down Expand Up @@ -199,6 +200,23 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
return last_successful_reformat


def _get_rev1_path(path: Path) -> Path:
"""Return the relative path to the file in the old revision

This is usually the same as the relative path on the command line. But in the
special case of VSCode temporary files (like ``file.py.12345.tmp``), we actually
want to diff against the corresponding ``.py`` file instead.

"""
if path.suffixes[-3::2] != [".py", ".tmp"]:
# The file name is not like `*.py.<HASH>.tmp`. Return it as such.
return path
# This is a VSCode temporary file. Drop the hash and the `.tmp` suffix to get the
# original file name for retrieving the previous revision to diff against.
path_with_hash = path.with_suffix("")
return path_with_hash.with_suffix("")


def modify_file(path: Path, new_content: TextDocument) -> None:
"""Write new content to a file and inform the user by logging"""
logger.info("Writing %s bytes into %s", len(new_content.string), path)
Expand Down
83 changes: 81 additions & 2 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Unit tests for :mod:`darker.__main__`"""

# pylint: disable=unused-argument
# pylint: disable=unused-argument,too-many-arguments,use-dict-literal,protected-access

import logging
import re
Expand Down Expand Up @@ -331,6 +331,85 @@ def test_format_edited_parts_historical(git_repo, rev1, rev2, expect):
assert list(result) == [(paths["a.py"], a_py[x[0]], a_py[x[1]]) for x in expect]


@pytest.mark.kwparametrize(
dict(),
dict(relative_path="file.py.12345.tmp"),
dict(
rev2_content="import modified\n\nprint( original )\n",
rev2_isorted="import modified\n\nprint( original )\n",
expect="import modified\n\nprint( original )\n",
),
dict(
rev2_content="import original\n\nprint(modified )\n",
rev2_isorted="import original\n\nprint(modified )\n",
expect="import original\n\nprint(modified)\n",
),
dict(
relative_path="file.py.12345.tmp",
rev2_content="import modified\n\nprint( original )\n",
rev2_isorted="import modified\n\nprint( original )\n",
expect="import modified\n\nprint( original )\n",
),
dict(
relative_path="file.py.12345.tmp",
rev2_content="import original\n\nprint(modified )\n",
rev2_isorted="import original\n\nprint(modified )\n",
expect="import original\n\nprint(modified)\n",
),
relative_path="file.py",
rev1="HEAD",
rev2=":WORKTREE",
rev2_content="import original\nprint( original )\n",
rev2_isorted="import original\nprint( original )\n",
enable_isort=False,
black_config={},
expect="import original\nprint( original )\n",
)
def test_reformat_single_file(
git_repo,
relative_path,
rev1,
rev2,
rev2_content,
rev2_isorted,
enable_isort,
black_config,
expect,
):
"""Test for ``_reformat_single_file``"""
git_repo.add(
{"file.py": "import original\nprint( original )\n"}, commit="Initial commit"
)
result = darker.__main__._reformat_single_file(
git_repo.root,
Path(relative_path),
RevisionRange(rev1, rev2),
TextDocument(rev2_content),
TextDocument(rev2_isorted),
enable_isort,
black_config,
)

assert result.string == expect


@pytest.mark.kwparametrize(
dict(path="file.py", expect="file.py"),
dict(path="subdir/file.py", expect="subdir/file.py"),
dict(path="file.py.12345.tmp", expect="file.py"),
dict(path="subdir/file.py.12345.tmp", expect="subdir/file.py"),
dict(path="file.py.tmp", expect="file.py.tmp"),
dict(path="subdir/file.py.tmp", expect="subdir/file.py.tmp"),
dict(path="file.12345.tmp", expect="file.12345.tmp"),
dict(path="subdir/file.12345.tmp", expect="subdir/file.12345.tmp"),
)
def test_get_rev1_path(path, expect):
"""``_get_rev1_path`` drops two suffixes from ``.py.<HASH>.tmp``"""
result = darker.__main__._get_rev1_path(Path(path))

assert result == Path(expect)


@pytest.mark.kwparametrize(
dict(arguments=["--diff"], expect_stdout=A_PY_DIFF_BLACK),
dict(arguments=["--isort"], expect_a_py=A_PY_BLACK_ISORT),
Expand Down Expand Up @@ -439,7 +518,7 @@ def test_main(
expect_retval,
root_as_cwd,
tmp_path_factory,
): # pylint: disable=too-many-arguments
):
"""Main function outputs diffs and modifies files correctly"""
if root_as_cwd:
cwd = git_repo.root
Expand Down