fix test pollution of sys.modules

This commit is contained in:
Anthony Sottile 2022-10-19 22:07:53 -04:00
parent 82344ba4f8
commit dc0cb0d149
2 changed files with 13 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import multiprocessing
import os
import sys
import time
from unittest import mock
import pytest
from py import error
@ -978,6 +979,12 @@ class TestExecution:
class TestImport:
@pytest.fixture(autouse=True)
def preserve_sys(self):
with mock.patch.dict(sys.modules):
with mock.patch.object(sys, "path", list(sys.path)):
yield
def test_pyimport(self, path1):
obj = path1.join("execfile.py").pyimport()
assert obj.x == 42

View File

@ -91,6 +91,12 @@ class TestImportPath:
yield path
assert path.joinpath("samplefile").exists()
@pytest.fixture(autouse=True)
def preserve_sys(self):
with unittest.mock.patch.dict(sys.modules):
with unittest.mock.patch.object(sys, "path", list(sys.path)):
yield
def setuptestfs(self, path: Path) -> None:
# print "setting up test fs for", repr(path)
samplefile = path / "samplefile"