Skip to content

Commit 7772d53

Browse files
committed
Remove non-mutated code from sys.path
This prevents that the tests accidentally import the unmodified code, rather than the mutated code from ./mutants/. Now we directly raise an import error at tests collection, rather than failing later on at the "forced fail test" run because the tests succeed on the original (non-mutated) code.
1 parent 69a5b7b commit 7772d53

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

mutmut/__main__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ class CollectTestsFailedException(Exception):
170170

171171

172172
class BadTestExecutionCommandsException(Exception):
173-
pass
173+
def __init__(self, pytest_args: list[str]) -> None:
174+
msg = f'Failed to run pytest with args: {pytest_args}. If your config sets debug=true, the original pytest error should be above.'
175+
super().__init__(msg)
174176

175177

176178

@@ -364,7 +366,7 @@ def new_tests(self):
364366

365367
class PytestRunner(TestRunner):
366368
# noinspection PyMethodMayBeStatic
367-
def execute_pytest(self, params, **kwargs):
369+
def execute_pytest(self, params: list[str], **kwargs):
368370
import pytest
369371
params += ['--rootdir=.']
370372
if mutmut.config.debug:
@@ -898,14 +900,18 @@ def _run(mutant_names: Union[tuple, list], max_children: Union[None, int]):
898900
time = datetime.now() - start
899901
print(f' done in {round(time.total_seconds()*1000)}ms', )
900902

901-
src_path = (Path('mutants') / 'src')
902-
source_path = (Path('mutants') / 'source')
903-
if src_path.exists():
904-
sys.path.insert(0, str(src_path.absolute()))
905-
elif source_path.exists():
906-
sys.path.insert(0, str(source_path.absolute))
907-
else:
908-
sys.path.insert(0, os.path.abspath('mutants'))
903+
# ensure that the mutated source code can be imported by the tests
904+
source_code_paths = [Path('.'), Path('src'), Path('source')]
905+
for path in source_code_paths:
906+
mutated_path = Path('mutants') / path
907+
if mutated_path.exists():
908+
sys.path.insert(1, str(mutated_path.absolute()))
909+
910+
# ensure that the original code CANNOT be imported by the tests
911+
for path in source_code_paths:
912+
for i in range(len(sys.path)):
913+
while i < len(sys.path) and Path(sys.path[i]).resolve() == path.resolve():
914+
del sys.path[i]
909915

910916
# TODO: config/option for runner
911917
# runner = HammettRunner()

0 commit comments

Comments
 (0)