Skip to content

Commit 8a37b0a

Browse files
authored
All tests will validate codemod registration (#656)
* remove unnecessary BaseSemgrepTest * move int test validate registration to setup class * all unit tests validate codemod registration
1 parent 24b5351 commit 8a37b0a

24 files changed

+78
-88
lines changed

src/codemodder/codemods/test/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
BaseCodemodTest,
55
BaseDjangoCodemodTest,
66
BaseSASTCodemodTest,
7-
BaseSemgrepCodemodTest,
87
DiffError,
98
)

src/codemodder/codemods/test/integration_utils.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
import jsonschema
1212

13-
from codemodder import __version__, registry
13+
from codemodder import __version__
1414
from core_codemods.sonar.api import process_sonar_findings
1515

16+
from .utils import validate_codemod_registration
1617
from .validations import execute_code
1718

1819

@@ -47,7 +48,11 @@ class BaseIntegrationTest(DependencyTestMixin):
4748

4849
@classmethod
4950
def setup_class(cls):
50-
cls.codemod_registry = registry.load_registered_codemods()
51+
codemod_id = (
52+
cls.codemod().id if isinstance(cls.codemod, type) else cls.codemod.id
53+
)
54+
cls.codemod_instance = validate_codemod_registration(codemod_id)
55+
5156
cls.output_path = tempfile.mkstemp()[1]
5257
cls.code_dir = tempfile.mkdtemp()
5358

@@ -86,20 +91,6 @@ def teardown_class(cls):
8691
if cls.requirements_file_name:
8792
pathlib.Path(cls.dependency_path).unlink(missing_ok=True)
8893

89-
def setup_method(self):
90-
try:
91-
codemod_id = (
92-
self.codemod().id if isinstance(self.codemod, type) else self.codemod.id
93-
)
94-
# This is how we ensure that the codemod is actually in the registry
95-
self.codemod_instance = self.codemod_registry.match_codemods(
96-
codemod_include=[codemod_id]
97-
)[0]
98-
except IndexError as exc:
99-
raise IndexError(
100-
"You must register the codemod to a CodemodCollection."
101-
) from exc
102-
10394
def _assert_run_fields(self, run, output_path):
10495
assert run["vendor"] == "pixee"
10596
assert run["tool"] == "codemodder-python"
@@ -255,7 +246,11 @@ class SonarIntegrationTest(BaseIntegrationTest):
255246

256247
@classmethod
257248
def setup_class(cls):
258-
cls.codemod_registry = registry.load_registered_codemods()
249+
codemod_id = (
250+
cls.codemod().id if isinstance(cls.codemod, type) else cls.codemod.id
251+
)
252+
cls.codemod_instance = validate_codemod_registration(codemod_id)
253+
259254
cls.output_path = tempfile.mkstemp()[1]
260255
cls.code_dir = SAMPLES_DIR
261256
cls.code_filename = os.path.relpath(cls.code_path, SAMPLES_DIR)

src/codemodder/codemods/test/utils.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55

66
import mock
77

8+
from codemodder import registry
9+
from codemodder.codemods.api import BaseCodemod
810
from codemodder.context import CodemodExecutionContext
911
from codemodder.diff import create_diff
1012
from codemodder.providers import load_providers
11-
from codemodder.registry import CodemodCollection, CodemodRegistry
12-
from codemodder.semgrep import run as semgrep_run
13+
14+
15+
def validate_codemod_registration(codemod_id: str) -> BaseCodemod:
16+
codemod_registry = registry.load_registered_codemods()
17+
try:
18+
return codemod_registry.match_codemods(codemod_include=[codemod_id])[0]
19+
except IndexError as exc:
20+
raise IndexError(
21+
"You must register the codemod to a CodemodCollection."
22+
) from exc
1323

1424

1525
class DiffError(Exception):
@@ -32,9 +42,14 @@ class BaseCodemodTest:
3242
def file_extension(self) -> str:
3343
return "py"
3444

45+
@classmethod
46+
def setup_class(cls):
47+
codemod_id = (
48+
cls.codemod().id if isinstance(cls.codemod, type) else cls.codemod.id
49+
)
50+
cls.codemod = validate_codemod_registration(codemod_id)
51+
3552
def setup_method(self):
36-
if isinstance(self.codemod, type):
37-
self.codemod = self.codemod()
3853
self.changeset = []
3954

