jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR][TEST] Add references under templates in existing section

- also skip comments if any

This reverts commit fe77c1adac33e2d76010f7893caff4869f037132.

Bug: T159307
Change-Id: I610db7bbea746be51bba543516c783804908df39
---
M scripts/noreferences.py
A tests/noreferences_tests.py
2 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/scripts/noreferences.py b/scripts/noreferences.py
index ddf43a1..2a69f54 100755
--- a/scripts/noreferences.py
+++ b/scripts/noreferences.py
@@ -566,11 +566,14 @@
pywikibot.output(
'Adding references tag to existing %s section...\n'
% section)
+ templates_or_comments = re.compile(
+ r'^((?:\s*(?:\{\{[^\{\}]*?\}\}|<!--.*?-->))*)',
+ flags=re.DOTALL)
new_text = (
- oldText[:match.end()] + '\n' +
- self.referencesText + '\n' +
- oldText[match.end():]
- )
+ oldText[:match.end() - 1]
+ + templates_or_comments.sub(
+ r'\1\n{0}\n'.format(self.referencesText),
+ oldText[match.end() - 1:]))
return new_text
else:
break
diff --git a/tests/noreferences_tests.py b/tests/noreferences_tests.py
new file mode 100644
index 0000000..6d7f8fc
--- /dev/null
+++ b/tests/noreferences_tests.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+"""Test noreferences bot module."""
+#
+# (C) Pywikibot team, 2018
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, unicode_literals
+
+import pywikibot
+
+from scripts.noreferences import NoReferencesBot
+
+from tests.aspects import TestCase, unittest
+
+
+class TestAddingReferences(TestCase):
+ """Test adding references to section."""
+
+ family = 'wikipedia'
+ code = 'cs'
+
+ def test_add(self):
+ """Test adding references section."""
+ page = pywikibot.Page(self.site, 'foo')
+ bot = NoReferencesBot(None)
+ bot.site = self.site
+ page.text = '\n== Reference ==\n* [http://www.baz.org Baz]'
+ new_text = bot.addReferences(page.text)
+ expected = ('\n== Reference ==\n<references />'
+ '\n\n* [http://www.baz.org Baz]')
+ self.assertEqual(new_text, expected)
+
+ def test_add_under_templates(self):
+ """Test adding references section under templates in section."""
+ page = pywikibot.Page(self.site, 'foo')
+ bot = NoReferencesBot(None)
+ bot.site = self.site
+ page.text = '\n== Reference ==\n{{Překlad|en|Baz|123456}}'
+ new_text = bot.addReferences(page.text)
+ expected = page.text + '\n<references />\n'
+ self.assertEqual(new_text, expected)
+
+
+if __name__ == '__main__':
+ unittest.main()

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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I610db7bbea746be51bba543516c783804908df39
Gerrit-Change-Number: 414105
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot <>