jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
Adopted use of Context Manager call for assertRaises/assertRaisesRegex

Bug: T267801
Change-Id: Ia47ff1d2457bad9532bae8bf38a8fa6607553861
---
M tests/deprecation_tests.py
M tests/diff_tests.py
M tests/djvu_tests.py
M tests/dry_api_tests.py
M tests/edit_failure_tests.py
5 files changed, 117 insertions(+), 100 deletions(-)

diff --git a/tests/deprecation_tests.py b/tests/deprecation_tests.py
index 68d3ce7..00221b7 100644
--- a/tests/deprecation_tests.py
+++ b/tests/deprecation_tests.py
@@ -61,18 +61,15 @@

def test_add_full_name_decorator(self):
"""Test add_decorated_full_name() method."""
- self.assertRaisesRegex(
- Exception,
- __name__ + '.decorated_func',
- decorated_func)
- self.assertRaisesRegex(
- Exception,
- __name__ + '.decorated_func2',
- decorated_func2)
- self.assertRaisesRegex(
- Exception,
- __name__ + '.decorated_func3',
- decorated_func3)
+ with self.assertRaisesRegex(Exception,
+ __name__ + '.decorated_func'):
+ decorated_func()
+ with self.assertRaisesRegex(Exception,
+ __name__ + '.decorated_func2'):
+ decorated_func2()
+ with self.assertRaisesRegex(Exception,
+ __name__ + '.decorated_func3'):
+ decorated_func3()


@deprecated()
@@ -446,11 +443,12 @@
self.assertEqual(rv, 1)
self.assertNoDeprecation()

- self.assertRaisesRegex(
- TypeError,
- r'deprecated_func_arg2?\(\) got multiple values for '
- "(keyword )?argument 'foo'", func, 'a', bah='b'
- )
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_func_arg2?\(\) got multiple values for '
+ "(keyword )?argument 'foo'"):
+ func('a', bah='b')
+
self._reset_messages()

tests(deprecated_func_arg)
@@ -607,45 +605,43 @@

def test_remove_last_args_invalid(self):
"""Test invalid @remove_last_args on functions."""
- self.assertRaisesRegex(
- TypeError,
- r'deprecated_all2\(\) missing 1 required positional argument: '
- r"'foo'",
- deprecated_all2)
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) missing 1 required positional argument: '
+ r"'foo'"):
+ deprecated_all2()

- self.assertRaisesRegex(
- TypeError,
- r"deprecated_all2\(\) got an unexpected keyword argument 'hello'",
- deprecated_all2,
- hello='world')
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) got an unexpected keyword argument '
+ r"'hello'"):
+ deprecated_all2(hello='world')

- self.assertRaisesRegex(
- TypeError,
- r'deprecated_all2\(\) takes (exactly )?1 (positional )?argument'
- r' (but 2 were given|\(2 given\))',
- deprecated_all2,
- 1, 2, 3)
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) takes (exactly )?1 (positional )?'
+ r'argument (but 2 were given|\(2 given\))'):
+ deprecated_all2(1, 2, 3)

f = DeprecatedMethodClass()

- self.assertRaisesRegex(
- TypeError,
- r'deprecated_all2\(\) missing 1 required positional argument: '
- r"'foo'",
- f.deprecated_all2)
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) missing 1 required positional argument: '
+ r"'foo'"):
+ f.deprecated_all2()

- self.assertRaisesRegex(
- TypeError,
- r"deprecated_all2\(\) got an unexpected keyword argument 'hello'",
- f.deprecated_all2,
- hello='world')
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) got an unexpected keyword argument '
+ r"'hello'"):
+ f.deprecated_all2(hello='world')

- self.assertRaisesRegex(
- TypeError,
- r'deprecated_all2\(\) takes (exactly )?2 (positional )?arguments '
- r'(but 3 were given|\(3 given\))',
- f.deprecated_all2,
- 1, 2, 3)
+ with self.assertRaisesRegex(
+ TypeError,
+ r'deprecated_all2\(\) takes (exactly )?2 (positional )?'
+ r'arguments (but 3 were given|\(3 given\))'):
+ f.deprecated_all2(1, 2, 3)

def test_deprecated_instance_method_zero_arg(self):
"""Test @deprecate_arg with classes, without arguments."""
diff --git a/tests/diff_tests.py b/tests/diff_tests.py
index 883b3d7..262a908 100644
--- a/tests/diff_tests.py
+++ b/tests/diff_tests.py
@@ -91,7 +91,8 @@

def test_html_comparator(self, mocked_import):
"""Test html_comparator when bs4 not installed."""
- self.assertRaises(ImportError, html_comparator, '')
+ with self.assertRaises(ImportError):
+ html_comparator('')
self.assertEqual(mocked_import.call_count, 1)
self.assertIn('bs4', mocked_import.call_args[0])

