Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to use .name() and rely on wrapper id #51

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions integration_tests/base_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# pylint: disable=no-member
# pylint: disable=no-member, protected-access, attribute-defined-outside-init
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ but also maybe still a code smell that indicates we can clean up the class hierarchy a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually because this integration test code is:

  1. calling on _codemods. I didn't want to write a class method just for tests calling it
  2. bc pylint isn't good at understanding that defining something inside setup_method in pytest is kinda the equivalent of defining it in an init method.

import git
import json
import os
import pathlib
import subprocess

from codemodder import __VERSION__
from codemodder import registry


SAMPLES_DIR = "tests/samples"
Expand Down Expand Up @@ -51,22 +52,33 @@ class BaseIntegrationTest(DependencyTestMixin, CleanRepoMixin):
_lines = []
num_changed_files = 1

@classmethod
def setup_class(cls):
cls.codemod_registry = registry.load_registered_codemods()

def setup_method(self):
self.codemod_wrapper = [
cmod
for cmod in self.codemod_registry._codemods
if cmod.codemod == self.codemod
][0]

def _assert_run_fields(self, run, output_path):
assert run["vendor"] == "pixee"
assert run["tool"] == "codemodder-python"
assert run["version"] == __VERSION__
assert run["elapsed"] != ""
assert (
run["commandLine"]
== f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod.name()}"
== f"codemodder {SAMPLES_DIR} --output {output_path} --codemod-include={self.codemod_wrapper.name}"
)
assert run["directory"] == os.path.abspath(SAMPLES_DIR)
assert run["sarifs"] == []

def _assert_results_fields(self, results, output_path):
assert len(results) == 1
result = results[0]
assert result["codemod"] == self.codemod.id()
assert result["codemod"] == self.codemod_wrapper.id
assert len(result["changeset"]) == self.num_changed_files

# A codemod may change multiple files. For now we will
Expand Down Expand Up @@ -123,7 +135,7 @@ def test_file_rewritten(self):
SAMPLES_DIR,
"--output",
self.output_path,
f"--codemod-include={self.codemod.name()}",
f"--codemod-include={self.codemod_wrapper.name}",
]

self.check_code_before()
Expand Down
5 changes: 0 additions & 5 deletions src/codemodder/codemods/base_codemod.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ def name(cls):
# pylint: disable=no-member
return cls.METADATA.NAME

@classmethod
def id(cls):
# pylint: disable=no-member
return f"pixee:python/{cls.name()}"

@property
def should_transform(self):
return True
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_django_debug_flag_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class TestDjangoDebugFlagOn(BaseDjangoCodemodTest):
codemod = DjangoDebugFlagOn

def test_rule_ids(self):
assert self.codemod.METADATA.NAME == "django-debug-flag-on"
def test_name(self):
assert self.codemod.name() == "django-debug-flag-on"

def test_settings_dot_py(self, tmpdir):
django_root, settings_folder = self.create_dir_structure(tmpdir)
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_django_session_cookie_secure_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class TestDjangoSessionSecureCookieOff(BaseDjangoCodemodTest):
codemod = DjangoSessionCookieSecureOff

def test_rule_ids(self):
assert self.codemod.METADATA.NAME == "django-session-cookie-secure-off"
def test_name(self):
assert self.codemod.name() == "django-session-cookie-secure-off"

def test_not_settings_dot_py(self, tmpdir):
django_root, settings_folder = self.create_dir_structure(tmpdir)
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_harden_pyyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
class TestHardenPyyaml(BaseSemgrepCodemodTest):
codemod = HardenPyyaml

def test_rule_ids(self):
assert self.codemod.NAME == "harden-pyyaml"
def test_name(self):
assert self.codemod.name() == "harden-pyyaml"

def test_safe_loader(self, tmpdir):
input_code = """import yaml
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_harden_ruamel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestHardenRuamel(BaseSemgrepCodemodTest):
codemod = HardenRuamel

def test_rule_ids(self):
assert self.codemod.NAME == "harden-ruamel"
def test_name(self):
assert self.codemod.name() == "harden-ruamel"

@pytest.mark.parametrize("loader", ["YAML()", "YAML(typ='rt')", "YAML(typ='safe')"])
def test_safe(self, tmpdir, loader):
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_jwt_decode_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestJwtDecodeVerify(BaseSemgrepCodemodTest):
codemod = JwtDecodeVerify

def test_rule_ids(self):
assert self.codemod.NAME == "jwt-decode-verify"
def test_name(self):
assert self.codemod.name() == "jwt-decode-verify"

def test_import(self, tmpdir):
input_code = """import jwt
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_limit_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class TestLimitReadline(BaseSemgrepCodemodTest):
codemod = LimitReadline

def test_rule_ids(self):
assert self.codemod.NAME == "limit-readline"
def test_name(self):
assert self.codemod.name() == "limit-readline"

def test_file_readline(self, tmpdir):
input_code = """file = open('some_file.txt')
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_process_creation_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestProcessCreationSandbox(BaseSemgrepCodemodTest):
codemod = ProcessSandbox

def test_rule_ids(self):
assert self.codemod.NAME == "sandbox-process-creation"
def test_name(self):
assert self.codemod.name() == "sandbox-process-creation"

def test_import_subprocess(self, tmpdir):
input_code = """import subprocess
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_request_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class TestRequestsVerify(BaseSemgrepCodemodTest):
codemod = RequestsVerify

def test_rule_ids(self):
assert self.codemod.NAME == "requests-verify"
def test_name(self):
assert self.codemod.name() == "requests-verify"

@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_default_verify(self, tmpdir, func):
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_secure_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestSecureRandom(BaseSemgrepCodemodTest):
codemod = SecureRandom

def test_rule_ids(self):
assert self.codemod.NAME == "secure-random"
def test_name(self):
assert self.codemod.name() == "secure-random"

def test_import_random(self, tmpdir):
input_code = """import random
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_tempfile_mktemp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestTempfileMktemp(BaseSemgrepCodemodTest):
codemod = TempfileMktemp

def test_rule_ids(self):
assert self.codemod.NAME == "secure-tempfile"
def test_name(self):
assert self.codemod.name() == "secure-tempfile"

def test_import(self, tmpdir):
input_code = """import tempfile
Expand Down
4 changes: 2 additions & 2 deletions tests/codemods/test_url_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestUrlSandbox(BaseSemgrepCodemodTest):
codemod = UrlSandbox

def test_rule_ids(self):
assert self.codemod.METADATA.NAME == "url-sandbox"
def test_name(self):
assert self.codemod.name() == "url-sandbox"

def test_import_requests(self, tmpdir):
input_code = """import requests
Expand Down