From fb75ed67691ad68451fb207c105b463ccb3787c6 Mon Sep 17 00:00:00 2001 From: Frank Hoffmann <15r10nk-git@polarbit.de> Date: Sat, 26 Oct 2024 15:06:31 +0200 Subject: [PATCH] feat: pyproject.toml support --- mutmut/__main__.py | 51 +++++++++++++++++++++++++++++++++------------- requirements.txt | 1 + 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/mutmut/__main__.py b/mutmut/__main__.py index 88f0eed5..aa4c7670 100644 --- a/mutmut/__main__.py +++ b/mutmut/__main__.py @@ -957,36 +957,59 @@ 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) ) diff --git a/requirements.txt b/requirements.txt index d491f2de..f9253d68 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ click junit-xml==1.8 setproctitle textual +toml>=0.10.2; python_version < '3.11'