Show an error if --confcutdir is not a valid directory

Fixes #2078
This commit is contained in:
Bruno Oliveira 2016-11-23 09:47:36 -02:00
parent a5b5090c72
commit 629d8e9fd6
3 changed files with 19 additions and 2 deletions

View File

@ -3,7 +3,10 @@
* *
* * An error message is now displayed if ``--confcutdir`` is not a valid directory, avoiding
subtle bugs (`#2078`_).
Thanks `@nicoddemus`_ for the PR.
* Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks * Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks
`@nedbat`_. `@nedbat`_.
@ -15,6 +18,7 @@
.. _@nedbat: https://github.com/nedbat .. _@nedbat: https://github.com/nedbat
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038 .. _#2038: https://github.com/pytest-dev/pytest/issues/2038
.. _#2078: https://github.com/pytest-dev/pytest/issues/2078
3.0.4 3.0.4

View File

@ -996,6 +996,7 @@ class Config(object):
"(are you using python -O?)\n") "(are you using python -O?)\n")
def _preparse(self, args, addopts=True): def _preparse(self, args, addopts=True):
import pytest
self._initini(args) self._initini(args)
if addopts: if addopts:
args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args
@ -1007,7 +1008,10 @@ class Config(object):
self.pluginmanager.load_setuptools_entrypoints(entrypoint_name) self.pluginmanager.load_setuptools_entrypoints(entrypoint_name)
self.pluginmanager.consider_env() self.pluginmanager.consider_env()
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy()) self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
if self.known_args_namespace.confcutdir is None and self.inifile: confcutdir = self.known_args_namespace.confcutdir
if confcutdir and not os.path.isdir(confcutdir):
raise pytest.UsageError('--confcutdir must be a directory, given: {0}'.format(confcutdir))
if confcutdir is None and self.inifile:
confcutdir = py.path.local(self.inifile).dirname confcutdir = py.path.local(self.inifile).dirname
self.known_args_namespace.confcutdir = confcutdir self.known_args_namespace.confcutdir = confcutdir
try: try:

View File

@ -294,6 +294,15 @@ class TestConfigAPI:
assert len(l) == 2 assert len(l) == 2
assert l == ["456", "123"] assert l == ["456", "123"]
def test_confcutdir_check_isdir(self, testdir):
"""Give an error if --confcutdir is not a valid directory (#2078)"""
with pytest.raises(pytest.UsageError):
testdir.parseconfig('--confcutdir', testdir.tmpdir.join('file').ensure(file=1))
with pytest.raises(pytest.UsageError):
testdir.parseconfig('--confcutdir', testdir.tmpdir.join('inexistant'))
config = testdir.parseconfig('--confcutdir', testdir.tmpdir.join('dir').ensure(dir=1))
assert config.getoption('confcutdir') == str(testdir.tmpdir.join('dir'))
class TestConfigFromdictargs: class TestConfigFromdictargs:
def test_basic_behavior(self): def test_basic_behavior(self):