Skip to content

Commit

Permalink
fix test pollution of sys.modules
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Oct 20, 2022
1 parent 82344ba commit dc0cb0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions testing/_py/test_local.py
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import time
from unittest import mock

import pytest
from py import error
Expand Down Expand Up @@ -978,6 +979,12 @@ def test_error_preservation(self, path1):


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
Expand Down
6 changes: 6 additions & 0 deletions testing/test_pathlib.py
Expand Up @@ -91,6 +91,12 @@ def path1(self, tmp_path_factory: TempPathFactory) -> Generator[Path, None, None
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"
Expand Down

0 comments on commit dc0cb0d

Please sign in to comment.