diff --git a/tests/djvu_tests.py b/tests/djvu_tests.py
index b34ec3d..d5d9058 100644
--- a/tests/djvu_tests.py
+++ b/tests/djvu_tests.py
@@ -59,7 +59,8 @@
"""Test file existence checks."""
djvu = DjVuFile(self.file_djvu)
self.assertEqual(os.path.abspath(self.file_djvu), djvu.file)
- self.assertRaises(IOError, DjVuFile, self.file_djvu_not_existing)
+ with self.assertRaises(IOError):
+ DjVuFile(self.file_djvu_not_existing)

def test_number_of_images(self):
"""Test page number generator."""
@@ -95,23 +96,27 @@
"""Test error is raised if djvu page number is out of range."""
djvu = DjVuFile(self.file_djvu)
self.assertTrue(djvu.has_text())
- self.assertRaises(ValueError, djvu.get_page, 100)
+ with self.assertRaises(ValueError):
+ djvu.get_page(100)

def test_get_not_existing_page(self):
"""Test error is raised if djvu file has no text."""
djvu = DjVuFile(self.file_djvu_wo_text)
self.assertFalse(djvu.has_text())
- self.assertRaises(ValueError, djvu.get_page, 1)
+ with self.assertRaises(ValueError):
+ djvu.get_page(1)

def test_whiten_not_existing_page_number(self):
"""Test djvu page cannot be whitend if page number is out of range."""
djvu = DjVuFile(self.file_djvu)
- self.assertRaises(ValueError, djvu.whiten_page, 100)
+ with self.assertRaises(ValueError):
+ djvu.whiten_page(100)

def test_delete_not_existing_page_number(self):
"""Test djvu page cannot be deleted if page number is out of range."""
djvu = DjVuFile(self.file_djvu)
- self.assertRaises(ValueError, djvu.delete_page, 100)
+ with self.assertRaises(ValueError):
+ djvu.delete_page(100)

def test_clear_cache(self):
"""Test if djvu file contains text."""
diff --git a/tests/dry_api_tests.py b/tests/dry_api_tests.py
index bd88503..083dcf3 100644
--- a/tests/dry_api_tests.py
+++ b/tests/dry_api_tests.py
@@ -243,16 +243,17 @@
"""Test Request object when not a user."""
self.site._userinfo = {}
with self.subTest(userinfo=self.site._userinfo):
- self.assertRaisesRegex(pywikibot.Error,
- 'API write action attempted without user',
- Request, site=self.site,
- parameters={'action': 'edit'})
+ with self.assertRaisesRegex(
+ pywikibot.Error,
+ 'API write action attempted without user'):
+ Request(site=self.site, parameters={'action': 'edit'})

self.site._userinfo = {'name': '1.2.3.4', 'groups': [], 'anon': ''}
with self.subTest(userinfo=self.site._userinfo):
- self.assertRaisesRegex(pywikibot.Error, " as IP '1.2.3.4'",
- Request, site=self.site,
- parameters={'action': 'edit'})
+ with self.assertRaisesRegex(
+ pywikibot.Error,
+ " as IP '1.2.3.4'"):
+ Request(site=self.site, parameters={'action': 'edit'})

def test_unexpected_user(self):
"""Test Request object when username is not correct."""
diff --git a/tests/edit_failure_tests.py b/tests/edit_failure_tests.py
index 7e64a66..4d45474 100644
--- a/tests/edit_failure_tests.py
+++ b/tests/edit_failure_tests.py
@@ -48,28 +48,35 @@
self.skipTest(
'Testing failure of edit protected with a sysop account')
page = pywikibot.Page(self.site, 'Wikipedia:Create a new page')
- self.assertRaises(LockedPage, page.save)
+ with self.assertRaises(LockedPage):
+ page.save()

def test_spam(self):
"""Test that spam in content raise the appropriate exception."""
page = pywikibot.Page(self.site, 'Wikipedia:Sandbox')
page.text = 'http://badsite.com'
try:
- self.assertRaisesRegex(SpamblacklistError, 'badsite.com',
- page.save)
+ with self.assertRaisesRegex(
+ SpamblacklistError,
+ 'badsite.com'):
+ page.save()
except OtherPageSaveError as e:
self.skipTest(e)

def test_titleblacklist(self):
"""Test that title blacklist raise the appropriate exception."""
page = pywikibot.Page(self.site, 'User:UpsandDowns1234/Blacklisttest')
- self.assertRaises(TitleblacklistError, page.save)
+ with self.assertRaises(TitleblacklistError):
+ page.save()

def test_nobots(self):
"""Test that {{nobots}} raise the appropriate exception."""
page = pywikibot.Page(self.site, 'User:John Vandenberg/nobots')
with patch.object(config, 'ignore_bot_templates', False):
- self.assertRaisesRegex(OtherPageSaveError, 'nobots', page.save)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ 'nobots'):
+ page.save()

