Skip to content

Commit

Permalink
Merge ea1dd91 into 0cb9007
Browse files Browse the repository at this point in the history
  • Loading branch information
phihos committed Nov 17, 2019
2 parents 0cb9007 + ea1dd91 commit 31be201
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: python
python:
- 3.3
- 3.4
- 3.5
- 3.6
- 3.7
install:
# python 3.3 compatibility
- if [[ $TRAVIS_PYTHON_VERSION == 3.3 ]]; then pip install virtualenv==15.1.0 setuptools==39.2.0; fi
Expand Down
2 changes: 2 additions & 0 deletions mutpy/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"""Wrapper module for astmonkey code generator."""

from astmonkey.visitors import to_source as astmonkey_to_source
import copy


def to_source(node, indent_with=' ' * 4):
node = copy.deepcopy(node)
return astmonkey_to_source(node=node, indent_with=indent_with)


Expand Down
7 changes: 4 additions & 3 deletions mutpy/test_runners/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ def create_test_suite(self, mutant_module):
if not issubclass(self.test_suite_cls, BaseTestSuite):
raise ValueError('{0} is not a subclass of {1}'.format(self.test_suite_cls, BaseTestSuite))
suite = self.create_empty_test_suite()
utils.InjectImporter(mutant_module).install()
self.remove_loaded_modules()
injector = utils.ModuleInjector(mutant_module)
for test_module, target_test in self.test_loader.load():
injector.inject_to(test_module)
suite.add_tests(test_module, target_test)
utils.InjectImporter.uninstall()
importer = utils.InjectImporter(mutant_module)
importer.install()
return suite

@utils.TimeRegister
Expand Down
38 changes: 38 additions & 0 deletions mutpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,44 @@ def uninstall(cls):
del sys.meta_path[0]


class ModuleInjector:

def __init__(self, source):
self.source = source

def inject_to(self, target):
for imported_as in target.__dict__.copy():
artifact = target.__dict__[imported_as]
self.__perform_injection(imported_as, artifact, target)

def __perform_injection(self, imported_as, artefact, target):
if inspect.ismodule(artefact):
self.try_inject_module(imported_as, artefact, target)
elif inspect.isclass(artefact) or inspect.isfunction(artefact):
self.try_incject_class_or_function(imported_as, artefact, target)
else:
self.try_inject_other(imported_as, target)

def try_inject_module(self, imported_as, module, target):
if self.safe_getattr(module, '__name__') == self.source.__name__:
self.source.__file__ = module.__file__
target.__dict__[imported_as] = self.source

def try_incject_class_or_function(self, imported_as, class_or_function, target):
if self.safe_getattr(class_or_function, '__name__') in self.source.__dict__:
target.__dict__[imported_as] = self.source.__dict__[self.safe_getattr(class_or_function, '__name__')]

def try_inject_other(self, imported_as, target):
if imported_as in self.source.__dict__ and not self.is_restricted(imported_as):
target.__dict__[imported_as] = self.source.__dict__[imported_as]

def is_restricted(self, name):
return name in ['__builtins__', '__name__', '__doc__', '__file__']

def safe_getattr(self, obj, name):
return object.__getattribute__(obj, name)


class StdoutManager:
def __init__(self, disable=True):
self.disable = disable
Expand Down
3 changes: 2 additions & 1 deletion requirements/development.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r production.txt

coverage
coverage
pytest
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
},
test_suite='mutpy.test',
classifiers=[
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Environment :: Console',
'Intended Audience :: Developers',
'Topic :: Software Development :: Testing',
Expand Down
5 changes: 2 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[tox]
envlist =
coverage-erase
test-py{33,34,35,36}
test-py{34,35,36,37}
coverage-report
[testenv]
deps=
tox
coverage
extras=
pytest
-r{toxinidir}/requirements/development.txt
commands =
coverage-erase: coverage erase
test: coverage run --source=mutpy -m unittest discover -s mutpy/test
Expand Down

0 comments on commit 31be201

Please sign in to comment.