Skip to content

Commit

Permalink
add code validation to int tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 9, 2023
1 parent 0603723 commit 62e6e0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion integration_tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from codemodder import __VERSION__
from codemodder import registry

from tests.validations import validate_code

SAMPLES_DIR = "tests/samples"

Expand Down Expand Up @@ -121,6 +121,7 @@ def check_code_after(self):
with open(self.code_path, "r", encoding="utf-8") as f:
new_code = f.read()
assert new_code == self.expected_new_code
validate_code(path=self.code_path)

def test_file_rewritten(self):
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/validations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import importlib.util
import tempfile

def validate_code(*, path=None, code=None):
"""
Ensure that code written in `path` or in `code` str is importable.
"""
assert (path is None) != (code is None), "Must pass either path to code or code as a str."

if path:
_try_code_import(path)
return
with tempfile.NamedTemporaryFile(suffix=".py", mode='w+t') as temp:
temp.write(code)
_try_code_import(temp.name)

def _try_code_import(path):
spec = importlib.util.spec_from_file_location("output_code", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

0 comments on commit 62e6e0b

Please sign in to comment.