def test_touch(self):
"""Test that Page.touch() does not do a real edit."""
@@ -83,18 +90,22 @@
def test_createonly(self):
"""Test that Page.save with createonly fails if page exists."""
page = pywikibot.Page(self.site, 'User:Xqt/sandbox')
- self.assertRaises(PageCreatedConflict, page.save, createonly=True)
+ with self.assertRaises(PageCreatedConflict):
+ page.save(createonly=True)

def test_nocreate(self):
"""Test that Page.save with nocreate fails if page does not exist."""
page = pywikibot.Page(self.site, 'User:John_Vandenberg/no_recreate')
- self.assertRaises(NoCreateError, page.save, nocreate=True)
+ with self.assertRaises(NoCreateError):
+ page.save(nocreate=True)

def test_no_recreate(self):
"""Test that Page.save with recreate disabled fails if page existed."""
page = pywikibot.Page(self.site, 'User:John_Vandenberg/no_recreate')
- self.assertRaisesRegex(OtherPageSaveError, "Page .* doesn't exist",
- page.save, recreate=False)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ "Page .* doesn't exist"):
+ page.save(recreate=False)


class TestActionFailure(TestCase):
@@ -116,13 +127,13 @@
"movepage test requires 'move' token not given to user on {}"
.format(self.site))

- self.assertRaises(Error, mysite.movepage,
- mainpage, mainpage.title(), 'test')
+ with self.assertRaises(Error):
+ mysite.movepage(mainpage, mainpage.title(), 'test')

page_from = self.get_missing_article()
if not page_from.exists():
- self.assertRaises(NoPage, mysite.movepage,
- page_from, 'Main Page', 'test')
+ with self.assertRaises(NoPage):
+ mysite.movepage(page_from, 'Main Page', 'test')


class TestWikibaseSaveTest(WikibaseTestCase):
@@ -139,7 +150,8 @@
"""Test ItemPage save method inherited from superclass Page."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q6')
- self.assertRaises(pywikibot.PageSaveRelatedError, item.save)
+ with self.assertRaises(pywikibot.PageSaveRelatedError):
+ item.save()

def _make_WbMonolingualText_claim(self, repo, text, language):
"""Make a WbMonolingualText and set its value."""
@@ -154,11 +166,11 @@
item = pywikibot.ItemPage(repo, 'Q68')
claim = self._make_WbMonolingualText_claim(repo, text='Test this!',
language='foo')
- self.assertRaisesRegex(
- OtherPageSaveError,
- r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
- r'modification-failed: "foo" is not a known language code.',
- item.addClaim, claim)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
+ r'modification-failed: "foo" is not a known language code.'):
+ item.addClaim(claim)

def test_WbMonolingualText_invalid_text(self):
"""Attempt adding a monolingual text with invalid non-string text."""
@@ -166,10 +178,10 @@
item = pywikibot.ItemPage(repo, 'Q68')
claim = self._make_WbMonolingualText_claim(repo, text=123456,
language='en')
- self.assertRaisesRegex(
- OtherPageSaveError,
- r'Edit to page \[\[(wikidata:test:)?Q68]] failed:',
- item.addClaim, claim)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:'):
+ item.addClaim(claim)

def test_math_invalid_function(self):
"""Attempt adding invalid latex to a math claim."""
@@ -177,11 +189,11 @@
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P717', datatype='math')
claim.setTarget('\foo')
- self.assertRaisesRegex(
- OtherPageSaveError,
- r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
- r'modification-failed: Malformed input:',
- item.addClaim, claim)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
+ r'modification-failed: Malformed input:'):
+ item.addClaim(claim)

def test_url_malformed_url(self):
"""Attempt adding a malformed URL to a url claim."""
@@ -189,11 +201,13 @@
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P506', datatype='url')
claim.setTarget('Not a URL at all')
- self.assertRaisesRegex(
- OtherPageSaveError,
- r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
- r'modification-failed: This URL misses a scheme like "https://": '
- r'Not a URL at all', item.addClaim, claim)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
+ r'modification-failed: This URL misses a scheme like '
+ r'"https://": '
+ r'Not a URL at all'):
+ item.addClaim(claim)

def test_url_invalid_protocol(self):
"""Attempt adding a URL with an invalid protocol to a url claim."""
@@ -201,11 +215,11 @@
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P506', datatype='url')
claim.setTarget('wtf://wikiba.se')
- self.assertRaisesRegex(
- OtherPageSaveError,
- r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
- r'modification-failed: An URL scheme "wtf" is not supported.',
- item.addClaim, claim)
+ with self.assertRaisesRegex(
+ OtherPageSaveError,
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
+ r'modification-failed: An URL scheme "wtf" is not supported.'):
+ item.addClaim(claim)


if __name__ == '__main__': # pragma: no cover

To view, visit change 658222. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ia47ff1d2457bad9532bae8bf38a8fa6607553861
Gerrit-Change-Number: 658222
Gerrit-PatchSet: 5
Gerrit-Owner: Homeboy 445 <akshitsan13@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged