Skip to content

Commit

Permalink
Simplify testing the warning filename (pythonGH-91868)
Browse files Browse the repository at this point in the history
The context manager result has the "filename" attribute.
  • Loading branch information
serhiy-storchaka authored Apr 24, 2022
1 parent b4e0484 commit 0907217
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 43 deletions.
16 changes: 8 additions & 8 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,12 +2712,12 @@ def get_event_loop(self):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

with self.assertRaisesRegex(RuntimeError, 'no running'):
asyncio.get_running_loop()
Expand All @@ -2734,13 +2734,13 @@ async def func():
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(TestError):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

finally:
asyncio.set_event_loop_policy(old_policy)
Expand All @@ -2762,12 +2762,12 @@ def test_get_event_loop_returns_running_loop2(self):
with self.assertWarns(DeprecationWarning) as cm:
loop2 = asyncio.get_event_loop()
self.addCleanup(loop2.close)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

with self.assertRaisesRegex(RuntimeError, 'no running'):
asyncio.get_running_loop()
Expand All @@ -2783,13 +2783,13 @@ async def func():
asyncio.set_event_loop(loop)
with self.assertWarns(DeprecationWarning) as cm:
self.assertIs(asyncio.get_event_loop(), loop)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

asyncio.set_event_loop(None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'no current'):
asyncio.get_event_loop()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

finally:
asyncio.set_event_loop_policy(old_policy)
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_constructor_without_loop(self):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
self._new_future()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_constructor_use_running_loop(self):
async def test():
Expand All @@ -163,7 +163,7 @@ def test_constructor_use_global_loop(self):
self.addCleanup(asyncio.set_event_loop, None)
with self.assertWarns(DeprecationWarning) as cm:
f = self._new_future()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(f._loop, self.loop)
self.assertIs(f.get_loop(), self.loop)

Expand Down Expand Up @@ -510,7 +510,7 @@ def run(arg):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(RuntimeError):
asyncio.wrap_future(f1)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
ex.shutdown(wait=True)

def test_wrap_future_use_running_loop(self):
Expand All @@ -534,7 +534,7 @@ def run(arg):
f1 = ex.submit(run, 'oi')
with self.assertWarns(DeprecationWarning) as cm:
f2 = asyncio.wrap_future(f1)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(self.loop, f2._loop)
ex.shutdown(wait=True)

Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def test_streamreader_constructor_without_loop(self):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
asyncio.StreamReader()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_streamreader_constructor_use_running_loop(self):
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
Expand All @@ -832,7 +832,7 @@ def test_streamreader_constructor_use_global_loop(self):
asyncio.set_event_loop(self.loop)
with self.assertWarns(DeprecationWarning) as cm:
reader = asyncio.StreamReader()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(reader._loop, self.loop)


Expand All @@ -841,7 +841,7 @@ def test_streamreaderprotocol_constructor_without_loop(self):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
asyncio.StreamReaderProtocol(reader)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_streamreaderprotocol_constructor_use_running_loop(self):
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
Expand All @@ -861,7 +861,7 @@ def test_streamreaderprotocol_constructor_use_global_loop(self):
reader = mock.Mock()
with self.assertWarns(DeprecationWarning) as cm:
protocol = asyncio.StreamReaderProtocol(reader)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(protocol._loop, self.loop)

def test_drain_raises(self):
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ async def notmuch():
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
asyncio.ensure_future(a)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

async def test():
return asyncio.ensure_future(notmuch())
Expand All @@ -226,7 +226,7 @@ async def test():
self.addCleanup(asyncio.set_event_loop, None)
with self.assertWarns(DeprecationWarning) as cm:
t = asyncio.ensure_future(notmuch())
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(t._loop, self.loop)
self.loop.run_until_complete(t)
self.assertTrue(t.done())
Expand Down Expand Up @@ -1449,7 +1449,7 @@ async def coro():
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
list(futs)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_as_completed_coroutine_use_running_loop(self):
loop = self.new_test_loop()
Expand Down Expand Up @@ -1881,7 +1881,7 @@ async def coro():
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
asyncio.shield(inner)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_shield_coroutine_use_running_loop(self):
async def coro():
Expand All @@ -1903,7 +1903,7 @@ async def coro():
self.addCleanup(asyncio.set_event_loop, None)
with self.assertWarns(DeprecationWarning) as cm:
outer = asyncio.shield(coro())
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertEqual(outer._loop, self.loop)
res = self.loop.run_until_complete(outer)
self.assertEqual(res, 42)
Expand Down Expand Up @@ -2911,7 +2911,7 @@ def test_constructor_empty_sequence_without_loop(self):
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(RuntimeError):
asyncio.gather()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_constructor_empty_sequence_use_running_loop(self):
async def gather():
Expand All @@ -2929,7 +2929,7 @@ def test_constructor_empty_sequence_use_global_loop(self):
self.addCleanup(asyncio.set_event_loop, None)
with self.assertWarns(DeprecationWarning) as cm:
fut = asyncio.gather()
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIsInstance(fut, asyncio.Future)
self.assertIs(fut._loop, self.one_loop)
self._run_loop(self.one_loop)
Expand Down Expand Up @@ -3020,7 +3020,7 @@ async def coro():
with self.assertWarns(DeprecationWarning) as cm:
with self.assertRaises(RuntimeError):
asyncio.gather(gen1, gen2)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)

