summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/base.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-19 14:52:46 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-19 14:52:46 +0000
commita2aa51f5702b18016c25d943499941323952704d (patch)
tree7ee43f79639ee53903e7ca389e548974e1497c3a /gitlint-core/gitlint/tests/base.py
parentAdding upstream version 0.17.0. (diff)
downloadgitlint-a2aa51f5702b18016c25d943499941323952704d.tar.xz
gitlint-a2aa51f5702b18016c25d943499941323952704d.zip
Adding upstream version 0.18.0.upstream/0.18.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint-core/gitlint/tests/base.py')
-rw-r--r--gitlint-core/gitlint/tests/base.py81
1 files changed, 50 insertions, 31 deletions
diff --git a/gitlint-core/gitlint/tests/base.py b/gitlint-core/gitlint/tests/base.py
index 9d2d165..710efe2 100644
--- a/gitlint-core/gitlint/tests/base.py
+++ b/gitlint-core/gitlint/tests/base.py
@@ -1,8 +1,5 @@
-# -*- coding: utf-8 -*-
-
import contextlib
import copy
-import io
import logging
import os
import re
@@ -13,12 +10,22 @@ import unittest
from unittest.mock import patch
-from gitlint.git import GitContext
+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
+EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING = (
+ "WARNING: gitlint.deprecated.regex_style_search {0} - {1}: gitlint will be switching from using "
+ "Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. "
+ "Please review your {1}.regex option accordingly. "
+ "To remove this warning, set general.regex-style-search=True. More details: "
+ "https://jorisroovers.github.io/gitlint/configuration/#regex-style-search"
+)
+
class BaseTestCase(unittest.TestCase):
- """ Base class of which all gitlint unit test classes are derived. Provides a number of convenience methods. """
+ """Base class of which all gitlint unit test classes are derived. Provides a number of convenience methods."""
# In case of assert failures, print the full error message
maxDiff = None
@@ -30,13 +37,24 @@ class BaseTestCase(unittest.TestCase):
def setUp(self):
self.logcapture = LogCapture()
self.logcapture.setFormatter(logging.Formatter(LOG_FORMAT))
- logging.getLogger('gitlint').setLevel(logging.DEBUG)
- logging.getLogger('gitlint').handlers = [self.logcapture]
+ logging.getLogger("gitlint").setLevel(logging.DEBUG)
+ logging.getLogger("gitlint").handlers = [self.logcapture]
+ DEPRECATION_LOG.handlers = [self.logcapture]
# Make sure we don't propagate anything to child loggers, we need to do this explicitly here
# because if you run a specific test file like test_lint.py, we won't be calling the setupLogging() method
# in gitlint.cli that normally takes care of this
- logging.getLogger('gitlint').propagate = False
+ # Example test where this matters (for DEPRECATION_LOG):
+ # gitlint-core/gitlint/tests/rules/test_configuration_rules.py::ConfigurationRuleTests::test_ignore_by_title
+ logging.getLogger("gitlint").propagate = False
+ DEPRECATION_LOG.propagate = False
+
+ # Make sure Deprecation has a clean config set at the start of each test.
+ # Tests that want to specifically test deprecation should override this.
+ Deprecation.config = LintConfig()
+ # Normally Deprecation only logs messages once per process.
+ # For tests we want to log every time, so we reset the warning_msgs set per test.
+ Deprecation.warning_msgs = set()
@staticmethod
@contextlib.contextmanager
@@ -57,25 +75,25 @@ class BaseTestCase(unittest.TestCase):
@staticmethod
def get_sample(filename=""):
- """ Read and return the contents of a file in gitlint/tests/samples """
+ """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:
+ with open(sample_path, encoding=DEFAULT_ENCODING) as content:
sample = content.read()
return sample
@staticmethod
def patch_input(side_effect):
- """ Patches the built-in input() with a provided side-effect """
+ """Patches the built-in input() with a provided side-effect"""
module_path = "builtins.input"
patched_module = patch(module_path, side_effect=side_effect)
return patched_module
@staticmethod
def get_expected(filename="", variable_dict=None):
- """ 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. """
+ """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 io.open(expected_path, encoding=DEFAULT_ENCODING) as content:
+ with open(expected_path, encoding=DEFAULT_ENCODING) as content:
expected = content.read()
if variable_dict:
@@ -87,20 +105,21 @@ class BaseTestCase(unittest.TestCase):
return os.path.join(BaseTestCase.SAMPLES_DIR, "user_rules")
@staticmethod
- def gitcontext(commit_msg_str, changed_files=None, ):
- """ Utility method to easily create gitcontext objects based on a given commit msg string and an optional set of
+ def gitcontext(commit_msg_str, changed_files=None):
+ """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 = "#"
gitcontext = GitContext.from_commit_msg(commit_msg_str)
commit = gitcontext.commits[-1]
if changed_files:
- commit.changed_files = changed_files
+ changed_file_stats = {filename: GitChangedFileStats(filename, 8, 3) for filename in changed_files}
+ commit.changed_files_stats = changed_file_stats
return gitcontext
@staticmethod
def gitcommit(commit_msg_str, changed_files=None, **kwargs):
- """ Utility method to easily create git commit given a commit msg string and an optional set of changed files"""
+ """Utility method to easily create git commit given a commit msg string and an optional set of changed files"""
gitcontext = BaseTestCase.gitcontext(commit_msg_str, changed_files)
commit = gitcontext.commits[-1]
for attr, value in kwargs.items():
@@ -108,31 +127,31 @@ class BaseTestCase(unittest.TestCase):
return commit
def assert_logged(self, expected):
- """ Asserts that the logs match an expected string or list.
- This method knows how to compare a passed list of log lines as well as a newline concatenated string
- of all loglines. """
+ """Asserts that the logs match an expected string or list.
+ This method knows how to compare a passed list of log lines as well as a newline concatenated string
+ of all loglines."""
if isinstance(expected, list):
self.assertListEqual(self.logcapture.messages, expected)
else:
self.assertEqual("\n".join(self.logcapture.messages), expected)
def assert_log_contains(self, line):
- """ Asserts that a certain line is in the logs """
+ """Asserts that a certain line is in the logs"""
self.assertIn(line, self.logcapture.messages)
def assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs):
- """ 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.
+ """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().assertRaisesRegex(expected_exception, re.escape(expected_regex), *args, **kwargs)
def clearlog(self):
- """ Clears the log capture """
+ """Clears the log capture"""
self.logcapture.clear()
@contextlib.contextmanager
def assertRaisesMessage(self, expected_exception, expected_msg): # pylint: disable=invalid-name
- """ Asserts an exception has occurred with a given error message """
+ """Asserts an exception has occurred with a given error message"""
try:
yield
except expected_exception as exc:
@@ -149,10 +168,10 @@ class BaseTestCase(unittest.TestCase):
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.
- Creates an object clone for every passed attribute and checks for (in)equality
- of the original object with the clone based on those attributes' values.
- This function assumes all attributes in `attr_list` can be passed to the ctor of `obj.__class__`.
+ """Helper function to easily implement object equality tests.
+ Creates an object clone for every passed attribute and checks for (in)equality
+ of the original object with the clone based on those attributes' values.
+ This function assumes all attributes in `attr_list` can be passed to the ctor of `obj.__class__`.
"""
if not ctor_kwargs:
ctor_kwargs = {}
@@ -178,7 +197,7 @@ class BaseTestCase(unittest.TestCase):
class LogCapture(logging.Handler):
- """ Mock logging handler used to capture any log messages during tests."""
+ """Mock logging handler used to capture any log messages during tests."""
def __init__(self, *args, **kwargs):
logging.Handler.__init__(self, *args, **kwargs)