Skip to content

Commit

Permalink
remove dedent and \ from unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Feb 6, 2024
1 parent 2e6b6b4 commit ba8b12f
Show file tree
Hide file tree
Showing 33 changed files with 441 additions and 465 deletions.
8 changes: 4 additions & 4 deletions tests/codemods/test_combine_startswith_endswith.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def test_name(self):

@each_func
def test_combine(self, tmpdir, func):
input_code = f"""\
input_code = f"""
x = "foo"
x.{func}("foo") or x.{func}("f")
"""
expected = f"""\
expected = f"""
x = "foo"
x.{func}(("foo", "f"))
"""
Expand All @@ -40,11 +40,11 @@ def test_no_change(self, tmpdir, code):
def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """\
) = """
x = "foo"
x.startswith("foo") or x.startswith("f")
"""
lines_to_exclude = [2]
lines_to_exclude = [3]
self.run_and_assert(
tmpdir,
input_code,
Expand Down
27 changes: 13 additions & 14 deletions tests/codemods/test_django_json_response_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from core_codemods.django_json_response_type import DjangoJsonResponseType
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest
from textwrap import dedent


class TestDjangoJsonResponseType(BaseSemgrepCodemodTest):
Expand All @@ -10,78 +9,78 @@ def test_name(self):
assert self.codemod.name == "django-json-response-type"

def test_simple(self, tmpdir):
input_code = """\
input_code = """
from django.http import HttpResponse
import json
def foo(request):
json_response = json.dumps({ "user_input": request.GET.get("input") })
return HttpResponse(json_response)
"""
expected = """\
expected = """
from django.http import HttpResponse
import json
def foo(request):
json_response = json.dumps({ "user_input": request.GET.get("input") })
return HttpResponse(json_response, content_type="application/json")
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_alias(self, tmpdir):
input_code = """\
input_code = """
from django.http import HttpResponse as response
import json as jsan
def foo(request):
json_response = jsan.dumps({ "user_input": request.GET.get("input") })
return response(json_response)
"""
expected = """\
expected = """
from django.http import HttpResponse as response
import json as jsan
def foo(request):
json_response = jsan.dumps({ "user_input": request.GET.get("input") })
return response(json_response, content_type="application/json")
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_direct(self, tmpdir):
input_code = """\
input_code = """
from django.http import HttpResponse
import json
def foo(request):
return HttpResponse(json.dumps({ "user_input": request.GET.get("input") }))
"""
expected = """\
expected = """
from django.http import HttpResponse
import json
def foo(request):
return HttpResponse(json.dumps({ "user_input": request.GET.get("input") }), content_type="application/json")
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_content_type_set(self, tmpdir):
input_code = """\
input_code = """
from django.http import HttpResponse
import json
def foo(request):
json_response = json.dumps({ "user_input": request.GET.get("input") })
return HttpResponse(json_response, content_type='application/json')
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)

def test_no_json_input(self, tmpdir):
input_code = """\
input_code = """
from django.http import HttpResponse
import json
def foo(request):
dict_reponse = { "user_input": request.GET.get("input") }
return HttpResponse(dict_response)
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)
25 changes: 12 additions & 13 deletions tests/codemods/test_django_receiver_on_top.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from core_codemods.django_receiver_on_top import DjangoReceiverOnTop
from tests.codemods.base_codemod_test import BaseCodemodTest
from textwrap import dedent


class TestDjangoReceiverOnTop(BaseCodemodTest):
Expand All @@ -10,69 +9,69 @@ def test_name(self):
assert self.codemod.name == "django-receiver-on-top"

def test_simple(self, tmpdir):
input_code = """\
input_code = """
from django.dispatch import receiver
@csrf_exempt
@receiver(request_finished)
def foo():
pass
"""
expected = """\
expected = """
from django.dispatch import receiver
@receiver(request_finished)
@csrf_exempt
def foo():
pass
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_simple_alias(self, tmpdir):
input_code = """\
input_code = """
from django.dispatch import receiver as rec
@csrf_exempt
@rec(request_finished)
def foo():
pass
"""
expected = """\
expected = """
from django.dispatch import receiver as rec
@rec(request_finished)
@csrf_exempt
def foo():
pass
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_no_receiver(self, tmpdir):
input_code = """\
input_code = """
@csrf_exempt
def foo():
pass
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)

def test_receiver_but_not_djangos(self, tmpdir):
input_code = """\
input_code = """
from not_django import receiver
@csrf_exempt
@receiver(request_finished)
def foo():
pass
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)

def test_receiver_on_top(self, tmpdir):
input_code = """\
input_code = """
from django.dispatch import receiver
@receiver(request_finished)
@csrf_exempt
def foo():
pass
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)
31 changes: 15 additions & 16 deletions tests/codemods/test_exception_without_raise.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from tests.codemods.base_codemod_test import BaseCodemodTest
from core_codemods.exception_without_raise import ExceptionWithoutRaise
from textwrap import dedent


class TestExceptionWithoutRaise(BaseCodemodTest):
Expand All @@ -10,54 +9,54 @@ def test_name(self):
assert self.codemod.name == "exception-without-raise"

def test_simple(self, tmpdir):
input_code = """\
input_code = """
ValueError
"""
expected = """\
expected = """
raise ValueError
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_simple_call(self, tmpdir):
input_code = """\
input_code = """
ValueError("Bad value!")
"""
expected = """\
expected = """
raise ValueError("Bad value!")
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_alias(self, tmpdir):
input_code = """\
input_code = """
from decimal import Overflow as error
error
"""
expected = """\
expected = """
from decimal import Overflow as error
raise error
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected))
self.run_and_assert(tmpdir, input_code, expected)

def test_unknown_exception(self, tmpdir):
input_code = """\
input_code = """
Something
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)

def test_raised_exception(self, tmpdir):
input_code = """\
input_code = """
raise ValueError
"""
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code))
self.run_and_assert(tmpdir, input_code, input_code)

def test_exclude_line(self, tmpdir):
input_code = (
expected
) = """\
) = """
print(1)
ValueError("Bad value!")
"""
lines_to_exclude = [2]
lines_to_exclude = [3]
self.run_and_assert(
tmpdir,
input_code,
Expand Down
Loading

0 comments on commit ba8b12f

Please sign in to comment.