From 3dd4d736a54c12891dea81babcc9cba09ce163a3 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Fri, 29 Dec 2023 09:04:50 -0300 Subject: [PATCH] new remove breakpoint codemod --- .../test_remove_debug_breakpoint.py | 20 +++++++++++++++++++ src/codemodder/scripts/generate_docs.py | 4 ++++ src/core_codemods/__init__.py | 2 ++ .../pixee_python_remove-debug-breakpoint.md | 7 +++++++ src/core_codemods/remove_debug_breakpoint.py | 4 ++-- tests/samples/remove_debug_breakpoint.py | 3 +++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 integration_tests/test_remove_debug_breakpoint.py create mode 100644 src/core_codemods/docs/pixee_python_remove-debug-breakpoint.md create mode 100644 tests/samples/remove_debug_breakpoint.py diff --git a/integration_tests/test_remove_debug_breakpoint.py b/integration_tests/test_remove_debug_breakpoint.py new file mode 100644 index 000000000..2bfe01202 --- /dev/null +++ b/integration_tests/test_remove_debug_breakpoint.py @@ -0,0 +1,20 @@ +from core_codemods.remove_debug_breakpoint import RemoveDebugBreakpoint +from integration_tests.base_test import ( + BaseIntegrationTest, + original_and_expected_from_code_path, +) + + +class TestRemoveDebugBreakpoint(BaseIntegrationTest): + codemod = RemoveDebugBreakpoint + code_path = "tests/samples/remove_debug_breakpoint.py" + original_code, _ = original_and_expected_from_code_path(code_path, []) + expected_new_code = """ +print("hello") +print("world") +""".lstrip() + expected_diff = ( + '--- \n+++ \n@@ -1,3 +1,2 @@\n print("hello")\n-breakpoint()\n print("world")\n' + ) + expected_line_change = "2" + change_description = RemoveDebugBreakpoint.CHANGE_DESCRIPTION diff --git a/src/codemodder/scripts/generate_docs.py b/src/codemodder/scripts/generate_docs.py index e2e77fb63..4cdc11b57 100644 --- a/src/codemodder/scripts/generate_docs.py +++ b/src/codemodder/scripts/generate_docs.py @@ -178,6 +178,10 @@ class DocMetadata: importance="Low", guidance_explained="Since literals and new objects have their own identities, comparisons against them using `is` operators are most likely a bug and thus we deem the change safe.", ), + "remove-debug-breakpoint": DocMetadata( + importance="Medium", + guidance_explained="Breakpoints are used for development debugging and can easily be forgotten before deploying code.", + ), } diff --git a/src/core_codemods/__init__.py b/src/core_codemods/__init__.py index a4e1cdba0..347ab2e55 100644 --- a/src/core_codemods/__init__.py +++ b/src/core_codemods/__init__.py @@ -38,6 +38,7 @@ from .sql_parameterization import SQLQueryParameterization from .exception_without_raise import ExceptionWithoutRaise from .literal_or_new_object_identity import LiteralOrNewObjectIdentity +from .remove_debug_breakpoint import RemoveDebugBreakpoint registry = CodemodCollection( origin="pixee", @@ -82,5 +83,6 @@ FlaskJsonResponseType, ExceptionWithoutRaise, LiteralOrNewObjectIdentity, + RemoveDebugBreakpoint, ], ) diff --git a/src/core_codemods/docs/pixee_python_remove-debug-breakpoint.md b/src/core_codemods/docs/pixee_python_remove-debug-breakpoint.md new file mode 100644 index 000000000..a4d06e339 --- /dev/null +++ b/src/core_codemods/docs/pixee_python_remove-debug-breakpoint.md @@ -0,0 +1,7 @@ +This codemod removes any calls to `breakpoint()` or `pdb.set_trace()` which should only be used for active debugging and not in production code. + +```diff + print("hello") +- breakpoint() + print("world") +``` diff --git a/src/core_codemods/remove_debug_breakpoint.py b/src/core_codemods/remove_debug_breakpoint.py index 8879912bb..21c324fb6 100644 --- a/src/core_codemods/remove_debug_breakpoint.py +++ b/src/core_codemods/remove_debug_breakpoint.py @@ -6,9 +6,9 @@ class RemoveDebugBreakpoint(BaseCodemod, NameResolutionMixin, AncestorPatternsMixin): NAME = "remove-debug-breakpoint" - SUMMARY = "TODORemove Module-level Global Call" + SUMMARY = "Remove Breakpoint" REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW - DESCRIPTION = "TODORemove Lines with `global` keyword at Module Level" + DESCRIPTION = "Remove calls to builtin `breakpoint` or `pdb.set_trace." REFERENCES: list = [] def leave_Expr( diff --git a/tests/samples/remove_debug_breakpoint.py b/tests/samples/remove_debug_breakpoint.py new file mode 100644 index 000000000..3e9f54a39 --- /dev/null +++ b/tests/samples/remove_debug_breakpoint.py @@ -0,0 +1,3 @@ +print("hello") +breakpoint() +print("world")