Skip to content

Commit 42b43a7

Browse files
committed
Paths after normal options are now properly used to discover rootdir and ini files
Fix #949
1 parent b25e41e commit 42b43a7

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@
150150
- issue951: add new record_xml_property fixture, that supports logging
151151
additional information on xml output. Thanks David Diaz for the PR.
152152

153+
- issue949: paths after normal options (for example `-s`, `-v`, etc) are now
154+
properly used to discover `rootdir` and `ini` files.
155+
Thanks Peter Lauri for the report and Bruno Oliveira for the PR.
156+
153157
2.7.3 (compared to 2.7.2)
154158
-----------------------------
155159

_pytest/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def parse_setoption(self, args, option):
479479
def parse_known_args(self, args):
480480
optparser = self._getparser()
481481
args = [str(x) for x in args]
482-
return optparser.parse_known_args(args)[0]
482+
return optparser.parse_known_args(args)
483483

484484
def addini(self, name, help, type=None, default=None):
485485
""" register an ini-file option.
@@ -879,8 +879,9 @@ def pytest_load_initial_conftests(self, early_config):
879879
self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
880880

881881
def _initini(self, args):
882-
parsed_args = self._parser.parse_known_args(args)
883-
r = determine_setup(parsed_args.inifilename, parsed_args.file_or_dir)
882+
parsed_args, extra_args = self._parser.parse_known_args(args)
883+
r = determine_setup(parsed_args.inifilename,
884+
parsed_args.file_or_dir + extra_args)
884885
self.rootdir, self.inifile, self.inicfg = r
885886
self._parser.extra_info['rootdir'] = self.rootdir
886887
self._parser.extra_info['inifile'] = self.inifile
@@ -900,7 +901,8 @@ def _preparse(self, args, addopts=True):
900901
except ImportError as e:
901902
self.warn("I2", "could not load setuptools entry import: %s" % (e,))
902903
self.pluginmanager.consider_env()
903-
self.known_args_namespace = ns = self._parser.parse_known_args(args)
904+
ns, _ = self._parser.parse_known_args(args)
905+
self.known_args_namespace = ns
904906
if self.known_args_namespace.confcutdir is None and self.inifile:
905907
confcutdir = py.path.local(self.inifile).dirname
906908
self.known_args_namespace.confcutdir = confcutdir

testing/test_config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,31 @@ def test_invalid_options_show_extra_information(testdir):
341341
"* rootdir: %s*" % testdir.tmpdir,
342342
])
343343

344+
345+
@pytest.mark.parametrize('args', [
346+
['dir1', 'dir2', '-v'],
347+
['dir1', '-v', 'dir2'],
348+
['dir2', '-v', 'dir1'],
349+
['-v', 'dir2', 'dir1'],
350+
])
351+
def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args):
352+
"""
353+
Consider all arguments in the command-line for rootdir and inifile
354+
discovery, even if they happen to occur after an option. #949
355+
"""
356+
# replace "dir1" and "dir2" from "args" into their real directory
357+
root = testdir.tmpdir.mkdir('myroot')
358+
d1 = root.mkdir('dir1')
359+
d2 = root.mkdir('dir2')
360+
for i, arg in enumerate(args):
361+
if arg == 'dir1':
362+
args[i] = d1
363+
elif arg == 'dir2':
364+
args[i] = d2
365+
result = testdir.runpytest(*args)
366+
result.stdout.fnmatch_lines(['*rootdir: *myroot, inifile: '])
367+
368+
344369
@pytest.mark.skipif("sys.platform == 'win32'")
345370
def test_toolongargs_issue224(testdir):
346371
result = testdir.runpytest("-m", "hello" * 500)

testing/test_parseopt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ def test_parse2(self, parser):
105105
def test_parse_known_args(self, parser):
106106
parser.parse_known_args([py.path.local()])
107107
parser.addoption("--hello", action="store_true")
108-
ns = parser.parse_known_args(["x", "--y", "--hello", "this"])
108+
ns, extra_args = parser.parse_known_args(["x", "--y", "--hello", "this"])
109109
assert ns.hello
110+
assert ns.file_or_dir == ['x']
111+
assert extra_args == ['--y', 'this']
110112

111113
def test_parse_will_set_default(self, parser):
112114
parser.addoption("--hello", dest="hello", default="x", action="store")

0 commit comments

Comments
 (0)