Skip to content

Commit

Permalink
Fixed pytest-dev#2148 - parse directory names properly when args cont…
Browse files Browse the repository at this point in the history
…ains ::.

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..
  • Loading branch information
pelme committed Dec 27, 2016
1 parent 2a16ce2 commit da051bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
expected warnings and the list of caught warnings is added to the
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
.. _@pelme: https://github.com/pelme

.. _#2150: https://github.com/pytest-dev/pytest/issues/2150
.. _#2148: https://github.com/pytest-dev/pytest/issues/2148


3.0.5 (2016-12-05)
Expand Down
45 changes: 31 additions & 14 deletions _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,25 +1228,22 @@ def getcfg(args, warnfunc=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
for arg in args:
if str(arg)[0] == "-":
continue
p = py.path.local(arg)
if not p.exists():
for path in paths:
if not path.exists():
continue
if common_ancestor is None:
common_ancestor = p
common_ancestor = path
else:
if p.relto(common_ancestor) or p == common_ancestor:
if path.relto(common_ancestor) or path == common_ancestor:
continue
elif common_ancestor.relto(p):
common_ancestor = p
elif common_ancestor.relto(path):
common_ancestor = path
else:
shared = p.common(common_ancestor)
shared = path.common(common_ancestor)
if shared is not None:
common_ancestor = shared
if common_ancestor is None:
Expand All @@ -1257,9 +1254,29 @@ def get_common_ancestor(args):


def get_dirs_from_args(args):
return [d for d in (py.path.local(x) for x in args
if not str(x).startswith("-"))
if d.exists()]
def is_option(x):
return str(x).startswith('-')

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 looks 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):
Expand Down

0 comments on commit da051bd

Please sign in to comment.