Skip to content

Commit

Permalink
Drop the duplicated get_path_in_repo()
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Apr 7, 2022
1 parent 37f7300 commit a38fe64
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 40 deletions.
20 changes: 2 additions & 18 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
EditedLinenumsDiffer,
RevisionRange,
get_missing_at_revision,
get_path_in_repo,
git_get_content_at_revision,
git_get_modified_python_files,
git_is_repository,
Expand Down Expand Up @@ -148,7 +149,7 @@ def _isort_and_blacken_single_file( # pylint: disable=too-many-arguments
content_after_reformatting = _blacken_single_file(
root,
relative_path_in_rev2,
_get_path_in_repo(relative_path_in_rev2),
get_path_in_repo(relative_path_in_rev2),
edited_linenums_differ,
rev2_content,
rev2_isorted,
Expand Down Expand Up @@ -261,23 +262,6 @@ def _blacken_single_file( # pylint: disable=too-many-arguments,too-many-locals
return last_successful_reformat


def _get_path_in_repo(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
4 changes: 2 additions & 2 deletions src/darker/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _with_common_ancestor(cls, rev1: str, rev2: str, cwd: Path) -> "RevisionRang
return cls(rev1 if common_ancestor == rev1_hash else common_ancestor, rev2)


def get_rev1_path(path: Path) -> Path:
def get_path_in_repo(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
Expand All @@ -188,7 +188,7 @@ def get_rev1_path(path: Path) -> Path:


def should_reformat_file(path: Path) -> bool:
return path.exists() and get_rev1_path(path).suffix == ".py"
return path.exists() and get_path_in_repo(path).suffix == ".py"


@lru_cache(maxsize=1)
Expand Down
6 changes: 3 additions & 3 deletions src/darker/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ def test_revisionrange_parse_with_common_ancestor(git_repo, revrange, expect):
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 = git.get_rev1_path(Path(path))
def test_get_path_in_repo(path, expect):
"""``get_path_in_repo`` drops two suffixes from ``.py.<HASH>.tmp``"""
result = git.get_path_in_repo(Path(path))

assert result == Path(expect)

Expand Down
17 changes: 0 additions & 17 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,6 @@ def test_blacken_single_file(
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_path_in_repo(path, expect):
"""``_get_path_in_repo`` drops two suffixes from ``.py.<HASH>.tmp``"""
result = darker.__main__._get_path_in_repo(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

0 comments on commit a38fe64

Please sign in to comment.