From 4bb991cfbca82f80b8b7195ad7bb54a0a6647845 Mon Sep 17 00:00:00 2001 From: Bona Fide IT GmbH <137388433+BonaFideIT@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:18:01 +0100 Subject: [PATCH 1/4] Update test_versioned_branches.py --- tests/fixers/test_versioned_branches.py | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/fixers/test_versioned_branches.py b/tests/fixers/test_versioned_branches.py index 6106e814..ad896a30 100644 --- a/tests/fixers/test_versioned_branches.py +++ b/tests/fixers/test_versioned_branches.py @@ -263,3 +263,69 @@ def test_removed_block_internal_comment(): """, ) + + +def test_removed_block_internal_comment1(): + check_transformed( + """\ + import django + + if django.VERSION < (3, 2): + foo() + # test comment 1 + # test comment 2 + """, + """\ + import django + + # test comment 2 + """, + ) + + +def test_removed_block_internal_comment2(): + check_transformed( + """\ + import django + + # test comment 0 + if django.VERSION < (3, 2): + foo() + # test comment 1 + # test comment 2 + foo() + """, + """\ + import django + + # test comment 0 + # test comment 2 + foo() + """, + ) + + +def test_removed_block_internal_comment3(): + check_transformed( + """\ + import django + + if True: + # test comment 0 + if django.VERSION < (3, 2): + foo() + # test comment 1 + foo() + # test comment 2 + foo() + """, + """\ + import django + + if True: + # test comment 0 + foo() + # test comment 2 + foo() + """, + ) From 7ee3f78ae0870c77a2bff3608ccff1536abadd3d Mon Sep 17 00:00:00 2001 From: Tobias Funke Date: Thu, 31 Oct 2024 17:30:29 +0100 Subject: [PATCH 2/4] add tests --- tests/fixers/test_versioned_branches.py | 25 +++++++++++++++++++++++++ tests/fixers/tools.py | 13 +++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/fixers/test_versioned_branches.py b/tests/fixers/test_versioned_branches.py index ad896a30..5841815a 100644 --- a/tests/fixers/test_versioned_branches.py +++ b/tests/fixers/test_versioned_branches.py @@ -8,6 +8,7 @@ settings = Settings(target_version=(4, 0)) check_noop = partial(tools.check_noop, settings=settings) check_transformed = partial(tools.check_transformed, settings=settings) +check_error_on_transformed = partial(tools.check_error_on_transformed, settings=settings) def test_future_version_gt(): @@ -329,3 +330,27 @@ def test_removed_block_internal_comment3(): foo() """, ) + + +def test_removed_block_internal_comment_with_error(): + check_error_on_transformed( + """\ + import django + + if True: + # test comment 0 + if django.VERSION < (3, 2): + foo() + # test comment 1 + # test comment 2 + foo() + """, + """\ + import django + + if True: + # test comment 0 + # test comment 2 + foo() + """, + ) diff --git a/tests/fixers/tools.py b/tests/fixers/tools.py index 1eaa0d18..5b844033 100644 --- a/tests/fixers/tools.py +++ b/tests/fixers/tools.py @@ -21,3 +21,16 @@ def check_transformed( ast.parse(dedented_after) # check that the target is valid python code fixed = apply_fixers(dedented_before, settings=settings, filename=filename) assert fixed == dedented_after + + +def check_error_on_transformed( + before: str, after: str, settings: Settings, filename: str = "example.py" +) -> None: + dedented_before = dedent(before) + dedented_after = dedent(after) + error = None + try: + ast.parse(dedented_after) # check that the target is valid python code + except SyntaxError as ex: + error = ex + assert error is not None From 671b2bce7fcee183a00a56ad5e745cfb14be66c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:34:59 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/fixers/test_versioned_branches.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/fixers/test_versioned_branches.py b/tests/fixers/test_versioned_branches.py index 5841815a..d193f72f 100644 --- a/tests/fixers/test_versioned_branches.py +++ b/tests/fixers/test_versioned_branches.py @@ -8,7 +8,9 @@ settings = Settings(target_version=(4, 0)) check_noop = partial(tools.check_noop, settings=settings) check_transformed = partial(tools.check_transformed, settings=settings) -check_error_on_transformed = partial(tools.check_error_on_transformed, settings=settings) +check_error_on_transformed = partial( + tools.check_error_on_transformed, settings=settings +) def test_future_version_gt(): From 042d492f4cbc164799329adc13ad181b77284ffd Mon Sep 17 00:00:00 2001 From: Tobias Funke Date: Thu, 31 Oct 2024 17:40:16 +0100 Subject: [PATCH 4/4] fix pre-commit error --- tests/fixers/tools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/fixers/tools.py b/tests/fixers/tools.py index 5b844033..ca6d2786 100644 --- a/tests/fixers/tools.py +++ b/tests/fixers/tools.py @@ -26,7 +26,6 @@ def check_transformed( def check_error_on_transformed( before: str, after: str, settings: Settings, filename: str = "example.py" ) -> None: - dedented_before = dedent(before) dedented_after = dedent(after) error = None try: