Skip to content

Commit 38f7562

Browse files
authored
Merge pull request #2080 from nicoddemus/confcutdir-check
Show an error if --confcutdir is not a valid directory
2 parents a5b5090 + 629d8e9 commit 38f7562

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGELOG.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
*
55

6-
*
6+
* An error message is now displayed if ``--confcutdir`` is not a valid directory, avoiding
7+
subtle bugs (`#2078`_).
8+
Thanks `@nicoddemus`_ for the PR.
9+
710

811
* Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks
912
`@nedbat`_.
@@ -15,6 +18,7 @@
1518
.. _@nedbat: https://github.com/nedbat
1619

1720
.. _#2038: https://github.com/pytest-dev/pytest/issues/2038
21+
.. _#2078: https://github.com/pytest-dev/pytest/issues/2078
1822

1923

2024
3.0.4

_pytest/config.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ def _warn_about_missing_assertion(self, mode):
996996
"(are you using python -O?)\n")
997997

998998
def _preparse(self, args, addopts=True):
999+
import pytest
9991000
self._initini(args)
10001001
if addopts:
10011002
args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args
@@ -1007,7 +1008,10 @@ def _preparse(self, args, addopts=True):
10071008
self.pluginmanager.load_setuptools_entrypoints(entrypoint_name)
10081009
self.pluginmanager.consider_env()
10091010
self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy())
1010-
if self.known_args_namespace.confcutdir is None and self.inifile:
1011+
confcutdir = self.known_args_namespace.confcutdir
1012+
if confcutdir and not os.path.isdir(confcutdir):
1013+
raise pytest.UsageError('--confcutdir must be a directory, given: {0}'.format(confcutdir))
1014+
if confcutdir is None and self.inifile:
10111015
confcutdir = py.path.local(self.inifile).dirname
10121016
self.known_args_namespace.confcutdir = confcutdir
10131017
try:

testing/test_config.py

+9
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ def pytest_addoption(parser):
294294
assert len(l) == 2
295295
assert l == ["456", "123"]
296296

297+
def test_confcutdir_check_isdir(self, testdir):
298+
"""Give an error if --confcutdir is not a valid directory (#2078)"""
299+
with pytest.raises(pytest.UsageError):
300+
testdir.parseconfig('--confcutdir', testdir.tmpdir.join('file').ensure(file=1))
301+
with pytest.raises(pytest.UsageError):
302+
testdir.parseconfig('--confcutdir', testdir.tmpdir.join('inexistant'))
303+
config = testdir.parseconfig('--confcutdir', testdir.tmpdir.join('dir').ensure(dir=1))
304+
assert config.getoption('confcutdir') == str(testdir.tmpdir.join('dir'))
305+
297306

298307
class TestConfigFromdictargs:
299308
def test_basic_behavior(self):

0 commit comments

Comments
 (0)