jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
edit_failure_tests.py: Fix the expected string form of page objects

site.py:
- sort exception imports by name
- raise CaptchaError if solving a captcha is required and
config.solve_captcha is False. (T191119)

tests/__init__.py
- Set config.solve_captcha to False. We normally do not want to
solve captchas during tests.

edit_failure_tests.py:
- test_spam: try page.save and skip OtherPageSaveError. (CaptchaError
is raised as OtherPageSaveError in allow_asynchronous[2])
- Make family and site code in regex assertions optional. Family and
site codes are only included if they are different from
config.family and config.lang values.
- Remove the "�" character from the expected error message of
test_math_invalid_function. Python 2's assertRaisesRegexp has issues
with unicode.[1]

[1]: See https://stackoverflow.com/questions/23276849/
[2]:
https://phabricator.wikimedia.org/diffusion/PWBC/browse/master/pywikibot/page.py;d1628307949f929f5abe512706f4ee3a2f0afb57$128

Bug: T191117
Bug: T191119
Change-Id: I1efdb617410d30aeed47ac45bf7b1934d420ea27
---
M pywikibot/site.py
M tests/__init__.py
M tests/edit_failure_tests.py
3 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/pywikibot/site.py b/pywikibot/site.py
index 65a2d50..29e2157 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -35,32 +35,33 @@
from pywikibot.data import api
from pywikibot.echo import Notification
from pywikibot.exceptions import (
- Error,
- PageRelatedError,
+ ArticleExistsConflict,
+ CaptchaError,
+ CascadeLockedPage,
+ CircularRedirect,
EditConflict,
+ EntityTypeUnknownException,
+ Error,
+ FamilyMaintenanceWarning,
+ FatalServerError,
+ InconsistentTitleReceived,
+ InterwikiRedirectPage,
+ IsNotRedirectPage,
+ LockedNoPage,
+ LockedPage,
+ NoCreateError,
+ NoPage,
+ NoUsername,
PageCreatedConflict,
PageDeletedConflict,
- ArticleExistsConflict,
- IsNotRedirectPage,
- CircularRedirect,
- InterwikiRedirectPage,
- InconsistentTitleReceived,
- LockedPage,
- CascadeLockedPage,
- LockedNoPage,
- NoPage,
+ PageRelatedError,
+ PageSaveRelatedError,
SiteDefinitionError,
- UnknownSite,
- UnknownExtension,
- FamilyMaintenanceWarning,
- NoUsername,
SpamfilterError,
TitleblacklistError,
- NoCreateError,
+ UnknownExtension,
+ UnknownSite,
UserBlocked,
- EntityTypeUnknownException,
- FatalServerError,
- PageSaveRelatedError,
)
from pywikibot.family import WikimediaFamily
from pywikibot.throttle import Throttle
@@ -5069,6 +5070,8 @@
@rtype: bool
@raises Error: No text to be saved
@raises NoPage: recreate is disabled and page does not exist
+ @raises CaptchaError: config.solve_captcha is False and saving
+ the page requires solving a captcha
"""
basetimestamp = True
text_overrides = self._ep_text_overrides.intersection(kwargs.keys())
@@ -5175,6 +5178,9 @@
return True
elif result["edit"]["result"] == "Failure":
if "captcha" in result["edit"]:
+ if not pywikibot.config.solve_captcha:
+ raise CaptchaError('captcha encountered while '
+ 'config.solve_captcha is False')
captcha = result["edit"]["captcha"]
req['captchaid'] = captcha['id']
if captcha["type"] == "math":
diff --git a/tests/__init__.py b/tests/__init__.py
index 9291a31..f548ec6 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -270,6 +270,9 @@
'tests: max_retries reduced from %d to 1' % config.max_retries)
config.max_retries = 1

+# Raise CaptchaError if a test requires solving a captcha
+config.solve_captcha = False
+
cache_misses = 0
cache_hits = 0

diff --git a/tests/edit_failure_tests.py b/tests/edit_failure_tests.py
index 12e9ff9..7ec8988 100644
--- a/tests/edit_failure_tests.py
+++ b/tests/edit_failure_tests.py
@@ -52,7 +52,10 @@
"""Test that spam in content raise the appropriate exception."""
page = pywikibot.Page(self.site, 'Wikipedia:Sandbox')
page.text = 'http://badsite.com'
- self.assertRaisesRegex(SpamfilterError, 'badsite.com', page.save)
+ try:
+ self.assertRaisesRegex(SpamfilterError, 'badsite.com', page.save)
+ except OtherPageSaveError as e:
+ self.skipTest(e)

def test_titleblacklist(self):
"""Test that title blacklist raise the appropriate exception."""
@@ -152,7 +155,7 @@
language='foo')
self.assertRaisesRegex(
OtherPageSaveError,
- r'Edit to page \[\[wikidata:test:Q68]] failed:\n'
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: "foo" is not a known language code.',
item.addClaim, claim)

@@ -163,7 +166,7 @@
claim = self._make_WbMonolingualText_claim(repo, text=123456, language='en')
self.assertRaisesRegex(
OtherPageSaveError,
- r'Edit to page \[\[wikidata:test:Q68]] failed:\n'
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'invalid-snak: Invalid snak data.',
item.addClaim, claim)

@@ -175,8 +178,8 @@
claim.setTarget('\foo')
self.assertRaisesRegex(
OtherPageSaveError,
- r'Edit to page \[\[wikidata:test:Q68]] failed:\n'
- r'modification-failed: Malformed input: �oo',
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
+ r'modification-failed: Malformed input:',
item.addClaim, claim)

def test_url_malformed_url(self):
@@ -187,7 +190,7 @@
claim.setTarget('Not a URL at all')
self.assertRaisesRegex(
OtherPageSaveError,
- r'Edit to page \[\[wikidata:test:Q68]] failed:\n'
+ 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)

@@ -199,7 +202,7 @@
claim.setTarget('wtf://wikiba.se')
self.assertRaisesRegex(
OtherPageSaveError,
- r'Edit to page \[\[wikidata:test:Q68]] failed:\n'
+ r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: An URL scheme "wtf" is not supported.',
item.addClaim, claim)

To view, visit change 423169. To unsubscribe, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I1efdb617410d30aeed47ac45bf7b1934d420ea27
Gerrit-Change-Number: 423169
Gerrit-PatchSet: 3
Gerrit-Owner: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot <>