diff --git a/README.md b/README.md index 20b34cf..48491d2 100644 --- a/README.md +++ b/README.md @@ -151,8 +151,23 @@ addopts = --maxfail=2 [pytest-watch] ignore = ./integration-tests nobeep = True +ext = .py,.txt ``` +CLI options can also be added to a `[tool.pytest-watch]` section in your +[pyproject.toml file](https://pytest.org/en/stable/customize.html#pyproject-toml): + +```toml +# pyproject.toml + +[tool.pytest.ini_options] +addopts = "-ra -q" + +[tool.pytest-watch] +ignore = "./integration-tests" +nobeep = true +ext = [ ".py", ".txt" ] +``` Alternatives ------------ diff --git a/README.rst b/README.rst index 60f12f4..9ab0391 100644 --- a/README.rst +++ b/README.rst @@ -153,6 +153,23 @@ persist them in your project. For example: [pytest-watch] ignore = ./integration-tests nobeep = True + ext = .py,.txt + +CLI options can also be added to a ``[tool.pytest-watch]`` section in +your `pyproject.toml +file `__: + +.. code:: toml + + # pyproject.toml + + [tool.pytest.ini_options] + addopts = "-ra -q" + + [tool.pytest-watch] + ignore = "./integration-tests" + nobeep = true + ext = [ ".py", ".txt" ] Alternatives ------------ diff --git a/pytest_watch/config.py b/pytest_watch/config.py index cd11c46..f0d28f0 100644 --- a/pytest_watch/config.py +++ b/pytest_watch/config.py @@ -6,6 +6,8 @@ from .util import silence +import toml + try: from configparser import ConfigParser except ImportError: @@ -79,6 +81,20 @@ def _collect_config(pytest_args, silent=True): return _run_pytest_collect(pytest_args) +def _parse_toml_config(path): + config = toml.load(path).get('tool', {}).get('pytest-watch', {}) + stringify = lambda v: ','.join(v) if isinstance(v, list) else str(v) + return {k: stringify(v) for k, v in config.items()} + + +def _parse_ini_config(path): + config = ConfigParser() + config.read(path) + if config.has_section('pytest-watch'): + return dict(config.items('pytest-watch')) + return {} + + def merge_config(args, pytest_args, silent=True, verbose=False): if verbose: print('Locating inifile...') @@ -91,9 +107,14 @@ def merge_config(args, pytest_args, silent=True, verbose=False): if not config_path: return True - config = ConfigParser() - config.read(config_path) - if not config.has_section('pytest-watch'): + if config_path.endswith('.toml'): + config = _parse_toml_config(config_path) + elif config_path.endswith('.ini'): + config = _parse_ini_config(config_path) + else: + config = {} + + if not config: return True for cli_name in args: @@ -106,15 +127,15 @@ def merge_config(args, pytest_args, silent=True, verbose=False): continue # Find config option - if not config.has_option('pytest-watch', config_name): + if not config_name in config: continue # Merge config option using the expected type if isinstance(args[cli_name], list): - args[cli_name].append(config.get('pytest-watch', config_name)) + args[cli_name].append(config.get(config_name)) elif isinstance(args[cli_name], bool): - args[cli_name] = config.getboolean('pytest-watch', config_name) + args[cli_name] = config.get(config_name).lower() != 'false' else: - args[cli_name] = config.get('pytest-watch', config_name) + args[cli_name] = config.get(config_name) return True