Skip to content

Commit a289142

Browse files
committed
Fix determining rootdir from common_ancestor
1 parent ffb583a commit a289142

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

_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

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)