Skip to content
Prev Previous commit
Next Next commit
Fix tests after rebase
  • Loading branch information
nicoddemus committed Apr 2, 2021
commit fe95061a945abde4f1cd4138da7dadd7df27a0b2
6 changes: 4 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def _try_load_conftest(
if anchor.is_dir():
for x in anchor.glob("test*"):
if x.is_dir():
self._getconftestmodules(x, importmode)
self._getconftestmodules(x, importmode, rootpath)

@lru_cache(maxsize=128)
def _getconftestmodules(
Expand Down Expand Up @@ -1444,7 +1444,9 @@ def _getini(self, name: str):
assert type in [None, "string"]
return value

def _getconftest_pathlist(self, name: str, path: Path, rootpath: Path) -> Optional[List[Path]]:
def _getconftest_pathlist(
self, name: str, path: Path, rootpath: Path
) -> Optional[List[Path]]:
try:
mod, relroots = self.pluginmanager._rget_with_confmod(
name, path, self.getoption("importmode"), rootpath
Expand Down
4 changes: 2 additions & 2 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_traceback_cut(self) -> None:
def test_traceback_cut_excludepath(self, pytester: Pytester) -> None:
p = pytester.makepyfile("def f(): raise ValueError")
with pytest.raises(ValueError) as excinfo:
import_path(p).f() # type: ignore[attr-defined]
import_path(p, root=pytester.path).f() # type: ignore[attr-defined]
basedir = Path(pytest.__file__).parent
newtraceback = excinfo.traceback.cut(excludepath=basedir)
for x in newtraceback:
Expand Down Expand Up @@ -443,7 +443,7 @@ def importasmod(source):
tmp_path.joinpath("__init__.py").touch()
modpath.write_text(source)
importlib.invalidate_caches()
return import_path(modpath)
return import_path(modpath, root=tmp_path)

return importasmod

Expand Down
2 changes: 1 addition & 1 deletion testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ def test_2(self):
reprec.assertoutcome(passed=8)
config = reprec.getcalls("pytest_unconfigure")[0].config
values = config.pluginmanager._getconftestmodules(
p, importmode="prepend", rootpath=Path(testdir.tmpdir)
p, importmode="prepend", rootpath=pytester.path
)[0].values
assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2

Expand Down
75 changes: 46 additions & 29 deletions testing/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def basedir(
def test_basic_init(self, basedir: Path) -> None:
conftest = PytestPluginManager()
p = basedir / "adir"
assert conftest._rget_with_confmod("a", p, importmode="prepend", rootpath=basedir)[1] == 1
assert (
conftest._rget_with_confmod("a", p, importmode="prepend", rootpath=basedir)[
1
]
== 1
)

def test_immediate_initialiation_and_incremental_are_the_same(
self, basedir: Path
Expand All @@ -69,9 +74,13 @@ def test_immediate_initialiation_and_incremental_are_the_same(
)
snap1 = len(conftest._dirpath2confmods)
assert snap1 == 1
conftest._getconftestmodules(basedir / "adir", importmode="prepend", rootpath=basedir)
conftest._getconftestmodules(
basedir / "adir", importmode="prepend", rootpath=basedir
)
assert len(conftest._dirpath2confmods) == snap1 + 1
conftest._getconftestmodules(basedir / "b", importmode="prepend", rootpath=basedir)
conftest._getconftestmodules(
basedir / "b", importmode="prepend", rootpath=basedir
)
assert len(conftest._dirpath2confmods) == snap1 + 2

def test_value_access_not_existing(self, basedir: Path) -> None:
Expand All @@ -84,9 +93,17 @@ def test_value_access_not_existing(self, basedir: Path) -> None:
def test_value_access_by_path(self, basedir: Path) -> None:
conftest = ConftestWithSetinitial(basedir)
adir = basedir / "adir"
assert conftest._rget_with_confmod("a", adir, importmode="prepend", rootpath=basedir)[1] == 1
assert (
conftest._rget_with_confmod("a", adir / "b", importmode="prepend", rootpath=basedir)[1] == 1.5
conftest._rget_with_confmod(
"a", adir, importmode="prepend", rootpath=basedir
)[1]
== 1
)
assert (
conftest._rget_with_confmod(
"a", adir / "b", importmode="prepend", rootpath=basedir
)[1]
== 1.5
)

def test_value_access_with_confmod(self, basedir: Path) -> None:
Expand Down Expand Up @@ -116,7 +133,9 @@ def test_doubledash_considered(pytester: Pytester) -> None:
conf.joinpath("conftest.py").touch()
conftest = PytestPluginManager()
conftest_setinitial(conftest, [conf.name, conf.name])
values = conftest._getconftestmodules(conf, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
conf, importmode="prepend", rootpath=pytester.path
)
assert len(values) == 1


Expand All @@ -137,31 +156,18 @@ def test_conftest_global_import(pytester: Pytester) -> None:
p = pytester.makepyfile(
"""
from pathlib import Path
<<<<<<< HEAD
import pytest
from _pytest.config import PytestPluginManager
conf = PytestPluginManager()
mod = conf._importconftest(Path("conftest.py"), importmode="prepend")
mod = conf._importconftest(Path("conftest.py"), importmode="prepend", rootpath=Path.cwd())
assert mod.x == 3
import conftest
assert conftest is mod, (conftest, mod)
sub = Path("sub")
sub.mkdir()
subconf = sub / "conftest.py"
subconf.write_text("y=4")
mod2 = conf._importconftest(subconf, importmode="prepend")
=======
import py, pytest
from _pytest.config import PytestPluginManager
conf = PytestPluginManager()
mod = conf._importconftest(py.path.local("conftest.py"), importmode="prepend", rootpath=Path.cwd)
assert mod.x == 3
import conftest
assert conftest is mod, (conftest, mod)
subconf = py.path.local().ensure("sub", "conftest.py")
subconf.write("y=4")
mod2 = conf._importconftest(subconf, importmode="prepend", rootpath=Path.cwd)
>>>>>>> 92960c5b3... Construct full module names when using importmode=importlib
mod2 = conf._importconftest(subconf, importmode="prepend", rootpath=Path.cwd())
assert mod != mod2
assert mod2.y == 4
import conftest
Expand All @@ -177,17 +183,25 @@ def test_conftestcutdir(pytester: Pytester) -> None:
p = pytester.mkdir("x")
conftest = PytestPluginManager()
conftest_setinitial(conftest, [pytester.path], confcutdir=p)
values = conftest._getconftestmodules(p, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
p, importmode="prepend", rootpath=pytester.path
)
assert len(values) == 0
values = conftest._getconftestmodules(conf.parent, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
conf.parent, importmode="prepend", rootpath=pytester.path
)
assert len(values) == 0
assert Path(conf) not in conftest._conftestpath2mod
# but we can still import a conftest directly
conftest._importconftest(conf, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(conf.parent, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
conf.parent, importmode="prepend", rootpath=pytester.path
)
assert values[0].__file__.startswith(str(conf))
# and all sub paths get updated properly
values = conftest._getconftestmodules(p, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
p, importmode="prepend", rootpath=pytester.path
)
assert len(values) == 1
assert values[0].__file__.startswith(str(conf))

Expand All @@ -196,7 +210,9 @@ def test_conftestcutdir_inplace_considered(pytester: Pytester) -> None:
conf = pytester.makeconftest("")
conftest = PytestPluginManager()
conftest_setinitial(conftest, [conf.parent], confcutdir=conf.parent)
values = conftest._getconftestmodules(conf.parent, importmode="prepend", rootpath=pytester.path)
values = conftest._getconftestmodules(
conf.parent, importmode="prepend", rootpath=pytester.path
)
assert len(values) == 1
assert values[0].__file__.startswith(str(conf))

Expand Down Expand Up @@ -372,11 +388,12 @@ def impct(p, importmode, root):
conftest = PytestPluginManager()
conftest._confcutdir = pytester.path
monkeypatch.setattr(conftest, "_importconftest", impct)
mods = cast(List[Path], conftest._getconftestmodules(sub, importmode="prepend", rootpath=pytester.path))
mods = cast(
List[Path],
conftest._getconftestmodules(sub, importmode="prepend", rootpath=pytester.path),
)
expected = [ct1, ct2]
assert mods == expected
assert conftest._getconftestmodules(sub, importmode="prepend", rootpath=pytester.path) == [ct1, ct2]



def test_fixture_dependency(pytester: Pytester) -> None:
Expand Down
19 changes: 13 additions & 6 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import shutil
import sys
import types
from pathlib import Path
from typing import List

import pytest
Expand Down Expand Up @@ -46,7 +45,7 @@ def pytest_myhook(xyz):
kwargs=dict(pluginmanager=config.pluginmanager)
)
config.pluginmanager._importconftest(
conf, importmode="prepend", rootpath=Path(testdir.tmpdir)
conf, importmode="prepend", rootpath=pytester.path
)
# print(config.pluginmanager.get_plugins())
res = config.hook.pytest_myhook(xyz=10)
Expand Down Expand Up @@ -74,7 +73,9 @@ def pytest_addoption(parser):
default=True)
"""
)
config.pluginmanager._importconftest(p, importmode="prepend", rootpath=pytester.path)
config.pluginmanager._importconftest(
p, importmode="prepend", rootpath=pytester.path
)
assert config.option.test123

def test_configure(self, pytester: Pytester) -> None:
Expand Down Expand Up @@ -139,10 +140,14 @@ def test_hook_proxy(self, pytester: Pytester) -> None:
conftest1 = pytester.path.joinpath("tests/conftest.py")
conftest2 = pytester.path.joinpath("tests/subdir/conftest.py")

config.pluginmanager._importconftest(conftest1, importmode="prepend", rootpath=pytester.path)
config.pluginmanager._importconftest(
conftest1, importmode="prepend", rootpath=pytester.path
)
ihook_a = session.gethookproxy(pytester.path / "tests")
assert ihook_a is not None
config.pluginmanager._importconftest(conftest2, importmode="prepend", rootpath=pytester.path)
config.pluginmanager._importconftest(
conftest2, importmode="prepend", rootpath=pytester.path
)
ihook_b = session.gethookproxy(pytester.path / "tests")
assert ihook_a is not ihook_b

Expand Down Expand Up @@ -353,7 +358,9 @@ def test_consider_conftest_deps(
pytester: Pytester,
pytestpm: PytestPluginManager,
) -> None:
mod = import_path(pytester.makepyfile("pytest_plugins='xyz'"))
mod = import_path(
pytester.makepyfile("pytest_plugins='xyz'"), root=pytester.path
)
with pytest.raises(ImportError):
pytestpm.consider_conftest(mod)

Expand Down