Skip to content

Commit ac5c39e

Browse files
authored
Merge pull request #1796 from nicoddemus/fix-rootdir-common-ancestor
Fix rootdir common ancestor
2 parents ffb583a + 21230aa commit ac5c39e

File tree

5 files changed

+88
-24
lines changed

5 files changed

+88
-24
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
deprecated but still present. Thanks to `@RedBeardCode`_ and `@tomviner`_
3636
for PR (`#1626`_).
3737

38+
* Refined logic for determining the ``rootdir``, considering only valid
39+
paths which fixes a number of issues: `#1594`_, `#1435`_ and `#1471`_.
40+
Thanks to `@blueyed`_ and `@davehunt`_ for the PR.
41+
3842
* Always include full assertion explanation. The previous behaviour was hiding
3943
sub-expressions that happened to be False, assuming this was redundant information.
4044
Thanks `@bagerard`_ for reporting (`#1503`_). Thanks to `@davehunt`_ and
@@ -70,11 +74,14 @@
7074
*
7175

7276
.. _#1210: https://github.com/pytest-dev/pytest/issues/1210
77+
.. _#1435: https://github.com/pytest-dev/pytest/issues/1435
78+
.. _#1471: https://github.com/pytest-dev/pytest/issues/1471
7379
.. _#1479: https://github.com/pytest-dev/pytest/issues/1479
7480
.. _#1503: https://github.com/pytest-dev/pytest/issues/1503
7581
.. _#1553: https://github.com/pytest-dev/pytest/issues/1553
7682
.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
7783
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
84+
.. _#1594: https://github.com/pytest-dev/pytest/issues/1594
7885
.. _#1597: https://github.com/pytest-dev/pytest/pull/1597
7986
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
8087
.. _#1626: https://github.com/pytest-dev/pytest/pull/1626

_pytest/config.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,8 @@ def get_common_ancestor(args):
10951095
if str(arg)[0] == "-":
10961096
continue
10971097
p = py.path.local(arg)
1098+
if not p.exists():
1099+
continue
10981100
if common_ancestor is None:
10991101
common_ancestor = p
11001102
else:
@@ -1108,29 +1110,42 @@ def get_common_ancestor(args):
11081110
common_ancestor = shared
11091111
if common_ancestor is None:
11101112
common_ancestor = py.path.local()
1111-
elif not common_ancestor.isdir():
1113+
elif common_ancestor.isfile():
11121114
common_ancestor = common_ancestor.dirpath()
11131115
return common_ancestor
11141116

11151117

1118+
def get_dirs_from_args(args):
1119+
return [d for d in (py.path.local(x) for x in args
1120+
if not str(x).startswith("-"))
1121+
if d.exists()]
1122+
1123+
11161124
def determine_setup(inifile, args):
1125+
dirs = get_dirs_from_args(args)
11171126
if inifile:
11181127
iniconfig = py.iniconfig.IniConfig(inifile)
11191128
try:
11201129
inicfg = iniconfig["pytest"]
11211130
except KeyError:
11221131
inicfg = None
1123-
rootdir = get_common_ancestor(args)
1132+
rootdir = get_common_ancestor(dirs)
11241133
else:
1125-
ancestor = get_common_ancestor(args)
1134+
ancestor = get_common_ancestor(dirs)
11261135
rootdir, inifile, inicfg = getcfg(
11271136
[ancestor], ["pytest.ini", "tox.ini", "setup.cfg"])
11281137
if rootdir is None:
11291138
for rootdir in ancestor.parts(reverse=True):
11301139
if rootdir.join("setup.py").exists():
11311140
break
11321141
else:
1133-
rootdir = ancestor
1142+
rootdir, inifile, inicfg = getcfg(
1143+
dirs, ["pytest.ini", "tox.ini", "setup.cfg"])
1144+
if rootdir is None:
1145+
rootdir = get_common_ancestor([py.path.local(), ancestor])
1146+
is_fs_root = os.path.splitdrive(str(rootdir))[1] == os.sep
1147+
if is_fs_root:
1148+
rootdir = ancestor
11341149
return rootdir, inifile, inicfg or {}
11351150

11361151

doc/en/customize.rst

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,29 @@ project/testrun-specific information.
2929

3030
Here is the algorithm which finds the rootdir from ``args``:
3131

32-
- determine the common ancestor directory for the specified ``args``.
32+
- determine the common ancestor directory for the specified ``args`` that are
33+
recognised as paths that exist in the file system. If no such paths are
34+
found, the common ancestor directory is set to the current working directory.
3335

34-
- look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the
35-
ancestor directory and upwards. If one is matched, it becomes the
36-
ini-file and its directory becomes the rootdir. An existing
37-
``pytest.ini`` file will always be considered a match whereas
38-
``tox.ini`` and ``setup.cfg`` will only match if they contain
39-
a ``[pytest]`` section.
36+
- look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the ancestor
37+
directory and upwards. If one is matched, it becomes the ini-file and its
38+
directory becomes the rootdir.
4039

41-
- if no ini-file was found, look for ``setup.py`` upwards from
42-
the common ancestor directory to determine the ``rootdir``.
40+
- if no ini-file was found, look for ``setup.py`` upwards from the common
41+
ancestor directory to determine the ``rootdir``.
4342

44-
- if no ini-file and no ``setup.py`` was found, use the already
45-
determined common ancestor as root directory. This allows to
46-
work with pytest in structures that are not part of a package
47-
and don't have any particular ini-file configuration.
43+
- if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini`` and
44+
``setup.cfg`` in each of the specified ``args`` and upwards. If one is
45+
matched, it becomes the ini-file and its directory becomes the rootdir.
4846

49-
Note that options from multiple ini-files candidates are never merged,
50-
the first one wins (``pytest.ini`` always wins even if it does not
47+
- if no ini-file was found, use the already determined common ancestor as root
48+
directory. This allows to work with pytest in structures that are not part of
49+
a package and don't have any particular ini-file configuration.
50+
51+
Note that an existing ``pytest.ini`` file will always be considered a match,
52+
whereas ``tox.ini`` and ``setup.cfg`` will only match if they contain a
53+
``[pytest]`` section. Options from multiple ini-files candidates are never
54+
merged - the first one wins (``pytest.ini`` always wins, even if it does not
5155
contain a ``[pytest]`` section).
5256

5357
The ``config`` object will subsequently carry these attributes:

testing/test_collection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ def test_method(self):
490490
class Test_getinitialnodes:
491491
def test_global_file(self, testdir, tmpdir):
492492
x = tmpdir.ensure("x.py")
493-
config = testdir.parseconfigure(x)
493+
with tmpdir.as_cwd():
494+
config = testdir.parseconfigure(x)
494495
col = testdir.getnode(config, x)
495496
assert isinstance(col, pytest.Module)
496497
assert col.name == 'x.py'
@@ -504,7 +505,8 @@ def test_pkgfile(self, testdir):
504505
subdir = tmpdir.join("subdir")
505506
x = subdir.ensure("x.py")
506507
subdir.ensure("__init__.py")
507-
config = testdir.parseconfigure(x)
508+
with subdir.as_cwd():
509+
config = testdir.parseconfigure(x)
508510
col = testdir.getnode(config, x)
509511
assert isinstance(col, pytest.Module)
510512
assert col.name == 'x.py'

testing/test_config.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ def test_consider_args_after_options_for_rootdir_and_inifile(testdir, args):
468468
args[i] = d1
469469
elif arg == 'dir2':
470470
args[i] = d2
471-
result = testdir.runpytest(*args)
471+
with root.as_cwd():
472+
result = testdir.runpytest(*args)
472473
result.stdout.fnmatch_lines(['*rootdir: *myroot, inifile: '])
473474

474475

@@ -547,10 +548,14 @@ def test_hello(fix):
547548
class TestRootdir:
548549
def test_simple_noini(self, tmpdir):
549550
assert get_common_ancestor([tmpdir]) == tmpdir
550-
assert get_common_ancestor([tmpdir.mkdir("a"), tmpdir]) == tmpdir
551-
assert get_common_ancestor([tmpdir, tmpdir.join("a")]) == tmpdir
551+
a = tmpdir.mkdir("a")
552+
assert get_common_ancestor([a, tmpdir]) == tmpdir
553+
assert get_common_ancestor([tmpdir, a]) == tmpdir
552554
with tmpdir.as_cwd():
553555
assert get_common_ancestor([]) == tmpdir
556+
no_path = tmpdir.join('does-not-exist')
557+
assert get_common_ancestor([no_path]) == tmpdir
558+
assert get_common_ancestor([no_path.join('a')]) == tmpdir
554559

555560
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
556561
def test_with_ini(self, tmpdir, name):
@@ -595,3 +600,34 @@ def test_with_specific_inifile(self, tmpdir):
595600
inifile = tmpdir.ensure("pytest.ini")
596601
rootdir, inifile, inicfg = determine_setup(inifile, [tmpdir])
597602
assert rootdir == tmpdir
603+
604+
def test_with_arg_outside_cwd_without_inifile(self, tmpdir):
605+
a = tmpdir.mkdir("a")
606+
b = tmpdir.mkdir("b")
607+
rootdir, inifile, inicfg = determine_setup(None, [a, b])
608+
assert rootdir == tmpdir
609+
assert inifile is None
610+
611+
def test_with_arg_outside_cwd_with_inifile(self, tmpdir):
612+
a = tmpdir.mkdir("a")
613+
b = tmpdir.mkdir("b")
614+
inifile = a.ensure("pytest.ini")
615+
rootdir, parsed_inifile, inicfg = determine_setup(None, [a, b])
616+
assert rootdir == a
617+
assert inifile == parsed_inifile
618+
619+
@pytest.mark.parametrize('dirs', ([], ['does-not-exist'],
620+
['a/does-not-exist']))
621+
def test_with_non_dir_arg(self, dirs, tmpdir):
622+
with tmpdir.ensure(dir=True).as_cwd():
623+
rootdir, inifile, inicfg = determine_setup(None, dirs)
624+
assert rootdir == tmpdir
625+
assert inifile is None
626+
627+
def test_with_existing_file_in_subdir(self, tmpdir):
628+
a = tmpdir.mkdir("a")
629+
a.ensure("exist")
630+
with tmpdir.as_cwd():
631+
rootdir, inifile, inicfg = determine_setup(None, ['a/exist'])
632+
assert rootdir == tmpdir
633+
assert inifile is None

0 commit comments

Comments
 (0)