jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[tests] Skip TestShortLink tests if urlshortener-ratelimit was exceeded

- add code parameter to utils.skipping decorator to specify the
APIError code to determine which specific APIError is to be skipped
- update documentations

Bug: T370596
Change-Id: Ie085e1f46522e1d090f63c2ee0517af8b9fd329e
---
M pywikibot/page/_basepage.py
M tests/page_tests.py
M tests/utils.py
3 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index 21181ba..e564b72 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -2303,18 +2303,18 @@
def create_short_link(self,
permalink: bool = False,
with_protocol: bool = True) -> str:
- """
- Return a shortened link that points to that page.
+ """Return a shortened link that points to that page.

- If shared_urlshortner_wiki is defined in family config, it'll use
- that site to create the link instead of the current wiki.
+ If shared_urlshortner_wiki is defined in family config, it'll
+ use that site to create the link instead of the current wiki.

- :param permalink: If true, the link will point to the actual revision
- of the page.
+ :param permalink: If true, the link will point to the actual
+ revision of the page.
:param with_protocol: If true, and if it's not already included,
- the link will have http(s) protocol prepended. On Wikimedia wikis
- the protocol is already present.
+ the link will have http(s) protocol prepended. On Wikimedia
+ wikis the protocol is already present.
:return: The reduced link.
+ :raises APIError: urlshortener-ratelimit exceeded
"""
wiki = self.site
if self.site.family.shared_urlshortner_wiki:
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 0df6d1a..7a19e16 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -17,6 +17,7 @@
import pywikibot.page
from pywikibot import config
from pywikibot.exceptions import (
+ APIError,
Error,
InvalidTitleError,
IsNotRedirectPageError,
@@ -1267,12 +1268,16 @@

site = self.get_site()
p1 = pywikibot.Page(site, 'User:Framawiki/pwb_tests/shortlink')
- with self.subTest(parameters='defaulted'):
+
+ with self.subTest(parameters='defaulted'), \
+ skipping(APIError, code='urlshortener-ratelimit'):
self.assertEqual(p1.create_short_link(), 'https://w.wiki/3Cy')
- with self.subTest(with_protocol=True):
+ with self.subTest(with_protocol=True), \
+ skipping(APIError, code='urlshortener-ratelimit'):
self.assertEqual(p1.create_short_link(with_protocol=True),
'https://w.wiki/3Cy')
- with self.subTest(permalink=True):
+ with self.subTest(permalink=True), \
+ skipping(APIError, code='urlshortener-ratelimit'):
self.assertEqual(p1.create_short_link(permalink=True,
with_protocol=False),
'w.wiki/3Cz')
diff --git a/tests/utils.py b/tests/utils.py
index 053743f..341cb47 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -553,7 +553,9 @@


@contextmanager
-def skipping(*exceptions: BaseException, msg: str | None = None):
+def skipping(*exceptions: BaseException,
+ msg: str | None = None,
+ code: str | None = None):
"""Context manager to skip test on specified exceptions.

For example Eventstreams raises ``NotImplementedError`` if no
@@ -581,13 +583,25 @@
.. note:: The last sample uses Python 3.10 syntax.

.. versionadded:: 6.2
+ .. versionchanged:: 9.3
+ *code* parameter was added

- :param msg: Optional skipping reason
:param exceptions: Exceptions to let test skip
+ :param msg: Optional skipping reason
+ :param code: if *exceptions* is a single :exc:`APIError` you can
+ specify the :attr:`APIError.code` for the right match to be
+ skipped.
"""
try:
yield
except exceptions as e:
+ if code:
+ if len(exceptions) != 1 \
+ or not hasattr(e, 'code') \
+ or e.code != code:
+ raise
+ msg = e.info
+
if msg is None:
msg = e
raise unittest.SkipTest(msg)

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

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie085e1f46522e1d090f63c2ee0517af8b9fd329e
Gerrit-Change-Number: 1055576
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot