diff --git a/src/codemodder/codemods/test/utils.py b/src/codemodder/codemods/test/utils.py index 44246c512..59242a5d0 100644 --- a/src/codemodder/codemods/test/utils.py +++ b/src/codemodder/codemods/test/utils.py @@ -225,5 +225,5 @@ def run_and_assert( def assert_findings(self, changes: list[Change]): assert all( - x.findings is not None for x in changes + x.findings for x in changes ), f"Expected all changes to have findings: {changes}" diff --git a/src/core_codemods/django_receiver_on_top.py b/src/core_codemods/django_receiver_on_top.py index 62c1699bc..26aad7be3 100644 --- a/src/core_codemods/django_receiver_on_top.py +++ b/src/core_codemods/django_receiver_on_top.py @@ -33,7 +33,8 @@ def leave_FunctionDef( new_decorators.extend( d for d in original_node.decorators if d != receiver ) - self.report_change(original_node) + for decorator in new_decorators: + self.report_change(decorator) return updated_node.with_changes(decorators=new_decorators) return updated_node diff --git a/src/core_codemods/fix_assert_tuple.py b/src/core_codemods/fix_assert_tuple.py index d0c4a7914..2209e7336 100644 --- a/src/core_codemods/fix_assert_tuple.py +++ b/src/core_codemods/fix_assert_tuple.py @@ -51,6 +51,7 @@ def _report_new_lines( Change( lineNumber=(line_number := start_line + idx), description=self.change_description, + # For now we can only link the finding to the first line changed findings=self.file_context.get_findings_for_location(line_number), ) ) diff --git a/src/core_codemods/remove_assertion_in_pytest_raises.py b/src/core_codemods/remove_assertion_in_pytest_raises.py index 9c7aca474..6067d10f6 100644 --- a/src/core_codemods/remove_assertion_in_pytest_raises.py +++ b/src/core_codemods/remove_assertion_in_pytest_raises.py @@ -137,6 +137,7 @@ def leave_With( body=[cst.SimpleStatementLine(body=[cst.Pass()])] ) ) + # TODO: need to report change for each line changed self.report_change(original_node) return cst.FlattenSentinel([new_with, *assert_stmts]) diff --git a/tests/codemods/sonar/test_sonar_break_or_continue_out_of_loop.py b/tests/codemods/sonar/test_sonar_break_or_continue_out_of_loop.py index 2dd9a760f..4e07a4aea 100644 --- a/tests/codemods/sonar/test_sonar_break_or_continue_out_of_loop.py +++ b/tests/codemods/sonar/test_sonar_break_or_continue_out_of_loop.py @@ -14,11 +14,11 @@ def test_name(self): assert self.codemod.name == "break-or-continue-out-of-loop" def test_simple(self, tmpdir): - input_code = """ + input_code = """\ def f(): continue """ - expected = """ + expected = """\ def f(): pass """ diff --git a/tests/codemods/sonar/test_sonar_django_model_without_dunder_str.py b/tests/codemods/sonar/test_sonar_django_model_without_dunder_str.py index 7095509c3..d4074ae21 100644 --- a/tests/codemods/sonar/test_sonar_django_model_without_dunder_str.py +++ b/tests/codemods/sonar/test_sonar_django_model_without_dunder_str.py @@ -15,14 +15,14 @@ def test_name(self): assert self.codemod.id == "sonar:python/django-model-without-dunder-str" def test_simple(self, tmpdir): - input_code = """ + input_code = """\ from django.db import models class User(models.Model): name = models.CharField(max_length=100) phone = models.IntegerField(blank=True) """ - expected = """ + expected = """\ from django.db import models class User(models.Model): diff --git a/tests/codemods/sonar/test_sonar_django_receiver_on_top.py b/tests/codemods/sonar/test_sonar_django_receiver_on_top.py index 9174d4517..513906cef 100644 --- a/tests/codemods/sonar/test_sonar_django_receiver_on_top.py +++ b/tests/codemods/sonar/test_sonar_django_receiver_on_top.py @@ -11,6 +11,11 @@ class TestSonarDjangoReceiverOnTop(BaseSASTCodemodTest): def test_name(self): assert self.codemod.name == "django-receiver-on-top" + def assert_findings(self, changes): + # For now we can only link the finding to the line with the receiver decorator + assert changes[0].findings + assert not changes[1].findings + def test_simple(self, tmpdir): input_code = """ from django.dispatch import receiver @@ -43,4 +48,6 @@ def foo(): } ] } - self.run_and_assert(tmpdir, input_code, expected, results=json.dumps(issues)) + self.run_and_assert( + tmpdir, input_code, expected, results=json.dumps(issues), num_changes=2 + ) diff --git a/tests/codemods/sonar/test_sonar_fix_assert_tuple.py b/tests/codemods/sonar/test_sonar_fix_assert_tuple.py index a766c9f26..3d371d138 100644 --- a/tests/codemods/sonar/test_sonar_fix_assert_tuple.py +++ b/tests/codemods/sonar/test_sonar_fix_assert_tuple.py @@ -11,6 +11,12 @@ class TestSonarFixAssertTuple(BaseSASTCodemodTest): def test_name(self): assert self.codemod.name == "fix-assert-tuple" + def assert_findings(self, changes): + # For now we can only link the finding to the first line changed + assert changes[0].findings + assert not changes[1].findings + assert not changes[2].findings + def test_simple(self, tmpdir): input_code = """ assert (1,2,3) diff --git a/tests/codemods/sonar/test_sonar_remove_assertion_in_pytest_raises.py b/tests/codemods/sonar/test_sonar_remove_assertion_in_pytest_raises.py index e81d31b8f..ca9eae0a0 100644 --- a/tests/codemods/sonar/test_sonar_remove_assertion_in_pytest_raises.py +++ b/tests/codemods/sonar/test_sonar_remove_assertion_in_pytest_raises.py @@ -13,6 +13,9 @@ class TestRemoveAssertionInPytestRaises(BaseSASTCodemodTest): def test_name(self): assert self.codemod.name == "remove-assertion-in-pytest-raises" + def assert_findings(self, changes): + assert not all(x.findings for x in changes) + def test_simple(self, tmpdir): input_code = """ import pytest diff --git a/tests/codemods/test_django_receiver_on_top.py b/tests/codemods/test_django_receiver_on_top.py index 69fb7b92e..3fccfc101 100644 --- a/tests/codemods/test_django_receiver_on_top.py +++ b/tests/codemods/test_django_receiver_on_top.py @@ -25,7 +25,7 @@ def foo(): def foo(): pass """ - self.run_and_assert(tmpdir, input_code, expected) + self.run_and_assert(tmpdir, input_code, expected, num_changes=2) def test_simple_alias(self, tmpdir): input_code = """ @@ -44,7 +44,7 @@ def foo(): def foo(): pass """ - self.run_and_assert(tmpdir, input_code, expected) + self.run_and_assert(tmpdir, input_code, expected, num_changes=2) def test_no_receiver(self, tmpdir): input_code = """