Skip to content

Commit 3d0c45b

Browse files
committed
Fixed pytest-dev#2148 - parse directory names properly when args contains ::.
This commit also improves readbility in get_dirs_from_args by using self documenting local functions. get_dirs_from_args also now only returns directories that actually exists, and not files to avoid confusion. This commit also removes redundant checks in get_common_ancestor that was already performed in get_dirs_from_args..
1 parent 2a16ce2 commit 3d0c45b

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
expected warnings and the list of caught warnings is added to the
1010
error message. Thanks `@lesteve`_ for the PR.
1111

12-
*
12+
* Specifying tests with colons like ``test_foo.py::test_bar`` for tests in
13+
subdirectories with ini configuration files now uses the correct ini file
14+
(`#2148`_). Thanks `@pelme`.
1315

1416
*
1517

1618
.. _@lesteve: https://github.com/lesteve
19+
.. _@pelme: https://github.com/pelme
1720

1821
.. _#2150: https://github.com/pytest-dev/pytest/issues/2150
22+
.. _#2148: https://github.com/pytest-dev/pytest/issues/2148
1923

2024

2125
3.0.5 (2016-12-05)

_pytest/config.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,25 +1228,22 @@ def getcfg(args, warnfunc=None):
12281228
return None, None, None
12291229

12301230

1231-
def get_common_ancestor(args):
1231+
def get_common_ancestor(paths):
12321232
# args are what we get after early command line parsing (usually
12331233
# strings, but can be py.path.local objects as well)
12341234
common_ancestor = None
1235-
for arg in args:
1236-
if str(arg)[0] == "-":
1237-
continue
1238-
p = py.path.local(arg)
1239-
if not p.exists():
1235+
for path in paths:
1236+
if not path.exists():
12401237
continue
12411238
if common_ancestor is None:
1242-
common_ancestor = p
1239+
common_ancestor = path
12431240
else:
1244-
if p.relto(common_ancestor) or p == common_ancestor:
1241+
if path.relto(common_ancestor) or path == common_ancestor:
12451242
continue
1246-
elif common_ancestor.relto(p):
1247-
common_ancestor = p
1243+
elif common_ancestor.relto(path):
1244+
common_ancestor = path
12481245
else:
1249-
shared = p.common(common_ancestor)
1246+
shared = path.common(common_ancestor)
12501247
if shared is not None:
12511248
common_ancestor = shared
12521249
if common_ancestor is None:
@@ -1257,9 +1254,29 @@ def get_common_ancestor(args):
12571254

12581255

12591256
def get_dirs_from_args(args):
1260-
return [d for d in (py.path.local(x) for x in args
1261-
if not str(x).startswith("-"))
1262-
if d.exists()]
1257+
def is_option(x):
1258+
return str(x).startswith('-')
1259+
1260+
def get_file_part_from_node_id(x):
1261+
return str(x).split('::')[0]
1262+
1263+
def get_dir_from_path(path):
1264+
if path.isdir():
1265+
return path
1266+
return py.path.local(path.dirname)
1267+
1268+
# These looks like paths but may not exist
1269+
possible_paths = (
1270+
py.path.local(get_file_part_from_node_id(arg))
1271+
for arg in args
1272+
if not is_option(arg)
1273+
)
1274+
1275+
return [
1276+
get_dir_from_path(path)
1277+
for path in possible_paths
1278+
if path.exists()
1279+
]
12631280

12641281

12651282
def determine_setup(inifile, args, warnfunc=None):

0 commit comments

Comments
 (0)