2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2016-01-24 01:08:18 +08:00
|
|
|
import os
|
2020-12-15 19:02:32 +08:00
|
|
|
from pathlib import Path
|
2018-11-01 01:22:42 +08:00
|
|
|
import re
|
2016-01-24 01:08:18 +08:00
|
|
|
import sys
|
2015-12-28 05:42:42 +08:00
|
|
|
import textwrap
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Dict
|
|
|
|
from typing import Generator
|
2020-10-03 04:02:22 +08:00
|
|
|
from typing import Type
|
2015-12-28 05:42:42 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
2020-05-01 19:40:17 +08:00
|
|
|
def mp() -> Generator[MonkeyPatch, None, None]:
|
2012-01-04 19:40:57 +08:00
|
|
|
cwd = os.getcwd()
|
|
|
|
sys_path = list(sys.path)
|
2017-01-04 21:31:44 +08:00
|
|
|
yield MonkeyPatch()
|
|
|
|
sys.path[:] = sys_path
|
|
|
|
os.chdir(cwd)
|
2012-01-04 19:40:57 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_setattr() -> None:
|
2017-02-17 02:41:51 +08:00
|
|
|
class A:
|
2009-09-06 22:59:39 +08:00
|
|
|
x = 1
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(AttributeError, monkeypatch.setattr, A, "notexists", 2)
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(A, "y", 2, raising=False)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert A.y == 2 # type: ignore
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.undo()
|
|
|
|
assert not hasattr(A, "y")
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setattr(A, "x", 2)
|
|
|
|
assert A.x == 2
|
|
|
|
monkeypatch.setattr(A, "x", 3)
|
|
|
|
assert A.x == 3
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
A.x = 5
|
2016-01-24 01:08:18 +08:00
|
|
|
monkeypatch.undo() # double-undo makes no modification
|
2009-09-06 22:59:39 +08:00
|
|
|
assert A.x == 5
|
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
monkeypatch.setattr(A, "y") # type: ignore[call-overload]
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSetattrWithImportPath:
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_string_expression(self, monkeypatch: MonkeyPatch) -> None:
|
2022-01-09 06:12:33 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.setattr("os.path.abspath", lambda x: "hello2")
|
|
|
|
assert os.path.abspath("123") == "hello2"
|
2013-08-07 21:35:27 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_string_expression_class(self, monkeypatch: MonkeyPatch) -> None:
|
2022-01-09 06:12:33 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.setattr("_pytest.config.Config", 42)
|
|
|
|
import _pytest
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2022-01-09 06:12:33 +08:00
|
|
|
assert _pytest.config.Config == 42 # type: ignore
|
2013-08-07 21:35:27 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_unicode_string(self, monkeypatch: MonkeyPatch) -> None:
|
2022-01-09 06:12:33 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.setattr("_pytest.config.Config", 42)
|
|
|
|
import _pytest
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2022-01-09 06:12:33 +08:00
|
|
|
assert _pytest.config.Config == 42 # type: ignore
|
|
|
|
mp.delattr("_pytest.config.Config")
|
2013-10-21 20:01:02 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_wrong_target(self, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
monkeypatch.setattr(None, None) # type: ignore[call-overload]
|
2013-08-07 22:49:29 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_unknown_import(self, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
monkeypatch.setattr("unkn123.classx", None)
|
2013-08-07 22:49:29 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_unknown_attr(self, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
monkeypatch.setattr("os.path.qweqwe", None)
|
2013-09-27 18:33:06 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_unknown_attr_non_raising(self, monkeypatch: MonkeyPatch) -> None:
|
2015-06-16 05:28:31 +08:00
|
|
|
# https://github.com/pytest-dev/pytest/issues/746
|
2022-01-09 06:12:33 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.setattr("os.path.qweqwe", 42, raising=False)
|
|
|
|
assert os.path.qweqwe == 42 # type: ignore
|
2015-05-20 04:59:49 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_delattr(self, monkeypatch: MonkeyPatch) -> None:
|
2022-01-09 06:12:33 +08:00
|
|
|
with monkeypatch.context() as mp:
|
|
|
|
mp.delattr("os.path.abspath")
|
|
|
|
assert not hasattr(os.path, "abspath")
|
|
|
|
mp.undo()
|
2022-11-15 19:53:23 +08:00
|
|
|
assert os.path.abspath # type:ignore[truthy-function]
|
2013-08-07 22:49:29 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_delattr() -> None:
|
2017-02-17 02:41:51 +08:00
|
|
|
class A:
|
2009-09-06 22:59:39 +08:00
|
|
|
x = 1
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delattr(A, "x")
|
|
|
|
assert not hasattr(A, "x")
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delattr(A, "x")
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(AttributeError, monkeypatch.delattr, A, "y")
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.delattr(A, "y", raising=False)
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setattr(A, "x", 5, raising=False)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert A.x == 5
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert A.x == 1
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_setitem() -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
d = {"x": 1}
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setitem(d, "x", 2)
|
|
|
|
monkeypatch.setitem(d, "y", 1700)
|
2009-09-07 23:53:50 +08:00
|
|
|
monkeypatch.setitem(d, "y", 1700)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert d["x"] == 2
|
|
|
|
assert d["y"] == 1700
|
|
|
|
monkeypatch.setitem(d, "x", 3)
|
|
|
|
assert d["x"] == 3
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d["x"] == 1
|
|
|
|
assert "y" not in d
|
|
|
|
d["x"] = 5
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d["x"] == 5
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_setitem_deleted_meanwhile() -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
d: Dict[str, object] = {}
|
2011-12-17 06:41:23 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.setitem(d, "x", 2)
|
|
|
|
del d["x"]
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert not d
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2011-12-28 23:49:13 +08:00
|
|
|
@pytest.mark.parametrize("before", [True, False])
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_setenv_deleted_meanwhile(before: bool) -> None:
|
2011-12-28 23:49:13 +08:00
|
|
|
key = "qwpeoip123"
|
|
|
|
if before:
|
|
|
|
os.environ[key] = "world"
|
2011-12-17 06:41:23 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2011-12-28 23:49:13 +08:00
|
|
|
monkeypatch.setenv(key, "hello")
|
|
|
|
del os.environ[key]
|
2011-12-17 06:41:23 +08:00
|
|
|
monkeypatch.undo()
|
2011-12-28 23:49:13 +08:00
|
|
|
if before:
|
|
|
|
assert os.environ[key] == "world"
|
|
|
|
del os.environ[key]
|
|
|
|
else:
|
|
|
|
assert key not in os.environ
|
2011-12-17 06:41:23 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_delitem() -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
d: Dict[str, object] = {"x": 1}
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delitem(d, "x")
|
|
|
|
assert "x" not in d
|
|
|
|
monkeypatch.delitem(d, "y", raising=False)
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(KeyError, monkeypatch.delitem, d, "y")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert not d
|
|
|
|
monkeypatch.setitem(d, "y", 1700)
|
|
|
|
assert d["y"] == 1700
|
|
|
|
d["hello"] = "world"
|
|
|
|
monkeypatch.setitem(d, "x", 1500)
|
|
|
|
assert d["x"] == 1500
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert d == {"hello": "world", "x": 1}
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_setenv() -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2018-10-02 05:48:41 +08:00
|
|
|
with pytest.warns(pytest.PytestWarning):
|
2020-06-07 17:44:11 +08:00
|
|
|
monkeypatch.setenv("XYZ123", 2) # type: ignore[arg-type]
|
2009-09-06 22:59:39 +08:00
|
|
|
import os
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
assert os.environ["XYZ123"] == "2"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert "XYZ123" not in os.environ
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_delenv() -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
name = "xyz1234"
|
2010-07-27 03:15:15 +08:00
|
|
|
assert name not in os.environ
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(KeyError, monkeypatch.delenv, name, raising=True)
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
monkeypatch.undo()
|
|
|
|
os.environ[name] = "1"
|
|
|
|
try:
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
monkeypatch.delenv(name)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert name not in os.environ
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch.setenv(name, "3")
|
|
|
|
assert os.environ[name] == "3"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert os.environ[name] == "1"
|
|
|
|
finally:
|
|
|
|
if name in os.environ:
|
|
|
|
del os.environ[name]
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2018-10-02 05:48:41 +08:00
|
|
|
class TestEnvironWarnings:
|
2018-09-30 06:55:04 +08:00
|
|
|
"""
|
2018-10-02 05:48:41 +08:00
|
|
|
os.environ keys and values should be native strings, otherwise it will cause problems with other modules (notably
|
|
|
|
subprocess). On Python 2 os.environ accepts anything without complaining, while Python 3 does the right thing
|
|
|
|
and raises an error.
|
2018-09-30 06:55:04 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
VAR_NAME = "PYTEST_INTERNAL_MY_VAR"
|
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_setenv_non_str_warning(self, monkeypatch: MonkeyPatch) -> None:
|
2018-10-02 05:48:41 +08:00
|
|
|
value = 2
|
|
|
|
msg = (
|
2018-11-01 01:22:42 +08:00
|
|
|
"Value of environment variable PYTEST_INTERNAL_MY_VAR type should be str, "
|
|
|
|
"but got 2 (type: int); converted to str implicitly"
|
2018-10-02 05:48:41 +08:00
|
|
|
)
|
2018-11-01 01:22:42 +08:00
|
|
|
with pytest.warns(pytest.PytestWarning, match=re.escape(msg)):
|
2020-06-07 17:44:11 +08:00
|
|
|
monkeypatch.setenv(str(self.VAR_NAME), value) # type: ignore[arg-type]
|
2018-10-02 05:48:41 +08:00
|
|
|
|
2018-09-30 06:55:04 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_setenv_prepend() -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
import os
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
2020-06-07 17:44:11 +08:00
|
|
|
monkeypatch.setenv("XYZ123", "2", prepend="-")
|
|
|
|
monkeypatch.setenv("XYZ123", "3", prepend="-")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert os.environ["XYZ123"] == "3-2"
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert "XYZ123" not in os.environ
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_monkeypatch_plugin(pytester: Pytester) -> None:
|
|
|
|
reprec = pytester.inline_runsource(
|
2009-09-06 22:59:39 +08:00
|
|
|
"""
|
|
|
|
def test_method(monkeypatch):
|
2016-07-12 09:03:53 +08:00
|
|
|
assert monkeypatch.__class__.__name__ == "MonkeyPatch"
|
2009-09-06 22:59:39 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
res = reprec.countoutcomes()
|
|
|
|
assert tuple(res) == (1, 0, 0), res
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_syspath_prepend(mp: MonkeyPatch) -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
old = list(sys.path)
|
2012-01-04 19:40:57 +08:00
|
|
|
mp.syspath_prepend("world")
|
|
|
|
mp.syspath_prepend("hello")
|
|
|
|
assert sys.path[0] == "hello"
|
|
|
|
assert sys.path[1] == "world"
|
|
|
|
mp.undo()
|
|
|
|
assert sys.path == old
|
|
|
|
mp.undo()
|
|
|
|
assert sys.path == old
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_syspath_prepend_double_undo(mp: MonkeyPatch) -> None:
|
2018-08-26 05:17:52 +08:00
|
|
|
old_syspath = sys.path[:]
|
|
|
|
try:
|
|
|
|
mp.syspath_prepend("hello world")
|
|
|
|
mp.undo()
|
|
|
|
sys.path.append("more hello world")
|
|
|
|
mp.undo()
|
|
|
|
assert sys.path[-1] == "more hello world"
|
|
|
|
finally:
|
|
|
|
sys.path[:] = old_syspath
|
2012-01-04 19:43:19 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chdir_with_path_local(mp: MonkeyPatch, tmp_path: Path) -> None:
|
|
|
|
mp.chdir(tmp_path)
|
|
|
|
assert os.getcwd() == str(tmp_path)
|
2012-01-04 19:43:19 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chdir_with_str(mp: MonkeyPatch, tmp_path: Path) -> None:
|
|
|
|
mp.chdir(str(tmp_path))
|
|
|
|
assert os.getcwd() == str(tmp_path)
|
2012-01-04 19:43:19 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chdir_undo(mp: MonkeyPatch, tmp_path: Path) -> None:
|
2012-01-04 19:43:19 +08:00
|
|
|
cwd = os.getcwd()
|
2020-12-15 19:02:32 +08:00
|
|
|
mp.chdir(tmp_path)
|
2012-01-04 19:43:19 +08:00
|
|
|
mp.undo()
|
|
|
|
assert os.getcwd() == cwd
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_chdir_double_undo(mp: MonkeyPatch, tmp_path: Path) -> None:
|
|
|
|
mp.chdir(str(tmp_path))
|
2012-01-04 19:43:19 +08:00
|
|
|
mp.undo()
|
2020-12-15 19:02:32 +08:00
|
|
|
os.chdir(tmp_path)
|
2012-01-04 19:43:19 +08:00
|
|
|
mp.undo()
|
2020-12-15 19:02:32 +08:00
|
|
|
assert os.getcwd() == str(tmp_path)
|
2012-09-01 15:58:10 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_issue185_time_breaks(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-09-01 15:58:10 +08:00
|
|
|
"""
|
|
|
|
import time
|
|
|
|
def test_m(monkeypatch):
|
|
|
|
def f():
|
|
|
|
raise Exception
|
|
|
|
monkeypatch.setattr(time, "time", f)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2012-09-01 15:58:10 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
|
|
|
*1 passed*
|
|
|
|
"""
|
|
|
|
)
|
2012-09-26 00:15:13 +08:00
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_importerror(pytester: Pytester) -> None:
|
|
|
|
p = pytester.mkpydir("package")
|
|
|
|
p.joinpath("a.py").write_text(
|
2015-12-28 05:42:42 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import doesnotexist
|
|
|
|
|
|
|
|
x = 1
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.path.joinpath("test_importerror.py").write_text(
|
2015-12-28 05:42:42 +08:00
|
|
|
textwrap.dedent(
|
2018-11-01 23:40:35 +08:00
|
|
|
"""\
|
2015-12-28 05:42:42 +08:00
|
|
|
def test_importerror(monkeypatch):
|
|
|
|
monkeypatch.setattr('package.a.x', 2)
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2015-12-28 05:42:42 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2019-05-28 07:31:52 +08:00
|
|
|
*import error in package.a: No module named 'doesnotexist'*
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2012-09-26 00:15:13 +08:00
|
|
|
|
|
|
|
|
2020-04-09 22:11:18 +08:00
|
|
|
class Sample:
|
2012-09-26 00:15:13 +08:00
|
|
|
@staticmethod
|
2020-04-09 22:11:18 +08:00
|
|
|
def hello() -> bool:
|
2012-09-26 00:15:13 +08:00
|
|
|
return True
|
|
|
|
|
2016-01-24 01:08:18 +08:00
|
|
|
|
2020-04-09 22:11:18 +08:00
|
|
|
class SampleInherit(Sample):
|
2012-09-26 00:15:13 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2020-12-30 17:56:09 +08:00
|
|
|
"Sample",
|
|
|
|
[Sample, SampleInherit],
|
|
|
|
ids=["new", "new-inherit"],
|
2012-09-26 00:15:13 +08:00
|
|
|
)
|
2020-10-03 04:02:22 +08:00
|
|
|
def test_issue156_undo_staticmethod(Sample: Type[Sample]) -> None:
|
2012-09-26 00:15:13 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
|
|
|
|
monkeypatch.setattr(Sample, "hello", None)
|
|
|
|
assert Sample.hello is None
|
|
|
|
|
2020-08-04 00:15:21 +08:00
|
|
|
monkeypatch.undo() # type: ignore[unreachable]
|
2012-09-26 00:15:13 +08:00
|
|
|
assert Sample.hello()
|
2016-01-24 02:12:10 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_undo_class_descriptors_delattr() -> None:
|
2018-12-13 04:20:24 +08:00
|
|
|
class SampleParent:
|
|
|
|
@classmethod
|
|
|
|
def hello(_cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def world():
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SampleChild(SampleParent):
|
|
|
|
pass
|
|
|
|
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
|
|
|
|
original_hello = SampleChild.hello
|
|
|
|
original_world = SampleChild.world
|
|
|
|
monkeypatch.delattr(SampleParent, "hello")
|
|
|
|
monkeypatch.delattr(SampleParent, "world")
|
|
|
|
assert getattr(SampleParent, "hello", None) is None
|
|
|
|
assert getattr(SampleParent, "world", None) is None
|
|
|
|
|
|
|
|
monkeypatch.undo()
|
|
|
|
assert original_hello == SampleChild.hello
|
|
|
|
assert original_world == SampleChild.world
|
|
|
|
|
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_issue1338_name_resolving() -> None:
|
2016-01-24 02:12:10 +08:00
|
|
|
pytest.importorskip("requests")
|
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
try:
|
2017-07-17 07:25:06 +08:00
|
|
|
monkeypatch.delattr("requests.sessions.Session.request")
|
2016-01-24 02:12:10 +08:00
|
|
|
finally:
|
2016-07-21 09:05:49 +08:00
|
|
|
monkeypatch.undo()
|
2018-04-11 01:18:05 +08:00
|
|
|
|
|
|
|
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_context() -> None:
|
2018-04-13 21:00:07 +08:00
|
|
|
monkeypatch = MonkeyPatch()
|
|
|
|
|
2018-04-11 01:18:05 +08:00
|
|
|
import functools
|
2018-04-13 21:00:07 +08:00
|
|
|
import inspect
|
2018-04-11 01:18:05 +08:00
|
|
|
|
2018-04-13 21:00:07 +08:00
|
|
|
with monkeypatch.context() as m:
|
|
|
|
m.setattr(functools, "partial", 3)
|
|
|
|
assert not inspect.isclass(functools.partial)
|
|
|
|
assert inspect.isclass(functools.partial)
|
2019-03-22 21:36:11 +08:00
|
|
|
|
|
|
|
|
2020-11-07 01:27:33 +08:00
|
|
|
def test_context_classmethod() -> None:
|
|
|
|
class A:
|
|
|
|
x = 1
|
|
|
|
|
|
|
|
with MonkeyPatch.context() as m:
|
|
|
|
m.setattr(A, "x", 2)
|
|
|
|
assert A.x == 2
|
|
|
|
assert A.x == 1
|
|
|
|
|
|
|
|
|
2023-04-25 16:51:10 +08:00
|
|
|
@pytest.mark.filterwarnings(r"ignore:.*\bpkg_resources\b:DeprecationWarning")
|
2020-06-07 17:44:11 +08:00
|
|
|
def test_syspath_prepend_with_namespace_packages(
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
2020-06-07 17:44:11 +08:00
|
|
|
) -> None:
|
2019-03-22 21:36:11 +08:00
|
|
|
for dirname in "hello", "world":
|
2020-12-15 19:02:32 +08:00
|
|
|
d = pytester.mkdir(dirname)
|
|
|
|
ns = d.joinpath("ns_pkg")
|
|
|
|
ns.mkdir()
|
|
|
|
ns.joinpath("__init__.py").write_text(
|
2023-06-20 19:55:39 +08:00
|
|
|
"__import__('pkg_resources').declare_namespace(__name__)", encoding="utf-8"
|
2019-03-22 21:36:11 +08:00
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
lib = ns.joinpath(dirname)
|
|
|
|
lib.mkdir()
|
2023-06-20 19:55:39 +08:00
|
|
|
lib.joinpath("__init__.py").write_text(
|
|
|
|
"def check(): return %r" % dirname, encoding="utf-8"
|
|
|
|
)
|
2019-03-22 21:36:11 +08:00
|
|
|
|
|
|
|
monkeypatch.syspath_prepend("hello")
|
|
|
|
import ns_pkg.hello
|
|
|
|
|
|
|
|
assert ns_pkg.hello.check() == "hello"
|
|
|
|
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
import ns_pkg.world
|
|
|
|
|
|
|
|
# Prepending should call fixup_namespace_packages.
|
|
|
|
monkeypatch.syspath_prepend("world")
|
|
|
|
import ns_pkg.world
|
|
|
|
|
|
|
|
assert ns_pkg.world.check() == "world"
|
2019-04-12 23:27:36 +08:00
|
|
|
|
|
|
|
# Should invalidate caches via importlib.invalidate_caches.
|
2020-12-15 19:02:32 +08:00
|
|
|
modules_tmpdir = pytester.mkdir("modules_tmpdir")
|
2019-04-12 23:27:36 +08:00
|
|
|
monkeypatch.syspath_prepend(str(modules_tmpdir))
|
2023-06-20 19:55:40 +08:00
|
|
|
modules_tmpdir.joinpath("main_app.py").write_text("app = True", encoding="utf-8")
|
2019-04-12 23:27:36 +08:00
|
|
|
from main_app import app # noqa: F401
|