Skip to content

Commit

Permalink
tests: Make pylint happy and fix python2.6 uses of assertRaisesRegex.
Browse files Browse the repository at this point in the history
Older unittest2.TestCase (as seen in CentOS 6) do not have an
assertRaisesRegex method. They only have the now-deprecated
assertRaisesRegexp.

We need our unit tests to work there and on newer python (3.6).
Simply making assertRaisesRegex = assertRaisesRegexp makes pylint
complain as described in pylint-dev/pylint#1946 .

What was here before this commit was actually broken.  This commit
makes assertRaisesRegex functional in CentOS 6 and works around
the invalid Deprecated warning from pylint.

To prove this, we use assertRaisesRegex in a unit test which will
be exectued in py27, py3 and py26.
  • Loading branch information
smoser authored and blackboxsw committed Mar 16, 2018
1 parent b7b7331 commit 95bb226
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions cloudinit/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ def __mock_assert_not_called(mmock):
mock.Mock.assert_not_called = __mock_assert_not_called


# older unittest2.TestCase (centos6) do not have assertRaisesRegex
# And setting assertRaisesRegex to assertRaisesRegexp causes
# https://github.com/PyCQA/pylint/issues/1653 . So the workaround.
# older unittest2.TestCase (centos6) have only the now-deprecated
# assertRaisesRegexp. Simple assignment makes pylint complain, about
# users of assertRaisesRegex so we use getattr to trick it.
# https://github.com/PyCQA/pylint/issues/1946
if not hasattr(unittest2.TestCase, 'assertRaisesRegex'):
def _tricky(*args, **kwargs):
return unittest2.TestCase.assertRaisesRegexp
unittest2.TestCase.assertRaisesRegex = _tricky
unittest2.TestCase.assertRaisesRegex = (
getattr(unittest2.TestCase, 'assertRaisesRegexp'))

# vi: ts=4 expandtab
3 changes: 2 additions & 1 deletion tests/unittests/test_handler/test_handler_apt_source_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ def test_convert_to_new_format_collision(self):
newcfg = cc_apt_configure.convert_to_v3_apt_format(cfg_3_only)
self.assertEqual(newcfg, cfg_3_only)
# collision (unequal)
with self.assertRaises(ValueError):
match = "Old and New.*unequal.*apt_proxy"
with self.assertRaisesRegex(ValueError, match):
cc_apt_configure.convert_to_v3_apt_format(cfgconflict)

def test_convert_to_new_format_dict_collision(self):
Expand Down

0 comments on commit 95bb226

Please sign in to comment.