4055
def run_and_assert(
@@ -126,25 +141,6 @@ def run_and_assert_filepath(
126141
)
127142

128143

129-
class BaseSemgrepCodemodTest(BaseCodemodTest):
130-
@classmethod
131-
def setup_class(cls):
132-
collection = CodemodCollection(
133-
origin="pixee",
134-
codemods=[cls.codemod],
135-
)
136-
cls.registry = CodemodRegistry()
137-
cls.registry.add_codemod_collection(collection)
138-
139-
def results_by_id_filepath(self, input_code, file_path):
140-
with open(file_path, "w", encoding="utf-8") as tmp_file:
141-
tmp_file.write(dedent(input_code))
142-
143-
name = self.codemod.name
144-
results = self.registry.match_codemods(codemod_include=[name])
145-
return semgrep_run(self.execution_context, results[0].yaml_files)
146-
147-
148144
class BaseDjangoCodemodTest(BaseCodemodTest):
149145
def create_dir_structure(self, tmpdir):
150146
django_root = Path(tmpdir) / "mysite"

tests/codemods/test_add_requests_timeouts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from codemodder.codemods.test import BaseSemgrepCodemodTest
3+
from codemodder.codemods.test import BaseCodemodTest
44
from core_codemods.add_requests_timeouts import (
55
AddRequestsTimeouts,
66
TransformAddRequestsTimeouts,
@@ -10,7 +10,7 @@
1010
TIMEOUT = TransformAddRequestsTimeouts.DEFAULT_TIMEOUT
1111

1212

13-
class TestAddRequestsTimeouts(BaseSemgrepCodemodTest):
13+
class TestAddRequestsTimeouts(BaseCodemodTest):
1414
codemod = AddRequestsTimeouts
1515

1616
@pytest.mark.parametrize("method", METHODS)

tests/codemods/test_django_json_response_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from codemodder.codemods.test import BaseSemgrepCodemodTest
1+
from codemodder.codemods.test import BaseCodemodTest
22
from core_codemods.django_json_response_type import DjangoJsonResponseType
33

44

5-
class TestDjangoJsonResponseType(BaseSemgrepCodemodTest):
5+
class TestDjangoJsonResponseType(BaseCodemodTest):
66
codemod = DjangoJsonResponseType
77

88
def test_name(self):

tests/codemods/test_enable_jinja2_autoescape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
22

3-
from codemodder.codemods.test import BaseSemgrepCodemodTest
3+
from codemodder.codemods.test import BaseCodemodTest
44
from core_codemods.enable_jinja2_autoescape import EnableJinja2Autoescape
55

66

7-
class TestEnableJinja2Autoescape(BaseSemgrepCodemodTest):
7+
class TestEnableJinja2Autoescape(BaseCodemodTest):
88
codemod = EnableJinja2Autoescape
99

1010
def test_name(self):

tests/codemods/test_fix_deprecated_logging_warn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
22

3-
from codemodder.codemods.test import BaseSemgrepCodemodTest
3+
from codemodder.codemods.test import BaseCodemodTest
44
from core_codemods.fix_deprecated_logging_warn import FixDeprecatedLoggingWarn
55

66

7-
class TestFixDeprecatedLoggingWarn(BaseSemgrepCodemodTest):
7+
class TestFixDeprecatedLoggingWarn(BaseCodemodTest):
88
codemod = FixDeprecatedLoggingWarn
99

1010
@pytest.mark.parametrize(

tests/codemods/test_fix_hasattr_call.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from codemodder.codemods.test import BaseSemgrepCodemodTest
1+
from codemodder.codemods.test import BaseCodemodTest
22
from core_codemods.fix_hasattr_call import TransformFixHasattrCall
33

44

5-
class TestTransformFixHasattrCall(BaseSemgrepCodemodTest):
5+
class TestTransformFixHasattrCall(BaseCodemodTest):
66
codemod = TransformFixHasattrCall
77

88
def test_name(self):

tests/codemods/test_harden_pyyaml.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import pytest
22
import yaml
33

4-
from codemodder.codemods.test import BaseSemgrepCodemodTest
4+
from codemodder.codemods.test import BaseCodemodTest
55
from core_codemods.harden_pyyaml import HardenPyyaml
66

77
UNSAFE_LOADERS = yaml.loader.__all__.copy() # type: ignore
88
UNSAFE_LOADERS.remove("SafeLoader")
99
loaders = pytest.mark.parametrize("loader", UNSAFE_LOADERS)
1010

1111

12-
class TestHardenPyyaml(BaseSemgrepCodemodTest):
12+
class TestHardenPyyaml(BaseCodemodTest):
1313
codemod = HardenPyyaml
1414

1515
def test_name(self):
@@ -114,7 +114,7 @@ def test_default_loader_unsafe(self, tmpdir):
114114
self.run_and_assert(tmpdir, input_code, expected)
115115

116116

117-
class TestHardenPyyamlClassInherit(BaseSemgrepCodemodTest):
117+
class TestHardenPyyamlClassInherit(BaseCodemodTest):
118118
codemod = HardenPyyaml
119119

120120
def test_safe_loader(self, tmpdir):

tests/codemods/test_harden_ruamel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
22

3-
from codemodder.codemods.test import BaseSemgrepCodemodTest
3+
from codemodder.codemods.test import BaseCodemodTest
44
from core_codemods.harden_ruamel import HardenRuamel
55

66

7-
class TestHardenRuamel(BaseSemgrepCodemodTest):
7+
class TestHardenRuamel(BaseCodemodTest):
88
codemod = HardenRuamel
99

1010
def test_name(self):

0 commit comments

Comments
 (0)