From 8011ff5bda074ca1b6419a768ec32277203106e6 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 5 Apr 2019 11:31:02 +0200 Subject: [PATCH] Add _sys_snapshot fixture and use it with more tests --- src/_pytest/pytester.py | 9 +++++++++ testing/acceptance_test.py | 2 +- testing/code/test_excinfo.py | 2 +- testing/code/test_source.py | 2 +- testing/test_config.py | 8 ++++---- testing/test_conftest.py | 14 ++------------ 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index a802a56f0..31cea5923 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -335,6 +335,15 @@ def testdir(request, tmpdir_factory): return Testdir(request, tmpdir_factory) +@pytest.fixture +def _sys_snapshot(): + snappaths = SysPathsSnapshot() + snapmods = SysModulesSnapshot() + yield + snapmods.restore() + snappaths.restore() + + @pytest.fixture def _config_for_test(): from _pytest.config import get_config diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 408fa076e..13a765411 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -485,7 +485,7 @@ class TestGeneralUsage(object): ["*source code not available*", "E*fixture 'invalid_fixture' not found"] ) - def test_plugins_given_as_strings(self, tmpdir, monkeypatch): + def test_plugins_given_as_strings(self, tmpdir, monkeypatch, _sys_snapshot): """test that str values passed to main() as `plugins` arg are interpreted as module names to be imported and registered. #855. diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 4e36fb946..5a4ab8808 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -441,7 +441,7 @@ def test_match_raises_error(testdir): class TestFormattedExcinfo(object): @pytest.fixture - def importasmod(self, request): + def importasmod(self, request, _sys_snapshot): def importasmod(source): source = textwrap.dedent(source) tmpdir = request.getfixturevalue("tmpdir") diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 965838dae..aa56273c4 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -410,7 +410,7 @@ def test_deindent(): assert lines == ["def f():", " def g():", " pass"] -def test_source_of_class_at_eof_without_newline(tmpdir): +def test_source_of_class_at_eof_without_newline(tmpdir, _sys_snapshot): # this test fails because the implicit inspect.getsource(A) below # does not return the "x = 1" last line. source = _pytest._code.Source( diff --git a/testing/test_config.py b/testing/test_config.py index c90333a00..68c948a41 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -436,7 +436,7 @@ class TestConfigAPI(object): class TestConfigFromdictargs(object): - def test_basic_behavior(self): + def test_basic_behavior(self, _sys_snapshot): from _pytest.config import Config option_dict = {"verbose": 444, "foo": "bar", "capture": "no"} @@ -450,7 +450,7 @@ class TestConfigFromdictargs(object): assert config.option.capture == "no" assert config.args == args - def test_origargs(self): + def test_origargs(self, _sys_snapshot): """Show that fromdictargs can handle args in their "orig" format""" from _pytest.config import Config @@ -1057,7 +1057,7 @@ class TestOverrideIniArgs(object): assert rootdir == tmpdir assert inifile is None - def test_addopts_before_initini(self, monkeypatch, _config_for_test): + def test_addopts_before_initini(self, monkeypatch, _config_for_test, _sys_snapshot): cache_dir = ".custom_cache" monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir) config = _config_for_test @@ -1092,7 +1092,7 @@ class TestOverrideIniArgs(object): ) assert result.ret == _pytest.main.EXIT_USAGEERROR - def test_override_ini_does_not_contain_paths(self, _config_for_test): + def test_override_ini_does_not_contain_paths(self, _config_for_test, _sys_snapshot): """Check that -o no longer swallows all options after it (#3103)""" config = _config_for_test config._preparse(["-o", "cache_dir=/cache", "/some/test/path"]) diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 2342cb318..2596dafe9 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -11,8 +11,6 @@ from _pytest.config import PytestPluginManager from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import EXIT_OK from _pytest.main import EXIT_USAGEERROR -from _pytest.pytester import SysModulesSnapshot -from _pytest.pytester import SysPathsSnapshot def ConftestWithSetinitial(path): @@ -32,6 +30,7 @@ def conftest_setinitial(conftest, args, confcutdir=None): conftest._set_initial_conftests(Namespace()) +@pytest.mark.usefixtures("_sys_snapshot") class TestConftestValueAccessGlobal(object): @pytest.fixture(scope="module", params=["global", "inpackage"]) def basedir(self, request, tmpdir_factory): @@ -44,15 +43,6 @@ class TestConftestValueAccessGlobal(object): yield tmpdir - @pytest.fixture(autouse=True) - def restore(self): - """Restore sys.modules to prevent ConftestImportFailure when run randomly.""" - snapmods = SysModulesSnapshot() - snappath = SysPathsSnapshot() - yield - snapmods.restore() - snappath.restore() - def test_basic_init(self, basedir): conftest = PytestPluginManager() p = basedir.join("adir") @@ -91,7 +81,7 @@ class TestConftestValueAccessGlobal(object): assert path.purebasename.startswith("conftest") -def test_conftest_in_nonpkg_with_init(tmpdir): +def test_conftest_in_nonpkg_with_init(tmpdir, _sys_snapshot): tmpdir.ensure("adir-1.0/conftest.py").write("a=1 ; Directory = 3") tmpdir.ensure("adir-1.0/b/conftest.py").write("b=2 ; a = 1.5") tmpdir.ensure("adir-1.0/b/__init__.py")