Skip to content

Commit

Permalink
Compare VSCode tmp files against original
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Oct 6, 2021
1 parent 80a7339 commit ed275b4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
21 changes: 20 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,24 @@ 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("")
original_path = path_with_hash.with_suffix("")
return original_path


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

0 comments on commit ed275b4

Please sign in to comment.