jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/511463 )
Change subject: [TEST] fix test_create_short_link ......................................................................
[TEST] fix test_create_short_link
Bug: T223865 Change-Id: Ib980e88bf0dec0ce16ecc7ce42a5110a508bc3aa --- M pywikibot/page.py M tests/page_tests.py 2 files changed, 13 insertions(+), 6 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 062ec9d..7cc9f3f 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -66,6 +66,8 @@ from urllib import quote as quote_from_bytes, unquote as unquote_to_bytes
+PROTOCOL_REGEX = r'\Ahttps?://' + __all__ = ( 'BasePage', 'Page', @@ -2218,7 +2220,7 @@ """ return self.content_model == 'flow-board'
- def create_short_link(self, permalink=False, with_protocol=False): + def create_short_link(self, permalink=False, with_protocol=True): """ Return a shortened link that points to that page.
@@ -2228,8 +2230,9 @@ @param permalink: If true, the link will point to the actual revision of the page. @type permalink: bool - @param with_protocol: If true, the link will have https propotol - prepend. + @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. @type with_protocol: bool @return: The reduced link. @rtype: str @@ -2241,7 +2244,10 @@ url = self.permalink() if permalink else self.full_url()
link = wiki.create_short_link(url) - if with_protocol: + if re.match(PROTOCOL_REGEX, link): + if not with_protocol: + return re.sub(PROTOCOL_REGEX, '', link) + elif with_protocol: return '{}://{}'.format(wiki.protocol(), link) return link
diff --git a/tests/page_tests.py b/tests/page_tests.py index 55ce2fe..7aa8008 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -1186,12 +1186,13 @@ site = self.get_site() p1 = pywikibot.Page(site, 'User:Framawiki/pwb_tests/shortlink') with self.subTest(parameters='defaulted'): - self.assertEqual(p1.create_short_link(), 'w.wiki/3Cy') + self.assertEqual(p1.create_short_link(), 'https://w.wiki/3Cy') with self.subTest(with_protocol=True): self.assertEqual(p1.create_short_link(with_protocol=True), 'https://w.wiki/3Cy') with self.subTest(permalink=True): - self.assertEqual(p1.create_short_link(permalink=True), + self.assertEqual(p1.create_short_link(permalink=True, + with_protocol=False), 'w.wiki/3Cz')