Merge pull request #2149 from pelme/issue2148

Fix handling of ini configuration in subdirs when specifying ::
This commit is contained in:
Bruno Oliveira 2016-12-27 21:54:14 -02:00 committed by GitHub
commit 088b742d40
3 changed files with 59 additions and 17 deletions

View File

@ -9,13 +9,17 @@
expected warnings and the list of caught warnings is added to the expected warnings and the list of caught warnings is added to the
error message. Thanks `@lesteve`_ for the PR. error message. Thanks `@lesteve`_ for the PR.
* * Specifying tests with colons like ``test_foo.py::test_bar`` for tests in
subdirectories with ini configuration files now uses the correct ini file
(`#2148`_). Thanks `@pelme`_.
* *
.. _@lesteve: https://github.com/lesteve .. _@lesteve: https://github.com/lesteve
.. _@pelme: https://github.com/pelme
.. _#2150: https://github.com/pytest-dev/pytest/issues/2150 .. _#2150: https://github.com/pytest-dev/pytest/issues/2150
.. _#2148: https://github.com/pytest-dev/pytest/issues/2148
3.0.5 (2016-12-05) 3.0.5 (2016-12-05)

View File

@ -1228,25 +1228,20 @@ def getcfg(args, warnfunc=None):
return None, None, None return None, None, None
def get_common_ancestor(args): def get_common_ancestor(paths):
# args are what we get after early command line parsing (usually
# strings, but can be py.path.local objects as well)
common_ancestor = None common_ancestor = None
for arg in args: for path in paths:
if str(arg)[0] == "-": if not path.exists():
continue
p = py.path.local(arg)
if not p.exists():
continue continue
if common_ancestor is None: if common_ancestor is None:
common_ancestor = p common_ancestor = path
else: else:
if p.relto(common_ancestor) or p == common_ancestor: if path.relto(common_ancestor) or path == common_ancestor:
continue continue
elif common_ancestor.relto(p): elif common_ancestor.relto(path):
common_ancestor = p common_ancestor = path
else: else:
shared = p.common(common_ancestor) shared = path.common(common_ancestor)
if shared is not None: if shared is not None:
common_ancestor = shared common_ancestor = shared
if common_ancestor is None: if common_ancestor is None:
@ -1257,9 +1252,29 @@ def get_common_ancestor(args):
def get_dirs_from_args(args): def get_dirs_from_args(args):
return [d for d in (py.path.local(x) for x in args def is_option(x):
if not str(x).startswith("-")) return str(x).startswith('-')
if d.exists()]
def get_file_part_from_node_id(x):
return str(x).split('::')[0]
def get_dir_from_path(path):
if path.isdir():
return path
return py.path.local(path.dirname)
# These look like paths but may not exist
possible_paths = (
py.path.local(get_file_part_from_node_id(arg))
for arg in args
if not is_option(arg)
)
return [
get_dir_from_path(path)
for path in possible_paths
if path.exists()
]
def determine_setup(inifile, args, warnfunc=None): def determine_setup(inifile, args, warnfunc=None):

View File

@ -527,6 +527,29 @@ def test_toolongargs_issue224(testdir):
result = testdir.runpytest("-m", "hello" * 500) result = testdir.runpytest("-m", "hello" * 500)
assert result.ret == EXIT_NOTESTSCOLLECTED assert result.ret == EXIT_NOTESTSCOLLECTED
def test_config_in_subdirectory_colon_command_line_issue2148(testdir):
conftest_source = '''
def pytest_addoption(parser):
parser.addini('foo', 'foo')
'''
testdir.makefile('.ini', **{
'pytest': '[pytest]\nfoo = root',
'subdir/pytest': '[pytest]\nfoo = subdir',
})
testdir.makepyfile(**{
'conftest': conftest_source,
'subdir/conftest': conftest_source,
'subdir/test_foo': '''
def test_foo(pytestconfig):
assert pytestconfig.getini('foo') == 'subdir'
'''})
result = testdir.runpytest('subdir/test_foo.py::test_foo')
assert result.ret == 0
def test_notify_exception(testdir, capfd): def test_notify_exception(testdir, capfd):
config = testdir.parseconfig() config = testdir.parseconfig()
excinfo = pytest.raises(ValueError, "raise ValueError(1)") excinfo = pytest.raises(ValueError, "raise ValueError(1)")