summaryrefslogtreecommitdiffstats
path: root/gitlint/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-03-19 14:00:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-03-19 14:00:14 +0000
commitdf9615bac55ac6f1c3f516b66279ac0007175030 (patch)
tree84dd81d1c97835271cea7fbdd67c074742365e07 /gitlint/tests
parentInitial commit. (diff)
downloadgitlint-df9615bac55ac6f1c3f516b66279ac0007175030.tar.xz
gitlint-df9615bac55ac6f1c3f516b66279ac0007175030.zip
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests')
-rw-r--r--gitlint/tests/__init__.py0
-rw-r--r--gitlint/tests/base.py169
-rw-r--r--gitlint/tests/cli/test_cli.py541
-rw-r--r--gitlint/tests/cli/test_cli_hooks.py96
-rw-r--r--gitlint/tests/config/test_config.py263
-rw-r--r--gitlint/tests/config/test_config_builder.py203
-rw-r--r--gitlint/tests/config/test_config_precedence.py100
-rw-r--r--gitlint/tests/config/test_rule_collection.py64
-rw-r--r--gitlint/tests/contrib/__init__.py0
-rw-r--r--gitlint/tests/contrib/test_contrib_rules.py72
-rw-r--r--gitlint/tests/contrib/test_conventional_commit.py47
-rw-r--r--gitlint/tests/contrib/test_signedoff_by.py32
-rw-r--r--gitlint/tests/expected/test_cli/test_contrib_13
-rw-r--r--gitlint/tests/expected/test_cli/test_debug_1102
-rw-r--r--gitlint/tests/expected/test_cli/test_input_stream_13
-rw-r--r--gitlint/tests/expected/test_cli/test_input_stream_debug_13
-rw-r--r--gitlint/tests/expected/test_cli/test_input_stream_debug_271
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_multiple_commits_18
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_multiple_commits_config_16
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_12
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_270
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_staged_stdin_13
-rw-r--r--gitlint/tests/expected/test_cli/test_lint_staged_stdin_272
-rw-r--r--gitlint/tests/git/test_git.py115
-rw-r--r--gitlint/tests/git/test_git_commit.py535
-rw-r--r--gitlint/tests/git/test_git_context.py89
-rw-r--r--gitlint/tests/rules/__init__.py0
-rw-r--r--gitlint/tests/rules/test_body_rules.py180
-rw-r--r--gitlint/tests/rules/test_configuration_rules.py71
-rw-r--r--gitlint/tests/rules/test_meta_rules.py50
-rw-r--r--gitlint/tests/rules/test_rules.py18
-rw-r--r--gitlint/tests/rules/test_title_rules.py154
-rw-r--r--gitlint/tests/rules/test_user_rules.py223
-rw-r--r--gitlint/tests/samples/commit_message/fixup1
-rw-r--r--gitlint/tests/samples/commit_message/merge3
-rw-r--r--gitlint/tests/samples/commit_message/revert3
-rw-r--r--gitlint/tests/samples/commit_message/sample114
-rw-r--r--gitlint/tests/samples/commit_message/sample21
-rw-r--r--gitlint/tests/samples/commit_message/sample36
-rw-r--r--gitlint/tests/samples/commit_message/sample47
-rw-r--r--gitlint/tests/samples/commit_message/sample57
-rw-r--r--gitlint/tests/samples/commit_message/squash3
-rw-r--r--gitlint/tests/samples/config/gitlintconfig15
-rw-r--r--gitlint/tests/samples/config/invalid-option-value11
-rw-r--r--gitlint/tests/samples/config/no-sections1
-rw-r--r--gitlint/tests/samples/config/nonexisting-general-option13
-rw-r--r--gitlint/tests/samples/config/nonexisting-option11
-rw-r--r--gitlint/tests/samples/config/nonexisting-rule11
-rw-r--r--gitlint/tests/samples/user_rules/bogus-file.txt2
-rw-r--r--gitlint/tests/samples/user_rules/import_exception/invalid_python.py3
-rw-r--r--gitlint/tests/samples/user_rules/incorrect_linerule/my_line_rule.py10
-rw-r--r--gitlint/tests/samples/user_rules/my_commit_rules.foo16
-rw-r--r--gitlint/tests/samples/user_rules/my_commit_rules.py26
-rw-r--r--gitlint/tests/samples/user_rules/parent_package/__init__.py13
-rw-r--r--gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py12
-rw-r--r--gitlint/tests/test_cache.py57
-rw-r--r--gitlint/tests/test_display.py74
-rw-r--r--gitlint/tests/test_hooks.py136
-rw-r--r--gitlint/tests/test_lint.py197
-rw-r--r--gitlint/tests/test_options.py179
-rw-r--r--gitlint/tests/test_utils.py78
61 files changed, 4275 insertions, 0 deletions
diff --git a/gitlint/tests/__init__.py b/gitlint/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gitlint/tests/__init__.py
diff --git a/gitlint/tests/base.py b/gitlint/tests/base.py
new file mode 100644
index 0000000..add4d71
--- /dev/null
+++ b/gitlint/tests/base.py
@@ -0,0 +1,169 @@
+# -*- coding: utf-8 -*-
+
+import copy
+import io
+import logging
+import os
+import re
+
+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, LOG_FORMAT, DEFAULT_ENCODING
+
+
+# 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
+
+
+class BaseTestCase(unittest.TestCase):
+ """ 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
+
+ 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]")
+
+ 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]
+
+ # Make sure we don't propagate anything to child loggers, we need to do this explicitely 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
+
+ @staticmethod
+ 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 ustr(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())
+ return sample
+
+ @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. """
+ expected_path = os.path.join(BaseTestCase.EXPECTED_DIR, filename)
+ with io.open(expected_path, encoding=DEFAULT_ENCODING) as content:
+ expected = ustr(content.read())
+
+ if variable_dict:
+ expected = expected.format(**variable_dict)
+ return expected
+
+ @staticmethod
+ def get_user_rules_path():
+ 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
+ changed files"""
+ with patch("gitlint.git.git_commentchar") as comment_char:
+ comment_char.return_value = u"#"
+ gitcontext = GitContext.from_commit_msg(commit_msg_str)
+ commit = gitcontext.commits[-1]
+ if changed_files:
+ commit.changed_files = changed_files
+ 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"""
+ gitcontext = BaseTestCase.gitcontext(commit_msg_str, changed_files)
+ commit = gitcontext.commits[-1]
+ for attr, value in kwargs.items():
+ setattr(commit, attr, value)
+ 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. """
+ 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 """
+ 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.
+ """
+ return super(BaseTestCase, self).assertRaisesRegex(expected_exception, re.escape(expected_regex),
+ *args, **kwargs)
+
+ 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__`.
+ """
+ if not ctor_kwargs:
+ ctor_kwargs = {}
+
+ attr_kwargs = {}
+ for attr in attr_list:
+ attr_kwargs[attr] = getattr(obj, attr)
+
+ # For every attr, clone the object and assert the clone and the original object are equal
+ # Then, change the current attr and assert objects are unequal
+ for attr in attr_list:
+ attr_kwargs_copy = copy.deepcopy(attr_kwargs)
+ attr_kwargs_copy.update(ctor_kwargs)
+ clone = obj.__class__(**attr_kwargs_copy)
+ self.assertEqual(obj, clone)
+
+ # Change attribute and assert objects are different (via both attribute set and ctor)
+ setattr(clone, attr, u"föo")
+ self.assertNotEqual(obj, clone)
+ attr_kwargs_copy[attr] = u"föo"
+
+ self.assertNotEqual(obj, obj.__class__(**attr_kwargs_copy))
+
+
+class LogCapture(logging.Handler):
+ """ Mock logging handler used to capture any log messages during tests."""
+
+ def __init__(self, *args, **kwargs):
+ logging.Handler.__init__(self, *args, **kwargs)
+ self.messages = []
+
+ def emit(self, record):
+ self.messages.append(ustr(self.format(record)))
diff --git a/gitlint/tests/cli/test_cli.py b/gitlint/tests/cli/test_cli.py
new file mode 100644
index 0000000..4d47f35
--- /dev/null
+++ b/gitlint/tests/cli/test_cli.py
@@ -0,0 +1,541 @@
+# -*- coding: utf-8 -*-
+
+import contextlib
+import io
+import os
+import sys
+import platform
+import shutil
+import tempfile
+
+import arrow
+
+try:
+ # python 2.x
+ from StringIO import StringIO
+except ImportError:
+ # python 3.x
+ from io import StringIO # pylint: disable=ungrouped-imports
+
+from click.testing import CliRunner
+
+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.shell import CommandNotFound
+
+from gitlint.tests.base import BaseTestCase
+from gitlint import cli
+from gitlint import __version__
+from gitlint.utils import DEFAULT_ENCODING
+
+
+@contextlib.contextmanager
+def tempdir():
+ tmpdir = tempfile.mkdtemp()
+ try:
+ yield tmpdir
+ finally:
+ shutil.rmtree(tmpdir)
+
+
+class CLITests(BaseTestCase):
+ USAGE_ERROR_CODE = 253
+ GIT_CONTEXT_ERROR_CODE = 254
+ CONFIG_ERROR_CODE = 255
+
+ def setUp(self):
+ super(CLITests, self).setUp()
+ self.cli = CliRunner()
+
+ # Patch gitlint.cli.git_version() so that we don't have to patch it separately in every test
+ self.git_version_path = patch('gitlint.cli.git_version')
+ cli.git_version = self.git_version_path.start()
+ cli.git_version.return_value = "git version 1.2.3"
+
+ def tearDown(self):
+ self.git_version_path.stop()
+
+ @staticmethod
+ def get_system_info_dict():
+ """ Returns a dict with items related to system values logged by `gitlint --debug` """
+ return {'platform': platform.platform(), "python_version": sys.version, 'gitlint_version': __version__,
+ 'GITLINT_USE_SH_LIB': BaseTestCase.GITLINT_USE_SH_LIB, 'target': os.path.realpath(os.getcwd())}
+
+ def test_version(self):
+ """ Test for --version option """
+ result = self.cli.invoke(cli.cli, ["--version"])
+ self.assertEqual(result.output.split("\n")[0], "cli, version {0}".format(__version__))
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_lint(self, sh, _):
+ """ Test for basic simple linting functionality """
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360",
+ u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"commït-title\n\ncommït-body",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n",
+ u"file1.txt\npåth/to/file2.txt\n"
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli)
+ self.assertEqual(stderr.getvalue(), u'3: B5 Body message is too short (11<20): "commït-body"\n')
+ self.assertEqual(result.exit_code, 1)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_lint_multiple_commits(self, sh, _):
+ """ Test for --commits option """
+
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360\n" + # git rev-list <SHA>
+ "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" +
+ "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n",
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"commït-title1\n\ncommït-body1",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha>
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n"
+ u"commït-title2\n\ncommït-body2",
+ u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha>
+ u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n"
+ u"commït-title3\n\ncommït-body3",
+ u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha>
+ u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--commits", "foo...bar"])
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_lint_multiple_commits_1"))
+ self.assertEqual(result.exit_code, 3)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_lint_multiple_commits_config(self, sh, _):
+ """ Test for --commits option where some of the commits have gitlint config in the commit message """
+
+ # Note that the second commit title has a trailing period that is being ignored by gitlint-ignore: T3
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360\n" + # git rev-list <SHA>
+ "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" +
+ "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n",
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"commït-title1\n\ncommït-body1",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha>
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n"
+ u"commït-title2.\n\ncommït-body2\ngitlint-ignore: T3\n",
+ u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha>
+ u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n"
+ u"commït-title3.\n\ncommït-body3",
+ u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha>
+ u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--commits", "foo...bar"])
+ # We expect that the second commit has no failures because of 'gitlint-ignore: T3' in its commit msg body
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_lint_multiple_commits_config_1"))
+ self.assertEqual(result.exit_code, 3)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_lint_multiple_commits_configuration_rules(self, sh, _):
+ """ Test for --commits option where where we have configured gitlint to ignore certain rules for certain commits
+ """
+
+ # Note that the second commit
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360\n" + # git rev-list <SHA>
+ "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" +
+ "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n",
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"commït-title1\n\ncommït-body1",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha>
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor2\x00test-email3@föo.com\x002016-12-04 15:28:15 +0100\x00åbc\n"
+ # Normally T3 violation (trailing punctuation), but this commit is ignored because of
+ # config below
+ u"commït-title2.\n\ncommït-body2\n",
+ u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha>
+ u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00åbc\n"
+ # Normally T1 and B5 violations, now only T1 because we're ignoring B5 in config below
+ u"commït-title3.\n\ncommït-body3 foo",
+ u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha>
+ u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--commits", "foo...bar", "-c", "I1.regex=^commït-title2(.*)",
+ "-c", "I2.regex=^commït-body3(.*)", "-c", "I2.ignore=B5"])
+ # We expect that the second commit has no failures because of it matching against I1.regex
+ # Because we do test for the 3th commit to return violations, this test also ensures that a unique
+ # config object is passed to each commit lint call
+ expected = (u"Commit 6f29bf81a8:\n"
+ u'3: B5 Body message is too short (12<20): "commït-body1"\n\n'
+ u"Commit 4da2656b0d:\n"
+ u'1: T3 Title has trailing punctuation (.): "commït-title3."\n')
+ self.assertEqual(stderr.getvalue(), expected)
+ self.assertEqual(result.exit_code, 2)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u'WIP: tïtle \n')
+ def test_input_stream(self, _):
+ """ Test for linting when a message is passed via stdin """
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli)
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_input_stream_1"))
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u'WIP: tïtle \n')
+ def test_input_stream_debug(self, _):
+ """ Test for linting when a message is passed via stdin, and debug is enabled.
+ This tests specifically that git commit meta is not fetched when not passing --staged """
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--debug"])
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_input_stream_debug_1"))
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+ expected_kwargs = self.get_system_info_dict()
+ expected_logs = self.get_expected('test_cli/test_input_stream_debug_2', expected_kwargs)
+ self.assert_logged(expected_logs)
+
+ @patch('gitlint.cli.get_stdin_data', return_value="Should be ignored\n")
+ @patch('gitlint.git.sh')
+ def test_lint_ignore_stdin(self, sh, stdin_data):
+ """ Test for ignoring stdin when --ignore-stdin flag is enabled"""
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360",
+ u"test åuthor\x00test-email@föo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"commït-title\n\ncommït-body",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha>
+ u"file1.txt\npåth/to/file2.txt\n" # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--ignore-stdin"])
+ self.assertEqual(stderr.getvalue(), u'3: B5 Body message is too short (11<20): "commït-body"\n')
+ self.assertEqual(result.exit_code, 1)
+
+ # Assert that we didn't even try to get the stdin data
+ self.assertEqual(stdin_data.call_count, 0)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u'WIP: tïtle \n')
+ @patch('arrow.now', return_value=arrow.get("2020-02-19T12:18:46.675182+01:00"))
+ @patch('gitlint.git.sh')
+ def test_lint_staged_stdin(self, sh, _, __):
+ """ Test for ignoring stdin when --ignore-stdin flag is enabled"""
+
+ sh.git.side_effect = [
+ u"#", # git config --get core.commentchar
+ u"föo user\n", # git config --get user.name
+ u"föo@bar.com\n", # git config --get user.email
+ u"my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch)
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--debug", "--staged"])
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_lint_staged_stdin_1"))
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+
+ expected_kwargs = self.get_system_info_dict()
+ expected_logs = self.get_expected('test_cli/test_lint_staged_stdin_2', expected_kwargs)
+ self.assert_logged(expected_logs)
+
+ @patch('arrow.now', return_value=arrow.get("2020-02-19T12:18:46.675182+01:00"))
+ @patch('gitlint.git.sh')
+ def test_lint_staged_msg_filename(self, sh, _):
+ """ Test for ignoring stdin when --ignore-stdin flag is enabled"""
+
+ sh.git.side_effect = [
+ u"#", # git config --get core.commentchar
+ u"föo user\n", # git config --get user.name
+ u"föo@bar.com\n", # git config --get user.email
+ u"my-branch\n", # git rev-parse --abbrev-ref HEAD (=current branch)
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ ]
+
+ with tempdir() as tmpdir:
+ msg_filename = os.path.join(tmpdir, "msg")
+ with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f:
+ f.write(u"WIP: msg-filename tïtle\n")
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--debug", "--staged", "--msg-filename", msg_filename])
+ self.assertEqual(stderr.getvalue(), self.get_expected("test_cli/test_lint_staged_msg_filename_1"))
+ self.assertEqual(result.exit_code, 2)
+ self.assertEqual(result.output, "")
+
+ expected_kwargs = self.get_system_info_dict()
+ expected_logs = self.get_expected('test_cli/test_lint_staged_msg_filename_2', expected_kwargs)
+ self.assert_logged(expected_logs)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ def test_lint_staged_negative(self, _):
+ result = self.cli.invoke(cli.cli, ["--staged"])
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+ self.assertEqual(result.output, (u"Error: The 'staged' option (--staged) can only be used when using "
+ u"'--msg-filename' or when piping data to gitlint via stdin.\n"))
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ def test_msg_filename(self, _):
+ expected_output = u"3: B6 Body message is missing\n"
+
+ with tempdir() as tmpdir:
+ msg_filename = os.path.join(tmpdir, "msg")
+ with io.open(msg_filename, 'w', encoding=DEFAULT_ENCODING) as f:
+ f.write(u"Commït title\n")
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--msg-filename", msg_filename])
+ self.assertEqual(stderr.getvalue(), expected_output)
+ self.assertEqual(result.exit_code, 1)
+ self.assertEqual(result.output, "")
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tïtle \n")
+ def test_silent_mode(self, _):
+ """ Test for --silent option """
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--silent"])
+ self.assertEqual(stderr.getvalue(), "")
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tïtle \n")
+ def test_verbosity(self, _):
+ """ Test for --verbosity option """
+ # We only test -v and -vv, more testing is really not required here
+ # -v
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["-v"])
+ self.assertEqual(stderr.getvalue(), "1: T2\n1: T5\n3: B6\n")
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+
+ # -vv
+ expected_output = "1: T2 Title has trailing whitespace\n" + \
+ "1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
+ "3: B6 Body message is missing\n"
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["-vv"], input=u"WIP: tïtle \n")
+ self.assertEqual(stderr.getvalue(), expected_output)
+ self.assertEqual(result.exit_code, 3)
+ self.assertEqual(result.output, "")
+
+ # -vvvv: not supported -> should print a config error
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["-vvvv"], input=u'WIP: tïtle \n')
+ self.assertEqual(stderr.getvalue(), "")
+ self.assertEqual(result.exit_code, CLITests.CONFIG_ERROR_CODE)
+ self.assertEqual(result.output, "Config Error: Option 'verbosity' must be set between 0 and 3\n")
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_debug(self, sh, _):
+ """ Test for --debug option """
+
+ sh.git.side_effect = [
+ "6f29bf81a8322a04071bb794666e48c443a90360\n" # git rev-list <SHA>
+ "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n"
+ "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n",
+ # git log --pretty <FORMAT> <SHA>
+ u"test åuthor1\x00test-email1@föo.com\x002016-12-03 15:28:15 +0100\x00abc\n"
+ u"commït-title1\n\ncommït-body1",
+ u"#", # git config --get core.commentchar
+ u"commit-1-branch-1\ncommit-1-branch-2\n", # git branch --contains <sha>
+ u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree
+ u"test åuthor2\x00test-email2@föo.com\x002016-12-04 15:28:15 +0100\x00abc\n"
+ u"commït-title2.\n\ncommït-body2",
+ u"commit-2-branch-1\ncommit-2-branch-2\n", # git branch --contains <sha>
+ u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree
+ u"test åuthor3\x00test-email3@föo.com\x002016-12-05 15:28:15 +0100\x00abc\n"
+ u"föo\nbar",
+ u"commit-3-branch-1\ncommit-3-branch-2\n", # git branch --contains <sha>
+ u"commit-3/file-1\ncommit-3/file-2\n", # git diff-tree
+ ]
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ config_path = self.get_sample_path(os.path.join("config", "gitlintconfig"))
+ result = self.cli.invoke(cli.cli, ["--config", config_path, "--debug", "--commits",
+ "foo...bar"])
+
+ expected = "Commit 6f29bf81a8:\n3: B5\n\n" + \
+ "Commit 25053ccec5:\n1: T3\n3: B5\n\n" + \
+ "Commit 4da2656b0d:\n2: B4\n3: B5\n3: B6\n"
+
+ self.assertEqual(stderr.getvalue(), expected)
+ self.assertEqual(result.exit_code, 6)
+
+ expected_kwargs = self.get_system_info_dict()
+ expected_kwargs.update({'config_path': config_path})
+ expected_logs = self.get_expected('test_cli/test_debug_1', expected_kwargs)
+ self.assert_logged(expected_logs)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n")
+ def test_extra_path(self, _):
+ """ Test for --extra-path flag """
+ # Test extra-path pointing to a directory
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ extra_path = self.get_sample_path("user_rules")
+ result = self.cli.invoke(cli.cli, ["--extra-path", extra_path, "--debug"])
+ expected_output = u"1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \
+ "3: B6 Body message is missing\n"
+ self.assertEqual(stderr.getvalue(), expected_output)
+ self.assertEqual(result.exit_code, 2)
+
+ # Test extra-path pointing to a file
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ extra_path = self.get_sample_path(os.path.join("user_rules", "my_commit_rules.py"))
+ result = self.cli.invoke(cli.cli, ["--extra-path", extra_path, "--debug"])
+ expected_output = u"1: UC1 Commit violåtion 1: \"Contënt 1\"\n" + \
+ "3: B6 Body message is missing\n"
+ self.assertEqual(stderr.getvalue(), expected_output)
+ self.assertEqual(result.exit_code, 2)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n\nMy body that is long enough")
+ def test_contrib(self, _):
+ # Test enabled contrib rules
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--contrib", "contrib-title-conventional-commits,CC1"])
+ expected_output = self.get_expected('test_cli/test_contrib_1')
+ self.assertEqual(stderr.getvalue(), expected_output)
+ self.assertEqual(result.exit_code, 3)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"Test tïtle\n")
+ def test_contrib_negative(self, _):
+ result = self.cli.invoke(cli.cli, ["--contrib", u"föobar,CC1"])
+ self.assertEqual(result.output, u"Config Error: No contrib rule with id or name 'föobar' found.\n")
+ self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: tëst")
+ def test_config_file(self, _):
+ """ Test for --config option """
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ config_path = self.get_sample_path(os.path.join("config", "gitlintconfig"))
+ result = self.cli.invoke(cli.cli, ["--config", config_path])
+ self.assertEqual(result.output, "")
+ self.assertEqual(stderr.getvalue(), "1: T5\n3: B6\n")
+ self.assertEqual(result.exit_code, 2)
+
+ def test_config_file_negative(self):
+ """ Negative test for --config option """
+ # Directory as config file
+ config_path = self.get_sample_path("config")
+ result = self.cli.invoke(cli.cli, ["--config", config_path])
+ expected_string = u"Error: Invalid value for \"-C\" / \"--config\": File \"{0}\" is a directory.".format(
+ config_path)
+ self.assertEqual(result.output.split("\n")[3], expected_string)
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+
+ # Non existing file
+ config_path = self.get_sample_path(u"föo")
+ result = self.cli.invoke(cli.cli, ["--config", config_path])
+ expected_string = u"Error: Invalid value for \"-C\" / \"--config\": File \"{0}\" does not exist.".format(
+ config_path)
+ self.assertEqual(result.output.split("\n")[3], expected_string)
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+
+ # Invalid config file
+ config_path = self.get_sample_path(os.path.join("config", "invalid-option-value"))
+ result = self.cli.invoke(cli.cli, ["--config", config_path])
+ self.assertEqual(result.exit_code, self.CONFIG_ERROR_CODE)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ def test_target(self, _):
+ """ Test for the --target option """
+ os.environ["LANGUAGE"] = "C" # Force language to english so we can check for error message
+ result = self.cli.invoke(cli.cli, ["--target", "/tmp"])
+ # We expect gitlint to tell us that /tmp is not a git repo (this proves that it takes the target parameter
+ # into account).
+ expected_path = os.path.realpath("/tmp")
+ self.assertEqual(result.output, "%s is not a git repository.\n" % expected_path)
+ self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE)
+
+ def test_target_negative(self):
+ """ Negative test for the --target option """
+ # try setting a non-existing target
+ result = self.cli.invoke(cli.cli, ["--target", u"/föo/bar"])
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+ expected_msg = u"Error: Invalid value for \"--target\": Directory \"/föo/bar\" does not exist."
+ self.assertEqual(result.output.split("\n")[3], expected_msg)
+
+ # try setting a file as target
+ target_path = self.get_sample_path(os.path.join("config", "gitlintconfig"))
+ result = self.cli.invoke(cli.cli, ["--target", target_path])
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+ expected_msg = u"Error: Invalid value for \"--target\": Directory \"{0}\" is a file.".format(target_path)
+ self.assertEqual(result.output.split("\n")[3], expected_msg)
+
+ @patch('gitlint.config.LintConfigGenerator.generate_config')
+ def test_generate_config(self, generate_config):
+ """ Test for the generate-config subcommand """
+ result = self.cli.invoke(cli.cli, ["generate-config"], input=u"tëstfile\n")
+ self.assertEqual(result.exit_code, 0)
+ expected_msg = u"Please specify a location for the sample gitlint config file [.gitlint]: tëstfile\n" + \
+ u"Successfully generated {0}\n".format(os.path.realpath(u"tëstfile"))
+ self.assertEqual(result.output, expected_msg)
+ generate_config.assert_called_once_with(os.path.realpath(u"tëstfile"))
+
+ def test_generate_config_negative(self):
+ """ Negative test for the generate-config subcommand """
+ # Non-existing directory
+ fake_dir = os.path.abspath(u"/föo")
+ fake_path = os.path.join(fake_dir, u"bar")
+ result = self.cli.invoke(cli.cli, ["generate-config"], input=fake_path)
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+ expected_msg = (u"Please specify a location for the sample gitlint config file [.gitlint]: {0}\n"
+ + u"Error: Directory '{1}' does not exist.\n").format(fake_path, fake_dir)
+ self.assertEqual(result.output, expected_msg)
+
+ # Existing file
+ sample_path = self.get_sample_path(os.path.join("config", "gitlintconfig"))
+ result = self.cli.invoke(cli.cli, ["generate-config"], input=sample_path)
+ self.assertEqual(result.exit_code, self.USAGE_ERROR_CODE)
+ expected_msg = "Please specify a location for the sample gitlint " + \
+ "config file [.gitlint]: {0}\n".format(sample_path) + \
+ "Error: File \"{0}\" already exists.\n".format(sample_path)
+ self.assertEqual(result.output, expected_msg)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_git_error(self, sh, _):
+ """ Tests that the cli handles git errors properly """
+ sh.git.side_effect = CommandNotFound("git")
+ result = self.cli.invoke(cli.cli)
+ self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE)
+
+ @patch('gitlint.cli.get_stdin_data', return_value=False)
+ @patch('gitlint.git.sh')
+ def test_no_commits_in_range(self, sh, _):
+ """ Test for --commits with the specified range being empty. """
+ sh.git.side_effect = lambda *_args, **_kwargs: ""
+ result = self.cli.invoke(cli.cli, ["--commits", "master...HEAD"])
+
+ self.assert_log_contains(u"DEBUG: gitlint.cli No commits in range \"master...HEAD\"")
+ self.assertEqual(result.exit_code, 0)
diff --git a/gitlint/tests/cli/test_cli_hooks.py b/gitlint/tests/cli/test_cli_hooks.py
new file mode 100644
index 0000000..0564808
--- /dev/null
+++ b/gitlint/tests/cli/test_cli_hooks.py
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+
+import os
+
+from click.testing import CliRunner
+
+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.tests.base import BaseTestCase
+from gitlint import cli
+from gitlint import hooks
+from gitlint import config
+
+
+class CLIHookTests(BaseTestCase):
+ USAGE_ERROR_CODE = 253
+ GIT_CONTEXT_ERROR_CODE = 254
+ CONFIG_ERROR_CODE = 255
+
+ def setUp(self):
+ super(CLIHookTests, self).setUp()
+ self.cli = CliRunner()
+
+ # Patch gitlint.cli.git_version() so that we don't have to patch it separately in every test
+ self.git_version_path = patch('gitlint.cli.git_version')
+ cli.git_version = self.git_version_path.start()
+ cli.git_version.return_value = "git version 1.2.3"
+
+ def tearDown(self):
+ self.git_version_path.stop()
+
+ @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook')
+ @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur"))
+ def test_install_hook(self, _, install_hook):
+ """ Test for install-hook subcommand """
+ result = self.cli.invoke(cli.cli, ["install-hook"])
+ expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH)
+ expected = u"Successfully installed gitlint commit-msg hook in {0}\n".format(expected_path)
+ self.assertEqual(result.output, expected)
+ self.assertEqual(result.exit_code, 0)
+ expected_config = config.LintConfig()
+ expected_config.target = os.path.realpath(os.getcwd())
+ install_hook.assert_called_once_with(expected_config)
+
+ @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook')
+ @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur"))
+ def test_install_hook_target(self, _, install_hook):
+ """ Test for install-hook subcommand with a specific --target option specified """
+ # Specified target
+ result = self.cli.invoke(cli.cli, ["--target", self.SAMPLES_DIR, "install-hook"])
+ expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH)
+ expected = "Successfully installed gitlint commit-msg hook in %s\n" % expected_path
+ self.assertEqual(result.exit_code, 0)
+ self.assertEqual(result.output, expected)
+
+ expected_config = config.LintConfig()
+ expected_config.target = self.SAMPLES_DIR
+ install_hook.assert_called_once_with(expected_config)
+
+ @patch('gitlint.hooks.GitHookInstaller.install_commit_msg_hook', side_effect=hooks.GitHookInstallerError(u"tëst"))
+ def test_install_hook_negative(self, install_hook):
+ """ Negative test for install-hook subcommand """
+ result = self.cli.invoke(cli.cli, ["install-hook"])
+ self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE)
+ self.assertEqual(result.output, u"tëst\n")
+ expected_config = config.LintConfig()
+ expected_config.target = os.path.realpath(os.getcwd())
+ install_hook.assert_called_once_with(expected_config)
+
+ @patch('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook')
+ @patch('gitlint.hooks.git_hooks_dir', return_value=os.path.join(u"/hür", u"dur"))
+ def test_uninstall_hook(self, _, uninstall_hook):
+ """ Test for uninstall-hook subcommand """
+ result = self.cli.invoke(cli.cli, ["uninstall-hook"])
+ expected_path = os.path.join(u"/hür", u"dur", hooks.COMMIT_MSG_HOOK_DST_PATH)
+ expected = u"Successfully uninstalled gitlint commit-msg hook from {0}\n".format(expected_path)
+ self.assertEqual(result.exit_code, 0)
+ self.assertEqual(result.output, expected)
+ expected_config = config.LintConfig()
+ expected_config.target = os.path.realpath(os.getcwd())
+ uninstall_hook.assert_called_once_with(expected_config)
+
+ @patch('gitlint.hooks.GitHookInstaller.uninstall_commit_msg_hook', side_effect=hooks.GitHookInstallerError(u"tëst"))
+ def test_uninstall_hook_negative(self, uninstall_hook):
+ """ Negative test for uninstall-hook subcommand """
+ result = self.cli.invoke(cli.cli, ["uninstall-hook"])
+ self.assertEqual(result.exit_code, self.GIT_CONTEXT_ERROR_CODE)
+ self.assertEqual(result.output, u"tëst\n")
+ expected_config = config.LintConfig()
+ expected_config.target = os.path.realpath(os.getcwd())
+ uninstall_hook.assert_called_once_with(expected_config)
diff --git a/gitlint/tests/config/test_config.py b/gitlint/tests/config/test_config.py
new file mode 100644
index 0000000..d3fdc2c
--- /dev/null
+++ b/gitlint/tests/config/test_config.py
@@ -0,0 +1,263 @@
+# -*- coding: utf-8 -*-
+
+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 import rules
+from gitlint.config import LintConfig, LintConfigError, LintConfigGenerator, GITLINT_CONFIG_TEMPLATE_SRC_PATH
+from gitlint import options
+from gitlint.tests.base import BaseTestCase, ustr
+
+
+class LintConfigTests(BaseTestCase):
+
+ def test_set_rule_option(self):
+ config = LintConfig()
+
+ # assert default title line-length
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 72)
+
+ # change line length and assert it is set
+ config.set_rule_option('title-max-length', 'line-length', 60)
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 60)
+
+ def test_set_rule_option_negative(self):
+ config = LintConfig()
+
+ # non-existing rule
+ expected_error_msg = u"No such rule 'föobar'"
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config.set_rule_option(u'föobar', u'lïne-length', 60)
+
+ # non-existing option
+ expected_error_msg = u"Rule 'title-max-length' has no option 'föobar'"
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config.set_rule_option('title-max-length', u'föobar', 60)
+
+ # invalid option value
+ expected_error_msg = u"'föo' is not a valid value for option 'title-max-length.line-length'. " + \
+ u"Option 'line-length' must be a positive integer (current value: 'föo')."
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config.set_rule_option('title-max-length', 'line-length', u"föo")
+
+ def test_set_general_option(self):
+ config = LintConfig()
+
+ # Check that default general options are correct
+ self.assertTrue(config.ignore_merge_commits)
+ self.assertTrue(config.ignore_fixup_commits)
+ self.assertTrue(config.ignore_squash_commits)
+ self.assertTrue(config.ignore_revert_commits)
+
+ self.assertFalse(config.ignore_stdin)
+ self.assertFalse(config.staged)
+ self.assertFalse(config.debug)
+ self.assertEqual(config.verbosity, 3)
+ active_rule_classes = tuple(type(rule) for rule in config.rules)
+ self.assertTupleEqual(active_rule_classes, config.default_rule_classes)
+
+ # ignore - set by string
+ config.set_general_option("ignore", "title-trailing-whitespace, B2")
+ self.assertEqual(config.ignore, ["title-trailing-whitespace", "B2"])
+
+ # ignore - set by list
+ config.set_general_option("ignore", ["T1", "B3"])
+ self.assertEqual(config.ignore, ["T1", "B3"])
+
+ # verbosity
+ config.set_general_option("verbosity", 1)
+ self.assertEqual(config.verbosity, 1)
+
+ # ignore_merge_commit
+ config.set_general_option("ignore-merge-commits", "false")
+ self.assertFalse(config.ignore_merge_commits)
+
+ # ignore_fixup_commit
+ config.set_general_option("ignore-fixup-commits", "false")
+ self.assertFalse(config.ignore_fixup_commits)
+
+ # ignore_squash_commit
+ config.set_general_option("ignore-squash-commits", "false")
+ self.assertFalse(config.ignore_squash_commits)
+
+ # ignore_revert_commit
+ config.set_general_option("ignore-revert-commits", "false")
+ self.assertFalse(config.ignore_revert_commits)
+
+ # debug
+ config.set_general_option("debug", "true")
+ self.assertTrue(config.debug)
+
+ # ignore-stdin
+ config.set_general_option("ignore-stdin", "true")
+ self.assertTrue(config.debug)
+
+ # staged
+ config.set_general_option("staged", "true")
+ self.assertTrue(config.staged)
+
+ # target
+ config.set_general_option("target", self.SAMPLES_DIR)
+ self.assertEqual(config.target, self.SAMPLES_DIR)
+
+ # extra_path has its own test: test_extra_path and test_extra_path_negative
+ # contrib has its own tests: test_contrib and test_contrib_negative
+
+ def test_contrib(self):
+ config = LintConfig()
+ contrib_rules = ["contrib-title-conventional-commits", "CC1"]
+ config.set_general_option("contrib", ",".join(contrib_rules))
+ self.assertEqual(config.contrib, contrib_rules)
+
+ # Check contrib-title-conventional-commits contrib rule
+ actual_rule = config.rules.find_rule("contrib-title-conventional-commits")
+ self.assertTrue(actual_rule.is_contrib)
+
+ self.assertEqual(ustr(type(actual_rule)), "<class 'conventional_commit.ConventionalCommit'>")
+ self.assertEqual(actual_rule.id, 'CT1')
+ self.assertEqual(actual_rule.name, u'contrib-title-conventional-commits')
+ self.assertEqual(actual_rule.target, rules.CommitMessageTitle)
+
+ expected_rule_option = options.ListOption(
+ "types",
+ ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert"],
+ "Comma separated list of allowed commit types.",
+ )
+
+ self.assertListEqual(actual_rule.options_spec, [expected_rule_option])
+ self.assertDictEqual(actual_rule.options, {'types': expected_rule_option})
+
+ # Check contrib-body-requires-signed-off-by contrib rule
+ actual_rule = config.rules.find_rule("contrib-body-requires-signed-off-by")
+ self.assertTrue(actual_rule.is_contrib)
+
+ self.assertEqual(ustr(type(actual_rule)), "<class 'signedoff_by.SignedOffBy'>")
+ self.assertEqual(actual_rule.id, 'CC1')
+ self.assertEqual(actual_rule.name, u'contrib-body-requires-signed-off-by')
+
+ # reset value (this is a different code path)
+ config.set_general_option("contrib", "contrib-body-requires-signed-off-by")
+ self.assertEqual(actual_rule, config.rules.find_rule("contrib-body-requires-signed-off-by"))
+ self.assertIsNone(config.rules.find_rule("contrib-title-conventional-commits"))
+
+ # empty value
+ config.set_general_option("contrib", "")
+ self.assertListEqual(config.contrib, [])
+
+ def test_contrib_negative(self):
+ config = LintConfig()
+ # non-existent contrib rule
+ with self.assertRaisesRegex(LintConfigError, u"No contrib rule with id or name 'föo' found."):
+ config.contrib = u"contrib-title-conventional-commits,föo"
+
+ # UserRuleError, RuleOptionError should be re-raised as LintConfigErrors
+ side_effects = [rules.UserRuleError(u"üser-rule"), options.RuleOptionError(u"rüle-option")]
+ for side_effect in side_effects:
+ with patch('gitlint.config.rule_finder.find_rule_classes', side_effect=side_effect):
+ with self.assertRaisesRegex(LintConfigError, ustr(side_effect)):
+ config.contrib = u"contrib-title-conventional-commits"
+
+ def test_extra_path(self):
+ config = LintConfig()
+
+ config.set_general_option("extra-path", self.get_user_rules_path())
+ self.assertEqual(config.extra_path, self.get_user_rules_path())
+ actual_rule = config.rules.find_rule('UC1')
+ self.assertTrue(actual_rule.is_user_defined)
+ self.assertEqual(ustr(type(actual_rule)), "<class 'my_commit_rules.MyUserCommitRule'>")
+ self.assertEqual(actual_rule.id, 'UC1')
+ self.assertEqual(actual_rule.name, u'my-üser-commit-rule')
+ self.assertEqual(actual_rule.target, None)
+ expected_rule_option = options.IntOption('violation-count', 1, u"Number of violåtions to return")
+ self.assertListEqual(actual_rule.options_spec, [expected_rule_option])
+ self.assertDictEqual(actual_rule.options, {'violation-count': expected_rule_option})
+
+ # reset value (this is a different code path)
+ config.set_general_option("extra-path", self.SAMPLES_DIR)
+ self.assertEqual(config.extra_path, self.SAMPLES_DIR)
+ self.assertIsNone(config.rules.find_rule("UC1"))
+
+ def test_extra_path_negative(self):
+ config = LintConfig()
+ regex = u"Option extra-path must be either an existing directory or file (current value: 'föo/bar')"
+ # incorrect extra_path
+ with self.assertRaisesRegex(LintConfigError, regex):
+ config.extra_path = u"föo/bar"
+
+ # extra path contains classes with errors
+ with self.assertRaisesRegex(LintConfigError,
+ "User-defined rule class 'MyUserLineRule' must have a 'validate' method"):
+ config.extra_path = self.get_sample_path("user_rules/incorrect_linerule")
+
+ def test_set_general_option_negative(self):
+ config = LintConfig()
+
+ # Note that we shouldn't test whether we can set unicode because python just doesn't allow unicode attributes
+ with self.assertRaisesRegex(LintConfigError, "'foo' is not a valid gitlint option"):
+ config.set_general_option("foo", u"bår")
+
+ # try setting _config_path, this is a real attribute of LintConfig, but the code should prevent it from
+ # being set
+ with self.assertRaisesRegex(LintConfigError, "'_config_path' is not a valid gitlint option"):
+ config.set_general_option("_config_path", u"bår")
+
+ # invalid verbosity
+ incorrect_values = [-1, u"föo"]
+ for value in incorrect_values:
+ expected_msg = u"Option 'verbosity' must be a positive integer (current value: '{0}')".format(value)
+ with self.assertRaisesRegex(LintConfigError, expected_msg):
+ config.verbosity = value
+
+ incorrect_values = [4]
+ for value in incorrect_values:
+ with self.assertRaisesRegex(LintConfigError, "Option 'verbosity' must be set between 0 and 3"):
+ config.verbosity = value
+
+ # invalid ignore_xxx_commits
+ ignore_attributes = ["ignore_merge_commits", "ignore_fixup_commits", "ignore_squash_commits",
+ "ignore_revert_commits"]
+ incorrect_values = [-1, 4, u"föo"]
+ for attribute in ignore_attributes:
+ for value in incorrect_values:
+ option_name = attribute.replace("_", "-")
+ with self.assertRaisesRegex(LintConfigError,
+ "Option '{0}' must be either 'true' or 'false'".format(option_name)):
+ setattr(config, attribute, value)
+
+ # invalid ignore -> not here because ignore is a ListOption which converts everything to a string before
+ # splitting which means it it will accept just about everything
+
+ # invalid boolean options
+ for attribute in ['debug', 'staged', 'ignore_stdin']:
+ option_name = attribute.replace("_", "-")
+ with self.assertRaisesRegex(LintConfigError,
+ "Option '{0}' must be either 'true' or 'false'".format(option_name)):
+ setattr(config, attribute, u"föobar")
+
+ # extra-path has its own negative test
+
+ # invalid target
+ with self.assertRaisesRegex(LintConfigError,
+ u"Option target must be an existing directory (current value: 'föo/bar')"):
+ config.target = u"föo/bar"
+
+ def test_ignore_independent_from_rules(self):
+ # Test that the lintconfig rules are not modified when setting config.ignore
+ # This was different in the past, this test is mostly here to catch regressions
+ config = LintConfig()
+ original_rules = config.rules
+ config.ignore = ["T1", "T2"]
+ self.assertEqual(config.ignore, ["T1", "T2"])
+ self.assertSequenceEqual(config.rules, original_rules)
+
+
+class LintConfigGeneratorTests(BaseTestCase):
+ @staticmethod
+ @patch('gitlint.config.shutil.copyfile')
+ def test_install_commit_msg_hook_negative(copy):
+ LintConfigGenerator.generate_config(u"föo/bar/test")
+ copy.assert_called_with(GITLINT_CONFIG_TEMPLATE_SRC_PATH, u"föo/bar/test")
diff --git a/gitlint/tests/config/test_config_builder.py b/gitlint/tests/config/test_config_builder.py
new file mode 100644
index 0000000..051a52f
--- /dev/null
+++ b/gitlint/tests/config/test_config_builder.py
@@ -0,0 +1,203 @@
+# -*- coding: utf-8 -*-
+
+from gitlint.tests.base import BaseTestCase
+
+from gitlint.config import LintConfig, LintConfigBuilder, LintConfigError
+
+
+class LintConfigBuilderTests(BaseTestCase):
+ def test_set_option(self):
+ config_builder = LintConfigBuilder()
+ config = config_builder.build()
+
+ # assert some defaults
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 72)
+ self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 80)
+ self.assertListEqual(config.get_rule_option('title-must-not-contain-word', 'words'), ["WIP"])
+ self.assertEqual(config.verbosity, 3)
+
+ # Make some changes and check blueprint
+ config_builder.set_option('title-max-length', 'line-length', 100)
+ config_builder.set_option('general', 'verbosity', 2)
+ config_builder.set_option('title-must-not-contain-word', 'words', ["foo", "bar"])
+ expected_blueprint = {'title-must-not-contain-word': {'words': ['foo', 'bar']},
+ 'title-max-length': {'line-length': 100}, 'general': {'verbosity': 2}}
+ self.assertDictEqual(config_builder._config_blueprint, expected_blueprint)
+
+ # Build config and verify that the changes have occurred and no other changes
+ config = config_builder.build()
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 100)
+ self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 80) # should be unchanged
+ self.assertListEqual(config.get_rule_option('title-must-not-contain-word', 'words'), ["foo", "bar"])
+ self.assertEqual(config.verbosity, 2)
+
+ def test_set_from_commit_ignore_all(self):
+ config = LintConfig()
+ original_rules = config.rules
+ original_rule_ids = [rule.id for rule in original_rules]
+
+ config_builder = LintConfigBuilder()
+
+ # nothing gitlint
+ config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint\nfoo"))
+ config = config_builder.build()
+ self.assertSequenceEqual(config.rules, original_rules)
+ self.assertListEqual(config.ignore, [])
+
+ # ignore all rules
+ config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: all\nfoo"))
+ config = config_builder.build()
+ self.assertEqual(config.ignore, original_rule_ids)
+
+ # ignore all rules, no space
+ config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore:all\nfoo"))
+ config = config_builder.build()
+ self.assertEqual(config.ignore, original_rule_ids)
+
+ # ignore all rules, more spacing
+ config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: \t all\nfoo"))
+ config = config_builder.build()
+ self.assertEqual(config.ignore, original_rule_ids)
+
+ def test_set_from_commit_ignore_specific(self):
+ # ignore specific rules
+ config_builder = LintConfigBuilder()
+ config_builder.set_config_from_commit(self.gitcommit(u"tëst\ngitlint-ignore: T1, body-hard-tab"))
+ config = config_builder.build()
+ self.assertEqual(config.ignore, ["T1", "body-hard-tab"])
+
+ def test_set_from_config_file(self):
+ # regular config file load, no problems
+ config_builder = LintConfigBuilder()
+ config_builder.set_from_config_file(self.get_sample_path("config/gitlintconfig"))
+ config = config_builder.build()
+
+ # Do some assertions on the config
+ self.assertEqual(config.verbosity, 1)
+ self.assertFalse(config.debug)
+ self.assertFalse(config.ignore_merge_commits)
+ self.assertIsNone(config.extra_path)
+ self.assertEqual(config.ignore, ["title-trailing-whitespace", "B2"])
+
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 20)
+ self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 30)
+
+ def test_set_from_config_file_negative(self):
+ config_builder = LintConfigBuilder()
+
+ # bad config file load
+ foo_path = self.get_sample_path(u"föo")
+ expected_error_msg = u"Invalid file path: {0}".format(foo_path)
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.set_from_config_file(foo_path)
+
+ # error during file parsing
+ path = self.get_sample_path("config/no-sections")
+ expected_error_msg = u"File contains no section headers."
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.set_from_config_file(path)
+
+ # non-existing rule
+ path = self.get_sample_path("config/nonexisting-rule")
+ config_builder = LintConfigBuilder()
+ config_builder.set_from_config_file(path)
+ expected_error_msg = u"No such rule 'föobar'"
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.build()
+
+ # non-existing general option
+ path = self.get_sample_path("config/nonexisting-general-option")
+ config_builder = LintConfigBuilder()
+ config_builder.set_from_config_file(path)
+ expected_error_msg = u"'foo' is not a valid gitlint option"
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.build()
+
+ # non-existing option
+ path = self.get_sample_path("config/nonexisting-option")
+ config_builder = LintConfigBuilder()
+ config_builder.set_from_config_file(path)
+ expected_error_msg = u"Rule 'title-max-length' has no option 'föobar'"
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.build()
+
+ # invalid option value
+ path = self.get_sample_path("config/invalid-option-value")
+ config_builder = LintConfigBuilder()
+ config_builder.set_from_config_file(path)
+ expected_error_msg = u"'föo' is not a valid value for option 'title-max-length.line-length'. " + \
+ u"Option 'line-length' must be a positive integer (current value: 'föo')."
+ with self.assertRaisesRegex(LintConfigError, expected_error_msg):
+ config_builder.build()
+
+ def test_set_config_from_string_list(self):
+ config = LintConfig()
+
+ # change and assert changes
+ config_builder = LintConfigBuilder()
+ config_builder.set_config_from_string_list(['general.verbosity=1', 'title-max-length.line-length=60',
+ 'body-max-line-length.line-length=120',
+ u"title-must-not-contain-word.words=håha"])
+
+ config = config_builder.build()
+ self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 60)
+ self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 120)
+ self.assertListEqual(config.get_rule_option('title-must-not-contain-word', 'words'), [u"håha"])
+ self.assertEqual(config.verbosity, 1)
+
+ def test_set_config_from_string_list_negative(self):
+ config_builder = LintConfigBuilder()
+
+ # assert error on incorrect rule - this happens at build time
+ config_builder.set_config_from_string_list([u"föo.bar=1"])
+ with self.assertRaisesRegex(LintConfigError, u"No such rule 'föo'"):
+ config_builder.build()
+
+ # no equal sign
+ expected_msg = u"'föo.bar' is an invalid configuration option. Use '<rule>.<option>=<value>'"
+ with self.assertRaisesRegex(LintConfigError, expected_msg):
+ config_builder.set_config_from_string_list([u"föo.bar"])
+
+ # missing value
+ expected_msg = u"'föo.bar=' is an invalid configuration option. Use '<rule>.<option>=<value>'"
+ with self.assertRaisesRegex(LintConfigError, expected_msg):
+ config_builder.set_config_from_string_list([u"föo.bar="])
+
+ # space instead of equal sign
+ expected_msg = u"'föo.bar 1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
+ with self.assertRaisesRegex(LintConfigError, expected_msg):
+ config_builder.set_config_from_string_list([u"föo.bar 1"])
+
+ # no period between rule and option names
+ expected_msg = u"'föobar=1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
+ with self.assertRaisesRegex(LintConfigError, expected_msg):
+ config_builder.set_config_from_string_list([u'föobar=1'])
+
+ def test_rebuild_config(self):
+ # normal config build
+ config_builder = LintConfigBuilder()
+ config_builder.set_option('general', 'verbosity', 3)
+ lint_config = config_builder.build()
+ self.assertEqual(lint_config.verbosity, 3)
+
+ # check that existing config gets overwritten when we pass it to a configbuilder with different options
+ existing_lintconfig = LintConfig()
+ existing_lintconfig.verbosity = 2
+ lint_config = config_builder.build(existing_lintconfig)
+ self.assertEqual(lint_config.verbosity, 3)
+ self.assertEqual(existing_lintconfig.verbosity, 3)
+
+ def test_clone(self):
+ config_builder = LintConfigBuilder()
+ config_builder.set_option('general', 'verbosity', 2)
+ config_builder.set_option('title-max-length', 'line-length', 100)
+ expected = {'title-max-length': {'line-length': 100}, 'general': {'verbosity': 2}}
+ self.assertDictEqual(config_builder._config_blueprint, expected)
+
+ # Clone and verify that the blueprint is the same as the original
+ cloned_builder = config_builder.clone()
+ self.assertDictEqual(cloned_builder._config_blueprint, expected)
+
+ # Modify the original and make sure we're not modifying the clone (i.e. check that the copy is a deep copy)
+ config_builder.set_option('title-max-length', 'line-length', 120)
+ self.assertDictEqual(cloned_builder._config_blueprint, expected)
diff --git a/gitlint/tests/config/test_config_precedence.py b/gitlint/tests/config/test_config_precedence.py
new file mode 100644
index 0000000..9689e55
--- /dev/null
+++ b/gitlint/tests/config/test_config_precedence.py
@@ -0,0 +1,100 @@
+# -*- coding: utf-8 -*-
+
+try:
+ # python 2.x
+ from StringIO import StringIO
+except ImportError:
+ # python 3.x
+ from io import StringIO
+
+from click.testing import CliRunner
+
+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.tests.base import BaseTestCase
+from gitlint import cli
+from gitlint.config import LintConfigBuilder
+
+
+class LintConfigPrecedenceTests(BaseTestCase):
+ def setUp(self):
+ self.cli = CliRunner()
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"WIP\n\nThis is å test message\n")
+ def test_config_precedence(self, _):
+ # TODO(jroovers): this test really only test verbosity, we need to do some refactoring to gitlint.cli
+ # to more easily test everything
+ # Test that the config precedence is followed:
+ # 1. commandline convenience flags
+ # 2. commandline -c flags
+ # 3. config file
+ # 4. default config
+ config_path = self.get_sample_path("config/gitlintconfig")
+
+ # 1. commandline convenience flags
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["-vvv", "-c", "general.verbosity=2", "--config", config_path])
+ self.assertEqual(result.output, "")
+ self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP\"\n")
+
+ # 2. commandline -c flags
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["-c", "general.verbosity=2", "--config", config_path])
+ self.assertEqual(result.output, "")
+ self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive)\n")
+
+ # 3. config file
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli, ["--config", config_path])
+ self.assertEqual(result.output, "")
+ self.assertEqual(stderr.getvalue(), "1: T5\n")
+
+ # 4. default config
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ result = self.cli.invoke(cli.cli)
+ self.assertEqual(result.output, "")
+ self.assertEqual(stderr.getvalue(), "1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP\"\n")
+
+ @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: This is å test")
+ def test_ignore_precedence(self, get_stdin_data):
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ # --ignore takes precedence over -c general.ignore
+ result = self.cli.invoke(cli.cli, ["-c", "general.ignore=T5", "--ignore", "B6"])
+ self.assertEqual(result.output, "")
+ self.assertEqual(result.exit_code, 1)
+ # We still expect the T5 violation, but no B6 violation as --ignore overwrites -c general.ignore
+ self.assertEqual(stderr.getvalue(),
+ u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This is å test\"\n")
+
+ # test that we can also still configure a rule that is first ignored but then not
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ get_stdin_data.return_value = u"This is å test"
+ # --ignore takes precedence over -c general.ignore
+ result = self.cli.invoke(cli.cli, ["-c", "general.ignore=title-max-length",
+ "-c", "title-max-length.line-length=5",
+ "--ignore", "B6"])
+ self.assertEqual(result.output, "")
+ self.assertEqual(result.exit_code, 1)
+
+ # We still expect the T1 violation with custom config,
+ # but no B6 violation as --ignore overwrites -c general.ignore
+ self.assertEqual(stderr.getvalue(), u"1: T1 Title exceeds max length (14>5): \"This is å test\"\n")
+
+ def test_general_option_after_rule_option(self):
+ # We used to have a bug where we didn't process general options before setting specific options, this would
+ # lead to errors when e.g.: trying to configure a user rule before the rule class was loaded by extra-path
+ # This test is here to test for regressions against this.
+
+ config_builder = LintConfigBuilder()
+ config_builder.set_option(u'my-üser-commit-rule', 'violation-count', 3)
+ user_rules_path = self.get_sample_path("user_rules")
+ config_builder.set_option('general', 'extra-path', user_rules_path)
+ config = config_builder.build()
+
+ self.assertEqual(config.extra_path, user_rules_path)
+ self.assertEqual(config.get_rule_option(u'my-üser-commit-rule', 'violation-count'), 3)
diff --git a/gitlint/tests/config/test_rule_collection.py b/gitlint/tests/config/test_rule_collection.py
new file mode 100644
index 0000000..089992c
--- /dev/null
+++ b/gitlint/tests/config/test_rule_collection.py
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+
+from collections import OrderedDict
+from gitlint import rules
+from gitlint.config import RuleCollection
+from gitlint.tests.base import BaseTestCase
+
+
+class RuleCollectionTests(BaseTestCase):
+
+ def test_add_rule(self):
+ collection = RuleCollection()
+ collection.add_rule(rules.TitleMaxLength, u"my-rüle", {"my_attr": u"föo", "my_attr2": 123})
+
+ expected = rules.TitleMaxLength()
+ expected.id = u"my-rüle"
+ expected.my_attr = u"föo"
+ expected.my_attr2 = 123
+
+ self.assertEqual(len(collection), 1)
+ self.assertDictEqual(collection._rules, OrderedDict({u"my-rüle": expected}))
+ # Need to explicitely compare expected attributes as the rule.__eq__ method does not compare these attributes
+ self.assertEqual(collection._rules[expected.id].my_attr, expected.my_attr)
+ self.assertEqual(collection._rules[expected.id].my_attr2, expected.my_attr2)
+
+ def test_add_find_rule(self):
+ collection = RuleCollection()
+ collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"my_attr": u"föo"})
+
+ # find by id
+ expected = rules.TitleMaxLength()
+ rule = collection.find_rule('T1')
+ self.assertEqual(rule, expected)
+ self.assertEqual(rule.my_attr, u"föo")
+
+ # find by name
+ expected2 = rules.TitleTrailingWhitespace()
+ rule = collection.find_rule('title-trailing-whitespace')
+ self.assertEqual(rule, expected2)
+ self.assertEqual(rule.my_attr, u"föo")
+
+ # find non-existing
+ rule = collection.find_rule(u'föo')
+ self.assertIsNone(rule)
+
+ def test_delete_rules_by_attr(self):
+ collection = RuleCollection()
+ collection.add_rules([rules.TitleMaxLength, rules.TitleTrailingWhitespace], {"foo": u"bår"})
+ collection.add_rules([rules.BodyHardTab], {"hur": u"dûr"})
+
+ # Assert all rules are there as expected
+ self.assertEqual(len(collection), 3)
+ for expected_rule in [rules.TitleMaxLength(), rules.TitleTrailingWhitespace(), rules.BodyHardTab()]:
+ self.assertEqual(collection.find_rule(expected_rule.id), expected_rule)
+
+ # Delete rules by attr, assert that we still have the right rules in the collection
+ collection.delete_rules_by_attr("foo", u"bår")
+ self.assertEqual(len(collection), 1)
+ self.assertIsNone(collection.find_rule(rules.TitleMaxLength.id), None)
+ self.assertIsNone(collection.find_rule(rules.TitleTrailingWhitespace.id), None)
+
+ found = collection.find_rule(rules.BodyHardTab.id)
+ self.assertEqual(found, rules.BodyHardTab())
+ self.assertEqual(found.hur, u"dûr")
diff --git a/gitlint/tests/contrib/__init__.py b/gitlint/tests/contrib/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gitlint/tests/contrib/__init__.py
diff --git a/gitlint/tests/contrib/test_contrib_rules.py b/gitlint/tests/contrib/test_contrib_rules.py
new file mode 100644
index 0000000..3fa4048
--- /dev/null
+++ b/gitlint/tests/contrib/test_contrib_rules.py
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+import os
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.contrib import rules as contrib_rules
+from gitlint.tests import contrib as contrib_tests
+from gitlint import rule_finder, rules
+
+from gitlint.utils import ustr
+
+
+class ContribRuleTests(BaseTestCase):
+
+ CONTRIB_DIR = os.path.dirname(os.path.realpath(contrib_rules.__file__))
+
+ def test_contrib_tests_exist(self):
+ """ Tests that every contrib rule file has an associated test file.
+ While this doesn't guarantee that every contrib rule has associated tests (as we don't check the content
+ of the tests file), it's a good leading indicator. """
+
+ contrib_tests_dir = os.path.dirname(os.path.realpath(contrib_tests.__file__))
+ contrib_test_files = os.listdir(contrib_tests_dir)
+
+ # Find all python files in the contrib dir and assert there's a corresponding test file
+ for filename in os.listdir(self.CONTRIB_DIR):
+ if filename.endswith(".py") and filename not in ["__init__.py"]:
+ expected_test_file = ustr(u"test_" + filename)
+ error_msg = u"Every Contrib Rule must have associated tests. " + \
+ "Expected test file {0} not found.".format(os.path.join(contrib_tests_dir,
+ expected_test_file))
+ self.assertIn(expected_test_file, contrib_test_files, error_msg)
+
+ def test_contrib_rule_naming_conventions(self):
+ """ Tests that contrib rules follow certain naming conventions.
+ We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
+ because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
+ again.
+ """
+ rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
+
+ for clazz in rule_classes:
+ # Contrib rule names start with "contrib-"
+ self.assertTrue(clazz.name.startswith("contrib-"))
+
+ # Contrib line rules id's start with "CL"
+ if issubclass(clazz, rules.LineRule):
+ if clazz.target == rules.CommitMessageTitle:
+ self.assertTrue(clazz.id.startswith("CT"))
+ elif clazz.target == rules.CommitMessageBody:
+ self.assertTrue(clazz.id.startswith("CB"))
+
+ def test_contrib_rule_uniqueness(self):
+ """ Tests that all contrib rules have unique identifiers.
+ We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
+ because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
+ again.
+ """
+ rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
+
+ # Not very efficient way of checking uniqueness, but it works :-)
+ class_names = [rule_class.name for rule_class in rule_classes]
+ class_ids = [rule_class.id for rule_class in rule_classes]
+ self.assertEqual(len(set(class_names)), len(class_names))
+ self.assertEqual(len(set(class_ids)), len(class_ids))
+
+ def test_contrib_rule_instantiated(self):
+ """ Tests that all contrib rules can be instantiated without errors. """
+ rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
+
+ # No exceptions = what we want :-)
+ for rule_class in rule_classes:
+ rule_class()
diff --git a/gitlint/tests/contrib/test_conventional_commit.py b/gitlint/tests/contrib/test_conventional_commit.py
new file mode 100644
index 0000000..ea808fd
--- /dev/null
+++ b/gitlint/tests/contrib/test_conventional_commit.py
@@ -0,0 +1,47 @@
+
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import RuleViolation
+from gitlint.contrib.rules.conventional_commit import ConventionalCommit
+from gitlint.config import LintConfig
+
+
+class ContribConventionalCommitTests(BaseTestCase):
+
+ def test_enable(self):
+ # Test that rule can be enabled in config
+ for rule_ref in ['CT1', 'contrib-title-conventional-commits']:
+ config = LintConfig()
+ config.contrib = [rule_ref]
+ self.assertIn(ConventionalCommit(), config.rules)
+
+ def test_conventional_commits(self):
+ rule = ConventionalCommit()
+
+ # No violations when using a correct type and format
+ for type in ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert"]:
+ violations = rule.validate(type + u": föo", None)
+ self.assertListEqual([], violations)
+
+ # assert violation on wrong type
+ expected_violation = RuleViolation("CT1", "Title does not start with one of fix, feat, chore, docs,"
+ " style, refactor, perf, test, revert", u"bår: foo")
+ violations = rule.validate(u"bår: foo", None)
+ self.assertListEqual([expected_violation], violations)
+
+ # assert violation on wrong format
+ expected_violation = RuleViolation("CT1", "Title does not follow ConventionalCommits.org format "
+ "'type(optional-scope): description'", u"fix föo")
+ violations = rule.validate(u"fix föo", None)
+ self.assertListEqual([expected_violation], violations)
+
+ # assert no violation when adding new type
+ rule = ConventionalCommit({'types': [u"föo", u"bär"]})
+ for typ in [u"föo", u"bär"]:
+ violations = rule.validate(typ + u": hür dur", None)
+ self.assertListEqual([], violations)
+
+ # assert violation when using incorrect type when types have been reconfigured
+ violations = rule.validate(u"fix: hür dur", None)
+ expected_violation = RuleViolation("CT1", u"Title does not start with one of föo, bär", u"fix: hür dur")
+ self.assertListEqual([expected_violation], violations)
diff --git a/gitlint/tests/contrib/test_signedoff_by.py b/gitlint/tests/contrib/test_signedoff_by.py
new file mode 100644
index 0000000..934aec5
--- /dev/null
+++ b/gitlint/tests/contrib/test_signedoff_by.py
@@ -0,0 +1,32 @@
+
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import RuleViolation
+from gitlint.contrib.rules.signedoff_by import SignedOffBy
+
+from gitlint.config import LintConfig
+
+
+class ContribSignedOffByTests(BaseTestCase):
+
+ def test_enable(self):
+ # Test that rule can be enabled in config
+ for rule_ref in ['CC1', 'contrib-body-requires-signed-off-by']:
+ config = LintConfig()
+ config.contrib = [rule_ref]
+ self.assertIn(SignedOffBy(), config.rules)
+
+ def test_signedoff_by(self):
+ # No violations when 'Signed-Off-By' line is present
+ rule = SignedOffBy()
+ violations = rule.validate(self.gitcommit(u"Föobar\n\nMy Body\nSigned-Off-By: John Smith"))
+ self.assertListEqual([], violations)
+
+ # Assert violation when no 'Signed-Off-By' line is present
+ violations = rule.validate(self.gitcommit(u"Föobar\n\nMy Body"))
+ expected_violation = RuleViolation("CC1", "Body does not contain a 'Signed-Off-By' line", line_nr=1)
+ self.assertListEqual(violations, [expected_violation])
+
+ # Assert violation when no 'Signed-Off-By' in title but not in body
+ violations = rule.validate(self.gitcommit(u"Signed-Off-By\n\nFöobar"))
+ self.assertListEqual(violations, [expected_violation])
diff --git a/gitlint/tests/expected/test_cli/test_contrib_1 b/gitlint/tests/expected/test_cli/test_contrib_1
new file mode 100644
index 0000000..ea5d353
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_contrib_1
@@ -0,0 +1,3 @@
+1: CC1 Body does not contain a 'Signed-Off-By' line
+1: CT1 Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert: "Test tïtle"
+1: CT1 Title does not follow ConventionalCommits.org format 'type(optional-scope): description': "Test tïtle"
diff --git a/gitlint/tests/expected/test_cli/test_debug_1 b/gitlint/tests/expected/test_cli/test_debug_1
new file mode 100644
index 0000000..612f78e
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_debug_1
@@ -0,0 +1,102 @@
+DEBUG: gitlint.cli To report issues, please visit https://github.com/jorisroovers/gitlint/issues
+DEBUG: gitlint.cli Platform: {platform}
+DEBUG: gitlint.cli Python version: {python_version}
+DEBUG: gitlint.cli Git version: git version 1.2.3
+DEBUG: gitlint.cli Gitlint version: {gitlint_version}
+DEBUG: gitlint.cli GITLINT_USE_SH_LIB: {GITLINT_USE_SH_LIB}
+DEBUG: gitlint.cli Configuration
+config-path: {config_path}
+[GENERAL]
+extra-path: None
+contrib: []
+ignore: title-trailing-whitespace,B2
+ignore-merge-commits: False
+ignore-fixup-commits: True
+ignore-squash-commits: True
+ignore-revert-commits: True
+ignore-stdin: False
+staged: False
+verbosity: 1
+debug: True
+target: {target}
+[RULES]
+ I1: ignore-by-title
+ ignore=all
+ regex=None
+ I2: ignore-by-body
+ ignore=all
+ regex=None
+ T1: title-max-length
+ line-length=20
+ T2: title-trailing-whitespace
+ T6: title-leading-whitespace
+ T3: title-trailing-punctuation
+ T4: title-hard-tab
+ T5: title-must-not-contain-word
+ words=WIP,bögus
+ T7: title-match-regex
+ regex=.*
+ B1: body-max-line-length
+ line-length=30
+ B5: body-min-length
+ min-length=20
+ B6: body-is-missing
+ ignore-merge-commits=True
+ B2: body-trailing-whitespace
+ B3: body-hard-tab
+ B4: body-first-line-empty
+ B7: body-changed-file-mention
+ files=
+ M1: author-valid-email
+ regex=[^@ ]+@[^@ ]+\.[^@ ]+
+
+DEBUG: gitlint.cli No --msg-filename flag, no or empty data passed to stdin. Using the local repo.
+DEBUG: gitlint.cli Linting 3 commit(s)
+DEBUG: gitlint.lint Linting commit 6f29bf81a8322a04071bb794666e48c443a90360
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+commït-title1
+
+commït-body1
+--- Meta info ---------
+Author: test åuthor1 <test-email1@föo.com>
+Date: 2016-12-03 15:28:15 +0100
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: ['commit-1-branch-1', 'commit-1-branch-2']
+Changed Files: ['commit-1/file-1', 'commit-1/file-2']
+-----------------------
+DEBUG: gitlint.lint Linting commit 25053ccec5e28e1bb8f7551fdbb5ab213ada2401
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+commït-title2.
+
+commït-body2
+--- Meta info ---------
+Author: test åuthor2 <test-email2@föo.com>
+Date: 2016-12-04 15:28:15 +0100
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: ['commit-2-branch-1', 'commit-2-branch-2']
+Changed Files: ['commit-2/file-1', 'commit-2/file-2']
+-----------------------
+DEBUG: gitlint.lint Linting commit 4da2656b0dadc76c7ee3fd0243a96cb64007f125
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+föo
+bar
+--- Meta info ---------
+Author: test åuthor3 <test-email3@föo.com>
+Date: 2016-12-05 15:28:15 +0100
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: ['commit-3-branch-1', 'commit-3-branch-2']
+Changed Files: ['commit-3/file-1', 'commit-3/file-2']
+-----------------------
+DEBUG: gitlint.cli Exit Code = 6 \ No newline at end of file
diff --git a/gitlint/tests/expected/test_cli/test_input_stream_1 b/gitlint/tests/expected/test_cli/test_input_stream_1
new file mode 100644
index 0000000..4326729
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_input_stream_1
@@ -0,0 +1,3 @@
+1: T2 Title has trailing whitespace: "WIP: tïtle "
+1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: tïtle "
+3: B6 Body message is missing
diff --git a/gitlint/tests/expected/test_cli/test_input_stream_debug_1 b/gitlint/tests/expected/test_cli/test_input_stream_debug_1
new file mode 100644
index 0000000..4326729
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_input_stream_debug_1
@@ -0,0 +1,3 @@
+1: T2 Title has trailing whitespace: "WIP: tïtle "
+1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: tïtle "
+3: B6 Body message is missing
diff --git a/gitlint/tests/expected/test_cli/test_input_stream_debug_2 b/gitlint/tests/expected/test_cli/test_input_stream_debug_2
new file mode 100644
index 0000000..a9028e1
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_input_stream_debug_2
@@ -0,0 +1,71 @@
+DEBUG: gitlint.cli To report issues, please visit https://github.com/jorisroovers/gitlint/issues
+DEBUG: gitlint.cli Platform: {platform}
+DEBUG: gitlint.cli Python version: {python_version}
+DEBUG: gitlint.cli Git version: git version 1.2.3
+DEBUG: gitlint.cli Gitlint version: {gitlint_version}
+DEBUG: gitlint.cli GITLINT_USE_SH_LIB: {GITLINT_USE_SH_LIB}
+DEBUG: gitlint.cli Configuration
+config-path: None
+[GENERAL]
+extra-path: None
+contrib: []
+ignore:
+ignore-merge-commits: True
+ignore-fixup-commits: True
+ignore-squash-commits: True
+ignore-revert-commits: True
+ignore-stdin: False
+staged: False
+verbosity: 3
+debug: True
+target: {target}
+[RULES]
+ I1: ignore-by-title
+ ignore=all
+ regex=None
+ I2: ignore-by-body
+ ignore=all
+ regex=None
+ T1: title-max-length
+ line-length=72
+ T2: title-trailing-whitespace
+ T6: title-leading-whitespace
+ T3: title-trailing-punctuation
+ T4: title-hard-tab
+ T5: title-must-not-contain-word
+ words=WIP
+ T7: title-match-regex
+ regex=.*
+ B1: body-max-line-length
+ line-length=80
+ B5: body-min-length
+ min-length=20
+ B6: body-is-missing
+ ignore-merge-commits=True
+ B2: body-trailing-whitespace
+ B3: body-hard-tab
+ B4: body-first-line-empty
+ B7: body-changed-file-mention
+ files=
+ M1: author-valid-email
+ regex=[^@ ]+@[^@ ]+\.[^@ ]+
+
+DEBUG: gitlint.cli Stdin data: 'WIP: tïtle
+'
+DEBUG: gitlint.cli Stdin detected and not ignored. Using as input.
+DEBUG: gitlint.cli Linting 1 commit(s)
+DEBUG: gitlint.lint Linting commit [SHA UNKNOWN]
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+WIP: tïtle
+--- Meta info ---------
+Author: None <None>
+Date: None
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: []
+Changed Files: []
+-----------------------
+DEBUG: gitlint.cli Exit Code = 3 \ No newline at end of file
diff --git a/gitlint/tests/expected/test_cli/test_lint_multiple_commits_1 b/gitlint/tests/expected/test_cli/test_lint_multiple_commits_1
new file mode 100644
index 0000000..be3288b
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_multiple_commits_1
@@ -0,0 +1,8 @@
+Commit 6f29bf81a8:
+3: B5 Body message is too short (12<20): "commït-body1"
+
+Commit 25053ccec5:
+3: B5 Body message is too short (12<20): "commït-body2"
+
+Commit 4da2656b0d:
+3: B5 Body message is too short (12<20): "commït-body3"
diff --git a/gitlint/tests/expected/test_cli/test_lint_multiple_commits_config_1 b/gitlint/tests/expected/test_cli/test_lint_multiple_commits_config_1
new file mode 100644
index 0000000..1bf0503
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_multiple_commits_config_1
@@ -0,0 +1,6 @@
+Commit 6f29bf81a8:
+3: B5 Body message is too short (12<20): "commït-body1"
+
+Commit 4da2656b0d:
+1: T3 Title has trailing punctuation (.): "commït-title3."
+3: B5 Body message is too short (12<20): "commït-body3"
diff --git a/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_1 b/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_1
new file mode 100644
index 0000000..9a9091b
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_1
@@ -0,0 +1,2 @@
+1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: msg-filename tïtle"
+3: B6 Body message is missing
diff --git a/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_2 b/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_2
new file mode 100644
index 0000000..3e5dcb6
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_2
@@ -0,0 +1,70 @@
+DEBUG: gitlint.cli To report issues, please visit https://github.com/jorisroovers/gitlint/issues
+DEBUG: gitlint.cli Platform: {platform}
+DEBUG: gitlint.cli Python version: {python_version}
+DEBUG: gitlint.cli Git version: git version 1.2.3
+DEBUG: gitlint.cli Gitlint version: {gitlint_version}
+DEBUG: gitlint.cli GITLINT_USE_SH_LIB: {GITLINT_USE_SH_LIB}
+DEBUG: gitlint.cli Configuration
+config-path: None
+[GENERAL]
+extra-path: None
+contrib: []
+ignore:
+ignore-merge-commits: True
+ignore-fixup-commits: True
+ignore-squash-commits: True
+ignore-revert-commits: True
+ignore-stdin: False
+staged: True
+verbosity: 3
+debug: True
+target: {target}
+[RULES]
+ I1: ignore-by-title
+ ignore=all
+ regex=None
+ I2: ignore-by-body
+ ignore=all
+ regex=None
+ T1: title-max-length
+ line-length=72
+ T2: title-trailing-whitespace
+ T6: title-leading-whitespace
+ T3: title-trailing-punctuation
+ T4: title-hard-tab
+ T5: title-must-not-contain-word
+ words=WIP
+ T7: title-match-regex
+ regex=.*
+ B1: body-max-line-length
+ line-length=80
+ B5: body-min-length
+ min-length=20
+ B6: body-is-missing
+ ignore-merge-commits=True
+ B2: body-trailing-whitespace
+ B3: body-hard-tab
+ B4: body-first-line-empty
+ B7: body-changed-file-mention
+ files=
+ M1: author-valid-email
+ regex=[^@ ]+@[^@ ]+\.[^@ ]+
+
+DEBUG: gitlint.cli Fetching additional meta-data from staged commit
+DEBUG: gitlint.cli Using --msg-filename.
+DEBUG: gitlint.cli Linting 1 commit(s)
+DEBUG: gitlint.lint Linting commit [SHA UNKNOWN]
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+WIP: msg-filename tïtle
+--- Meta info ---------
+Author: föo user <föo@bar.com>
+Date: 2020-02-19 12:18:46 +0100
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: ['my-branch']
+Changed Files: ['commit-1/file-1', 'commit-1/file-2']
+-----------------------
+DEBUG: gitlint.cli Exit Code = 2 \ No newline at end of file
diff --git a/gitlint/tests/expected/test_cli/test_lint_staged_stdin_1 b/gitlint/tests/expected/test_cli/test_lint_staged_stdin_1
new file mode 100644
index 0000000..4326729
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_staged_stdin_1
@@ -0,0 +1,3 @@
+1: T2 Title has trailing whitespace: "WIP: tïtle "
+1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: tïtle "
+3: B6 Body message is missing
diff --git a/gitlint/tests/expected/test_cli/test_lint_staged_stdin_2 b/gitlint/tests/expected/test_cli/test_lint_staged_stdin_2
new file mode 100644
index 0000000..03fd8c3
--- /dev/null
+++ b/gitlint/tests/expected/test_cli/test_lint_staged_stdin_2
@@ -0,0 +1,72 @@
+DEBUG: gitlint.cli To report issues, please visit https://github.com/jorisroovers/gitlint/issues
+DEBUG: gitlint.cli Platform: {platform}
+DEBUG: gitlint.cli Python version: {python_version}
+DEBUG: gitlint.cli Git version: git version 1.2.3
+DEBUG: gitlint.cli Gitlint version: {gitlint_version}
+DEBUG: gitlint.cli GITLINT_USE_SH_LIB: {GITLINT_USE_SH_LIB}
+DEBUG: gitlint.cli Configuration
+config-path: None
+[GENERAL]
+extra-path: None
+contrib: []
+ignore:
+ignore-merge-commits: True
+ignore-fixup-commits: True
+ignore-squash-commits: True
+ignore-revert-commits: True
+ignore-stdin: False
+staged: True
+verbosity: 3
+debug: True
+target: {target}
+[RULES]
+ I1: ignore-by-title
+ ignore=all
+ regex=None
+ I2: ignore-by-body
+ ignore=all
+ regex=None
+ T1: title-max-length
+ line-length=72
+ T2: title-trailing-whitespace
+ T6: title-leading-whitespace
+ T3: title-trailing-punctuation
+ T4: title-hard-tab
+ T5: title-must-not-contain-word
+ words=WIP
+ T7: title-match-regex
+ regex=.*
+ B1: body-max-line-length
+ line-length=80
+ B5: body-min-length
+ min-length=20
+ B6: body-is-missing
+ ignore-merge-commits=True
+ B2: body-trailing-whitespace
+ B3: body-hard-tab
+ B4: body-first-line-empty
+ B7: body-changed-file-mention
+ files=
+ M1: author-valid-email
+ regex=[^@ ]+@[^@ ]+\.[^@ ]+
+
+DEBUG: gitlint.cli Fetching additional meta-data from staged commit
+DEBUG: gitlint.cli Stdin data: 'WIP: tïtle
+'
+DEBUG: gitlint.cli Stdin detected and not ignored. Using as input.
+DEBUG: gitlint.cli Linting 1 commit(s)
+DEBUG: gitlint.lint Linting commit [SHA UNKNOWN]
+DEBUG: gitlint.lint Commit Object
+--- Commit Message ----
+WIP: tïtle
+--- Meta info ---------
+Author: föo user <föo@bar.com>
+Date: 2020-02-19 12:18:46 +0100
+is-merge-commit: False
+is-fixup-commit: False
+is-squash-commit: False
+is-revert-commit: False
+Branches: ['my-branch']
+Changed Files: ['commit-1/file-1', 'commit-1/file-2']
+-----------------------
+DEBUG: gitlint.cli Exit Code = 3 \ No newline at end of file
diff --git a/gitlint/tests/git/test_git.py b/gitlint/tests/git/test_git.py
new file mode 100644
index 0000000..297b10c
--- /dev/null
+++ b/gitlint/tests/git/test_git.py
@@ -0,0 +1,115 @@
+# -*- coding: utf-8 -*-
+import os
+
+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.shell import ErrorReturnCode, CommandNotFound
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.git import GitContext, GitContextError, GitNotInstalledError, git_commentchar, git_hooks_dir
+
+
+class GitTests(BaseTestCase):
+
+ # Expected special_args passed to 'sh'
+ expected_sh_special_args = {
+ '_tty_out': False,
+ '_cwd': u"fåke/path"
+ }
+
+ @patch('gitlint.git.sh')
+ def test_get_latest_commit_command_not_found(self, sh):
+ sh.git.side_effect = CommandNotFound("git")
+ expected_msg = "'git' command not found. You need to install git to use gitlint on a local repository. " + \
+ "See https://git-scm.com/book/en/v2/Getting-Started-Installing-Git on how to install git."
+ with self.assertRaisesRegex(GitNotInstalledError, expected_msg):
+ GitContext.from_local_repository(u"fåke/path")
+
+ # assert that commit message was read using git command
+ sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args)
+
+ @patch('gitlint.git.sh')
+ def test_get_latest_commit_git_error(self, sh):
+ # Current directory not a git repo
+ err = b"fatal: Not a git repository (or any of the parent directories): .git"
+ sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err)
+
+ with self.assertRaisesRegex(GitContextError, u"fåke/path is not a git repository."):
+ GitContext.from_local_repository(u"fåke/path")
+
+ # assert that commit message was read using git command
+ sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args)
+ sh.git.reset_mock()
+
+ err = b"fatal: Random git error"
+ sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err)
+
+ expected_msg = u"An error occurred while executing 'git log -1 --pretty=%H': {0}".format(err)
+ with self.assertRaisesRegex(GitContextError, expected_msg):
+ GitContext.from_local_repository(u"fåke/path")
+
+ # assert that commit message was read using git command
+ sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args)
+
+ @patch('gitlint.git.sh')
+ def test_git_no_commits_error(self, sh):
+ # No commits: returned by 'git log'
+ err = b"fatal: your current branch 'master' does not have any commits yet"
+
+ sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"", err)
+
+ expected_msg = u"Current branch has no commits. Gitlint requires at least one commit to function."
+ with self.assertRaisesRegex(GitContextError, expected_msg):
+ GitContext.from_local_repository(u"fåke/path")
+
+ # assert that commit message was read using git command
+ sh.git.assert_called_once_with("log", "-1", "--pretty=%H", **self.expected_sh_special_args)
+ sh.git.reset_mock()
+
+ # Unknown reference 'HEAD' commits: returned by 'git rev-parse'
+ err = (b"HEAD"
+ b"fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree."
+ b"Use '--' to separate paths from revisions, like this:"
+ b"'git <command> [<revision>...] -- [<file>...]'")
+
+ sh.git.side_effect = [
+ u"#\n", # git config --get core.commentchar
+ ErrorReturnCode("rev-parse --abbrev-ref HEAD", b"", err)
+ ]
+
+ with self.assertRaisesRegex(GitContextError, expected_msg):
+ context = GitContext.from_commit_msg(u"test")
+ context.current_branch
+
+ # assert that commit message was read using git command
+ sh.git.assert_called_with("rev-parse", "--abbrev-ref", "HEAD", _tty_out=False, _cwd=None)
+
+ @patch("gitlint.git._git")
+ def test_git_commentchar(self, git):
+ git.return_value.exit_code = 1
+ self.assertEqual(git_commentchar(), "#")
+
+ git.return_value.exit_code = 0
+ git.return_value.__str__ = lambda _: u"ä"
+ git.return_value.__unicode__ = lambda _: u"ä"
+ self.assertEqual(git_commentchar(), u"ä")
+
+ git.return_value = ';\n'
+ self.assertEqual(git_commentchar(os.path.join(u"/föo", u"bar")), ';')
+
+ git.assert_called_with("config", "--get", "core.commentchar", _ok_code=[0, 1],
+ _cwd=os.path.join(u"/föo", u"bar"))
+
+ @patch("gitlint.git._git")
+ def test_git_hooks_dir(self, git):
+ hooks_dir = os.path.join(u"föo", ".git", "hooks")
+ git.return_value.__str__ = lambda _: hooks_dir + "\n"
+ git.return_value.__unicode__ = lambda _: hooks_dir + "\n"
+ self.assertEqual(git_hooks_dir(u"/blä"), os.path.abspath(os.path.join(u"/blä", hooks_dir)))
+
+ git.assert_called_once_with("rev-parse", "--git-path", "hooks", _cwd=u"/blä")
diff --git a/gitlint/tests/git/test_git_commit.py b/gitlint/tests/git/test_git_commit.py
new file mode 100644
index 0000000..dc83ccb
--- /dev/null
+++ b/gitlint/tests/git/test_git_commit.py
@@ -0,0 +1,535 @@
+# -*- coding: utf-8 -*-
+import copy
+import datetime
+
+import dateutil
+
+import arrow
+
+try:
+ # python 2.x
+ from mock import patch, call
+except ImportError:
+ # python 3.x
+ from unittest.mock import patch, call # pylint: disable=no-name-in-module, import-error
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.git import GitContext, GitCommit, LocalGitCommit, StagedLocalGitCommit, GitCommitMessage
+
+
+class GitCommitTests(BaseTestCase):
+
+ # Expected special_args passed to 'sh'
+ expected_sh_special_args = {
+ '_tty_out': False,
+ '_cwd': u"fåke/path"
+ }
+
+ @patch('gitlint.git.sh')
+ def test_get_latest_commit(self, sh):
+ sample_sha = "d8ac47e9f2923c7f22d8668e3a1ed04eb4cdbca9"
+
+ sh.git.side_effect = [
+ sample_sha,
+ u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"cömmit-title\n\ncömmit-body",
+ u"#", # git config --get core.commentchar
+ u"file1.txt\npåth/to/file2.txt\n",
+ u"foöbar\n* hürdur\n"
+ ]
+
+ context = GitContext.from_local_repository(u"fåke/path")
+ # assert that commit info was read using git command
+ expected_calls = [
+ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args),
+ call("log", sample_sha, "-1", "--pretty=%aN%x00%aE%x00%ai%x00%P%n%B", **self.expected_sh_special_args),
+ call('config', '--get', 'core.commentchar', _ok_code=[0, 1], **self.expected_sh_special_args),
+ call('diff-tree', '--no-commit-id', '--name-only', '-r', '--root', sample_sha,
+ **self.expected_sh_special_args),
+ call('branch', '--contains', sample_sha, **self.expected_sh_special_args)
+ ]
+
+ # Only first 'git log' call should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:1])
+
+ last_commit = context.commits[-1]
+ self.assertIsInstance(last_commit, LocalGitCommit)
+ self.assertEqual(last_commit.sha, sample_sha)
+ self.assertEqual(last_commit.message.title, u"cömmit-title")
+ self.assertEqual(last_commit.message.body, ["", u"cömmit-body"])
+ self.assertEqual(last_commit.author_name, u"test åuthor")
+ self.assertEqual(last_commit.author_email, u"test-emåil@foo.com")
+ self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15,
+ tzinfo=dateutil.tz.tzoffset("+0100", 3600)))
+ self.assertListEqual(last_commit.parents, [u"åbc"])
+ self.assertFalse(last_commit.is_merge_commit)
+ self.assertFalse(last_commit.is_fixup_commit)
+ self.assertFalse(last_commit.is_squash_commit)
+ self.assertFalse(last_commit.is_revert_commit)
+
+ # First 2 'git log' calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:3])
+
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+ # 'git diff-tree' should have happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:4])
+
+ self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"])
+ # All expected calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls)
+
+ @patch('gitlint.git.sh')
+ def test_from_local_repository_specific_ref(self, sh):
+ sample_sha = "myspecialref"
+
+ sh.git.side_effect = [
+ sample_sha,
+ u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"cömmit-title\n\ncömmit-body",
+ u"#", # git config --get core.commentchar
+ u"file1.txt\npåth/to/file2.txt\n",
+ u"foöbar\n* hürdur\n"
+ ]
+
+ context = GitContext.from_local_repository(u"fåke/path", sample_sha)
+ # assert that commit info was read using git command
+ expected_calls = [
+ call("rev-list", sample_sha, **self.expected_sh_special_args),
+ call("log", sample_sha, "-1", "--pretty=%aN%x00%aE%x00%ai%x00%P%n%B", **self.expected_sh_special_args),
+ call('config', '--get', 'core.commentchar', _ok_code=[0, 1], **self.expected_sh_special_args),
+ call('diff-tree', '--no-commit-id', '--name-only', '-r', '--root', sample_sha,
+ **self.expected_sh_special_args),
+ call('branch', '--contains', sample_sha, **self.expected_sh_special_args)
+ ]
+
+ # Only first 'git log' call should've happened at this point
+ self.assertEqual(sh.git.mock_calls, expected_calls[:1])
+
+ last_commit = context.commits[-1]
+ self.assertIsInstance(last_commit, LocalGitCommit)
+ self.assertEqual(last_commit.sha, sample_sha)
+ self.assertEqual(last_commit.message.title, u"cömmit-title")
+ self.assertEqual(last_commit.message.body, ["", u"cömmit-body"])
+ self.assertEqual(last_commit.author_name, u"test åuthor")
+ self.assertEqual(last_commit.author_email, u"test-emåil@foo.com")
+ self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15,
+ tzinfo=dateutil.tz.tzoffset("+0100", 3600)))
+ self.assertListEqual(last_commit.parents, [u"åbc"])
+ self.assertFalse(last_commit.is_merge_commit)
+ self.assertFalse(last_commit.is_fixup_commit)
+ self.assertFalse(last_commit.is_squash_commit)
+ self.assertFalse(last_commit.is_revert_commit)
+
+ # First 2 'git log' calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:3])
+
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+ # 'git diff-tree' should have happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:4])
+
+ self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"])
+ # All expected calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls)
+
+ @patch('gitlint.git.sh')
+ def test_get_latest_commit_merge_commit(self, sh):
+ sample_sha = "d8ac47e9f2923c7f22d8668e3a1ed04eb4cdbca9"
+
+ sh.git.side_effect = [
+ sample_sha,
+ u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc def\n"
+ u"Merge \"foo bår commit\"",
+ u"#", # git config --get core.commentchar
+ u"file1.txt\npåth/to/file2.txt\n",
+ u"foöbar\n* hürdur\n"
+ ]
+
+ context = GitContext.from_local_repository(u"fåke/path")
+ # assert that commit info was read using git command
+ expected_calls = [
+ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args),
+ call("log", sample_sha, "-1", "--pretty=%aN%x00%aE%x00%ai%x00%P%n%B", **self.expected_sh_special_args),
+ call('config', '--get', 'core.commentchar', _ok_code=[0, 1], **self.expected_sh_special_args),
+ call('diff-tree', '--no-commit-id', '--name-only', '-r', '--root', sample_sha,
+ **self.expected_sh_special_args),
+ call('branch', '--contains', sample_sha, **self.expected_sh_special_args)
+ ]
+
+ # Only first 'git log' call should've happened at this point
+ self.assertEqual(sh.git.mock_calls, expected_calls[:1])
+
+ last_commit = context.commits[-1]
+ self.assertIsInstance(last_commit, LocalGitCommit)
+ self.assertEqual(last_commit.sha, sample_sha)
+ self.assertEqual(last_commit.message.title, u"Merge \"foo bår commit\"")
+ self.assertEqual(last_commit.message.body, [])
+ self.assertEqual(last_commit.author_name, u"test åuthor")
+ self.assertEqual(last_commit.author_email, u"test-emåil@foo.com")
+ self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15,
+ tzinfo=dateutil.tz.tzoffset("+0100", 3600)))
+ self.assertListEqual(last_commit.parents, [u"åbc", "def"])
+ self.assertTrue(last_commit.is_merge_commit)
+ self.assertFalse(last_commit.is_fixup_commit)
+ self.assertFalse(last_commit.is_squash_commit)
+ self.assertFalse(last_commit.is_revert_commit)
+
+ # First 2 'git log' calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:3])
+
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+ # 'git diff-tree' should have happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:4])
+
+ self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"])
+ # All expected calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls)
+
+ @patch('gitlint.git.sh')
+ def test_get_latest_commit_fixup_squash_commit(self, sh):
+ commit_types = ["fixup", "squash"]
+ for commit_type in commit_types:
+ sample_sha = "d8ac47e9f2923c7f22d8668e3a1ed04eb4cdbca9"
+
+ sh.git.side_effect = [
+ sample_sha,
+ u"test åuthor\x00test-emåil@foo.com\x002016-12-03 15:28:15 +0100\x00åbc\n"
+ u"{0}! \"foo bår commit\"".format(commit_type),
+ u"#", # git config --get core.commentchar
+ u"file1.txt\npåth/to/file2.txt\n",
+ u"foöbar\n* hürdur\n"
+ ]
+
+ context = GitContext.from_local_repository(u"fåke/path")
+ # assert that commit info was read using git command
+ expected_calls = [
+ call("log", "-1", "--pretty=%H", **self.expected_sh_special_args),
+ call("log", sample_sha, "-1", "--pretty=%aN%x00%aE%x00%ai%x00%P%n%B", **self.expected_sh_special_args),
+ call('config', '--get', 'core.commentchar', _ok_code=[0, 1], **self.expected_sh_special_args),
+ call('diff-tree', '--no-commit-id', '--name-only', '-r', '--root', sample_sha,
+ **self.expected_sh_special_args),
+ call('branch', '--contains', sample_sha, **self.expected_sh_special_args)
+ ]
+
+ # Only first 'git log' call should've happened at this point
+ self.assertEqual(sh.git.mock_calls, expected_calls[:-4])
+
+ last_commit = context.commits[-1]
+ self.assertIsInstance(last_commit, LocalGitCommit)
+ self.assertEqual(last_commit.sha, sample_sha)
+ self.assertEqual(last_commit.message.title, u"{0}! \"foo bår commit\"".format(commit_type))
+ self.assertEqual(last_commit.message.body, [])
+ self.assertEqual(last_commit.author_name, u"test åuthor")
+ self.assertEqual(last_commit.author_email, u"test-emåil@foo.com")
+ self.assertEqual(last_commit.date, datetime.datetime(2016, 12, 3, 15, 28, 15,
+ tzinfo=dateutil.tz.tzoffset("+0100", 3600)))
+ self.assertListEqual(last_commit.parents, [u"åbc"])
+
+ # First 2 'git log' calls should've happened at this point
+ self.assertEqual(sh.git.mock_calls, expected_calls[:3])
+
+ # Asserting that squash and fixup are correct
+ for type in commit_types:
+ attr = "is_" + type + "_commit"
+ self.assertEqual(getattr(last_commit, attr), commit_type == type)
+
+ self.assertFalse(last_commit.is_merge_commit)
+ self.assertFalse(last_commit.is_revert_commit)
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+ # 'git diff-tree' should have happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[:4])
+
+ self.assertListEqual(last_commit.branches, [u"foöbar", u"hürdur"])
+ # All expected calls should've happened at this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls)
+
+ sh.git.reset_mock()
+
+ @patch("gitlint.git.git_commentchar")
+ def test_from_commit_msg_full(self, commentchar):
+ commentchar.return_value = u"#"
+ gitcontext = GitContext.from_commit_msg(self.get_sample("commit_message/sample1"))
+
+ expected_title = u"Commit title contåining 'WIP', as well as trailing punctuation."
+ expected_body = ["This line should be empty",
+ "This is the first line of the commit message body and it is meant to test a " +
+ "line that exceeds the maximum line length of 80 characters.",
+ u"This line has a tråiling space. ",
+ "This line has a trailing tab.\t"]
+ expected_full = expected_title + "\n" + "\n".join(expected_body)
+ expected_original = expected_full + (
+ u"\n# This is a cömmented line\n"
+ u"# ------------------------ >8 ------------------------\n"
+ u"# Anything after this line should be cleaned up\n"
+ u"# this line appears on `git commit -v` command\n"
+ u"diff --git a/gitlint/tests/samples/commit_message/sample1 "
+ u"b/gitlint/tests/samples/commit_message/sample1\n"
+ u"index 82dbe7f..ae71a14 100644\n"
+ u"--- a/gitlint/tests/samples/commit_message/sample1\n"
+ u"+++ b/gitlint/tests/samples/commit_message/sample1\n"
+ u"@@ -1 +1 @@\n"
+ )
+
+ commit = gitcontext.commits[-1]
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, expected_title)
+ self.assertEqual(commit.message.body, expected_body)
+ self.assertEqual(commit.message.full, expected_full)
+ self.assertEqual(commit.message.original, expected_original)
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertFalse(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ def test_from_commit_msg_just_title(self):
+ gitcontext = GitContext.from_commit_msg(self.get_sample("commit_message/sample2"))
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, u"Just a title contåining WIP")
+ self.assertEqual(commit.message.body, [])
+ self.assertEqual(commit.message.full, u"Just a title contåining WIP")
+ self.assertEqual(commit.message.original, u"Just a title contåining WIP")
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertFalse(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ def test_from_commit_msg_empty(self):
+ gitcontext = GitContext.from_commit_msg("")
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, "")
+ self.assertEqual(commit.message.body, [])
+ self.assertEqual(commit.message.full, "")
+ self.assertEqual(commit.message.original, "")
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertFalse(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ @patch("gitlint.git.git_commentchar")
+ def test_from_commit_msg_comment(self, commentchar):
+ commentchar.return_value = u"#"
+ gitcontext = GitContext.from_commit_msg(u"Tïtle\n\nBödy 1\n#Cömment\nBody 2")
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, u"Tïtle")
+ self.assertEqual(commit.message.body, ["", u"Bödy 1", "Body 2"])
+ self.assertEqual(commit.message.full, u"Tïtle\n\nBödy 1\nBody 2")
+ self.assertEqual(commit.message.original, u"Tïtle\n\nBödy 1\n#Cömment\nBody 2")
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertFalse(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ def test_from_commit_msg_merge_commit(self):
+ commit_msg = "Merge f919b8f34898d9b48048bcd703bc47139f4ff621 into 8b0409a26da6ba8a47c1fd2e746872a8dab15401"
+ gitcontext = GitContext.from_commit_msg(commit_msg)
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, commit_msg)
+ self.assertEqual(commit.message.body, [])
+ self.assertEqual(commit.message.full, commit_msg)
+ self.assertEqual(commit.message.original, commit_msg)
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertTrue(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertFalse(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ def test_from_commit_msg_revert_commit(self):
+ commit_msg = "Revert \"Prev commit message\"\n\nThis reverts commit a8ad67e04164a537198dea94a4fde81c5592ae9c."
+ gitcontext = GitContext.from_commit_msg(commit_msg)
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, "Revert \"Prev commit message\"")
+ self.assertEqual(commit.message.body, ["", "This reverts commit a8ad67e04164a537198dea94a4fde81c5592ae9c."])
+ self.assertEqual(commit.message.full, commit_msg)
+ self.assertEqual(commit.message.original, commit_msg)
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_fixup_commit)
+ self.assertFalse(commit.is_squash_commit)
+ self.assertTrue(commit.is_revert_commit)
+ self.assertEqual(len(gitcontext.commits), 1)
+
+ def test_from_commit_msg_fixup_squash_commit(self):
+ commit_types = ["fixup", "squash"]
+ for commit_type in commit_types:
+ commit_msg = "{0}! Test message".format(commit_type)
+ gitcontext = GitContext.from_commit_msg(commit_msg)
+ commit = gitcontext.commits[-1]
+
+ self.assertIsInstance(commit, GitCommit)
+ self.assertFalse(isinstance(commit, LocalGitCommit))
+ self.assertEqual(commit.message.title, commit_msg)
+ self.assertEqual(commit.message.body, [])
+ self.assertEqual(commit.message.full, commit_msg)
+ self.assertEqual(commit.message.original, commit_msg)
+ self.assertEqual(commit.author_name, None)
+ self.assertEqual(commit.author_email, None)
+ self.assertEqual(commit.date, None)
+ self.assertListEqual(commit.parents, [])
+ self.assertListEqual(commit.branches, [])
+ self.assertEqual(len(gitcontext.commits), 1)
+ self.assertFalse(commit.is_merge_commit)
+ self.assertFalse(commit.is_revert_commit)
+ # Asserting that squash and fixup are correct
+ for type in commit_types:
+ attr = "is_" + type + "_commit"
+ self.assertEqual(getattr(commit, attr), commit_type == type)
+
+ @patch('gitlint.git.sh')
+ @patch('arrow.now')
+ def test_staged_commit(self, now, sh):
+ # StagedLocalGitCommit()
+
+ sh.git.side_effect = [
+ u"#", # git config --get core.commentchar
+ u"test åuthor\n", # git config --get user.name
+ u"test-emåil@foo.com\n", # git config --get user.email
+ u"my-brånch\n", # git rev-parse --abbrev-ref HEAD
+ u"file1.txt\npåth/to/file2.txt\n",
+ ]
+ now.side_effect = [arrow.get("2020-02-19T12:18:46.675182+01:00")]
+
+ # We use a fixup commit, just to test a non-default path
+ context = GitContext.from_staged_commit(u"fixup! Foōbar 123\n\ncömmit-body\n", u"fåke/path")
+
+ # git calls we're expexting
+ expected_calls = [
+ call('config', '--get', 'core.commentchar', _ok_code=[0, 1], **self.expected_sh_special_args),
+ call('config', '--get', 'user.name', **self.expected_sh_special_args),
+ call('config', '--get', 'user.email', **self.expected_sh_special_args),
+ call("rev-parse", "--abbrev-ref", "HEAD", **self.expected_sh_special_args),
+ call("diff", "--staged", "--name-only", "-r", **self.expected_sh_special_args)
+ ]
+
+ last_commit = context.commits[-1]
+ self.assertIsInstance(last_commit, StagedLocalGitCommit)
+ self.assertIsNone(last_commit.sha, None)
+ self.assertEqual(last_commit.message.title, u"fixup! Foōbar 123")
+ self.assertEqual(last_commit.message.body, ["", u"cömmit-body"])
+ # Only `git config --get core.commentchar` should've happened up until this point
+ self.assertListEqual(sh.git.mock_calls, expected_calls[0:1])
+
+ self.assertEqual(last_commit.author_name, u"test åuthor")
+ self.assertListEqual(sh.git.mock_calls, expected_calls[0:2])
+
+ self.assertEqual(last_commit.author_email, u"test-emåil@foo.com")
+ self.assertListEqual(sh.git.mock_calls, expected_calls[0:3])
+
+ self.assertEqual(last_commit.date, datetime.datetime(2020, 2, 19, 12, 18, 46,
+ tzinfo=dateutil.tz.tzoffset("+0100", 3600)))
+ now.assert_called_once()
+
+ self.assertListEqual(last_commit.parents, [])
+ self.assertFalse(last_commit.is_merge_commit)
+ self.assertTrue(last_commit.is_fixup_commit)
+ self.assertFalse(last_commit.is_squash_commit)
+ self.assertFalse(last_commit.is_revert_commit)
+
+ self.assertListEqual(last_commit.branches, [u"my-brånch"])
+ self.assertListEqual(sh.git.mock_calls, expected_calls[0:4])
+
+ self.assertListEqual(last_commit.changed_files, ["file1.txt", u"påth/to/file2.txt"])
+ self.assertListEqual(sh.git.mock_calls, expected_calls[0:5])
+
+ def test_gitcommitmessage_equality(self):
+ commit_message1 = GitCommitMessage(GitContext(), u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"])
+ attrs = ['original', 'full', 'title', 'body']
+ self.object_equality_test(commit_message1, attrs, {"context": commit_message1.context})
+
+ def test_gitcommit_equality(self):
+ # Test simple equality case
+ now = datetime.datetime.utcnow()
+ context1 = GitContext()
+ commit_message1 = GitCommitMessage(context1, u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"])
+ commit1 = GitCommit(context1, commit_message1, u"shä", now, u"Jöhn Smith", u"jöhn.smith@test.com", None,
+ [u"föo/bar"], [u"brånch1", u"brånch2"])
+ context1.commits = [commit1]
+
+ context2 = GitContext()
+ commit_message2 = GitCommitMessage(context2, u"tëst\n\nfoo", u"tëst\n\nfoo", u"tēst", ["", u"föo"])
+ commit2 = GitCommit(context2, commit_message1, u"shä", now, u"Jöhn Smith", u"jöhn.smith@test.com", None,
+ [u"föo/bar"], [u"brånch1", u"brånch2"])
+ context2.commits = [commit2]
+
+ self.assertEqual(context1, context2)
+ self.assertEqual(commit_message1, commit_message2)
+ self.assertEqual(commit1, commit2)
+
+ # Check that objects are unequal when changing a single attribute
+ kwargs = {'message': commit1.message, 'sha': commit1.sha, 'date': commit1.date,
+ 'author_name': commit1.author_name, 'author_email': commit1.author_email, 'parents': commit1.parents,
+ 'changed_files': commit1.changed_files, 'branches': commit1.branches}
+
+ self.object_equality_test(commit1, kwargs.keys(), {"context": commit1.context})
+
+ # Check that the is_* attributes that are affected by the commit message affect equality
+ special_messages = {'is_merge_commit': u"Merge: foöbar", 'is_fixup_commit': u"fixup! foöbar",
+ 'is_squash_commit': u"squash! foöbar", 'is_revert_commit': u"Revert: foöbar"}
+ for key in special_messages:
+ kwargs_copy = copy.deepcopy(kwargs)
+ clone1 = GitCommit(context=commit1.context, **kwargs_copy)
+ clone1.message = GitCommitMessage.from_full_message(context1, special_messages[key])
+ self.assertTrue(getattr(clone1, key))
+
+ clone2 = GitCommit(context=commit1.context, **kwargs_copy)
+ clone2.message = GitCommitMessage.from_full_message(context1, u"foöbar")
+ self.assertNotEqual(clone1, clone2)
+
+ @patch("gitlint.git.git_commentchar")
+ def test_commit_msg_custom_commentchar(self, patched):
+ patched.return_value = u"ä"
+ context = GitContext()
+ message = GitCommitMessage.from_full_message(context, u"Tïtle\n\nBödy 1\näCömment\nBody 2")
+
+ self.assertEqual(message.title, u"Tïtle")
+ self.assertEqual(message.body, ["", u"Bödy 1", "Body 2"])
+ self.assertEqual(message.full, u"Tïtle\n\nBödy 1\nBody 2")
+ self.assertEqual(message.original, u"Tïtle\n\nBödy 1\näCömment\nBody 2")
diff --git a/gitlint/tests/git/test_git_context.py b/gitlint/tests/git/test_git_context.py
new file mode 100644
index 0000000..b243d5e
--- /dev/null
+++ b/gitlint/tests/git/test_git_context.py
@@ -0,0 +1,89 @@
+# -*- coding: utf-8 -*-
+
+try:
+ # python 2.x
+ from mock import patch, call
+except ImportError:
+ # python 3.x
+ from unittest.mock import patch, call # pylint: disable=no-name-in-module, import-error
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.git import GitContext
+
+
+class GitContextTests(BaseTestCase):
+
+ # Expected special_args passed to 'sh'
+ expected_sh_special_args = {
+ '_tty_out': False,
+ '_cwd': u"fåke/path"
+ }
+
+ @patch('gitlint.git.sh')
+ def test_gitcontext(self, sh):
+
+ sh.git.side_effect = [
+ u"#", # git config --get core.commentchar
+ u"\nfoöbar\n"
+ ]
+
+ expected_calls = [
+ call("config", "--get", "core.commentchar", _ok_code=[0, 1], **self.expected_sh_special_args),
+ call("rev-parse", "--abbrev-ref", "HEAD", **self.expected_sh_special_args)
+ ]
+
+ context = GitContext(u"fåke/path")
+ self.assertEqual(sh.git.mock_calls, [])
+
+ # gitcontext.comment_branch
+ self.assertEqual(context.commentchar, u"#")
+ self.assertEqual(sh.git.mock_calls, expected_calls[0:1])
+
+ # gitcontext.current_branch
+ self.assertEqual(context.current_branch, u"foöbar")
+ self.assertEqual(sh.git.mock_calls, expected_calls)
+
+ @patch('gitlint.git.sh')
+ def test_gitcontext_equality(self, sh):
+
+ sh.git.side_effect = [
+ u"û\n", # context1: git config --get core.commentchar
+ u"û\n", # context2: git config --get core.commentchar
+ u"my-brånch\n", # context1: git rev-parse --abbrev-ref HEAD
+ u"my-brånch\n", # context2: git rev-parse --abbrev-ref HEAD
+ ]
+
+ context1 = GitContext(u"fåke/path")
+ context1.commits = [u"fōo", u"bår"] # we don't need real commits to check for equality
+
+ context2 = GitContext(u"fåke/path")
+ context2.commits = [u"fōo", u"bår"]
+ self.assertEqual(context1, context2)
+
+ # INEQUALITY
+ # Different commits
+ context2.commits = [u"hür", u"dür"]
+ self.assertNotEqual(context1, context2)
+
+ # Different repository_path
+ context2.commits = context1.commits
+ context2.repository_path = u"ōther/path"
+ self.assertNotEqual(context1, context2)
+
+ # Different comment_char
+ context3 = GitContext(u"fåke/path")
+ context3.commits = [u"fōo", u"bår"]
+ sh.git.side_effect = ([
+ u"ç\n", # context3: git config --get core.commentchar
+ u"my-brånch\n" # context3: git rev-parse --abbrev-ref HEAD
+ ])
+ self.assertNotEqual(context1, context3)
+
+ # Different current_branch
+ context4 = GitContext(u"fåke/path")
+ context4.commits = [u"fōo", u"bår"]
+ sh.git.side_effect = ([
+ u"û\n", # context4: git config --get core.commentchar
+ u"different-brånch\n" # context4: git rev-parse --abbrev-ref HEAD
+ ])
+ self.assertNotEqual(context1, context4)
diff --git a/gitlint/tests/rules/__init__.py b/gitlint/tests/rules/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gitlint/tests/rules/__init__.py
diff --git a/gitlint/tests/rules/test_body_rules.py b/gitlint/tests/rules/test_body_rules.py
new file mode 100644
index 0000000..fcb1b30
--- /dev/null
+++ b/gitlint/tests/rules/test_body_rules.py
@@ -0,0 +1,180 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint import rules
+
+
+class BodyRuleTests(BaseTestCase):
+ def test_max_line_length(self):
+ rule = rules.BodyMaxLineLength()
+
+ # assert no error
+ violation = rule.validate(u"å" * 80, None)
+ self.assertIsNone(violation)
+
+ # assert error on line length > 80
+ expected_violation = rules.RuleViolation("B1", "Line exceeds max length (81>80)", u"å" * 81)
+ violations = rule.validate(u"å" * 81, None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # set line length to 120, and check no violation on length 73
+ rule = rules.BodyMaxLineLength({'line-length': 120})
+ violations = rule.validate(u"å" * 73, None)
+ self.assertIsNone(violations)
+
+ # assert raise on 121
+ expected_violation = rules.RuleViolation("B1", "Line exceeds max length (121>120)", u"å" * 121)
+ violations = rule.validate(u"å" * 121, None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_trailing_whitespace(self):
+ rule = rules.BodyTrailingWhitespace()
+
+ # assert no error
+ violations = rule.validate(u"å", None)
+ self.assertIsNone(violations)
+
+ # trailing space
+ expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", u"å ")
+ violations = rule.validate(u"å ", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # trailing tab
+ expected_violation = rules.RuleViolation("B2", "Line has trailing whitespace", u"å\t")
+ violations = rule.validate(u"å\t", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_hard_tabs(self):
+ rule = rules.BodyHardTab()
+
+ # assert no error
+ violations = rule.validate(u"This is ã test", None)
+ self.assertIsNone(violations)
+
+ # contains hard tab
+ expected_violation = rules.RuleViolation("B3", "Line contains hard tab characters (\\t)", u"This is å\ttest")
+ violations = rule.validate(u"This is å\ttest", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_body_first_line_empty(self):
+ rule = rules.BodyFirstLineEmpty()
+
+ # assert no error
+ commit = self.gitcommit(u"Tïtle\n\nThis is the secōnd body line")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # second line not empty
+ expected_violation = rules.RuleViolation("B4", "Second line is not empty", u"nöt empty", 2)
+
+ commit = self.gitcommit(u"Tïtle\nnöt empty\nThis is the secönd body line")
+ violations = rule.validate(commit)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_body_min_length(self):
+ rule = rules.BodyMinLength()
+
+ # assert no error - body is long enough
+ commit = self.gitcommit("Title\n\nThis is the second body line\n")
+
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert no error - no body
+ commit = self.gitcommit(u"Tïtle\n")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # body is too short
+ expected_violation = rules.RuleViolation("B5", "Body message is too short (8<20)", u"töoshort", 3)
+
+ commit = self.gitcommit(u"Tïtle\n\ntöoshort\n")
+ violations = rule.validate(commit)
+ self.assertListEqual(violations, [expected_violation])
+
+ # assert error - short across multiple lines
+ expected_violation = rules.RuleViolation("B5", "Body message is too short (11<20)", u"secöndthïrd", 3)
+ commit = self.gitcommit(u"Tïtle\n\nsecönd\nthïrd\n")
+ violations = rule.validate(commit)
+ self.assertListEqual(violations, [expected_violation])
+
+ # set line length to 120, and check violation on length 21
+ expected_violation = rules.RuleViolation("B5", "Body message is too short (21<120)", u"å" * 21, 3)
+
+ rule = rules.BodyMinLength({'min-length': 120})
+ commit = self.gitcommit(u"Title\n\n%s\n" % (u"å" * 21))
+ violations = rule.validate(commit)
+ self.assertListEqual(violations, [expected_violation])
+
+ # Make sure we don't get the error if the body-length is exactly the min-length
+ rule = rules.BodyMinLength({'min-length': 8})
+ commit = self.gitcommit(u"Tïtle\n\n%s\n" % (u"å" * 8))
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ def test_body_missing(self):
+ rule = rules.BodyMissing()
+
+ # assert no error - body is present
+ commit = self.gitcommit(u"Tïtle\n\nThis ïs the first body line\n")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # body is too short
+ expected_violation = rules.RuleViolation("B6", "Body message is missing", None, 3)
+
+ commit = self.gitcommit(u"Tïtle\n")
+ violations = rule.validate(commit)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_body_missing_merge_commit(self):
+ rule = rules.BodyMissing()
+
+ # assert no error - merge commit
+ commit = self.gitcommit(u"Merge: Tïtle\n")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert error for merge commits if ignore-merge-commits is disabled
+ rule = rules.BodyMissing({'ignore-merge-commits': False})
+ violations = rule.validate(commit)
+ expected_violation = rules.RuleViolation("B6", "Body message is missing", None, 3)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_body_changed_file_mention(self):
+ rule = rules.BodyChangedFileMention()
+
+ # assert no error when no files have changed and no files need to be mentioned
+ commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert no error when no files have changed but certain files need to be mentioned on change
+ rule = rules.BodyChangedFileMention({'files': u"bar.txt,föo/test.py"})
+ commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert no error if a file has changed and is mentioned
+ commit = self.gitcommit(u"This is a test\n\nHere is a mention of föo/test.py", [u"föo/test.py"])
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert no error if multiple files have changed and are mentioned
+ commit_msg = u"This is a test\n\nHere is a mention of föo/test.py\nAnd here is a mention of bar.txt"
+ commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"])
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert error if file has changed and is not mentioned
+ commit_msg = u"This is a test\n\nHere is å mention of\nAnd here is a mention of bar.txt"
+ commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"])
+ violations = rule.validate(commit)
+ expected_violation = rules.RuleViolation("B7", u"Body does not mention changed file 'föo/test.py'", None, 4)
+ self.assertEqual([expected_violation], violations)
+
+ # assert multiple errors if multiple files habe changed and are not mentioned
+ commit_msg = u"This is å test\n\nHere is a mention of\nAnd here is a mention of"
+ commit = self.gitcommit(commit_msg, [u"föo/test.py", "bar.txt"])
+ violations = rule.validate(commit)
+ expected_violation_2 = rules.RuleViolation("B7", "Body does not mention changed file 'bar.txt'", None, 4)
+ self.assertEqual([expected_violation_2, expected_violation], violations)
diff --git a/gitlint/tests/rules/test_configuration_rules.py b/gitlint/tests/rules/test_configuration_rules.py
new file mode 100644
index 0000000..73d42f3
--- /dev/null
+++ b/gitlint/tests/rules/test_configuration_rules.py
@@ -0,0 +1,71 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint import rules
+from gitlint.config import LintConfig
+
+
+class ConfigurationRuleTests(BaseTestCase):
+ def test_ignore_by_title(self):
+ commit = self.gitcommit(u"Releäse\n\nThis is the secōnd body line")
+
+ # No regex specified -> Config shouldn't be changed
+ rule = rules.IgnoreByTitle()
+ config = LintConfig()
+ rule.apply(config, commit)
+ self.assertEqual(config, LintConfig())
+ self.assert_logged([]) # nothing logged -> nothing ignored
+
+ # Matching regex -> expect config to ignore all rules
+ rule = rules.IgnoreByTitle({"regex": u"^Releäse(.*)"})
+ expected_config = LintConfig()
+ expected_config.ignore = "all"
+ rule.apply(config, commit)
+ self.assertEqual(config, expected_config)
+
+ expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
+ u"Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: all"
+ self.assert_log_contains(expected_log_message)
+
+ # Matching regex with specific ignore
+ rule = rules.IgnoreByTitle({"regex": u"^Releäse(.*)",
+ "ignore": "T1,B2"})
+ expected_config = LintConfig()
+ expected_config.ignore = "T1,B2"
+ rule.apply(config, commit)
+ self.assertEqual(config, expected_config)
+
+ expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
+ u"Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: T1,B2"
+
+ def test_ignore_by_body(self):
+ commit = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line")
+
+ # No regex specified -> Config shouldn't be changed
+ rule = rules.IgnoreByBody()
+ config = LintConfig()
+ rule.apply(config, commit)
+ self.assertEqual(config, LintConfig())
+ self.assert_logged([]) # nothing logged -> nothing ignored
+
+ # Matching regex -> expect config to ignore all rules
+ rule = rules.IgnoreByBody({"regex": u"(.*)relëase(.*)"})
+ expected_config = LintConfig()
+ expected_config.ignore = "all"
+ rule.apply(config, commit)
+ self.assertEqual(config, expected_config)
+
+ expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \
+ u"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)'," + \
+ u" ignoring rules: all"
+ self.assert_log_contains(expected_log_message)
+
+ # Matching regex with specific ignore
+ rule = rules.IgnoreByBody({"regex": u"(.*)relëase(.*)",
+ "ignore": "T1,B2"})
+ expected_config = LintConfig()
+ expected_config.ignore = "T1,B2"
+ rule.apply(config, commit)
+ self.assertEqual(config, expected_config)
+
+ expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
+ u"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)', ignoring rules: T1,B2"
diff --git a/gitlint/tests/rules/test_meta_rules.py b/gitlint/tests/rules/test_meta_rules.py
new file mode 100644
index 0000000..c94b8b3
--- /dev/null
+++ b/gitlint/tests/rules/test_meta_rules.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import AuthorValidEmail, RuleViolation
+
+
+class MetaRuleTests(BaseTestCase):
+ def test_author_valid_email_rule(self):
+ rule = AuthorValidEmail()
+
+ # valid email addresses
+ valid_email_addresses = [u"föo@bar.com", u"Jöhn.Doe@bar.com", u"jöhn+doe@bar.com", u"jöhn/doe@bar.com",
+ u"jöhn.doe@subdomain.bar.com"]
+ for email in valid_email_addresses:
+ commit = self.gitcommit(u"", author_email=email)
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # No email address (=allowed for now, as gitlint also lints messages passed via stdin that don't have an
+ # email address)
+ commit = self.gitcommit(u"")
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # Invalid email addresses: no TLD, no domain, no @, space anywhere (=valid but not allowed by gitlint)
+ invalid_email_addresses = [u"föo@bar", u"JöhnDoe", u"Jöhn Doe", u"Jöhn Doe@foo.com", u" JöhnDoe@foo.com",
+ u"JöhnDoe@ foo.com", u"JöhnDoe@foo. com", u"JöhnDoe@foo. com", u"@bår.com",
+ u"föo@.com"]
+ for email in invalid_email_addresses:
+ commit = self.gitcommit(u"", author_email=email)
+ violations = rule.validate(commit)
+ self.assertListEqual(violations,
+ [RuleViolation("M1", "Author email for commit is invalid", email)])
+
+ def test_author_valid_email_rule_custom_regex(self):
+ # Custom domain
+ rule = AuthorValidEmail({'regex': u"[^@]+@bår.com"})
+ valid_email_addresses = [
+ u"föo@bår.com", u"Jöhn.Doe@bår.com", u"jöhn+doe@bår.com", u"jöhn/doe@bår.com"]
+ for email in valid_email_addresses:
+ commit = self.gitcommit(u"", author_email=email)
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # Invalid email addresses
+ invalid_email_addresses = [u"föo@hur.com"]
+ for email in invalid_email_addresses:
+ commit = self.gitcommit(u"", author_email=email)
+ violations = rule.validate(commit)
+ self.assertListEqual(violations,
+ [RuleViolation("M1", "Author email for commit is invalid", email)])
diff --git a/gitlint/tests/rules/test_rules.py b/gitlint/tests/rules/test_rules.py
new file mode 100644
index 0000000..89caa27
--- /dev/null
+++ b/gitlint/tests/rules/test_rules.py
@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import Rule, RuleViolation
+
+
+class RuleTests(BaseTestCase):
+
+ def test_rule_equality(self):
+ self.assertEqual(Rule(), Rule())
+ # Ensure rules are not equal if they differ on their attributes
+ for attr in ["id", "name", "target", "options"]:
+ rule = Rule()
+ setattr(rule, attr, u"åbc")
+ self.assertNotEqual(Rule(), rule)
+
+ def test_rule_violation_equality(self):
+ violation1 = RuleViolation(u"ïd1", u"My messåge", u"My cöntent", 1)
+ self.object_equality_test(violation1, ["rule_id", "message", "content", "line_nr"])
diff --git a/gitlint/tests/rules/test_title_rules.py b/gitlint/tests/rules/test_title_rules.py
new file mode 100644
index 0000000..07d2323
--- /dev/null
+++ b/gitlint/tests/rules/test_title_rules.py
@@ -0,0 +1,154 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import TitleMaxLength, TitleTrailingWhitespace, TitleHardTab, TitleMustNotContainWord, \
+ TitleTrailingPunctuation, TitleLeadingWhitespace, TitleRegexMatches, RuleViolation
+
+
+class TitleRuleTests(BaseTestCase):
+ def test_max_line_length(self):
+ rule = TitleMaxLength()
+
+ # assert no error
+ violation = rule.validate(u"å" * 72, None)
+ self.assertIsNone(violation)
+
+ # assert error on line length > 72
+ expected_violation = RuleViolation("T1", "Title exceeds max length (73>72)", u"å" * 73)
+ violations = rule.validate(u"å" * 73, None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # set line length to 120, and check no violation on length 73
+ rule = TitleMaxLength({'line-length': 120})
+ violations = rule.validate(u"å" * 73, None)
+ self.assertIsNone(violations)
+
+ # assert raise on 121
+ expected_violation = RuleViolation("T1", "Title exceeds max length (121>120)", u"å" * 121)
+ violations = rule.validate(u"å" * 121, None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_trailing_whitespace(self):
+ rule = TitleTrailingWhitespace()
+
+ # assert no error
+ violations = rule.validate(u"å", None)
+ self.assertIsNone(violations)
+
+ # trailing space
+ expected_violation = RuleViolation("T2", "Title has trailing whitespace", u"å ")
+ violations = rule.validate(u"å ", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # trailing tab
+ expected_violation = RuleViolation("T2", "Title has trailing whitespace", u"å\t")
+ violations = rule.validate(u"å\t", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_hard_tabs(self):
+ rule = TitleHardTab()
+
+ # assert no error
+ violations = rule.validate(u"This is å test", None)
+ self.assertIsNone(violations)
+
+ # contains hard tab
+ expected_violation = RuleViolation("T4", "Title contains hard tab characters (\\t)", u"This is å\ttest")
+ violations = rule.validate(u"This is å\ttest", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_trailing_punctuation(self):
+ rule = TitleTrailingPunctuation()
+
+ # assert no error
+ violations = rule.validate(u"This is å test", None)
+ self.assertIsNone(violations)
+
+ # assert errors for different punctuations
+ punctuation = u"?:!.,;"
+ for char in punctuation:
+ line = u"This is å test" + char # note that make sure to include some unicode!
+ gitcontext = self.gitcontext(line)
+ expected_violation = RuleViolation("T3", u"Title has trailing punctuation ({0})".format(char), line)
+ violations = rule.validate(line, gitcontext)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_title_must_not_contain_word(self):
+ rule = TitleMustNotContainWord()
+
+ # no violations
+ violations = rule.validate(u"This is å test", None)
+ self.assertIsNone(violations)
+
+ # no violation if WIP occurs inside a wor
+ violations = rule.validate(u"This is å wiping test", None)
+ self.assertIsNone(violations)
+
+ # match literally
+ violations = rule.validate(u"WIP This is å test", None)
+ expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"WIP This is å test")
+ self.assertListEqual(violations, [expected_violation])
+
+ # match case insensitive
+ violations = rule.validate(u"wip This is å test", None)
+ expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"wip This is å test")
+ self.assertListEqual(violations, [expected_violation])
+
+ # match if there is a colon after the word
+ violations = rule.validate(u"WIP:This is å test", None)
+ expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"WIP:This is å test")
+ self.assertListEqual(violations, [expected_violation])
+
+ # match multiple words
+ rule = TitleMustNotContainWord({'words': u"wip,test,å"})
+ violations = rule.validate(u"WIP:This is å test", None)
+ expected_violation = RuleViolation("T5", "Title contains the word 'wip' (case-insensitive)",
+ u"WIP:This is å test")
+ expected_violation2 = RuleViolation("T5", "Title contains the word 'test' (case-insensitive)",
+ u"WIP:This is å test")
+ expected_violation3 = RuleViolation("T5", u"Title contains the word 'å' (case-insensitive)",
+ u"WIP:This is å test")
+ self.assertListEqual(violations, [expected_violation, expected_violation2, expected_violation3])
+
+ def test_leading_whitespace(self):
+ rule = TitleLeadingWhitespace()
+
+ # assert no error
+ violations = rule.validate("a", None)
+ self.assertIsNone(violations)
+
+ # leading space
+ expected_violation = RuleViolation("T6", "Title has leading whitespace", " a")
+ violations = rule.validate(" a", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # leading tab
+ expected_violation = RuleViolation("T6", "Title has leading whitespace", "\ta")
+ violations = rule.validate("\ta", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ # unicode test
+ expected_violation = RuleViolation("T6", "Title has leading whitespace", u" ☺")
+ violations = rule.validate(u" ☺", None)
+ self.assertListEqual(violations, [expected_violation])
+
+ def test_regex_matches(self):
+ commit = self.gitcommit(u"US1234: åbc\n")
+
+ # assert no violation on default regex (=everything allowed)
+ rule = TitleRegexMatches()
+ violations = rule.validate(commit.message.title, commit)
+ self.assertIsNone(violations)
+
+ # assert no violation on matching regex
+ rule = TitleRegexMatches({'regex': u"^US[0-9]*: å"})
+ violations = rule.validate(commit.message.title, commit)
+ self.assertIsNone(violations)
+
+ # assert violation when no matching regex
+ rule = TitleRegexMatches({'regex': u"^UÅ[0-9]*"})
+ violations = rule.validate(commit.message.title, commit)
+ expected_violation = RuleViolation("T7", u"Title does not match regex (^UÅ[0-9]*)", u"US1234: åbc")
+ self.assertListEqual(violations, [expected_violation])
diff --git a/gitlint/tests/rules/test_user_rules.py b/gitlint/tests/rules/test_user_rules.py
new file mode 100644
index 0000000..57c03a0
--- /dev/null
+++ b/gitlint/tests/rules/test_user_rules.py
@@ -0,0 +1,223 @@
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.rule_finder import find_rule_classes, assert_valid_rule_class
+from gitlint.rules import UserRuleError
+from gitlint.utils import ustr
+
+from gitlint import options, rules
+
+
+class UserRuleTests(BaseTestCase):
+ def test_find_rule_classes(self):
+ # Let's find some user classes!
+ user_rule_path = self.get_sample_path("user_rules")
+ classes = find_rule_classes(user_rule_path)
+
+ # Compare string representations because we can't import MyUserCommitRule here since samples/user_rules is not
+ # a proper python package
+ # Note that the following check effectively asserts that:
+ # - There is only 1 rule recognized and it is MyUserCommitRule
+ # - Other non-python files in the directory are ignored
+ # - Other members of the my_commit_rules module are ignored
+ # (such as func_should_be_ignored, global_variable_should_be_ignored)
+ # - Rules are loaded non-recursively (user_rules/import_exception directory is ignored)
+ self.assertEqual("[<class 'my_commit_rules.MyUserCommitRule'>]", ustr(classes))
+
+ # Assert that we added the new user_rules directory to the system path and modules
+ self.assertIn(user_rule_path, sys.path)
+ self.assertIn("my_commit_rules", sys.modules)
+
+ # Do some basic asserts on our user rule
+ self.assertEqual(classes[0].id, "UC1")
+ self.assertEqual(classes[0].name, u"my-üser-commit-rule")
+ expected_option = options.IntOption('violation-count', 1, u"Number of violåtions to return")
+ self.assertListEqual(classes[0].options_spec, [expected_option])
+ self.assertTrue(hasattr(classes[0], "validate"))
+
+ # Test that we can instantiate the class and can execute run the validate method and that it returns the
+ # expected result
+ rule_class = classes[0]()
+ violations = rule_class.validate("false-commit-object (ignored)")
+ self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)])
+
+ # Have it return more violations
+ rule_class.options['violation-count'].value = 2
+ violations = rule_class.validate("false-commit-object (ignored)")
+ self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1),
+ rules.RuleViolation("UC1", u"Commit violåtion 2", u"Contënt 2", 2)])
+
+ def test_extra_path_specified_by_file(self):
+ # Test that find_rule_classes can handle an extra path given as a file name instead of a directory
+ user_rule_path = self.get_sample_path("user_rules")
+ user_rule_module = os.path.join(user_rule_path, "my_commit_rules.py")
+ classes = find_rule_classes(user_rule_module)
+
+ rule_class = classes[0]()
+ violations = rule_class.validate("false-commit-object (ignored)")
+ self.assertListEqual(violations, [rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)])
+
+ def test_rules_from_init_file(self):
+ # Test that we can import rules that are defined in __init__.py files
+ # This also tests that we can import rules from python packages. This use to cause issues with pypy
+ # So this is also a regression test for that.
+ user_rule_path = self.get_sample_path(os.path.join("user_rules", "parent_package"))
+ classes = find_rule_classes(user_rule_path)
+
+ # convert classes to strings and sort them so we can compare them
+ class_strings = sorted([ustr(clazz) for clazz in classes])
+ expected = [u"<class 'my_commit_rules.MyUserCommitRule'>", u"<class 'parent_package.InitFileRule'>"]
+ self.assertListEqual(class_strings, expected)
+
+ def test_empty_user_classes(self):
+ # Test that we don't find rules if we scan a different directory
+ user_rule_path = self.get_sample_path("config")
+ classes = find_rule_classes(user_rule_path)
+ self.assertListEqual(classes, [])
+
+ # Importantly, ensure that the directory is not added to the syspath as this happens only when we actually
+ # find modules
+ self.assertNotIn(user_rule_path, sys.path)
+
+ def test_failed_module_import(self):
+ # test importing a bogus module
+ user_rule_path = self.get_sample_path("user_rules/import_exception")
+ # We don't check the entire error message because that is different based on the python version and underlying
+ # operating system
+ expected_msg = "Error while importing extra-path module 'invalid_python'"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ find_rule_classes(user_rule_path)
+
+ def test_find_rule_classes_nonexisting_path(self):
+ with self.assertRaisesRegex(UserRuleError, u"Invalid extra-path: föo/bar"):
+ find_rule_classes(u"föo/bar")
+
+ def test_assert_valid_rule_class(self):
+ class MyLineRuleClass(rules.LineRule):
+ id = 'UC1'
+ name = u'my-lïne-rule'
+ target = rules.CommitMessageTitle
+
+ def validate(self):
+ pass
+
+ class MyCommitRuleClass(rules.CommitRule):
+ id = 'UC2'
+ name = u'my-cömmit-rule'
+
+ def validate(self):
+ pass
+
+ # Just assert that no error is raised
+ self.assertIsNone(assert_valid_rule_class(MyLineRuleClass))
+ self.assertIsNone(assert_valid_rule_class(MyCommitRuleClass))
+
+ def test_assert_valid_rule_class_negative(self):
+ # general test to make sure that incorrect rules will raise an exception
+ user_rule_path = self.get_sample_path("user_rules/incorrect_linerule")
+ with self.assertRaisesRegex(UserRuleError,
+ "User-defined rule class 'MyUserLineRule' must have a 'validate' method"):
+ find_rule_classes(user_rule_path)
+
+ def test_assert_valid_rule_class_negative_parent(self):
+ # rule class must extend from LineRule or CommitRule
+ class MyRuleClass(object):
+ pass
+
+ expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule " + \
+ "or gitlint.rules.CommitRule"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_id(self):
+ class MyRuleClass(rules.LineRule):
+ pass
+
+ # Rule class must have an id
+ expected_msg = "User-defined rule class 'MyRuleClass' must have an 'id' attribute"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # Rule ids must be non-empty
+ MyRuleClass.id = ""
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # Rule ids must not start with one of the reserved id letters
+ for letter in ["T", "R", "B", "M"]:
+ MyRuleClass.id = letter + "1"
+ expected_msg = "The id '{0}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M"
+ with self.assertRaisesRegex(UserRuleError, expected_msg.format(letter)):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_name(self):
+ class MyRuleClass(rules.LineRule):
+ id = "UC1"
+
+ # Rule class must have an name
+ expected_msg = "User-defined rule class 'MyRuleClass' must have a 'name' attribute"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # Rule names must be non-empty
+ MyRuleClass.name = ""
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_option_spec(self):
+ class MyRuleClass(rules.LineRule):
+ id = "UC1"
+ name = u"my-rüle-class"
+
+ # if set, option_spec must be a list of gitlint options
+ MyRuleClass.options_spec = u"föo"
+ expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \
+ "of gitlint.options.RuleOption"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # option_spec is a list, but not of gitlint options
+ MyRuleClass.options_spec = [u"föo", 123] # pylint: disable=bad-option-value,redefined-variable-type
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_validate(self):
+ class MyRuleClass(rules.LineRule):
+ id = "UC1"
+ name = u"my-rüle-class"
+
+ with self.assertRaisesRegex(UserRuleError,
+ "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ assert_valid_rule_class(MyRuleClass)
+
+ # validate attribute - not a method
+ MyRuleClass.validate = u"föo"
+ with self.assertRaisesRegex(UserRuleError,
+ "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_target(self):
+ class MyRuleClass(rules.LineRule):
+ id = "UC1"
+ name = u"my-rüle-class"
+
+ def validate(self):
+ pass
+
+ # no target
+ expected_msg = "The target attribute of the user-defined LineRule class 'MyRuleClass' must be either " + \
+ "gitlint.rules.CommitMessageTitle or gitlint.rules.CommitMessageBody"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # invalid target
+ MyRuleClass.target = u"föo"
+ with self.assertRaisesRegex(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # valid target, no exception should be raised
+ MyRuleClass.target = rules.CommitMessageTitle # pylint: disable=bad-option-value,redefined-variable-type
+ self.assertIsNone(assert_valid_rule_class(MyRuleClass))
diff --git a/gitlint/tests/samples/commit_message/fixup b/gitlint/tests/samples/commit_message/fixup
new file mode 100644
index 0000000..2539dd1
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/fixup
@@ -0,0 +1 @@
+fixup! WIP: This is a fixup cömmit with violations.
diff --git a/gitlint/tests/samples/commit_message/merge b/gitlint/tests/samples/commit_message/merge
new file mode 100644
index 0000000..764e131
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/merge
@@ -0,0 +1,3 @@
+Merge: "This is a merge commit with a long title that most definitely exceeds the normål limit of 72 chars"
+This line should be ëmpty
+This is the first line is meant to test å line that exceeds the maximum line length of 80 characters.
diff --git a/gitlint/tests/samples/commit_message/revert b/gitlint/tests/samples/commit_message/revert
new file mode 100644
index 0000000..6dc8368
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/revert
@@ -0,0 +1,3 @@
+Revert "WIP: this is a tïtle"
+
+This reverts commit a8ad67e04164a537198dea94a4fde81c5592ae9c. \ No newline at end of file
diff --git a/gitlint/tests/samples/commit_message/sample1 b/gitlint/tests/samples/commit_message/sample1
new file mode 100644
index 0000000..646c0cb
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/sample1
@@ -0,0 +1,14 @@
+Commit title contåining 'WIP', as well as trailing punctuation.
+This line should be empty
+This is the first line of the commit message body and it is meant to test a line that exceeds the maximum line length of 80 characters.
+This line has a tråiling space.
+This line has a trailing tab.
+# This is a cömmented line
+# ------------------------ >8 ------------------------
+# Anything after this line should be cleaned up
+# this line appears on `git commit -v` command
+diff --git a/gitlint/tests/samples/commit_message/sample1 b/gitlint/tests/samples/commit_message/sample1
+index 82dbe7f..ae71a14 100644
+--- a/gitlint/tests/samples/commit_message/sample1
++++ b/gitlint/tests/samples/commit_message/sample1
+@@ -1 +1 @@
diff --git a/gitlint/tests/samples/commit_message/sample2 b/gitlint/tests/samples/commit_message/sample2
new file mode 100644
index 0000000..356540c
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/sample2
@@ -0,0 +1 @@
+Just a title contåining WIP \ No newline at end of file
diff --git a/gitlint/tests/samples/commit_message/sample3 b/gitlint/tests/samples/commit_message/sample3
new file mode 100644
index 0000000..d67d70b
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/sample3
@@ -0,0 +1,6 @@
+ Commit title containing 'WIP', leading and tråiling whitespace and longer than 72 characters.
+This line should be empty
+This is the first line is meånt to test a line that exceeds the maximum line length of 80 characters.
+This line has a trailing space.
+This line has a tråiling tab.
+# This is a commented line
diff --git a/gitlint/tests/samples/commit_message/sample4 b/gitlint/tests/samples/commit_message/sample4
new file mode 100644
index 0000000..c858d89
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/sample4
@@ -0,0 +1,7 @@
+ Commit title containing 'WIP', leading and tråiling whitespace and longer than 72 characters.
+This line should be empty
+This is the first line is meånt to test a line that exceeds the maximum line length of 80 characters.
+This line has a tråiling space.
+This line has a trailing tab.
+# This is a commented line
+gitlint-ignore: all
diff --git a/gitlint/tests/samples/commit_message/sample5 b/gitlint/tests/samples/commit_message/sample5
new file mode 100644
index 0000000..77ccbe8
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/sample5
@@ -0,0 +1,7 @@
+ Commit title containing 'WIP', leading and tråiling whitespace and longer than 72 characters.
+This line should be ëmpty
+This is the first line is meånt to test a line that exceeds the maximum line length of 80 characters.
+This line has a tråiling space.
+This line has a trailing tab.
+# This is a commented line
+gitlint-ignore: T3, T6, body-max-line-length
diff --git a/gitlint/tests/samples/commit_message/squash b/gitlint/tests/samples/commit_message/squash
new file mode 100644
index 0000000..538a93a
--- /dev/null
+++ b/gitlint/tests/samples/commit_message/squash
@@ -0,0 +1,3 @@
+squash! WIP: This is a squash cömmit with violations.
+
+Body töo short
diff --git a/gitlint/tests/samples/config/gitlintconfig b/gitlint/tests/samples/config/gitlintconfig
new file mode 100644
index 0000000..8c93f71
--- /dev/null
+++ b/gitlint/tests/samples/config/gitlintconfig
@@ -0,0 +1,15 @@
+[general]
+ignore=title-trailing-whitespace,B2
+verbosity = 1
+ignore-merge-commits = false
+debug = false
+
+[title-max-length]
+line-length=20
+
+[B1]
+# B1 = body-max-line-length
+line-length=30
+
+[title-must-not-contain-word]
+words=WIP,bögus \ No newline at end of file
diff --git a/gitlint/tests/samples/config/invalid-option-value b/gitlint/tests/samples/config/invalid-option-value
new file mode 100644
index 0000000..92015aa
--- /dev/null
+++ b/gitlint/tests/samples/config/invalid-option-value
@@ -0,0 +1,11 @@
+[general]
+ignore=title-trailing-whitespace,B2
+verbosity = 1
+
+[title-max-length]
+line-length=föo
+
+
+[B1]
+# B1 = body-max-line-length
+line-length=30 \ No newline at end of file
diff --git a/gitlint/tests/samples/config/no-sections b/gitlint/tests/samples/config/no-sections
new file mode 100644
index 0000000..ec82b25
--- /dev/null
+++ b/gitlint/tests/samples/config/no-sections
@@ -0,0 +1 @@
+ignore=title-max-length, T3
diff --git a/gitlint/tests/samples/config/nonexisting-general-option b/gitlint/tests/samples/config/nonexisting-general-option
new file mode 100644
index 0000000..d5cfef2
--- /dev/null
+++ b/gitlint/tests/samples/config/nonexisting-general-option
@@ -0,0 +1,13 @@
+[general]
+ignore=title-trailing-whitespace,B2
+verbosity = 1
+ignore-merge-commits = false
+foo = bar
+
+[title-max-length]
+line-length=20
+
+
+[B1]
+# B1 = body-max-line-length
+line-length=30 \ No newline at end of file
diff --git a/gitlint/tests/samples/config/nonexisting-option b/gitlint/tests/samples/config/nonexisting-option
new file mode 100644
index 0000000..6964c77
--- /dev/null
+++ b/gitlint/tests/samples/config/nonexisting-option
@@ -0,0 +1,11 @@
+[general]
+ignore=title-trailing-whitespace,B2
+verbosity = 1
+
+[title-max-length]
+föobar=foo
+
+
+[B1]
+# B1 = body-max-line-length
+line-length=30 \ No newline at end of file
diff --git a/gitlint/tests/samples/config/nonexisting-rule b/gitlint/tests/samples/config/nonexisting-rule
new file mode 100644
index 0000000..c0f0d2b
--- /dev/null
+++ b/gitlint/tests/samples/config/nonexisting-rule
@@ -0,0 +1,11 @@
+[general]
+ignore=title-trailing-whitespace,B2
+verbosity = 1
+
+[föobar]
+line-length=20
+
+
+[B1]
+# B1 = body-max-line-length
+line-length=30 \ No newline at end of file
diff --git a/gitlint/tests/samples/user_rules/bogus-file.txt b/gitlint/tests/samples/user_rules/bogus-file.txt
new file mode 100644
index 0000000..2a56650
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/bogus-file.txt
@@ -0,0 +1,2 @@
+This is just a bogus file.
+This file being here is part of the test: gitlint should ignore it. \ No newline at end of file
diff --git a/gitlint/tests/samples/user_rules/import_exception/invalid_python.py b/gitlint/tests/samples/user_rules/import_exception/invalid_python.py
new file mode 100644
index 0000000..e75fed3
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/import_exception/invalid_python.py
@@ -0,0 +1,3 @@
+# flake8: noqa
+# This is invalid python code which will cause an import exception
+class MyObject:
diff --git a/gitlint/tests/samples/user_rules/incorrect_linerule/my_line_rule.py b/gitlint/tests/samples/user_rules/incorrect_linerule/my_line_rule.py
new file mode 100644
index 0000000..004ef9d
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/incorrect_linerule/my_line_rule.py
@@ -0,0 +1,10 @@
+# -*- coding: utf-8 -*-
+
+from gitlint.rules import LineRule
+
+
+class MyUserLineRule(LineRule):
+ id = "UC2"
+ name = "my-lïne-rule"
+
+ # missing validate method, missing target attribute
diff --git a/gitlint/tests/samples/user_rules/my_commit_rules.foo b/gitlint/tests/samples/user_rules/my_commit_rules.foo
new file mode 100644
index 0000000..605d704
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/my_commit_rules.foo
@@ -0,0 +1,16 @@
+# This rule is ignored because it doesn't have a .py extension
+from gitlint.rules import CommitRule, RuleViolation
+from gitlint.options import IntOption
+
+
+class MyUserCommitRule2(CommitRule):
+ name = "my-user-commit-rule2"
+ id = "TUC2"
+ options_spec = [IntOption('violation-count', 0, "Number of violations to return")]
+
+ def validate(self, _commit):
+ violations = []
+ for i in range(1, self.options['violation-count'].value + 1):
+ violations.append(RuleViolation(self.id, "Commit violation %d" % i, "Content %d" % i, i))
+
+ return violations
diff --git a/gitlint/tests/samples/user_rules/my_commit_rules.py b/gitlint/tests/samples/user_rules/my_commit_rules.py
new file mode 100644
index 0000000..5456487
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/my_commit_rules.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+
+from gitlint.rules import CommitRule, RuleViolation
+from gitlint.options import IntOption
+
+
+class MyUserCommitRule(CommitRule):
+ name = u"my-üser-commit-rule"
+ id = "UC1"
+ options_spec = [IntOption('violation-count', 1, u"Number of violåtions to return")]
+
+ def validate(self, _commit):
+ violations = []
+ for i in range(1, self.options['violation-count'].value + 1):
+ violations.append(RuleViolation(self.id, u"Commit violåtion %d" % i, u"Contënt %d" % i, i))
+
+ return violations
+
+
+# The below code is present so that we can test that we actually ignore it
+
+def func_should_be_ignored():
+ pass
+
+
+global_variable_should_be_ignored = True
diff --git a/gitlint/tests/samples/user_rules/parent_package/__init__.py b/gitlint/tests/samples/user_rules/parent_package/__init__.py
new file mode 100644
index 0000000..32c05fc
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/parent_package/__init__.py
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+# This file is meant to test that we can also load rules from __init__.py files, this was an issue with pypy before.
+
+from gitlint.rules import CommitRule
+
+
+class InitFileRule(CommitRule):
+ name = u"my-init-cömmit-rule"
+ id = "UC1"
+ options_spec = []
+
+ def validate(self, _commit):
+ return []
diff --git a/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py b/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py
new file mode 100644
index 0000000..b73a305
--- /dev/null
+++ b/gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+
+from gitlint.rules import CommitRule
+
+
+class MyUserCommitRule(CommitRule):
+ name = u"my-user-cömmit-rule"
+ id = "UC2"
+ options_spec = []
+
+ def validate(self, _commit):
+ return []
diff --git a/gitlint/tests/test_cache.py b/gitlint/tests/test_cache.py
new file mode 100644
index 0000000..5d78953
--- /dev/null
+++ b/gitlint/tests/test_cache.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+from gitlint.tests.base import BaseTestCase
+from gitlint.cache import PropertyCache, cache
+
+
+class CacheTests(BaseTestCase):
+
+ class MyClass(PropertyCache):
+ """ Simple class that has cached properties, used for testing. """
+
+ def __init__(self):
+ PropertyCache.__init__(self)
+ self.counter = 0
+
+ @property
+ @cache
+ def foo(self):
+ self.counter += 1
+ return u"bår"
+
+ @property
+ @cache(cachekey=u"hür")
+ def bar(self):
+ self.counter += 1
+ return u"fōo"
+
+ def test_cache(self):
+ # Init new class with cached properties
+ myclass = self.MyClass()
+ self.assertEqual(myclass.counter, 0)
+ self.assertDictEqual(myclass._cache, {})
+
+ # Assert that function is called on first access, cache is set
+ self.assertEqual(myclass.foo, u"bår")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"foo": u"bår"})
+
+ # After function is not called on subsequent access, cache is still set
+ self.assertEqual(myclass.foo, u"bår")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {"foo": u"bår"})
+
+ def test_cache_custom_key(self):
+ # Init new class with cached properties
+ myclass = self.MyClass()
+ self.assertEqual(myclass.counter, 0)
+ self.assertDictEqual(myclass._cache, {})
+
+ # Assert that function is called on first access, cache is set with custom key
+ self.assertEqual(myclass.bar, u"fōo")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {u"hür": u"fōo"})
+
+ # After function is not called on subsequent access, cache is still set
+ self.assertEqual(myclass.bar, u"fōo")
+ self.assertEqual(myclass.counter, 1)
+ self.assertDictEqual(myclass._cache, {u"hür": u"fōo"})
diff --git a/gitlint/tests/test_display.py b/gitlint/tests/test_display.py
new file mode 100644
index 0000000..1c64b34
--- /dev/null
+++ b/gitlint/tests/test_display.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+
+try:
+ # python 2.x
+ from StringIO import StringIO
+except ImportError:
+ # python 3.x
+ from io import StringIO
+
+
+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.display import Display
+from gitlint.config import LintConfig
+from gitlint.tests.base import BaseTestCase
+
+
+class DisplayTests(BaseTestCase):
+ def test_v(self):
+ display = Display(LintConfig())
+ display.config.verbosity = 2
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ # Non exact outputting, should output both v and vv output
+ with patch('gitlint.display.stdout', new=StringIO()) as stdout:
+ display.v(u"tëst")
+ display.vv(u"tëst2")
+ # vvvv should be ignored regardless
+ display.vvv(u"tëst3.1")
+ display.vvv(u"tëst3.2", exact=True)
+ self.assertEqual(u"tëst\ntëst2\n", stdout.getvalue())
+
+ # exact outputting, should only output v
+ with patch('gitlint.display.stdout', new=StringIO()) as stdout:
+ display.v(u"tëst", exact=True)
+ display.vv(u"tëst2", exact=True)
+ # vvvv should be ignored regardless
+ display.vvv(u"tëst3.1")
+ display.vvv(u"tëst3.2", exact=True)
+ self.assertEqual(u"tëst2\n", stdout.getvalue())
+
+ # standard error should be empty throughtout all of this
+ self.assertEqual('', stderr.getvalue())
+
+ def test_e(self):
+ display = Display(LintConfig())
+ display.config.verbosity = 2
+
+ with patch('gitlint.display.stdout', new=StringIO()) as stdout:
+ # Non exact outputting, should output both v and vv output
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ display.e(u"tëst")
+ display.ee(u"tëst2")
+ # vvvv should be ignored regardless
+ display.eee(u"tëst3.1")
+ display.eee(u"tëst3.2", exact=True)
+ self.assertEqual(u"tëst\ntëst2\n", stderr.getvalue())
+
+ # exact outputting, should only output v
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ display.e(u"tëst", exact=True)
+ display.ee(u"tëst2", exact=True)
+ # vvvv should be ignored regardless
+ display.eee(u"tëst3.1")
+ display.eee(u"tëst3.2", exact=True)
+ self.assertEqual(u"tëst2\n", stderr.getvalue())
+
+ # standard output should be empty throughtout all of this
+ self.assertEqual('', stdout.getvalue())
diff --git a/gitlint/tests/test_hooks.py b/gitlint/tests/test_hooks.py
new file mode 100644
index 0000000..08bd730
--- /dev/null
+++ b/gitlint/tests/test_hooks.py
@@ -0,0 +1,136 @@
+# -*- coding: utf-8 -*-
+
+import os
+
+try:
+ # python 2.x
+ from mock import patch, ANY, mock_open
+except ImportError:
+ # python 3.x
+ from unittest.mock import patch, ANY, mock_open # pylint: disable=no-name-in-module, import-error
+
+from gitlint.tests.base import BaseTestCase
+from gitlint.config import LintConfig
+from gitlint.hooks import GitHookInstaller, GitHookInstallerError, COMMIT_MSG_HOOK_SRC_PATH, COMMIT_MSG_HOOK_DST_PATH, \
+ GITLINT_HOOK_IDENTIFIER
+
+
+class HookTests(BaseTestCase):
+
+ @patch('gitlint.hooks.git_hooks_dir')
+ def test_commit_msg_hook_path(self, git_hooks_dir):
+ git_hooks_dir.return_value = os.path.join(u"/föo", u"bar")
+ lint_config = LintConfig()
+ lint_config.target = self.SAMPLES_DIR
+ expected_path = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ path = GitHookInstaller.commit_msg_hook_path(lint_config)
+
+ git_hooks_dir.assert_called_once_with(self.SAMPLES_DIR)
+ self.assertEqual(path, expected_path)
+
+ @staticmethod
+ @patch('os.chmod')
+ @patch('os.stat')
+ @patch('gitlint.hooks.shutil.copy')
+ @patch('os.path.exists', return_value=False)
+ @patch('os.path.isdir', return_value=True)
+ @patch('gitlint.hooks.git_hooks_dir')
+ def test_install_commit_msg_hook(git_hooks_dir, isdir, path_exists, copy, stat, chmod):
+ lint_config = LintConfig()
+ lint_config.target = os.path.join(u"/hür", u"dur")
+ git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
+ expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ GitHookInstaller.install_commit_msg_hook(lint_config)
+ isdir.assert_called_with(git_hooks_dir.return_value)
+ path_exists.assert_called_once_with(expected_dst)
+ copy.assert_called_once_with(COMMIT_MSG_HOOK_SRC_PATH, expected_dst)
+ stat.assert_called_once_with(expected_dst)
+ chmod.assert_called_once_with(expected_dst, ANY)
+ git_hooks_dir.assert_called_with(lint_config.target)
+
+ @patch('gitlint.hooks.shutil.copy')
+ @patch('os.path.exists', return_value=False)
+ @patch('os.path.isdir', return_value=True)
+ @patch('gitlint.hooks.git_hooks_dir')
+ def test_install_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, copy):
+ lint_config = LintConfig()
+ lint_config.target = os.path.join(u"/hür", u"dur")
+ git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
+ # mock that current dir is not a git repo
+ isdir.return_value = False
+ expected_msg = u"{0} is not a git repository".format(lint_config.target)
+ with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
+ GitHookInstaller.install_commit_msg_hook(lint_config)
+ isdir.assert_called_with(git_hooks_dir.return_value)
+ path_exists.assert_not_called()
+ copy.assert_not_called()
+
+ # mock that there is already a commit hook present
+ isdir.return_value = True
+ path_exists.return_value = True
+ expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ expected_msg = u"There is already a commit-msg hook file present in {0}.\n".format(expected_dst) + \
+ "gitlint currently does not support appending to an existing commit-msg file."
+ with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
+ GitHookInstaller.install_commit_msg_hook(lint_config)
+
+ @staticmethod
+ @patch('os.remove')
+ @patch('os.path.exists', return_value=True)
+ @patch('os.path.isdir', return_value=True)
+ @patch('gitlint.hooks.git_hooks_dir')
+ def test_uninstall_commit_msg_hook(git_hooks_dir, isdir, path_exists, remove):
+ lint_config = LintConfig()
+ git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
+ lint_config.target = os.path.join(u"/hür", u"dur")
+ read_data = "#!/bin/sh\n" + GITLINT_HOOK_IDENTIFIER
+ with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True):
+ GitHookInstaller.uninstall_commit_msg_hook(lint_config)
+
+ expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ isdir.assert_called_with(git_hooks_dir.return_value)
+ path_exists.assert_called_once_with(expected_dst)
+ remove.assert_called_with(expected_dst)
+ git_hooks_dir.assert_called_with(lint_config.target)
+
+ @patch('os.remove')
+ @patch('os.path.exists', return_value=True)
+ @patch('os.path.isdir', return_value=True)
+ @patch('gitlint.hooks.git_hooks_dir')
+ def test_uninstall_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, remove):
+ lint_config = LintConfig()
+ lint_config.target = os.path.join(u"/hür", u"dur")
+ git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
+
+ # mock that the current directory is not a git repo
+ isdir.return_value = False
+ expected_msg = u"{0} is not a git repository".format(lint_config.target)
+ with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
+ GitHookInstaller.uninstall_commit_msg_hook(lint_config)
+ isdir.assert_called_with(git_hooks_dir.return_value)
+ path_exists.assert_not_called()
+ remove.assert_not_called()
+
+ # mock that there is no commit hook present
+ isdir.return_value = True
+ path_exists.return_value = False
+ expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ expected_msg = u"There is no commit-msg hook present in {0}.".format(expected_dst)
+ with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
+ GitHookInstaller.uninstall_commit_msg_hook(lint_config)
+ isdir.assert_called_with(git_hooks_dir.return_value)
+ path_exists.assert_called_once_with(expected_dst)
+ remove.assert_not_called()
+
+ # mock that there is a different (=not gitlint) commit hook
+ isdir.return_value = True
+ path_exists.return_value = True
+ read_data = "#!/bin/sh\nfoo"
+ expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
+ expected_msg = u"The commit-msg hook in {0} was not installed by gitlint ".format(expected_dst) + \
+ "(or it was modified).\nUninstallation of 3th party or modified gitlint hooks " + \
+ "is not supported."
+ with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True):
+ with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
+ GitHookInstaller.uninstall_commit_msg_hook(lint_config)
+ remove.assert_not_called()
diff --git a/gitlint/tests/test_lint.py b/gitlint/tests/test_lint.py
new file mode 100644
index 0000000..bcdd984
--- /dev/null
+++ b/gitlint/tests/test_lint.py
@@ -0,0 +1,197 @@
+# -*- coding: utf-8 -*-
+
+try:
+ # python 2.x
+ from StringIO import StringIO
+except ImportError:
+ # python 3.x
+ from io import StringIO
+
+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.tests.base import BaseTestCase
+from gitlint.lint import GitLinter
+from gitlint.rules import RuleViolation
+from gitlint.config import LintConfig, LintConfigBuilder
+
+
+class LintTests(BaseTestCase):
+
+ def test_lint_sample1(self):
+ linter = GitLinter(LintConfig())
+ gitcontext = self.gitcontext(self.get_sample("commit_message/sample1"))
+ violations = linter.lint(gitcontext.commits[-1])
+ expected_errors = [RuleViolation("T3", "Title has trailing punctuation (.)",
+ u"Commit title contåining 'WIP', as well as trailing punctuation.", 1),
+ RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"Commit title contåining 'WIP', as well as trailing punctuation.", 1),
+ RuleViolation("B4", "Second line is not empty", "This line should be empty", 2),
+ RuleViolation("B1", "Line exceeds max length (135>80)",
+ "This is the first line of the commit message body and it is meant to test " +
+ "a line that exceeds the maximum line length of 80 characters.", 3),
+ RuleViolation("B2", "Line has trailing whitespace", u"This line has a tråiling space. ", 4),
+ RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5),
+ RuleViolation("B3", "Line contains hard tab characters (\\t)",
+ "This line has a trailing tab.\t", 5)]
+
+ self.assertListEqual(violations, expected_errors)
+
+ def test_lint_sample2(self):
+ linter = GitLinter(LintConfig())
+ gitcontext = self.gitcontext(self.get_sample("commit_message/sample2"))
+ violations = linter.lint(gitcontext.commits[-1])
+ expected = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"Just a title contåining WIP", 1),
+ RuleViolation("B6", "Body message is missing", None, 3)]
+
+ self.assertListEqual(violations, expected)
+
+ def test_lint_sample3(self):
+ linter = GitLinter(LintConfig())
+ gitcontext = self.gitcontext(self.get_sample("commit_message/sample3"))
+ violations = linter.lint(gitcontext.commits[-1])
+
+ title = u" Commit title containing 'WIP', \tleading and tråiling whitespace and longer than 72 characters."
+ expected = [RuleViolation("T1", "Title exceeds max length (95>72)", title, 1),
+ RuleViolation("T3", "Title has trailing punctuation (.)", title, 1),
+ RuleViolation("T4", "Title contains hard tab characters (\\t)", title, 1),
+ RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", title, 1),
+ RuleViolation("T6", "Title has leading whitespace", title, 1),
+ RuleViolation("B4", "Second line is not empty", "This line should be empty", 2),
+ RuleViolation("B1", "Line exceeds max length (101>80)",
+ u"This is the first line is meånt to test a line that exceeds the maximum line " +
+ "length of 80 characters.", 3),
+ RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing space. ", 4),
+ RuleViolation("B2", "Line has trailing whitespace", u"This line has a tråiling tab.\t", 5),
+ RuleViolation("B3", "Line contains hard tab characters (\\t)",
+ u"This line has a tråiling tab.\t", 5)]
+
+ self.assertListEqual(violations, expected)
+
+ def test_lint_sample4(self):
+ commit = self.gitcommit(self.get_sample("commit_message/sample4"))
+ config_builder = LintConfigBuilder()
+ config_builder.set_config_from_commit(commit)
+ linter = GitLinter(config_builder.build())
+ violations = linter.lint(commit)
+ # expect no violations because sample4 has a 'gitlint: disable line'
+ expected = []
+ self.assertListEqual(violations, expected)
+
+ def test_lint_sample5(self):
+ commit = self.gitcommit(self.get_sample("commit_message/sample5"))
+ config_builder = LintConfigBuilder()
+ config_builder.set_config_from_commit(commit)
+ linter = GitLinter(config_builder.build())
+ violations = linter.lint(commit)
+
+ title = u" Commit title containing 'WIP', \tleading and tråiling whitespace and longer than 72 characters."
+ # expect only certain violations because sample5 has a 'gitlint-ignore: T3, T6, body-max-line-length'
+ expected = [RuleViolation("T1", "Title exceeds max length (95>72)", title, 1),
+ RuleViolation("T4", "Title contains hard tab characters (\\t)", title, 1),
+ RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)", title, 1),
+ RuleViolation("B4", "Second line is not empty", u"This line should be ëmpty", 2),
+ RuleViolation("B2", "Line has trailing whitespace", u"This line has a tråiling space. ", 4),
+ RuleViolation("B2", "Line has trailing whitespace", "This line has a trailing tab.\t", 5),
+ RuleViolation("B3", "Line contains hard tab characters (\\t)",
+ "This line has a trailing tab.\t", 5)]
+ self.assertListEqual(violations, expected)
+
+ def test_lint_meta(self):
+ """ Lint sample2 but also add some metadata to the commit so we that get's linted as well """
+ linter = GitLinter(LintConfig())
+ gitcontext = self.gitcontext(self.get_sample("commit_message/sample2"))
+ gitcontext.commits[0].author_email = u"foo bår"
+ violations = linter.lint(gitcontext.commits[-1])
+ expected = [RuleViolation("M1", "Author email for commit is invalid", u"foo bår", None),
+ RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"Just a title contåining WIP", 1),
+ RuleViolation("B6", "Body message is missing", None, 3)]
+
+ self.assertListEqual(violations, expected)
+
+ def test_lint_ignore(self):
+ lint_config = LintConfig()
+ lint_config.ignore = ["T1", "T3", "T4", "T5", "T6", "B1", "B2"]
+ linter = GitLinter(lint_config)
+ violations = linter.lint(self.gitcommit(self.get_sample("commit_message/sample3")))
+
+ expected = [RuleViolation("B4", "Second line is not empty", "This line should be empty", 2),
+ RuleViolation("B3", "Line contains hard tab characters (\\t)",
+ u"This line has a tråiling tab.\t", 5)]
+
+ self.assertListEqual(violations, expected)
+
+ def test_lint_configuration_rule(self):
+ # Test that all rules are ignored because of matching regex
+ lint_config = LintConfig()
+ lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")
+
+ linter = GitLinter(lint_config)
+ violations = linter.lint(self.gitcommit(self.get_sample("commit_message/sample2")))
+ self.assertListEqual(violations, [])
+
+ # Test ignoring only certain rules
+ lint_config = LintConfig()
+ lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")
+ lint_config.set_rule_option("I1", "ignore", "B6")
+
+ linter = GitLinter(lint_config)
+ violations = linter.lint(self.gitcommit(self.get_sample("commit_message/sample2")))
+
+ # Normally we'd expect a B6 violation, but that one is skipped because of the specific ignore set above
+ expected = [RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
+ u"Just a title contåining WIP", 1)]
+
+ self.assertListEqual(violations, expected)
+
+ def test_lint_special_commit(self):
+ for commit_type in ["merge", "revert", "squash", "fixup"]:
+ commit = self.gitcommit(self.get_sample("commit_message/{0}".format(commit_type)))
+ lintconfig = LintConfig()
+ linter = GitLinter(lintconfig)
+ violations = linter.lint(commit)
+ # Even though there are a number of violations in the commit message, they are ignored because
+ # we are dealing with a merge commit
+ self.assertListEqual(violations, [])
+
+ # Check that we do see violations if we disable 'ignore-merge-commits'
+ setattr(lintconfig, "ignore_{0}_commits".format(commit_type), False)
+ linter = GitLinter(lintconfig)
+ violations = linter.lint(commit)
+ self.assertTrue(len(violations) > 0)
+
+ def test_print_violations(self):
+ violations = [RuleViolation("RULE_ID_1", u"Error Messåge 1", "Violating Content 1", None),
+ RuleViolation("RULE_ID_2", "Error Message 2", u"Violåting Content 2", 2)]
+ linter = GitLinter(LintConfig())
+
+ # test output with increasing verbosity
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ linter.config.verbosity = 0
+ linter.print_violations(violations)
+ self.assertEqual("", stderr.getvalue())
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ linter.config.verbosity = 1
+ linter.print_violations(violations)
+ expected = u"-: RULE_ID_1\n2: RULE_ID_2\n"
+ self.assertEqual(expected, stderr.getvalue())
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ linter.config.verbosity = 2
+ linter.print_violations(violations)
+ expected = u"-: RULE_ID_1 Error Messåge 1\n2: RULE_ID_2 Error Message 2\n"
+ self.assertEqual(expected, stderr.getvalue())
+
+ with patch('gitlint.display.stderr', new=StringIO()) as stderr:
+ linter.config.verbosity = 3
+ linter.print_violations(violations)
+ expected = u"-: RULE_ID_1 Error Messåge 1: \"Violating Content 1\"\n" + \
+ u"2: RULE_ID_2 Error Message 2: \"Violåting Content 2\"\n"
+ self.assertEqual(expected, stderr.getvalue())
diff --git a/gitlint/tests/test_options.py b/gitlint/tests/test_options.py
new file mode 100644
index 0000000..2c17226
--- /dev/null
+++ b/gitlint/tests/test_options.py
@@ -0,0 +1,179 @@
+# -*- coding: utf-8 -*-
+import os
+
+from gitlint.tests.base import BaseTestCase
+
+from gitlint.options import IntOption, BoolOption, StrOption, ListOption, PathOption, RuleOptionError
+
+
+class RuleOptionTests(BaseTestCase):
+ def test_option_equality(self):
+ # 2 options are equal if their name, value and description match
+ option1 = IntOption("test-option", 123, u"Test Dëscription")
+ option2 = IntOption("test-option", 123, u"Test Dëscription")
+ self.assertEqual(option1, option2)
+
+ # Not equal: name, description, value are different
+ self.assertNotEqual(option1, IntOption("test-option1", 123, u"Test Dëscription"))
+ self.assertNotEqual(option1, IntOption("test-option", 1234, u"Test Dëscription"))
+ self.assertNotEqual(option1, IntOption("test-option", 123, u"Test Dëscription2"))
+
+ def test_int_option(self):
+ # normal behavior
+ option = IntOption("test-name", 123, "Test Description")
+ self.assertEqual(option.value, 123)
+ self.assertEqual(option.name, "test-name")
+ self.assertEqual(option.description, "Test Description")
+
+ # re-set value
+ option.set(456)
+ self.assertEqual(option.value, 456)
+
+ # error on negative int when not allowed
+ expected_error = u"Option 'test-name' must be a positive integer (current value: '-123')"
+ with self.assertRaisesRegex(RuleOptionError, expected_error):
+ option.set(-123)
+
+ # error on non-int value
+ expected_error = u"Option 'test-name' must be a positive integer (current value: 'foo')"
+ with self.assertRaisesRegex(RuleOptionError, expected_error):
+ option.set("foo")
+
+ # no error on negative value when allowed and negative int is passed
+ option = IntOption("test-name", 123, "Test Description", allow_negative=True)
+ option.set(-456)
+ self.assertEqual(option.value, -456)
+
+ # error on non-int value when negative int is allowed
+ expected_error = u"Option 'test-name' must be an integer (current value: 'foo')"
+ with self.assertRaisesRegex(RuleOptionError, expected_error):
+ option.set("foo")
+
+ def test_str_option(self):
+ # normal behavior
+ option = StrOption("test-name", u"föo", "Test Description")
+ self.assertEqual(option.value, u"föo")
+ self.assertEqual(option.name, "test-name")
+ self.assertEqual(option.description, "Test Description")
+
+ # re-set value
+ option.set(u"bår")
+ self.assertEqual(option.value, u"bår")
+
+ # conversion to str
+ option.set(123)
+ self.assertEqual(option.value, "123")
+
+ # conversion to str
+ option.set(-123)
+ self.assertEqual(option.value, "-123")
+
+ def test_boolean_option(self):
+ # normal behavior
+ option = BoolOption("test-name", "true", "Test Description")
+ self.assertEqual(option.value, True)
+
+ # re-set value
+ option.set("False")
+ self.assertEqual(option.value, False)
+
+ # Re-set using actual boolean
+ option.set(True)
+ self.assertEqual(option.value, True)
+
+ # error on incorrect value
+ incorrect_values = [1, -1, "foo", u"bår", ["foo"], {'foo': "bar"}]
+ for value in incorrect_values:
+ with self.assertRaisesRegex(RuleOptionError, "Option 'test-name' must be either 'true' or 'false'"):
+ option.set(value)
+
+ def test_list_option(self):
+ # normal behavior
+ option = ListOption("test-name", u"å,b,c,d", "Test Description")
+ self.assertListEqual(option.value, [u"å", u"b", u"c", u"d"])
+
+ # re-set value
+ option.set(u"1,2,3,4")
+ self.assertListEqual(option.value, [u"1", u"2", u"3", u"4"])
+
+ # set list
+ option.set([u"foo", u"bår", u"test"])
+ self.assertListEqual(option.value, [u"foo", u"bår", u"test"])
+
+ # empty string
+ option.set("")
+ self.assertListEqual(option.value, [])
+
+ # whitespace string
+ option.set(" \t ")
+ self.assertListEqual(option.value, [])
+
+ # empty list
+ option.set([])
+ self.assertListEqual(option.value, [])
+
+ # trailing comma
+ option.set(u"ë,f,g,")
+ self.assertListEqual(option.value, [u"ë", u"f", u"g"])
+
+ # leading and trailing whitespace should be trimmed, but only deduped within text
+ option.set(" abc , def , ghi \t , jkl mno ")
+ self.assertListEqual(option.value, ["abc", "def", "ghi", "jkl mno"])
+
+ # Also strip whitespace within a list
+ option.set(["\t foo", "bar \t ", " test 123 "])
+ self.assertListEqual(option.value, ["foo", "bar", "test 123"])
+
+ # conversion to string before split
+ option.set(123)
+ self.assertListEqual(option.value, ["123"])
+
+ def test_path_option(self):
+ option = PathOption("test-directory", ".", u"Test Description", type=u"dir")
+ self.assertEqual(option.value, os.getcwd())
+ self.assertEqual(option.name, "test-directory")
+ self.assertEqual(option.description, u"Test Description")
+ self.assertEqual(option.type, u"dir")
+
+ # re-set value
+ option.set(self.SAMPLES_DIR)
+ self.assertEqual(option.value, self.SAMPLES_DIR)
+
+ # set to int
+ expected = u"Option test-directory must be an existing directory (current value: '1234')"
+ with self.assertRaisesRegex(RuleOptionError, expected):
+ option.set(1234)
+
+ # set to non-existing directory
+ non_existing_path = os.path.join(u"/föo", u"bar")
+ expected = u"Option test-directory must be an existing directory (current value: '{0}')"
+ with self.assertRaisesRegex(RuleOptionError, expected.format(non_existing_path)):
+ option.set(non_existing_path)
+
+ # set to a file, should raise exception since option.type = dir
+ sample_path = self.get_sample_path(os.path.join("commit_message", "sample1"))
+ expected = u"Option test-directory must be an existing directory (current value: '{0}')".format(sample_path)
+ with self.assertRaisesRegex(RuleOptionError, expected):
+ option.set(sample_path)
+
+ # set option.type = file, file should now be accepted, directories not
+ option.type = u"file"
+ option.set(sample_path)
+ self.assertEqual(option.value, sample_path)
+ expected = u"Option test-directory must be an existing file (current value: '{0}')".format(
+ self.get_sample_path())
+ with self.assertRaisesRegex(RuleOptionError, expected):
+ option.set(self.get_sample_path())
+
+ # set option.type = both, files and directories should now be accepted
+ option.type = u"both"
+ option.set(sample_path)
+ self.assertEqual(option.value, sample_path)
+ option.set(self.get_sample_path())
+ self.assertEqual(option.value, self.get_sample_path())
+
+ # Expect exception if path type is invalid
+ option.type = u'föo'
+ expected = u"Option test-directory type must be one of: 'file', 'dir', 'both' (current: 'föo')"
+ with self.assertRaisesRegex(RuleOptionError, expected):
+ option.set("haha")
diff --git a/gitlint/tests/test_utils.py b/gitlint/tests/test_utils.py
new file mode 100644
index 0000000..6f667c2
--- /dev/null
+++ b/gitlint/tests/test_utils.py
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+
+from gitlint import utils
+from gitlint.tests.base import BaseTestCase
+
+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
+
+
+class UtilsTests(BaseTestCase):
+
+ def tearDown(self):
+ # Since we're messing around with `utils.PLATFORM_IS_WINDOWS` during these tests, we need to reset
+ # its value after we're done this doesn't influence other tests
+ utils.PLATFORM_IS_WINDOWS = utils.platform_is_windows()
+
+ @patch('os.environ')
+ def test_use_sh_library(self, patched_env):
+ patched_env.get.return_value = "1"
+ self.assertEqual(utils.use_sh_library(), True)
+ patched_env.get.assert_called_once_with("GITLINT_USE_SH_LIB", None)
+
+ for invalid_val in ["0", u"foöbar"]:
+ patched_env.get.reset_mock() # reset mock call count
+ patched_env.get.return_value = invalid_val
+ self.assertEqual(utils.use_sh_library(), False, invalid_val)
+ patched_env.get.assert_called_once_with("GITLINT_USE_SH_LIB", None)
+
+ # Assert that when GITLINT_USE_SH_LIB is not set, we fallback to checking whether we're on Windows
+ utils.PLATFORM_IS_WINDOWS = True
+ patched_env.get.return_value = None
+ self.assertEqual(utils.use_sh_library(), False)
+
+ utils.PLATFORM_IS_WINDOWS = False
+ self.assertEqual(utils.use_sh_library(), True)
+
+ @patch('gitlint.utils.locale')
+ def test_default_encoding_non_windows(self, mocked_locale):
+ utils.PLATFORM_IS_WINDOWS = False
+ mocked_locale.getpreferredencoding.return_value = u"foöbar"
+ self.assertEqual(utils.getpreferredencoding(), u"foöbar")
+ mocked_locale.getpreferredencoding.assert_called_once()
+
+ mocked_locale.getpreferredencoding.return_value = False
+ self.assertEqual(utils.getpreferredencoding(), u"UTF-8")
+
+ @patch('os.environ')
+ def test_default_encoding_windows(self, patched_env):
+ utils.PLATFORM_IS_WINDOWS = True
+ # Mock out os.environ
+ mock_env = {}
+
+ def mocked_get(key, default):
+ return mock_env.get(key, default)
+
+ patched_env.get.side_effect = mocked_get
+
+ # Assert getpreferredencoding reads env vars in order: LC_ALL, LC_CTYPE, LANG
+ mock_env = {"LC_ALL": u"lc_all_välue", "LC_CTYPE": u"foo", "LANG": u"bar"}
+ self.assertEqual(utils.getpreferredencoding(), u"lc_all_välue")
+ mock_env = {"LC_CTYPE": u"lc_ctype_välue", "LANG": u"hur"}
+ self.assertEqual(utils.getpreferredencoding(), u"lc_ctype_välue")
+ mock_env = {"LANG": u"lang_välue"}
+ self.assertEqual(utils.getpreferredencoding(), u"lang_välue")
+
+ # Assert split on dot
+ mock_env = {"LANG": u"foo.bär"}
+ self.assertEqual(utils.getpreferredencoding(), u"bär")
+
+ # assert default encoding is UTF-8
+ mock_env = {}
+ self.assertEqual(utils.getpreferredencoding(), "UTF-8")
+ mock_env = {"FOO": u"föo"}
+ self.assertEqual(utils.getpreferredencoding(), "UTF-8")