Skip to content

Commit

Permalink
feat: pyproject.toml support
Browse files Browse the repository at this point in the history
# Conflicts:
#	mutmut/__main__.py
  • Loading branch information
15r10nk authored and boxed committed Oct 26, 2024
1 parent 4f09b38 commit 97e81e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
60 changes: 41 additions & 19 deletions mutmut/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import itertools
import json
import os
import resource
import shutil
import sys
from abc import ABC
from collections import defaultdict
from configparser import (
ConfigParser,
NoOptionError,
Expand Down Expand Up @@ -37,8 +37,6 @@
dedent,
indent,
)
import resource

from threading import Thread
from time import process_time
from typing import (
Expand All @@ -56,7 +54,6 @@

import mutmut


# Document: surviving mutants are retested when you ask mutmut to retest them, interactively in the UI or via command line

# TODO: only count a test for a function if the stack depth to get to the test < some configurable limit.
Expand Down Expand Up @@ -698,12 +695,14 @@ def execute_pytest(self, params, **kwargs):

def run_stats(self, *, tests):
class StatsCollector:
# noinspection PyMethodMayBeStatic
def pytest_runtest_teardown(self, item, nextitem):
unused(nextitem)
for function in mutmut._stats:
mutmut.tests_by_mangled_function_name[function].add(strip_prefix(item._nodeid, prefix='mutants/'))
mutmut._stats.clear()

# noinspection PyMethodMayBeStatic
def pytest_runtest_makereport(self, item, call):
mutmut.duration_by_test[item.nodeid] = call.duration

Expand Down Expand Up @@ -772,7 +771,6 @@ def run_tests(self, *, mutant_name, tests):

def mangle_function_name(*, name, class_name):
assert CLASS_NAME_SEPARATOR not in name
prefix = ''
if class_name:
assert CLASS_NAME_SEPARATOR not in class_name
prefix = f'x{CLASS_NAME_SEPARATOR}{class_name}{CLASS_NAME_SEPARATOR}'
Expand Down Expand Up @@ -948,36 +946,60 @@ def should_ignore_for_mutation(self, path):
return False


@lru_cache()
def read_config():
def config_reader():
path=Path("pyproject.toml")
if path.exists():
if sys.version_info >= (3, 11):
from tomllib import loads
else:
from toml import loads
data = loads(path.read_text("utf-8"))

try:
config = data["tool"]["mutmut"]
except KeyError:
pass
else:
def s(key, default):
try:
result = config[key]
except KeyError:
return default
return result
return s

config_parser = ConfigParser()
config_parser.read('setup.cfg')

def s(key, default):
try:
return config_parser.get('mutmut', key)
result = config_parser.get('mutmut', key)
except (NoOptionError, NoSectionError):
return default
if isinstance(default, list):
result = [x for x in result.split("\n") if x]
elif isinstance(default, int):
result = int(result)
return result
return s


@lru_cache()
def read_config():

s = config_reader()

mutmut.config = Config(
do_not_mutate=[
x
for x in s('do_not_mutate', '').split('\n')
if x
],
do_not_mutate=s('do_not_mutate', []),
also_copy=[
Path(y)
for y in [
x
for x in s('also_copy', '').split('\n')
if x
]
for y in s('also_copy', [])
]+[
Path('tests/'),
Path('test/'),
Path('tests.py'),
],
max_stack_depth=int(s('max_stack_depth', '-1')),
max_stack_depth=s('max_stack_depth', -1),
debug=s('debug', 'False').lower() in ('1', 't', 'true'),
)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ click
junit-xml==1.8
setproctitle
textual
toml>=0.10.2; python_version < '3.11'

3 comments on commit 97e81e5

@WillGibson
Copy link
Contributor

Choose a reason for hiding this comment

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

@15r10nk Why is toml restricted to python_version < '3.11'?

@15r10nk
Copy link
Contributor Author

@15r10nk 15r10nk commented on 97e81e5 Dec 30, 2024

Choose a reason for hiding this comment

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

because I used the build in tomllib for >= 3.11

   if sys.version_info >= (3, 11):
            from tomllib import loads
        else:
            from toml import loads

@WillGibson
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining @15r10nk.

#356

Please sign in to comment.