jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/463100 )
Change subject: [cleanup] cleanup tests/[ui_options_tests.py to uploadbot_tests.py] ......................................................................
[cleanup] cleanup tests/[ui_options_tests.py to uploadbot_tests.py]
- use str.format(...) instead of modulo for type specifier arguments - use single quotes for string literals - remove preleading "u" fron strings - indentation to make sure code lines are less than 79 characters
Change-Id: I10baebe8921d627f847bd5485434249265da8ef6 --- M tests/ui_options_tests.py M tests/ui_tests.py M tests/uploadbot_tests.py 3 files changed, 93 insertions(+), 73 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/ui_options_tests.py b/tests/ui_options_tests.py index eb49d69..62b6bca 100644 --- a/tests/ui_options_tests.py +++ b/tests/ui_options_tests.py @@ -18,7 +18,7 @@
"""Test cases for input_choice Option."""
- TEST_RE = ''int' object has no attribute 'lower'' + TEST_RE = "'int' object has no attribute 'lower'" SEQ_EMPTY_RE = 'The sequence is empty.' net = False
@@ -74,20 +74,22 @@ self.assertEqual(option.format(default='r2'), 'r<number> [1-[2]-5]') self.assertEqual(option.format(), 'r<number> [1-5]') self.assertEqual(message('?', [option], None), '? (r<number> [1-5])') - self.assertEqual(message('?', [option], 'r3'), '? (r<number> [1-[3]-5])') + self.assertEqual(message('?', [option], 'r3'), + '? (r<number> [1-[3]-5])') self.assertRaisesRegex(AttributeError, self.TEST_RE, option.test, 1) self.assertFalse(option.test('0')) self.assertFalse(option.test('r0')) self.assertFalse(option.test('r6')) self.assertIsNone(option.handled('r6')) for i in range(1, 6): - self.assertTrue(option.test('r%d' % i)) - self.assertEqual(option.handled('r%d' % i), option) - self.assertEqual(option.result('r%d' % i), ('r', i)) + self.assertTrue(option.test('r{}'.format(i))) + self.assertEqual(option.handled('r{}'.format(i)), option) + self.assertEqual(option.result('r{}'.format(i)), ('r', i))
def test_List(self): """Test ListOption.""" - self.assertRaisesRegex(ValueError, self.SEQ_EMPTY_RE, bot.ListOption, []) + self.assertRaisesRegex(ValueError, self.SEQ_EMPTY_RE, + bot.ListOption, []) options = ['foo', 'bar'] option = bot.ListOption(options) self.assertEqual(message('?', [option], None), '? (<number> [1-2])') @@ -99,7 +101,8 @@ self.assertEqual(message('?', [option], None), '? (<number> [1])') self.assertEqual(message('?', [option], '1'), '? (<number> [[1]])') options.pop() - self.assertRaisesRegex(ValueError, self.SEQ_EMPTY_RE, option.format, None) + self.assertRaisesRegex(ValueError, self.SEQ_EMPTY_RE, option.format, + None) self.assertRaisesRegex(ValueError, self.SEQ_EMPTY_RE, option.format) self.assertFalse(option.test('0')) options += ['baz', 'quux', 'norf'] @@ -107,15 +110,17 @@ for prefix in ('', 'r', 'st'): option = bot.ListOption(options, prefix=prefix) self.assertEqual(message('?', [option]), - '? (%s<number> [1-3])' % prefix) + '? ({}<number> [1-3])'.format(prefix)) for i, elem in enumerate(options, 1): - self.assertTrue(option.test('%s%d' % (prefix, i))) - self.assertIs(option.handled('%s%d' % (prefix, i)), option) - self.assertEqual(option.result('%s%d' % (prefix, i)), + self.assertTrue(option.test('{}{}'.format(prefix, i))) + self.assertIs(option.handled('{}{}' + .format(prefix, i)), option) + self.assertEqual(option.result('{}{}'.format(prefix, i)), (prefix, elem)) - self.assertFalse(option.test('%s%d' % (prefix, len(options) + 1))) - self.assertIsNone(option.handled('%s%d' - % (prefix, len(options) + 1))) + self.assertFalse(option.test('{}{}' + .format(prefix, len(options) + 1))) + self.assertIsNone(option.handled('{}{}'.format( + prefix, len(options) + 1)))
if __name__ == '__main__': # pragma: no cover diff --git a/tests/ui_tests.py b/tests/ui_tests.py index 5c853c9..7fc1934 100644 --- a/tests/ui_tests.py +++ b/tests/ui_tests.py @@ -49,7 +49,7 @@ from tests.aspects import TestCase, TestCaseBase from tests.utils import unittest, FakeModule
-if os.name == "nt": +if os.name == 'nt': from multiprocessing.managers import BaseManager import threading
@@ -89,7 +89,7 @@ patched_streams[self._original] = self._stream
def __repr__(self): - return '<patched %s %r wrapping %r>' % ( + return '<patched {} {!r} wrapping {!r}>'.format( self._name, self._stream, self._original)
def reset(self): @@ -98,7 +98,7 @@ self._stream.seek(0)
-if os.name == "nt": +if os.name == 'nt':
class pywikibotWrapper(object):
@@ -141,7 +141,7 @@ _manager = pywikibotManager( address=('127.0.0.1', 47228), authkey=b'4DJSchgwy5L5JxueZEWbxyeG') - if len(sys.argv) > 1 and sys.argv[1] == "--run-as-slave-interpreter": + if len(sys.argv) > 1 and sys.argv[1] == '--run-as-slave-interpreter': s = _manager.get_server() s.serve_forever()
@@ -336,7 +336,8 @@ except TestException: pywikibot.exception('exception') self.assertEqual(newstdout.getvalue(), '') - self.assertEqual(newstderr.getvalue(), 'ERROR: TestException: Testing Exception\n') + self.assertEqual(newstderr.getvalue(), + 'ERROR: TestException: Testing Exception\n')
def test_exception_tb(self): class TestException(Exception): @@ -349,9 +350,11 @@ pywikibot.exception('exception', tb=True) self.assertEqual(newstdout.getvalue(), '') stderrlines = newstderr.getvalue().split('\n') - self.assertEqual(stderrlines[0], 'ERROR: TestException: Testing Exception') + self.assertEqual(stderrlines[0], + 'ERROR: TestException: Testing Exception') self.assertEqual(stderrlines[1], 'Traceback (most recent call last):') - self.assertEqual(stderrlines[3], " raise TestException('Testing Exception')") + self.assertEqual(stderrlines[3], + " raise TestException('Testing Exception')") self.assertTrue(stderrlines[4].endswith(': Testing Exception'))
self.assertNotEqual(stderrlines[-1], '\n') @@ -373,15 +376,15 @@ self.assertEqual(newstderr.getvalue(), 'question: ')
self.assertIsInstance(returned, unicode) - self.assertEqual(returned, u'input to read') + self.assertEqual(returned, 'input to read')
def _call_input_choice(self): rv = pywikibot.input_choice( 'question', - (('answer 1', u'A'), - ('answer 2', u'N'), - ('answer 3', u'S')), - u'A', + (('answer 1', 'A'), + ('answer 2', 'N'), + ('answer 3', 'S')), + 'A', automatic_quit=False)
self.assertEqual(newstdout.getvalue(), '') @@ -473,25 +476,25 @@ """Terminal output tests for unix."""
def testOutputUnicodeText(self): - pywikibot.output(u'Заглавная_страница') + pywikibot.output('Заглавная_страница') self.assertEqual(newstdout.getvalue(), '') self.assertEqual( newstderr.getvalue(), - self._encode(u'Заглавная_страница\n', 'utf-8')) + self._encode('Заглавная_страница\n', 'utf-8'))
def testInputUnicodeText(self): - newstdin.write(self._encode(u'Заглавная_страница\n', 'utf-8')) + newstdin.write(self._encode('Заглавная_страница\n', 'utf-8')) newstdin.seek(0)
- returned = pywikibot.input(u'Википедию? ') + returned = pywikibot.input('Википедию? ')
self.assertEqual(newstdout.getvalue(), '') self.assertEqual( newstderr.getvalue(), - self._encode(u'Википедию? ', 'utf-8')) + self._encode('Википедию? ', 'utf-8'))
self.assertIsInstance(returned, unicode) - self.assertEqual(returned, u'Заглавная_страница') + self.assertEqual(returned, 'Заглавная_страница')
@unittest.skipUnless(os.name == 'posix', 'requires Unix console') @@ -502,7 +505,7 @@ def testOutputTransliteratedUnicodeText(self): pywikibot.ui.encoding = 'latin-1' pywikibot.config.transliterate = True - pywikibot.output(u'abcd АБГД αβγδ あいうえお') + pywikibot.output('abcd АБГД αβγδ あいうえお') self.assertEqual(newstdout.getvalue(), '') self.assertEqual( newstderr.getvalue(), @@ -534,16 +537,16 @@ try: cls._app = pywinauto.application.Application() except AttributeError as e2: - raise unittest.SkipTest('pywinauto Application failed: %s\n%s' - % (e1, e2)) + raise unittest.SkipTest('pywinauto Application failed: {}\n{}' + .format(e1, e2)) super(WindowsTerminalTestCase, cls).setUpClass()
@classmethod def setUpProcess(cls, command): si = subprocess.STARTUPINFO() si.dwFlags = subprocess.STARTF_USESTDHANDLES - cls._process = subprocess.Popen(command, - creationflags=subprocess.CREATE_NEW_CONSOLE) + cls._process = subprocess.Popen( + command, creationflags=subprocess.CREATE_NEW_CONSOLE)
cls._app.connect_(process=cls._process.pid)
@@ -552,15 +555,17 @@ window = cls._app.window_() except Exception as e: cls.tearDownProcess() - raise unittest.SkipTest('Windows package pywinauto could not locate window: %r' - % e) + raise unittest.SkipTest( + 'Windows package pywinauto could not locate window: {!r}' + .format(e))
try: window.TypeKeys('% {UP}{ENTER}%L{HOME}L{ENTER}', with_spaces=True) except Exception as e: cls.tearDownProcess() - raise unittest.SkipTest('Windows package pywinauto could not use window TypeKeys: %r' - % e) + raise unittest.SkipTest( + 'Windows package pywinauto could not use window TypeKeys: {!r}' + .format(e))
@classmethod def tearDownProcess(cls): @@ -568,19 +573,20 @@
def setUp(self): super(WindowsTerminalTestCase, self).setUp() - self.setclip(u'') + self.setclip('')
def waitForWindow(self): while not self._app.window_().IsEnabled(): time.sleep(0.01)
def getstdouterr(self): - sentinel = u'~~~~SENTINEL~~~~cedcfc9f-7eed-44e2-a176-d8c73136c185' + sentinel = '~~~~SENTINEL~~~~cedcfc9f-7eed-44e2-a176-d8c73136c185' # select all and copy to clipboard self._app.window_().SetFocus() self.waitForWindow() - self._app.window_().TypeKeys('% {UP}{UP}{UP}{RIGHT}{DOWN}{DOWN}{DOWN}{ENTER}{ENTER}', - with_spaces=True) + self._app.window_().TypeKeys( + '% {UP}{UP}{UP}{RIGHT}{DOWN}{DOWN}{DOWN}{ENTER}{ENTER}', + with_spaces=True)
while True: data = self.getclip() @@ -590,22 +596,25 @@
def setclip(self, text): win32clipboard.OpenClipboard() - win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, unicode(text)) + win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, + unicode(text)) win32clipboard.CloseClipboard()
def getclip(self): win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) win32clipboard.CloseClipboard() - data = data.split(u'\x00')[0] - data = data.replace(u'\r\n', u'\n') + data = data.split('\x00')[0] + data = data.replace('\r\n', '\n') return data
def sendstdin(self, text): - self.setclip(text.replace(u'\n', u'\r\n')) + self.setclip(text.replace('\n', '\r\n')) self._app.window_().SetFocus() self.waitForWindow() - self._app.window_().TypeKeys('% {UP}{UP}{UP}{RIGHT}{DOWN}{DOWN}{ENTER}', with_spaces=True) + self._app.window_().TypeKeys( + '% {UP}{UP}{UP}{RIGHT}{DOWN}{DOWN}{ENTER}', + with_spaces=True)
class TestWindowsTerminalUnicode(WindowsTerminalTestCase): @@ -616,7 +625,8 @@ def setUpClass(cls): super(TestWindowsTerminalUnicode, cls).setUpClass() fn = inspect.getfile(inspect.currentframe()) - cls.setUpProcess(['python', 'pwb.py', fn, '--run-as-slave-interpreter']) + cls.setUpProcess(['python', 'pwb.py', fn, + '--run-as-slave-interpreter'])
_manager.connect() cls.pywikibot = _manager.pywikibot() @@ -638,24 +648,24 @@ self.pywikibot.cls()
def testOutputUnicodeText_no_transliterate(self): - self.pywikibot.output(u'Заглавная_страница') - self.assertEqual(self.getstdouterr(), u'Заглавная_страница\n') + self.pywikibot.output('Заглавная_страница') + self.assertEqual(self.getstdouterr(), 'Заглавная_страница\n')
def testOutputUnicodeText_transliterate(self): self.pywikibot.set_config('transliterate', True) self.pywikibot.set_ui('transliteration_target', 'latin-1') - self.pywikibot.output(u'Заглавная_страница') + self.pywikibot.output('Заглавная_страница') self.assertEqual(self.getstdouterr(), 'Zaglavnaya_stranica\n')
def testInputUnicodeText(self): self.pywikibot.set_config('transliterate', True)
- self.pywikibot.request_input(u'Википедию? ') - self.assertEqual(self.getstdouterr(), u'Википедию?') - self.sendstdin(u'Заглавная_страница\n') + self.pywikibot.request_input('Википедию? ') + self.assertEqual(self.getstdouterr(), 'Википедию?') + self.sendstdin('Заглавная_страница\n') returned = self.pywikibot.get_input()
- self.assertEqual(returned, u'Заглавная_страница') + self.assertEqual(returned, 'Заглавная_страница')
class TestWindowsTerminalUnicodeArguments(WindowsTerminalTestCase): @@ -673,9 +683,9 @@
def testOutputUnicodeText_no_transliterate(self): self.sendstdin( - u"python -c "import os, pywikibot; os.system('cls'); " - u"pywikibot.output(u'\n'.join(pywikibot.handleArgs()))" " - u"Alpha Bετα Гамма دلتا\n") + "python -c "import os, pywikibot; os.system('cls'); " + "pywikibot.output('\n'.join(pywikibot.handleArgs()))" " + 'Alpha Bετα Гамма دلتا\n') lines = []
for i in range(3): @@ -690,7 +700,7 @@ time.sleep(1)
# empty line is the new command line - self.assertEqual(lines, [u'Alpha', u'Bετα', u'Гамма', u'دلتا', u'']) + self.assertEqual(lines, ['Alpha', 'Bετα', 'Гамма', 'دلتا', ''])
# TODO: add tests for background colors. @@ -748,7 +758,8 @@
def test_flat_color(self): """Test using colors with defaulting in between.""" - self._colors = (('red', 6), ('default', 6), ('yellow', 3), ('default', 1)) + self._colors = (('red', 6), ('default', 6), ('yellow', 3), + ('default', 1)) self.ui_obj._print('Hello \03{red}world \03{default}you\03{yellow}!', self.stream) self.assertEqual(self._getvalue(), self.expected) @@ -815,9 +826,9 @@ """ Test case to allow doing colorized Win32 tests in any environment.
- This only patches the ctypes import in the terminal_interface_win32 module. - As the Win32CtypesUI is using the std-streams from another import these will - be unpatched. + This only patches the ctypes import in the terminal_interface_win32 + module. As the Win32CtypesUI is using the std-streams from another + import these will be unpatched. """
net = False diff --git a/tests/uploadbot_tests.py b/tests/uploadbot_tests.py index 7b6ebde..37c96f8 100644 --- a/tests/uploadbot_tests.py +++ b/tests/uploadbot_tests.py @@ -35,26 +35,30 @@ for dir_file in directory_info[2]: image_list.append(os.path.join(directory_info[0], dir_file)) bot = upload.UploadRobot(url=image_list, - description="pywikibot upload.py script test", + description='pywikibot upload.py script test', useFilename=None, keepFilename=True, verifyDescription=True, aborts=set(), - ignoreWarning=True, targetSite=self.get_site()) + ignoreWarning=True, + targetSite=self.get_site()) bot.run()
def test_png(self): """Test uploading a png using upload.py.""" - bot = upload.UploadRobot(url=[join_images_path("MP_sounds.png")], - description="pywikibot upload.py script test", + bot = upload.UploadRobot(url=[join_images_path('MP_sounds.png')], + description='pywikibot upload.py script test', useFilename=None, keepFilename=True, verifyDescription=True, aborts=set(), - ignoreWarning=True, targetSite=self.get_site()) + ignoreWarning=True, + targetSite=self.get_site()) bot.run()
def test_png_url(self): """Test uploading a png from url using upload.py.""" + link = 'https://upload.wikimedia.org/' + link += 'wikipedia/commons/f/fc/MP_sounds.png' bot = upload.UploadRobot( - url=['https://upload.wikimedia.org/wikipedia/commons/f/fc/MP_sounds.png'], - description="pywikibot upload.py script test", + url=[link], + description='pywikibot upload.py script test', useFilename=None, keepFilename=True, verifyDescription=True, aborts=set(), ignoreWarning=True, targetSite=self.get_site())
pywikibot-commits@lists.wikimedia.org