jenkins-bot has submitted this change and it was merged.
Change subject: Fix replaceCategoryInPlace site
......................................................................
Fix replaceCategoryInPlace site
Also add tests that it does not alter the sortkey
Bug: T95804
Change-Id: I0bca72f10819104d29385ec24cb91a13aa0a4dc0
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 53 insertions(+), 3 deletions(-)
Approvals:
Mpaa: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 410c100..4c2da87 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -795,14 +795,17 @@
# an entire line, if the category is the only thing on that line (this
# prevents blank lines left over in category lists following a removal.)
text = replaceExcept(oldtext, categoryRN, '',
- ['nowiki', 'comment', 'math',
'pre', 'source'])
+ ['nowiki', 'comment', 'math',
'pre', 'source'],
+ site=site)
text = replaceExcept(text, categoryR, '',
- ['nowiki', 'comment', 'math',
'pre', 'source'])
+ ['nowiki', 'comment', 'math',
'pre', 'source'],
+ site=site)
else:
text = replaceExcept(oldtext, categoryR,
'[[%s:%s\\2' % (site.namespace(14),
newcat.title(withNamespace=False)),
- ['nowiki', 'comment', 'math',
'pre', 'source'])
+ ['nowiki', 'comment', 'math',
'pre', 'source'],
+ site=site)
return text
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 6baae06..7b1aa9a 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -163,6 +163,53 @@
config.line_separator = sep
self.assertEqual(old, new)
+ def test_in_place_replace(self):
+ """Test in-place category change is reversible."""
+ dummy = pywikibot.Category(self.site, 'foo')
+ dummy.sortKey = 'bah'
+
+ cats = textlib.getCategoryLinks(self.old, site=self.site)
+
+ # Sanity checking
+ temp = textlib.replaceCategoryInPlace(self.old, cats[0], dummy, site=self.site)
+ self.assertNotEqual(temp, self.old)
+ new = textlib.replaceCategoryInPlace(temp, dummy, cats[0], site=self.site)
+ self.assertEqual(self.old, new)
+
+ temp = textlib.replaceCategoryInPlace(self.old, cats[1], dummy, site=self.site)
+ self.assertNotEqual(temp, self.old)
+ new = textlib.replaceCategoryInPlace(temp, dummy, cats[1], site=self.site)
+ self.assertEqual(self.old, new)
+
+ temp = textlib.replaceCategoryInPlace(self.old, cats[2], dummy, site=self.site)
+ self.assertNotEqual(temp, self.old)
+ new = textlib.replaceCategoryInPlace(temp, dummy, cats[2], site=self.site)
+ self.assertEqual(self.old, new)
+
+ temp = textlib.replaceCategoryInPlace(self.old, cats[3], dummy, site=self.site)
+ self.assertNotEqual(temp, self.old)
+ new = textlib.replaceCategoryInPlace(temp, dummy, cats[3], site=self.site)
+ self.assertEqual(self.old, new)
+
+ new_cats = textlib.getCategoryLinks(new, site=self.site)
+ self.assertEqual(cats, new_cats)
+
+ def test_in_place_retain_sort(self):
+ """Test in-place category change does not alter the
sortkey."""
+ # sort key should be retained when the new cat sortKey is None
+ dummy = pywikibot.Category(self.site, 'foo')
+ self.assertIsNone(dummy.sortKey)
+
+ cats = textlib.getCategoryLinks(self.old, site=self.site)
+
+ self.assertEqual(cats[3].sortKey, 'key')
+ orig_sortkey = cats[3].sortKey
+ temp = textlib.replaceCategoryInPlace(self.old, cats[3], dummy, site=self.site)
+ self.assertNotEqual(self.old, temp)
+ new_dummy = textlib.getCategoryLinks(temp, site=self.site)[3]
+ self.assertIsNotNone(new_dummy.sortKey)
+ self.assertEqual(orig_sortkey, new_dummy.sortKey)
+
class TestTemplatesInCategory(TestCase):
--
To view, visit
https://gerrit.wikimedia.org/r/204991
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0bca72f10819104d29385ec24cb91a13aa0a4dc0
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>