def test_constructor_use_running_loop(self):
async def coro():
Expand All @@ -3043,7 +3043,7 @@ async def coro():
gen2 = coro()
with self.assertWarns(DeprecationWarning) as cm:
fut = asyncio.gather(gen1, gen2)
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(cm.filename, __file__)
self.assertIs(fut._loop, self.other_loop)
self.other_loop.run_until_complete(fut)

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2608,11 +2608,11 @@ def test_deprecated_modules(self):
for name in deprecated:
with self.subTest(module=name):
sys.modules.pop(name, None)
with self.assertWarns(DeprecationWarning) as cm:
with self.assertWarns(DeprecationWarning) as w:
__import__(name)
self.assertEqual(str(cm.warnings[0].message),
self.assertEqual(str(w.warning),
f"module {name!r} is deprecated")
self.assertEqual(cm.warnings[0].filename, __file__)
self.assertEqual(w.filename, __file__)
self.assertIn(name, sys.modules)
mod = sys.modules[name]
self.assertEqual(mod.__name__, name)
Expand Down
12 changes: 6 additions & 6 deletions Lib/unittest/test/test_async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ async def test2(self):

with self.assertWarns(DeprecationWarning) as w:
Test('test1').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
self.assertIn('test1', str(w.warnings[0].message))
self.assertEqual(w.warnings[0].filename, __file__)
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('test1', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Test('test2').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
self.assertIn('test2', str(w.warnings[0].message))
self.assertEqual(w.warnings[0].filename, __file__)
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('test2', str(w.warning))
self.assertEqual(w.filename, __file__)

def test_cleanups_interleave_order(self):
events = []
Expand Down
12 changes: 6 additions & 6 deletions Lib/unittest/test/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ def test2(self):

with self.assertWarns(DeprecationWarning) as w:
Foo('test1').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
self.assertIn('test1', str(w.warnings[0].message))
self.assertEqual(w.warnings[0].filename, __file__)
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('test1', str(w.warning))
self.assertEqual(w.filename, __file__)

with self.assertWarns(DeprecationWarning) as w:
Foo('test2').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
self.assertIn('test2', str(w.warnings[0].message))
self.assertEqual(w.warnings[0].filename, __file__)
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
self.assertIn('test2', str(w.warning))
self.assertEqual(w.filename, __file__)

def _check_call_order__subtests(self, result, events, expected_events):
class Foo(Test.LoggingTestCase):
Expand Down
6 changes: 3 additions & 3 deletions Lib/unittest/test/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,15 +1609,15 @@ def test_getTestCaseNames(self):
tests = unittest.getTestCaseNames(self.MyTestCase,
prefix='check', sortUsing=self.reverse_three_way_cmp,
testNamePatterns=None)
self.assertEqual(w.warnings[0].filename, __file__)
self.assertEqual(w.filename, __file__)
self.assertEqual(tests, ['check_2', 'check_1'])

def test_makeSuite(self):
with self.assertWarns(DeprecationWarning) as w:
suite = unittest.makeSuite(self.MyTestCase,
prefix='check', sortUsing=self.reverse_three_way_cmp,
suiteClass=self.MyTestSuite)
self.assertEqual(w.warnings[0].filename, __file__)
self.assertEqual(w.filename, __file__)
self.assertIsInstance(suite, self.MyTestSuite)
expected = self.MyTestSuite([self.MyTestCase('check_2'),
self.MyTestCase('check_1')])
Expand All @@ -1631,7 +1631,7 @@ def test_findTestCases(self):
suite = unittest.findTestCases(m,
prefix='check', sortUsing=self.reverse_three_way_cmp,
suiteClass=self.MyTestSuite)
self.assertEqual(w.warnings[0].filename, __file__)
self.assertEqual(w.filename, __file__)
self.assertIsInstance(suite, self.MyTestSuite)
expected = [self.MyTestSuite([self.MyTestCase('check_2'),
self.MyTestCase('check_1')])]
Expand Down

0 comments on commit 0907217

Please sign in to comment.