jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/408018 )
Change subject: [IMPR][TEST] Add references under templates in existing section ......................................................................
[IMPR][TEST] Add references under templates in existing section
- also skip comments if any
Bug: T159307 Change-Id: I1c68cd3a68e3573852806b98a48bfbe0d3bfec4b --- M scripts/noreferences.py A tests/noreferences_tests.py 2 files changed, 54 insertions(+), 7 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/noreferences.py b/scripts/noreferences.py index 932e10c..60a03a2 100755 --- a/scripts/noreferences.py +++ b/scripts/noreferences.py @@ -34,7 +34,7 @@ a list of affected articles """ # -# (C) Pywikibot team, 2007-2017 +# (C) Pywikibot team, 2007-2018 # # Distributed under the terms of the MIT license. # @@ -564,12 +564,15 @@ pywikibot.output( 'Adding references tag to existing %s section...\n' % section) - newText = ( - oldText[:match.end()] + u'\n' + - self.referencesText + u'\n' + - oldText[match.end():] - ) - return newText + templates_or_comments = re.compile( + r'^((?:\s*(?:{{[^{}]*?}}|<!--.*?-->))*)', + flags=re.DOTALL) + new_text = ( + oldText[:match.end()] + + templates_or_comments.sub( + r'\1\n{0}\n'.format(self.referencesText), + oldText[match.end():])) + return new_text else: break
diff --git a/tests/noreferences_tests.py b/tests/noreferences_tests.py new file mode 100644 index 0000000..d9661a9 --- /dev/null +++ b/tests/noreferences_tests.py @@ -0,0 +1,44 @@ +# -*- 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() + page.text = '== Reference ==\n* [http://www.baz.org Baz]' + new_text = bot.addReferences(page.text) + expected = ('== 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() + page.text = '== 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()