Skip to content

Commit

Permalink
[3.13] gh-122255: Add black box tests in test_warnings (GH-122227) (G…
Browse files Browse the repository at this point in the history
…H-122818)

gh-122255: Add black box tests in test_warnings (GH-122227)

They are similar to white box tests for gh-86298 in test_importlib.
(cherry picked from commit fe13c9b)

Co-authored-by: Serhiy Storchaka <[email protected]>
  • Loading branch information
miss-islington and serhiy-storchaka authored Aug 9, 2024
1 parent bcce4cc commit 46f973f
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,97 @@ class NonWarningSubclass:
self.module.warn('good warning category', MyWarningClass)
self.assertIsInstance(cm.warning, Warning)

def check_module_globals(self, module_globals):
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('default')
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, UserWarning)
self.assertEqual(str(w[0].message), 'eggs')

def check_module_globals_error(self, module_globals, errmsg, errtype=ValueError):
if self.module is py_warnings:
self.check_module_globals(module_globals)
return
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('always')
with self.assertRaisesRegex(errtype, re.escape(errmsg)):
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 0)

def check_module_globals_deprecated(self, module_globals, msg):
if self.module is py_warnings:
self.check_module_globals(module_globals)
return
with original_warnings.catch_warnings(module=self.module, record=True) as w:
self.module.filterwarnings('always')
self.module.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals=module_globals)
self.assertEqual(len(w), 2)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertEqual(str(w[0].message), msg)
self.assertEqual(w[1].category, UserWarning)
self.assertEqual(str(w[1].message), 'eggs')

def test_gh86298_no_loader_and_no_spec(self):
self.check_module_globals({'__name__': 'bar'})

def test_gh86298_loader_is_none_and_no_spec(self):
self.check_module_globals({'__name__': 'bar', '__loader__': None})

def test_gh86298_no_loader_and_spec_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__spec__': None},
'Module globals is missing a __spec__.loader')

def test_gh86298_loader_is_none_and_spec_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__loader__': None, '__spec__': None},
'Module globals is missing a __spec__.loader')

def test_gh86298_loader_is_none_and_spec_loader_is_none(self):
self.check_module_globals_error(
{'__name__': 'bar', '__loader__': None,
'__spec__': types.SimpleNamespace(loader=None)},
'Module globals is missing a __spec__.loader')

def test_gh86298_no_spec(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object()},
'Module globals is missing a __spec__.loader')

def test_gh86298_spec_is_none(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(), '__spec__': None},
'Module globals is missing a __spec__.loader')

def test_gh86298_no_spec_loader(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(),
'__spec__': types.SimpleNamespace()},
'Module globals is missing a __spec__.loader')

def test_gh86298_loader_and_spec_loader_disagree(self):
self.check_module_globals_deprecated(
{'__name__': 'bar', '__loader__': object(),
'__spec__': types.SimpleNamespace(loader=object())},
'Module globals; __loader__ != __spec__.loader')

def test_gh86298_no_loader_and_no_spec_loader(self):
self.check_module_globals_error(
{'__name__': 'bar', '__spec__': types.SimpleNamespace()},
'Module globals is missing a __spec__.loader', AttributeError)

def test_gh86298_no_loader_with_spec_loader_okay(self):
self.check_module_globals(
{'__name__': 'bar',
'__spec__': types.SimpleNamespace(loader=object())})

class CWarnTests(WarnTests, unittest.TestCase):
module = c_warnings

Expand Down

0 comments on commit 46f973f

Please sign in to comment.