jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/616514 )
Change subject: [bugfix] Fix str2size to allow space separators ......................................................................
[bugfix] Fix str2size to allow space separators
Concurrently, use re.fullmatch (Python 3.4) to make it stricter about the input. Better safe than sorry.
Also improve test coverage.
Change-Id: I8c17129ea5604d1038720704447966c732d010ba --- M scripts/archivebot.py M tests/archivebot_tests.py 2 files changed, 18 insertions(+), 4 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/archivebot.py b/scripts/archivebot.py index e64c4db..674160b 100755 --- a/scripts/archivebot.py +++ b/scripts/archivebot.py @@ -256,10 +256,10 @@ Returns a tuple (size,unit), where size is an integer and unit is 'B' (bytes) or 'T' (threads). """ - match = re.search(r'(\d+) *([BkKMT]?)', string) + match = re.fullmatch(r'(\d{1,3}(?: \d{3})+|\d+) *([BkKMT]?)', string) if not match: raise MalformedConfigError("Couldn't parse size: {}".format(string)) - val, unit = (int(match.group(1)), match.group(2)) + val, unit = (int(match.group(1).replace(' ', '')), match.group(2)) if unit == 'M': val *= 1024 unit = 'K' diff --git a/tests/archivebot_tests.py b/tests/archivebot_tests.py index 32ffc39..e01f524 100644 --- a/tests/archivebot_tests.py +++ b/tests/archivebot_tests.py @@ -114,8 +114,22 @@ self.assertEqual(archivebot.str2size('4 K'), (4096, 'B')) self.assertEqual(archivebot.str2size('1 M'), (1048576, 'B')) self.assertEqual(archivebot.str2size('2T'), (2, 'T')) - # TODO: should probably be recognized 2000? - self.assertEqual(archivebot.str2size('2 000'), (2, 'B')) + self.assertEqual(archivebot.str2size('2 000'), (2000, 'B')) + self.assertEqual(archivebot.str2size('2 000B'), (2000, 'B')) + self.assertEqual(archivebot.str2size('2 000 B'), (2000, 'B')) + + def test_str2size_failures(self): + """Test for rejecting of invalid shorthand notation of sizes.""" + self.assertRaises(archivebot.MalformedConfigError, archivebot.str2size, + '4 KK') + self.assertRaises(archivebot.MalformedConfigError, archivebot.str2size, + 'K4') + self.assertRaises(archivebot.MalformedConfigError, archivebot.str2size, + '4X') + self.assertRaises(archivebot.MalformedConfigError, archivebot.str2size, + '1 234 56') + self.assertRaises(archivebot.MalformedConfigError, archivebot.str2size, + '1234 567')
class TestArchiveBot(TestCase):
pywikibot-commits@lists.wikimedia.org