Barry Warsaw pushed to branch master at mailman / Mailman
Commits: 39fd2905 by Barry Warsaw at 2015-12-26T23:16:01Z Boost coverage. Use Python 3.5 to run the coverage tests. Clean up the code style a bit. - - - - - 4 changed files: - coverage.ini - + src/mailman/utilities/tests/test_uid.py - src/mailman/utilities/uid.py - tox.ini Changes: ===================================== coverage.ini ===================================== --- a/coverage.ini +++ b/coverage.ini @@ -4,7 +4,7 @@ parallel = true omit = setup* */showme.py - .tox/coverage/lib/python3.4/site-packages/* + .tox/coverage/lib/python3.5/site-packages/* [paths] source = ===================================== src/mailman/utilities/tests/test_uid.py ===================================== --- /dev/null +++ b/src/mailman/utilities/tests/test_uid.py @@ -0,0 +1,84 @@ +# Copyright (C) 2015 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Test the uid module.""" + +__all__ = [ + 'TestUID', + ] + + +import os +import uuid +import unittest + +from contextlib import ExitStack +from mailman.config import config +from mailman.testing.layers import ConfigLayer +from mailman.utilities import uid +from unittest.mock import patch + + + +class TestUID(unittest.TestCase): + layer = ConfigLayer + + def _uid_files(self): + return [filename + for filename in os.listdir(os.path.join(config.VAR_DIR)) + if filename.startswith('.uid') + ] + + def test_context(self): + self.assertNotIn('.uid.foo', self._uid_files()) + uid.UIDFactory('foo').new() + self.assertIn('.uid.foo', self._uid_files()) + + def test_no_context(self): + self.assertNotIn('.uid', self._uid_files()) + uid.UIDFactory().new() + self.assertIn('.uid', self._uid_files()) + + def test_unpredictable_id(self): + with patch('mailman.utilities.uid.layers.is_testing', + return_value=False): + self.assertNotEqual(uid.UIDFactory().new().int, 1) + + def test_uid_record_try_again(self): + called = False + def record_second(ignore): + nonlocal called + if not called: + called = True + raise ValueError + with ExitStack() as resources: + resources.enter_context( + patch('mailman.utilities.uid.layers.is_testing', + return_value=False)) + resources.enter_context( + patch('mailman.utilities.uid.UID.record', record_second)) + mock = resources.enter_context( + patch('mailman.utilities.uid.uuid.uuid4', + return_value=uuid.UUID(int=1))) + uid.UIDFactory().new() + self.assertEqual(mock.call_count, 2) + + def test_unpredictable_token_factory(self): + with patch('mailman.utilities.uid.layers.is_testing', + return_value=False): + self.assertNotEqual(uid.TokenFactory().new(), + '0000000000000000000000000000000000000001') ===================================== src/mailman/utilities/uid.py ===================================== --- a/src/mailman/utilities/uid.py +++ b/src/mailman/utilities/uid.py @@ -30,7 +30,6 @@ __all__ = [ import os import time import uuid -import errno import random import hashlib @@ -64,7 +63,7 @@ class _PredictableIDGenerator: # These will get automatically cleaned up by the test # infrastructure. self._uid_file = os.path.join(config.VAR_DIR, '.uid') - if self._context: + if self._context is not None: self._uid_file += '.' + self._context self._lock_file = self._uid_file + '.lock' self._lockobj = Lock(self._lock_file) @@ -90,7 +89,7 @@ class _PredictableIDGenerator: The type of the returned id is intended to be the type that makes sense for the subclass overriding this method. """ - raise NotImplementedError + raise NotImplementedError # pragma: no cover def _next_predictable_id(self): """Generate a predictable id for when Mailman being tested. @@ -98,20 +97,18 @@ class _PredictableIDGenerator: The type of the returned id is intended to be the type that makes sense for the subclass overriding this method. """ - raise NotImplementedError + raise NotImplementedError # pragma: no cover def _next_id(self): with self._lock: try: with open(self._uid_file) as fp: uid = int(fp.read().strip()) - next_uid = uid + 1 + next_uid = uid + 1 # pragma: no branch with open(self._uid_file, 'w') as fp: - fp.write(str(next_uid)) + fp.write(str(next_uid)) # pragma: no branch return uid - except IOError as error: - if error.errno != errno.ENOENT: - raise + except FileNotFoundError: with open(self._uid_file, 'w') as fp: fp.write('2') return 1 ===================================== tox.ini ===================================== --- a/tox.ini +++ b/tox.ini @@ -22,7 +22,7 @@ rcfile = {toxinidir}/coverage.ini rc = --rcfile={[coverage]rcfile} [testenv:coverage] -basepython = python3.4 +basepython = python3.5 commands = coverage run {[coverage]rc} -m nose2 -v coverage combine {[coverage]rc} View it on GitLab: https://gitlab.com/mailman/mailman/commit/39fd2905cc34221ca2907f14f3f018190a3f62c5
_______________________________________________ Mailman-checkins mailing list Mailman-checkins@python.org Unsubscribe: https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org