Skip to content

Commit

Permalink
Clean up config logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyespo committed Dec 4, 2015
1 parent c2169dd commit 7c75e25
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 93 deletions.
62 changes: 4 additions & 58 deletions pytest_watch/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@
import colorama
from docopt import docopt

from .watcher import watch
from . import __version__

from ini_config_helpers import get_ini_option, get_ini_option_boolean, get_pytest_ini_path

try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
from .watcher import watch
from .config import merge_config


def main(argv=None):
Expand All @@ -55,57 +49,9 @@ def main(argv=None):
usage = __doc__[__doc__.find('Usage:'):]
version = 'pytest-watch ' + __version__
argv = argv if argv is not None else sys.argv[1:]
args_cmd = docopt(usage, argv=argv, version=version)

config_ini = ConfigParser()
pytest_ini_path = get_pytest_ini_path()
config_ini.read(pytest_ini_path)
args_ini = {}

# string ini config values
args_ini["--onpass"] = get_ini_option(config_ini, "onpass")

args_ini["--onfail"] = get_ini_option(config_ini, "onfail")

args_ini["--beforerun"] = get_ini_option(config_ini, "beforerun")

args_ini["--onexit"] = get_ini_option(config_ini, "onexit")

args_ini["--ext"] = get_ini_option(config_ini, "ext")

args_ini["--ignore"] = get_ini_option(config_ini, "ignore")

# boolean ini config values
args_ini["--help"] = get_ini_option_boolean(config_ini, "help")

args_ini["--version"] = get_ini_option_boolean(config_ini, "version")

args_ini["--clear"] = get_ini_option_boolean(config_ini, "clear")

args_ini["--nobeep"] = get_ini_option_boolean(config_ini, "nobeep")

args_ini["--poll"] = get_ini_option_boolean(config_ini, "poll")

args_ini["--no-spool"] = get_ini_option_boolean(config_ini, "no-spool")

args_ini["--verbose"] = get_ini_option_boolean(config_ini, "verbose")

args_ini["--quiet"] = get_ini_option_boolean(config_ini, "quiet")

# other config values
if config_ini.has_option("pytest-watch", "directories"):
args_ini['<directories>'] = config_ini.get("pytest-watch", "directories")
args_ini['<directories>'] = args_ini['<directories>'].split(", ")
else:
args_ini['<directories>'] = []

args_ini["<args>"] = get_ini_option(config_ini, "addopts")
args = docopt(usage, argv=argv, version=version)

args = {}
for arg_key in args_cmd:
args[arg_key] = args_cmd[arg_key] or args_ini.get(arg_key)
if args["--verbose"]:
print("pytest-watch args: " + str(args))
merge_config(args)

pytest_args = []
directories = args['<directories>']
Expand Down
51 changes: 51 additions & 0 deletions pytest_watch/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser

import pytest


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):
self.path = str(config.inifile)


def merge_config(args):
collect_config = CollectConfig()
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):

This comment has been minimized.

Copy link
@bendtherules

bendtherules Dec 4, 2015

Contributor

This means ini files can not set options like <directories>, in any way. Indeed, both <directories> and directories are ignored from the ini file.

I dont think thats really a desirable behavior.

This comment has been minimized.

Copy link
@joeyespo

joeyespo Dec 20, 2015

Author Owner

The <directories> option doesn't really belong in the config.

For one, pytest doesn't do this. Worse though, the input directories are what pytest uses to locate the config file in the first place (and should also be the case for ptw, as you've pointed out in #40).

For these reasons, this will be kept as-is.

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)
35 changes: 0 additions & 35 deletions pytest_watch/ini_config_helpers.py

This file was deleted.

0 comments on commit 7c75e25

Please sign in to comment.