Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EV-3866] Fix toml config #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://pytest.org/en/stable/customize.html#pyproject-toml>`__:

.. code:: toml

# pyproject.toml

[tool.pytest.ini_options]
addopts = "-ra -q"

[tool.pytest-watch]
ignore = "./integration-tests"
nobeep = true
ext = [ ".py", ".txt" ]

Alternatives
------------
Expand Down
35 changes: 28 additions & 7 deletions pytest_watch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from .util import silence

import toml

try:
from configparser import ConfigParser
except ImportError:
Expand Down Expand Up @@ -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...')
Expand All @@ -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:
Expand All @@ -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