From f49c0dc7416a3da5afd1d53f78c04cefbe6c78bc Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Mon, 4 Nov 2024 22:15:20 +0200 Subject: [PATCH] test: speed up `test_revision` --- src/darker/tests/test_main_revision.py | 71 ++++++++++++++------------ 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/src/darker/tests/test_main_revision.py b/src/darker/tests/test_main_revision.py index 029f8b8c5..d64dcc84f 100644 --- a/src/darker/tests/test_main_revision.py +++ b/src/darker/tests/test_main_revision.py @@ -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. @@ -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: + # 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="", @@ -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, "."]