From 629d8e9fd62999ffbb80fcb8881c1c88e51d345f Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 23 Nov 2016 09:47:36 -0200 Subject: [PATCH] Show an error if --confcutdir is not a valid directory Fixes #2078 --- CHANGELOG.rst | 6 +++++- _pytest/config.py | 6 +++++- testing/test_config.py | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b87302e8cf6..eeb41a648d6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `@nedbat`_. @@ -15,6 +18,7 @@ .. _@nedbat: https://github.com/nedbat .. _#2038: https://github.com/pytest-dev/pytest/issues/2038 +.. _#2078: https://github.com/pytest-dev/pytest/issues/2078 3.0.4 diff --git a/_pytest/config.py b/_pytest/config.py index ab5e1b99481..61123f6acc2 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -996,6 +996,7 @@ def _warn_about_missing_assertion(self, mode): "(are you using python -O?)\n") def _preparse(self, args, addopts=True): + import pytest self._initini(args) if addopts: args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args @@ -1007,7 +1008,10 @@ def _preparse(self, args, addopts=True): self.pluginmanager.load_setuptools_entrypoints(entrypoint_name) self.pluginmanager.consider_env() 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 self.known_args_namespace.confcutdir = confcutdir try: diff --git a/testing/test_config.py b/testing/test_config.py index 75a806c4a99..1567dd27ca0 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -294,6 +294,15 @@ def pytest_addoption(parser): assert len(l) == 2 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: def test_basic_behavior(self):