jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/571686 )
Change subject: [tests] Suppress Userwarning about already created sites ......................................................................
[tests] Suppress Userwarning about already created sites
- introduce a new context manager "empty_sites" to clean the pywikibot._sites and pywikibot._url_cache - use empty_sites in several tests - also suppress deprecation warning for outdated method "ReplaceRobot.doReplacement()" tests for "ReplaceRobot.apply_replacements()" are done already
Bug: T225594 Change-Id: I457f0ee93f3280bb6f75867c772e1f8cadbe9d25 --- M tests/data_ingestion_tests.py M tests/deletionbot_tests.py M tests/interwikidata_tests.py M tests/replacebot_tests.py M tests/utils.py 5 files changed, 38 insertions(+), 16 deletions(-)
Approvals: Dvorapa: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/data_ingestion_tests.py b/tests/data_ingestion_tests.py index 919dbd9..885110c 100644 --- a/tests/data_ingestion_tests.py +++ b/tests/data_ingestion_tests.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """Unit tests for data_ingestion.py script.""" # -# (C) Pywikibot team, 2012-2018 +# (C) Pywikibot team, 2012-2020 # # Distributed under the terms of the MIT license. # @@ -12,6 +12,7 @@
from tests import join_data_path, join_images_path from tests.aspects import unittest, TestCase, ScriptMainTestCase +from tests.utils import empty_sites
class TestPhoto(TestCase): @@ -123,9 +124,10 @@
def test_existing_file(self): """Test uploading a file that already exists.""" - data_ingestion.main( - '-csvdir:tests/data', - '-page:User:John_Vandenberg/data_ingestion_test_template') + with empty_sites(): + data_ingestion.main( + '-csvdir:tests/data', + '-page:User:John_Vandenberg/data_ingestion_test_template')
if __name__ == '__main__': # pragma: no cover diff --git a/tests/deletionbot_tests.py b/tests/deletionbot_tests.py index 3a46f67..2e111ee 100644 --- a/tests/deletionbot_tests.py +++ b/tests/deletionbot_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Tests for scripts/delete.py.""" # -# (C) Pywikibot team, 2014-2019 +# (C) Pywikibot team, 2014-2020 # # Distributed under the terms of the MIT license. # @@ -13,6 +13,7 @@ from scripts import delete
from tests.aspects import unittest, ScriptMainTestCase +from tests.utils import empty_sites
class TestDeletionBotWrite(ScriptMainTestCase): @@ -104,11 +105,14 @@
def test_dry(self): """Test dry run of bot.""" - delete.main('-page:Main Page', '-always', '-summary:foo') - self.assertEqual(self.delete_args, ['[[Main Page]]', 'foo', False, - True, True]) - delete.main('-page:FoooOoOooO', '-always', '-summary:foo', '-undelete') - self.assertEqual(self.undelete_args, ['[[FoooOoOooO]]', 'foo']) + with empty_sites(): + delete.main('-page:Main Page', '-always', '-summary:foo') + self.assertEqual(self.delete_args, + ['[[Main Page]]', 'foo', False, True, True]) + with empty_sites(): + delete.main( + '-page:FoooOoOooO', '-always', '-summary:foo', '-undelete') + self.assertEqual(self.undelete_args, ['[[FoooOoOooO]]', 'foo'])
def delete_dummy(self, reason, prompt, mark, quit): diff --git a/tests/interwikidata_tests.py b/tests/interwikidata_tests.py index ca3cfe7..e0255a1 100644 --- a/tests/interwikidata_tests.py +++ b/tests/interwikidata_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Tests for scripts/interwikidata.py.""" # -# (C) Pywikibot team, 2015-2019 +# (C) Pywikibot team, 2015-2020 # # Distributed under the terms of the MIT license. # @@ -14,6 +14,7 @@ from scripts import interwikidata
from tests.aspects import unittest, SiteAttributeTestCase +from tests.utils import empty_sites
class DummyBot(interwikidata.IWBot): @@ -55,7 +56,8 @@ def test_main(self): """Test main function interwikidata.py.""" # The main function should return False when no generator is defined. - self.assertFalse(interwikidata.main()) + with empty_sites(): + self.assertFalse(interwikidata.main())
def test_iw_bot(self): """Test IWBot class.""" diff --git a/tests/replacebot_tests.py b/tests/replacebot_tests.py index 1096072..219147f 100644 --- a/tests/replacebot_tests.py +++ b/tests/replacebot_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Tests for the replace script and ReplaceRobot class.""" # -# (C) Pywikibot team, 2015-2019 +# (C) Pywikibot team, 2015-2020 # # Distributed under the terms of the MIT license. # @@ -10,6 +10,7 @@ import pywikibot
from pywikibot import fixes +from pywikibot.tools import suppress_warnings
from scripts import replace
@@ -17,6 +18,7 @@
from tests.aspects import unittest from tests.bot_tests import TWNBotTestCase +from tests.utils import empty_sites
# Load only the custom fixes fixes.fixes.clear() @@ -79,7 +81,8 @@ replace.ReplaceRobot = self._original_bot replace.pywikibot.input = self._original_input replace.pywikibot.Site = self._original_site - super(TestReplacementsMain, self).tearDown() + with empty_sites(): + super(TestReplacementsMain, self).tearDown()
def _fake_input(self, message): """Cache the message and return static text "TESTRUN".""" @@ -98,7 +101,8 @@ # old and new need to be together self.assertFalse(self._run('foo', '-pairsfile:/dev/null', 'bar')) # only old provided - self.assertFalse(self._run('foo')) + with empty_sites(): + self.assertFalse(self._run('foo'))
# In the end no bots should've been created self.assertFalse(self.bots) @@ -159,7 +163,8 @@ self.assertEqual(expected, bot.apply_replacements('Hello 1', applied, page)) self.assertEqual(applied, required_applied) - self.assertEqual(expected, bot.doReplacements('Hello 1', page)) + with suppress_warnings('scripts.replace.ReplaceRobot.doReplacements'): + self.assertEqual(expected, bot.doReplacements('Hello 1', page))
def test_only_cmd(self): """Test command line replacements only.""" diff --git a/tests/utils.py b/tests/utils.py index f9118da..91c55ac 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -7,6 +7,7 @@ # from __future__ import absolute_import, division, unicode_literals
+from contextlib import contextmanager import inspect import json import os @@ -697,3 +698,11 @@
return execute(command=command + args, data_in=data_in, timeout=timeout, error=error) + + +@contextmanager +def empty_sites(): + """Empty pywikibot._sites and pywikibot._url_cache cache on entry point.""" + pywikibot._sites = {} + pywikibot._url_cache = {} + yield
pywikibot-commits@lists.wikimedia.org