From a2aa51f5702b18016c25d943499941323952704d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 19 Nov 2022 15:52:46 +0100 Subject: Adding upstream version 0.18.0. Signed-off-by: Daniel Baumann --- gitlint-core/gitlint/tests/config/test_config.py | 109 ++++++++++++++--------- 1 file changed, 69 insertions(+), 40 deletions(-) (limited to 'gitlint-core/gitlint/tests/config/test_config.py') diff --git a/gitlint-core/gitlint/tests/config/test_config.py b/gitlint-core/gitlint/tests/config/test_config.py index c3fd78a..852bf75 100644 --- a/gitlint-core/gitlint/tests/config/test_config.py +++ b/gitlint-core/gitlint/tests/config/test_config.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from unittest.mock import patch from gitlint import rules @@ -9,16 +7,15 @@ from gitlint.tests.base import BaseTestCase 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) + 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) + 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() @@ -26,18 +23,20 @@ class LintConfigTests(BaseTestCase): # non-existing rule expected_error_msg = "No such rule 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): - config.set_rule_option(u'föobar', u'lïne-length', 60) + config.set_rule_option("föobar", "lïne-length", 60) # non-existing option expected_error_msg = "Rule 'title-max-length' has no option 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): - config.set_rule_option('title-max-length', u'föobar', 60) + config.set_rule_option("title-max-length", "föobar", 60) # invalid option value - expected_error_msg = "'föo' is not a valid value for option 'title-max-length.line-length'. " + \ - "Option 'line-length' must be a positive integer (current value: 'föo')." + expected_error_msg = ( + "'föo' is not a valid value for option 'title-max-length.line-length'. " + "Option 'line-length' must be a positive integer (current value: 'föo')." + ) with self.assertRaisesMessage(LintConfigError, expected_error_msg): - config.set_rule_option('title-max-length', 'line-length', "föo") + config.set_rule_option("title-max-length", "line-length", "föo") def test_set_general_option(self): config = LintConfig() @@ -45,12 +44,14 @@ class LintConfigTests(BaseTestCase): # Check that default general options are correct self.assertTrue(config.ignore_merge_commits) self.assertTrue(config.ignore_fixup_commits) + self.assertTrue(config.ignore_fixup_amend_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.fail_without_commits) + self.assertFalse(config.regex_style_search) self.assertFalse(config.debug) self.assertEqual(config.verbosity, 3) active_rule_classes = tuple(type(rule) for rule in config.rules) @@ -76,6 +77,10 @@ class LintConfigTests(BaseTestCase): config.set_general_option("ignore-fixup-commits", "false") self.assertFalse(config.ignore_fixup_commits) + # ignore_fixup_amend_commit + config.set_general_option("ignore-fixup-amend-commits", "false") + self.assertFalse(config.ignore_fixup_amend_commits) + # ignore_squash_commit config.set_general_option("ignore-squash-commits", "false") self.assertFalse(config.ignore_squash_commits) @@ -100,6 +105,10 @@ class LintConfigTests(BaseTestCase): config.set_general_option("fail-without-commits", "true") self.assertTrue(config.fail_without_commits) + # regex-style-search + config.set_general_option("regex-style-search", "true") + self.assertTrue(config.regex_style_search) + # target config.set_general_option("target", self.SAMPLES_DIR) self.assertEqual(config.target, self.SAMPLES_DIR) @@ -118,8 +127,8 @@ class LintConfigTests(BaseTestCase): self.assertTrue(actual_rule.is_contrib) self.assertEqual(str(type(actual_rule)), "") - self.assertEqual(actual_rule.id, 'CT1') - self.assertEqual(actual_rule.name, u'contrib-title-conventional-commits') + self.assertEqual(actual_rule.id, "CT1") + self.assertEqual(actual_rule.name, "contrib-title-conventional-commits") self.assertEqual(actual_rule.target, rules.CommitMessageTitle) expected_rule_option = options.ListOption( @@ -129,15 +138,15 @@ class LintConfigTests(BaseTestCase): ) self.assertListEqual(actual_rule.options_spec, [expected_rule_option]) - self.assertDictEqual(actual_rule.options, {'types': 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(str(type(actual_rule)), "") - self.assertEqual(actual_rule.id, 'CC1') - self.assertEqual(actual_rule.name, u'contrib-body-requires-signed-off-by') + self.assertEqual(actual_rule.id, "CC1") + self.assertEqual(actual_rule.name, "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") @@ -157,7 +166,7 @@ class LintConfigTests(BaseTestCase): # UserRuleError, RuleOptionError should be re-raised as LintConfigErrors side_effects = [rules.UserRuleError("üser-rule"), options.RuleOptionError("rüle-option")] for side_effect in side_effects: - with patch('gitlint.config.rule_finder.find_rule_classes', side_effect=side_effect): + with patch("gitlint.config.rule_finder.find_rule_classes", side_effect=side_effect): with self.assertRaisesMessage(LintConfigError, str(side_effect)): config.contrib = "contrib-title-conventional-commits" @@ -166,15 +175,15 @@ class LintConfigTests(BaseTestCase): 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') + actual_rule = config.rules.find_rule("UC1") self.assertTrue(actual_rule.is_user_defined) self.assertEqual(str(type(actual_rule)), "") - self.assertEqual(actual_rule.id, 'UC1') - self.assertEqual(actual_rule.name, u'my-üser-commit-rule') + self.assertEqual(actual_rule.id, "UC1") + self.assertEqual(actual_rule.name, "my-üser-commit-rule") self.assertEqual(actual_rule.target, None) - expected_rule_option = options.IntOption('violation-count', 1, "Number of violåtions to return") + expected_rule_option = options.IntOption("violation-count", 1, "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}) + 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) @@ -189,8 +198,9 @@ class LintConfigTests(BaseTestCase): config.extra_path = "föo/bar" # extra path contains classes with errors - with self.assertRaisesMessage(LintConfigError, - "User-defined rule class 'MyUserLineRule' must have a 'validate' method"): + with self.assertRaisesMessage( + 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): @@ -218,31 +228,37 @@ class LintConfigTests(BaseTestCase): config.verbosity = value # invalid ignore_xxx_commits - ignore_attributes = ["ignore_merge_commits", "ignore_fixup_commits", "ignore_squash_commits", - "ignore_revert_commits"] + ignore_attributes = [ + "ignore_merge_commits", + "ignore_fixup_commits", + "ignore_fixup_amend_commits", + "ignore_squash_commits", + "ignore_revert_commits", + ] incorrect_values = [-1, 4, "föo"] for attribute in ignore_attributes: for value in incorrect_values: option_name = attribute.replace("_", "-") - with self.assertRaisesMessage(LintConfigError, - f"Option '{option_name}' must be either 'true' or 'false'"): + with self.assertRaisesMessage( + LintConfigError, f"Option '{option_name}' must be either 'true' or 'false'" + ): 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', 'fail_without_commits']: + for attribute in ["debug", "staged", "ignore_stdin", "fail_without_commits", "regex_style_search"]: option_name = attribute.replace("_", "-") - with self.assertRaisesMessage(LintConfigError, - f"Option '{option_name}' must be either 'true' or 'false'"): + with self.assertRaisesMessage(LintConfigError, f"Option '{option_name}' must be either 'true' or 'false'"): setattr(config, attribute, "föobar") # extra-path has its own negative test # invalid target - with self.assertRaisesMessage(LintConfigError, - "Option target must be an existing directory (current value: 'föo/bar')"): + with self.assertRaisesMessage( + LintConfigError, "Option target must be an existing directory (current value: 'föo/bar')" + ): config.target = "föo/bar" def test_ignore_independent_from_rules(self): @@ -259,12 +275,25 @@ class LintConfigTests(BaseTestCase): self.assertNotEqual(LintConfig(), LintConfigGenerator()) # Ensure LintConfig are not equal if they differ on their attributes - attrs = [("verbosity", 1), ("rules", []), ("ignore_stdin", True), ("debug", True), - ("ignore", ["T1"]), ("staged", True), ("_config_path", self.get_sample_path()), - ("ignore_merge_commits", False), ("ignore_fixup_commits", False), - ("ignore_squash_commits", False), ("ignore_revert_commits", False), - ("extra_path", self.get_sample_path("user_rules")), ("target", self.get_sample_path()), - ("contrib", ["CC1"])] + attrs = [ + ("verbosity", 1), + ("rules", []), + ("ignore_stdin", True), + ("fail_without_commits", True), + ("regex_style_search", True), + ("debug", True), + ("ignore", ["T1"]), + ("staged", True), + ("_config_path", self.get_sample_path()), + ("ignore_merge_commits", False), + ("ignore_fixup_commits", False), + ("ignore_fixup_amend_commits", False), + ("ignore_squash_commits", False), + ("ignore_revert_commits", False), + ("extra_path", self.get_sample_path("user_rules")), + ("target", self.get_sample_path()), + ("contrib", ["CC1"]), + ] for attr, val in attrs: config = LintConfig() setattr(config, attr, val) @@ -281,7 +310,7 @@ class LintConfigTests(BaseTestCase): class LintConfigGeneratorTests(BaseTestCase): @staticmethod - @patch('gitlint.config.shutil.copyfile') + @patch("gitlint.config.shutil.copyfile") def test_install_commit_msg_hook_negative(copy): LintConfigGenerator.generate_config("föo/bar/test") copy.assert_called_with(GITLINT_CONFIG_TEMPLATE_SRC_PATH, "föo/bar/test") -- cgit v1.2.3