jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/414105 )
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
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(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
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()