Skip to content

Commit

Permalink
add local assertRaisesRegexp() on python older then 2.7
Browse files Browse the repository at this point in the history
When running tests on python older then 2.7, monkeypatch
unittest.TestCase class to include assertRaisesRegexp() method.
  • Loading branch information
Elmir Jagudin committed Apr 5, 2013
1 parent 815519f commit 7ced54f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
13 changes: 12 additions & 1 deletion slave/buildslave/monkeypatches/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,16 @@ def patch_bug5079():
from buildslave.monkeypatches import bug5079
bug5079.patch()

def patch_all():
def patch_testcase_assert_raises_regexp():
# pythons before 2.7 does not have TestCase.assertRaisesRegexp() method
# add our local implementation if needed
import sys
if sys.version_info[:2] < (2,7):
from buildslave.monkeypatches import testcase_assert
testcase_assert.patch()

def patch_all(for_tests=False):
patch_bug4881()

if for_tests:
patch_testcase_assert_raises_regexp()
48 changes: 48 additions & 0 deletions slave/buildslave/monkeypatches/testcase_assert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This file is part of Buildbot. Buildbot 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, version 2.
#
# This program 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
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members


import re
import unittest


def _assertRaisesRegexp(self, expected_exception, expected_regexp,
callable_obj, *args, **kwds):
"""
Asserts that the message in a raised exception matches a regexp.
This is a simple clone of unittest.TestCase.assertRaisesRegexp() method
introduced in python 2.7. The goal for this function is to behave exactly
as assertRaisesRegexp() in standard library.
"""
exception = None
try:
callable_obj(*args, **kwds)
except expected_exception, ex: # let unexpected exceptions pass through
exception = ex

if exception == None:
self.fail("%s not raised" % str(expected_exception.__name__))

if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)

if not expected_regexp.search(str(exception)):
self.fail('"%s" does not match "%s"' %
(expected_regexp.pattern, str(exception)))


def patch():
unittest.TestCase.assertRaisesRegexp = _assertRaisesRegexp
2 changes: 1 addition & 1 deletion slave/buildslave/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from buildslave import monkeypatches

# apply the same patches the slave does when it starts
monkeypatches.patch_all()
monkeypatches.patch_all(for_tests=True)

def add_debugging_monkeypatches():
"""
Expand Down

0 comments on commit 7ced54f

Please sign in to comment.