diff options
Diffstat (limited to 'gitlint/tests/base.py')
-rw-r--r-- | gitlint/tests/base.py | 60 |
1 files changed, 18 insertions, 42 deletions
diff --git a/gitlint/tests/base.py b/gitlint/tests/base.py index c8f68c4..9406240 100644 --- a/gitlint/tests/base.py +++ b/gitlint/tests/base.py @@ -9,33 +9,12 @@ import re import shutil import tempfile -try: - # python 2.x - import unittest2 as unittest -except ImportError: - # python 3.x - import unittest - -try: - # python 2.x - from mock import patch -except ImportError: - # python 3.x - from unittest.mock import patch # pylint: disable=no-name-in-module, import-error - -from gitlint.git import GitContext -from gitlint.utils import ustr, IS_PY2, LOG_FORMAT, DEFAULT_ENCODING +import unittest +from unittest.mock import patch -# unittest2's assertRaisesRegex doesn't do unicode comparison. -# Let's monkeypatch the str() function to point to unicode() so that it does :) -# For reference, this is where this patch is required: -# https://hg.python.org/unittest2/file/tip/unittest2/case.py#l227 -try: - # python 2.x - unittest.case.str = unicode -except (AttributeError, NameError): - pass # python 3.x +from gitlint.git import GitContext +from gitlint.utils import LOG_FORMAT, DEFAULT_ENCODING class BaseTestCase(unittest.TestCase): @@ -72,24 +51,22 @@ class BaseTestCase(unittest.TestCase): def get_sample_path(filename=""): # Don't join up empty files names because this will add a trailing slash if filename == "": - return ustr(BaseTestCase.SAMPLES_DIR) + return BaseTestCase.SAMPLES_DIR - return ustr(os.path.join(BaseTestCase.SAMPLES_DIR, filename)) + return os.path.join(BaseTestCase.SAMPLES_DIR, filename) @staticmethod def get_sample(filename=""): """ Read and return the contents of a file in gitlint/tests/samples """ sample_path = BaseTestCase.get_sample_path(filename) with io.open(sample_path, encoding=DEFAULT_ENCODING) as content: - sample = ustr(content.read()) + sample = content.read() return sample @staticmethod def patch_input(side_effect): """ Patches the built-in input() with a provided side-effect """ module_path = "builtins.input" - if IS_PY2: - module_path = "__builtin__.raw_input" patched_module = patch(module_path, side_effect=side_effect) return patched_module @@ -99,7 +76,7 @@ class BaseTestCase(unittest.TestCase): Optionally replace template variables specified by variable_dict. """ expected_path = os.path.join(BaseTestCase.EXPECTED_DIR, filename) with io.open(expected_path, encoding=DEFAULT_ENCODING) as content: - expected = ustr(content.read()) + expected = content.read() if variable_dict: expected = expected.format(**variable_dict) @@ -114,7 +91,7 @@ class BaseTestCase(unittest.TestCase): """ Utility method to easily create gitcontext objects based on a given commit msg string and an optional set of changed files""" with patch("gitlint.git.git_commentchar") as comment_char: - comment_char.return_value = u"#" + comment_char.return_value = "#" gitcontext = GitContext.from_commit_msg(commit_msg_str) commit = gitcontext.commits[-1] if changed_files: @@ -147,8 +124,7 @@ class BaseTestCase(unittest.TestCase): """ Pass-through method to unittest.TestCase.assertRaisesRegex that applies re.escape() to the passed `expected_regex`. This is useful to automatically escape all file paths that might be present in the regex. """ - return super(BaseTestCase, self).assertRaisesRegex(expected_exception, re.escape(expected_regex), - *args, **kwargs) + return super().assertRaisesRegex(expected_exception, re.escape(expected_regex), *args, **kwargs) @contextlib.contextmanager def assertRaisesMessage(self, expected_exception, expected_msg): # pylint: disable=invalid-name @@ -156,17 +132,17 @@ class BaseTestCase(unittest.TestCase): try: yield except expected_exception as exc: - exception_msg = ustr(exc) + exception_msg = str(exc) if exception_msg != expected_msg: - error = u"Right exception, wrong message:\n got: {0}\n expected: {1}" - raise self.fail(error.format(exception_msg, expected_msg)) + error = f"Right exception, wrong message:\n got: {exception_msg}\n expected: {expected_msg}" + raise self.fail(error) # else: everything is fine, just return return except Exception as exc: - raise self.fail(u"Expected '{0}' got '{1}'".format(expected_exception.__name__, exc.__class__.__name__)) + raise self.fail(f"Expected '{expected_exception.__name__}' got '{exc.__class__.__name__}'") # No exception raised while we expected one - raise self.fail("Expected to raise {0}, didn't get an exception at all".format(expected_exception.__name__)) + raise self.fail(f"Expected to raise {expected_exception.__name__}, didn't get an exception at all") def object_equality_test(self, obj, attr_list, ctor_kwargs=None): """ Helper function to easily implement object equality tests. @@ -190,9 +166,9 @@ class BaseTestCase(unittest.TestCase): self.assertEqual(obj, clone) # Change attribute and assert objects are different (via both attribute set and ctor) - setattr(clone, attr, u"föo") + setattr(clone, attr, "föo") self.assertNotEqual(obj, clone) - attr_kwargs_copy[attr] = u"föo" + attr_kwargs_copy[attr] = "föo" self.assertNotEqual(obj, obj.__class__(**attr_kwargs_copy)) @@ -205,4 +181,4 @@ class LogCapture(logging.Handler): self.messages = [] def emit(self, record): - self.messages.append(ustr(self.format(record))) + self.messages.append(self.format(record)) |