summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint-core/gitlint/tests/base.py')
-rw-r--r--gitlint-core/gitlint/tests/base.py49
1 files changed, 33 insertions, 16 deletions
diff --git a/gitlint-core/gitlint/tests/base.py b/gitlint-core/gitlint/tests/base.py
index 710efe2..3899a5f 100644
--- a/gitlint-core/gitlint/tests/base.py
+++ b/gitlint-core/gitlint/tests/base.py
@@ -5,15 +5,15 @@ import os
import re
import shutil
import tempfile
-
import unittest
-
+from pathlib import Path
from unittest.mock import patch
from gitlint.config import LintConfig
-from gitlint.deprecation import Deprecation, LOG as DEPRECATION_LOG
-from gitlint.git import GitContext, GitChangedFileStats
-from gitlint.utils import LOG_FORMAT, DEFAULT_ENCODING
+from gitlint.deprecation import LOG as DEPRECATION_LOG
+from gitlint.deprecation import Deprecation
+from gitlint.git import GitChangedFileStats, GitContext
+from gitlint.utils import FILE_ENCODING, LOG_FORMAT
EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING = (
"WARNING: gitlint.deprecated.regex_style_search {0} - {1}: gitlint will be switching from using "
@@ -30,10 +30,28 @@ class BaseTestCase(unittest.TestCase):
# In case of assert failures, print the full error message
maxDiff = None
+ # Working directory in which tests in this class are executed
+ working_dir = None
+ # Originally working dir when the test was started
+ original_working_dir = None
+
SAMPLES_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "samples")
EXPECTED_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected")
GITLINT_USE_SH_LIB = os.environ.get("GITLINT_USE_SH_LIB", "[NOT SET]")
+ @classmethod
+ def setUpClass(cls):
+ # Run tests a temporary directory to shield them from any local git config
+ cls.original_working_dir = os.getcwd()
+ cls.working_dir = tempfile.mkdtemp()
+ os.chdir(cls.working_dir)
+
+ @classmethod
+ def tearDownClass(cls):
+ # Go back to original working dir and remove our temp working dir
+ os.chdir(cls.original_working_dir)
+ shutil.rmtree(cls.working_dir)
+
def setUp(self):
self.logcapture = LogCapture()
self.logcapture.setFormatter(logging.Formatter(LOG_FORMAT))
@@ -77,9 +95,7 @@ class BaseTestCase(unittest.TestCase):
def get_sample(filename=""):
"""Read and return the contents of a file in gitlint/tests/samples"""
sample_path = BaseTestCase.get_sample_path(filename)
- with open(sample_path, encoding=DEFAULT_ENCODING) as content:
- sample = content.read()
- return sample
+ return Path(sample_path).read_text(encoding=FILE_ENCODING)
@staticmethod
def patch_input(side_effect):
@@ -93,8 +109,7 @@ class BaseTestCase(unittest.TestCase):
"""Utility method to read an expected file from gitlint/tests/expected and return it as a string.
Optionally replace template variables specified by variable_dict."""
expected_path = os.path.join(BaseTestCase.EXPECTED_DIR, filename)
- with open(expected_path, encoding=DEFAULT_ENCODING) as content:
- expected = content.read()
+ expected = Path(expected_path).read_text(encoding=FILE_ENCODING)
if variable_dict:
expected = expected.format(**variable_dict)
@@ -150,22 +165,24 @@ class BaseTestCase(unittest.TestCase):
self.logcapture.clear()
@contextlib.contextmanager
- def assertRaisesMessage(self, expected_exception, expected_msg): # pylint: disable=invalid-name
+ def assertRaisesMessage(self, expected_exception, expected_msg):
"""Asserts an exception has occurred with a given error message"""
try:
yield
except expected_exception as exc:
exception_msg = str(exc)
- if exception_msg != expected_msg:
+ if exception_msg != expected_msg: # pragma: nocover
error = f"Right exception, wrong message:\n got: {exception_msg}\n expected: {expected_msg}"
- raise self.fail(error)
+ raise self.fail(error) from exc
# else: everything is fine, just return
return
- except Exception as exc:
- raise self.fail(f"Expected '{expected_exception.__name__}' got '{exc.__class__.__name__}'")
+ except Exception as exc: # pragma: nocover
+ raise self.fail(f"Expected '{expected_exception.__name__}' got '{exc.__class__.__name__}'") from exc
# No exception raised while we expected one
- raise self.fail(f"Expected to raise {expected_exception.__name__}, didn't get an exception at all")
+ raise self.fail(
+ f"Expected to raise {expected_exception.__name__}, didn't get an exception at all"
+ ) # pragma: nocover
def object_equality_test(self, obj, attr_list, ctor_kwargs=None):
"""Helper function to easily implement object equality tests.