Skip to content

Commit

Permalink
test: speed up test_revision
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Nov 4, 2024
1 parent 8889f72 commit f49c0dc
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/darker/tests/test_main_revision.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Unit tests for the ``--revision`` argument in `darker.main`"""

# pylint: disable=too-many-arguments,use-dict-literal
# pylint: disable=no-member,redefined-outer-name,too-many-arguments,use-dict-literal

import pytest

from darker.__main__ import main
from darkgraylib.testtools.git_repo_plugin import GitRepoFixture
from darkgraylib.testtools.helpers import raises_if_exception

# The following test is a bit dense, so some explanation is due.
Expand Down Expand Up @@ -43,6 +44,41 @@
# (`ORIGINAL=1`) at HEAD~2.


@pytest.fixture(scope="module")
def revision_files(request, tmp_path_factory):
"""Git repository fixture for testing `--revision`."""
with GitRepoFixture.context(request, tmp_path_factory) as repo:

Check failure on line 50 in src/darker/tests/test_main_revision.py

View workflow job for this annotation

GitHub Actions / Mypy

src/darker/tests/test_main_revision.py#L50

"type[GitRepoFixture]" has no attribute "context" [attr-defined]
# 2: HEAD~2:
paths = repo.add(
{
"+2.py": "ORIGINAL=1\n",
"+2M1.py": "ORIGINAL=1\n",
"+2-1.py": "ORIGINAL=1\n",
"+2M1-0.py": "ORIGINAL=1\n",
},
commit="First commit",
)
# 1: HEAD~1 i.e. HEAD^
paths.update(
repo.add(
{
"+2M1.py": "MODIFIED=1\n",
"+1.py": "ORIGINAL=1\n",
"+1M0.py": "ORIGINAL=1\n",
"+2-1.py": None,
"+2M1-0.py": "MODIFIED=1\n",
},
commit="Second commit",
)
)
# 0: HEAD~0 i.e. HEAD:
repo.add(
{"+1M0.py": "MODIFIED=1\n", "+2M1-0.py": None},
commit="Third commit",
)
yield paths


@pytest.mark.kwparametrize(
dict(
revision="",
Expand Down Expand Up @@ -109,39 +145,10 @@
),
dict(revision="HEAD~3", worktree_content=b"USERMOD=1\n", expect=SystemExit),
)
def test_revision(git_repo, monkeypatch, capsys, revision, worktree_content, expect):
def test_revision(revision_files, capsys, revision, worktree_content, expect):
"""``--diff`` with ``--revision`` reports correct files as modified"""
monkeypatch.chdir(git_repo.root)
# 2: HEAD~2:
paths = git_repo.add(
{
"+2.py": "ORIGINAL=1\n",
"+2M1.py": "ORIGINAL=1\n",
"+2-1.py": "ORIGINAL=1\n",
"+2M1-0.py": "ORIGINAL=1\n",
},
commit="First commit",
)
# 1: HEAD~1 i.e. HEAD^
paths.update(
git_repo.add(
{
"+2M1.py": "MODIFIED=1\n",
"+1.py": "ORIGINAL=1\n",
"+1M0.py": "ORIGINAL=1\n",
"+2-1.py": None,
"+2M1-0.py": "MODIFIED=1\n",
},
commit="Second commit",
)
)
# 0: HEAD~0 i.e. HEAD:
git_repo.add(
{"+1M0.py": "MODIFIED=1\n", "+2M1-0.py": None},
commit="Third commit",
)
# Working tree:
for path in paths.values():
for path in revision_files.values():
path.write_bytes(worktree_content)
arguments = ["--diff", "--revision", revision, "."]

Expand Down

0 comments on commit f49c0dc

Please sign in to comment.