-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.py
55 lines (41 loc) · 1.36 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser
import pytest
from .util import silence
CLI_OPTION_PREFIX = '--'
class CollectConfig(object):
"""
A pytest plugin to gets the configuration file.
"""
def __init__(self):
self.path = None
def pytest_cmdline_main(self, config):
if config.inifile:
self.path = str(config.inifile)
def merge_config(args):
collect_config = CollectConfig()
with silence():
pytest.main(['--collect-only'], plugins=[collect_config])
if not collect_config.path:
return
config = ConfigParser()
config.read(collect_config.path)
if not config.has_section('pytest-watch'):
return
for cli_name in args:
if not cli_name.startswith(CLI_OPTION_PREFIX):
continue
config_name = cli_name[len(CLI_OPTION_PREFIX):]
# Let CLI options take precedence
if args[cli_name]:
continue
# Find config option
if not config.has_option('pytest-watch', config_name):
continue
# Merge config option using the expected type
if isinstance(args[cli_name], bool):
args[cli_name] = config.getboolean('pytest-watch', config_name)
else:
args[cli_name] = config.get('pytest-watch', config_name)