Skip to content

[3.6] bpo-33747: Avoid mutating the global sys.modules dict in unittest.mock tests (GH-8520) #11032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance

from test.test_importlib.util import uncache
from unittest.mock import (
NonCallableMock, CallableMixin, sentinel,
MagicMock, Mock, NonCallableMagicMock, patch, _patch,
Expand Down Expand Up @@ -1660,20 +1661,19 @@ def test_mock_calls_with_patch(self):


def test_patch_imports_lazily(self):
sys.modules.pop('squizz', None)

p1 = patch('squizz.squozz')
self.assertRaises(ImportError, p1.start)

squizz = Mock()
squizz.squozz = 6
sys.modules['squizz'] = squizz
p1 = patch('squizz.squozz')
squizz.squozz = 3
p1.start()
p1.stop()
self.assertEqual(squizz.squozz, 3)
with uncache('squizz'):
squizz = Mock()
sys.modules['squizz'] = squizz

squizz.squozz = 6
p1 = patch('squizz.squozz')
squizz.squozz = 3
p1.start()
p1.stop()
self.assertEqual(squizz.squozz, 3)

def test_patch_propogrates_exc_on_exit(self):
class holder:
Expand All @@ -1696,7 +1696,12 @@ def with_custom_patch(target):
def test(mock):
raise RuntimeError

self.assertRaises(RuntimeError, test)
with uncache('squizz'):
squizz = Mock()
sys.modules['squizz'] = squizz

self.assertRaises(RuntimeError, test)

self.assertIs(holder.exc_info[0], RuntimeError)
self.assertIsNotNone(holder.exc_info[1],
'exception value not propgated')
Expand Down