From df9615bac55ac6f1c3f516b66279ac0007175030 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 19 Mar 2020 15:00:14 +0100 Subject: Adding upstream version 0.13.1. Signed-off-by: Daniel Baumann --- gitlint/tests/__init__.py | 0 gitlint/tests/base.py | 169 +++++++ gitlint/tests/cli/test_cli.py | 541 +++++++++++++++++++++ gitlint/tests/cli/test_cli_hooks.py | 96 ++++ gitlint/tests/config/test_config.py | 263 ++++++++++ gitlint/tests/config/test_config_builder.py | 203 ++++++++ gitlint/tests/config/test_config_precedence.py | 100 ++++ gitlint/tests/config/test_rule_collection.py | 64 +++ gitlint/tests/contrib/__init__.py | 0 gitlint/tests/contrib/test_contrib_rules.py | 72 +++ gitlint/tests/contrib/test_conventional_commit.py | 47 ++ gitlint/tests/contrib/test_signedoff_by.py | 32 ++ gitlint/tests/expected/test_cli/test_contrib_1 | 3 + gitlint/tests/expected/test_cli/test_debug_1 | 102 ++++ .../tests/expected/test_cli/test_input_stream_1 | 3 + .../expected/test_cli/test_input_stream_debug_1 | 3 + .../expected/test_cli/test_input_stream_debug_2 | 71 +++ .../expected/test_cli/test_lint_multiple_commits_1 | 8 + .../test_cli/test_lint_multiple_commits_config_1 | 6 + .../test_cli/test_lint_staged_msg_filename_1 | 2 + .../test_cli/test_lint_staged_msg_filename_2 | 70 +++ .../expected/test_cli/test_lint_staged_stdin_1 | 3 + .../expected/test_cli/test_lint_staged_stdin_2 | 72 +++ gitlint/tests/git/test_git.py | 115 +++++ gitlint/tests/git/test_git_commit.py | 535 ++++++++++++++++++++ gitlint/tests/git/test_git_context.py | 89 ++++ gitlint/tests/rules/__init__.py | 0 gitlint/tests/rules/test_body_rules.py | 180 +++++++ gitlint/tests/rules/test_configuration_rules.py | 71 +++ gitlint/tests/rules/test_meta_rules.py | 50 ++ gitlint/tests/rules/test_rules.py | 18 + gitlint/tests/rules/test_title_rules.py | 154 ++++++ gitlint/tests/rules/test_user_rules.py | 223 +++++++++ gitlint/tests/samples/commit_message/fixup | 1 + gitlint/tests/samples/commit_message/merge | 3 + gitlint/tests/samples/commit_message/revert | 3 + gitlint/tests/samples/commit_message/sample1 | 14 + gitlint/tests/samples/commit_message/sample2 | 1 + gitlint/tests/samples/commit_message/sample3 | 6 + gitlint/tests/samples/commit_message/sample4 | 7 + gitlint/tests/samples/commit_message/sample5 | 7 + gitlint/tests/samples/commit_message/squash | 3 + gitlint/tests/samples/config/gitlintconfig | 15 + gitlint/tests/samples/config/invalid-option-value | 11 + gitlint/tests/samples/config/no-sections | 1 + .../samples/config/nonexisting-general-option | 13 + gitlint/tests/samples/config/nonexisting-option | 11 + gitlint/tests/samples/config/nonexisting-rule | 11 + gitlint/tests/samples/user_rules/bogus-file.txt | 2 + .../user_rules/import_exception/invalid_python.py | 3 + .../user_rules/incorrect_linerule/my_line_rule.py | 10 + .../tests/samples/user_rules/my_commit_rules.foo | 16 + .../tests/samples/user_rules/my_commit_rules.py | 26 + .../samples/user_rules/parent_package/__init__.py | 13 + .../user_rules/parent_package/my_commit_rules.py | 12 + gitlint/tests/test_cache.py | 57 +++ gitlint/tests/test_display.py | 74 +++ gitlint/tests/test_hooks.py | 136 ++++++ gitlint/tests/test_lint.py | 197 ++++++++ gitlint/tests/test_options.py | 179 +++++++ gitlint/tests/test_utils.py | 78 +++ 61 files changed, 4275 insertions(+) create mode 100644 gitlint/tests/__init__.py create mode 100644 gitlint/tests/base.py create mode 100644 gitlint/tests/cli/test_cli.py create mode 100644 gitlint/tests/cli/test_cli_hooks.py create mode 100644 gitlint/tests/config/test_config.py create mode 100644 gitlint/tests/config/test_config_builder.py create mode 100644 gitlint/tests/config/test_config_precedence.py create mode 100644 gitlint/tests/config/test_rule_collection.py create mode 100644 gitlint/tests/contrib/__init__.py create mode 100644 gitlint/tests/contrib/test_contrib_rules.py create mode 100644 gitlint/tests/contrib/test_conventional_commit.py create mode 100644 gitlint/tests/contrib/test_signedoff_by.py create mode 100644 gitlint/tests/expected/test_cli/test_contrib_1 create mode 100644 gitlint/tests/expected/test_cli/test_debug_1 create mode 100644 gitlint/tests/expected/test_cli/test_input_stream_1 create mode 100644 gitlint/tests/expected/test_cli/test_input_stream_debug_1 create mode 100644 gitlint/tests/expected/test_cli/test_input_stream_debug_2 create mode 100644 gitlint/tests/expected/test_cli/test_lint_multiple_commits_1 create mode 100644 gitlint/tests/expected/test_cli/test_lint_multiple_commits_config_1 create mode 100644 gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_1 create mode 100644 gitlint/tests/expected/test_cli/test_lint_staged_msg_filename_2 create mode 100644 gitlint/tests/expected/test_cli/test_lint_staged_stdin_1 create mode 100644 gitlint/tests/expected/test_cli/test_lint_staged_stdin_2 create mode 100644 gitlint/tests/git/test_git.py create mode 100644 gitlint/tests/git/test_git_commit.py create mode 100644 gitlint/tests/git/test_git_context.py create mode 100644 gitlint/tests/rules/__init__.py create mode 100644 gitlint/tests/rules/test_body_rules.py create mode 100644 gitlint/tests/rules/test_configuration_rules.py create mode 100644 gitlint/tests/rules/test_meta_rules.py create mode 100644 gitlint/tests/rules/test_rules.py create mode 100644 gitlint/tests/rules/test_title_rules.py create mode 100644 gitlint/tests/rules/test_user_rules.py create mode 100644 gitlint/tests/samples/commit_message/fixup create mode 100644 gitlint/tests/samples/commit_message/merge create mode 100644 gitlint/tests/samples/commit_message/revert create mode 100644 gitlint/tests/samples/commit_message/sample1 create mode 100644 gitlint/tests/samples/commit_message/sample2 create mode 100644 gitlint/tests/samples/commit_message/sample3 create mode 100644 gitlint/tests/samples/commit_message/sample4 create mode 100644 gitlint/tests/samples/commit_message/sample5 create mode 100644 gitlint/tests/samples/commit_message/squash create mode 100644 gitlint/tests/samples/config/gitlintconfig create mode 100644 gitlint/tests/samples/config/invalid-option-value create mode 100644 gitlint/tests/samples/config/no-sections create mode 100644 gitlint/tests/samples/config/nonexisting-general-option create mode 100644 gitlint/tests/samples/config/nonexisting-option create mode 100644 gitlint/tests/samples/config/nonexisting-rule create mode 100644 gitlint/tests/samples/user_rules/bogus-file.txt create mode 100644 gitlint/tests/samples/user_rules/import_exception/invalid_python.py create mode 100644 gitlint/tests/samples/user_rules/incorrect_linerule/my_line_rule.py create mode 100644 gitlint/tests/samples/user_rules/my_commit_rules.foo create mode 100644 gitlint/tests/samples/user_rules/my_commit_rules.py create mode 100644 gitlint/tests/samples/user_rules/parent_package/__init__.py create mode 100644 gitlint/tests/samples/user_rules/parent_package/my_commit_rules.py create mode 100644 gitlint/tests/test_cache.py create mode 100644 gitlint/tests/test_display.py create mode 100644 gitlint/tests/test_hooks.py create mode 100644 gitlint/tests/test_lint.py create mode 100644 gitlint/tests/test_options.py create mode 100644 gitlint/tests/test_utils.py (limited to 'gitlint/tests') diff --git a/gitlint/tests/__init__.py b/gitlint/tests/__init__.py new file mode 100644 index 0000000..e69de29 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 + "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", + # git log --pretty + 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 + u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + # git log --pretty + 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 + u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + # git log --pretty + 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 + 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 + "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", + # git log --pretty + 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 + u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + # git log --pretty + 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 + u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + # git log --pretty + 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 + 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 + "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", + # git log --pretty + 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 + u"commit-1/file-1\ncommit-1/file-2\n", # git diff-tree + # git log --pretty + 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 + u"commit-2/file-1\ncommit-2/file-2\n", # git diff-tree + # git log --pretty + 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 + 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 + 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 + "25053ccec5e28e1bb8f7551fdbb5ab213ada2401\n" + "4da2656b0dadc76c7ee3fd0243a96cb64007f125\n", + # git log --pretty + 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 + 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 + 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 + 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)), "") + 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)), "") + 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)), "") + 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 '.