Skip to content

Commit

Permalink
fix findings assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jul 24, 2024
1 parent bcf146a commit 9db597e
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/codemodder/codemods/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
3 changes: 2 additions & 1 deletion src/core_codemods/django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/core_codemods/fix_assert_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
)
Expand Down
1 change: 1 addition & 0 deletions src/core_codemods/remove_assertion_in_pytest_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion tests/codemods/sonar/test_sonar_django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
6 changes: 6 additions & 0 deletions tests/codemods/sonar/test_sonar_fix_assert_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_django_receiver_on_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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 = """
Expand Down

0 comments on commit 9db597e

Please sign in to comment.