diff options
Diffstat (limited to 'gitlint/tests/config')
-rw-r--r-- | gitlint/tests/config/test_config.py | 62 | ||||
-rw-r--r-- | gitlint/tests/config/test_config_builder.py | 81 | ||||
-rw-r--r-- | gitlint/tests/config/test_config_precedence.py | 26 |
3 files changed, 131 insertions, 38 deletions
diff --git a/gitlint/tests/config/test_config.py b/gitlint/tests/config/test_config.py index d3fdc2c..b981a86 100644 --- a/gitlint/tests/config/test_config.py +++ b/gitlint/tests/config/test_config.py @@ -30,18 +30,18 @@ class LintConfigTests(BaseTestCase): # non-existing rule expected_error_msg = u"No such rule 'föobar'" - with self.assertRaisesRegex(LintConfigError, expected_error_msg): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(LintConfigError, expected_error_msg): config.set_rule_option('title-max-length', 'line-length', u"föo") def test_set_general_option(self): @@ -124,7 +124,7 @@ class LintConfigTests(BaseTestCase): expected_rule_option = options.ListOption( "types", - ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert"], + ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert", "ci", "build"], "Comma separated list of allowed commit types.", ) @@ -151,14 +151,14 @@ class LintConfigTests(BaseTestCase): 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."): + with self.assertRaisesMessage(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)): + with self.assertRaisesMessage(LintConfigError, ustr(side_effect)): config.contrib = u"contrib-title-conventional-commits" def test_extra_path(self): @@ -185,36 +185,36 @@ class LintConfigTests(BaseTestCase): 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): + with self.assertRaisesMessage(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"): + 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): 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"): + with self.assertRaisesMessage(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"): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(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"): + with self.assertRaisesMessage(LintConfigError, "Option 'verbosity' must be set between 0 and 3"): config.verbosity = value # invalid ignore_xxx_commits @@ -224,8 +224,8 @@ class LintConfigTests(BaseTestCase): 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)): + with self.assertRaisesMessage(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 @@ -234,15 +234,15 @@ class LintConfigTests(BaseTestCase): # 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)): + with self.assertRaisesMessage(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')"): + with self.assertRaisesMessage(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): @@ -254,6 +254,30 @@ class LintConfigTests(BaseTestCase): self.assertEqual(config.ignore, ["T1", "T2"]) self.assertSequenceEqual(config.rules, original_rules) + def test_config_equality(self): + self.assertEqual(LintConfig(), LintConfig()) + 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"])] + for attr, val in attrs: + config = LintConfig() + setattr(config, attr, val) + self.assertNotEqual(LintConfig(), config) + + # Other attributes don't matter + config1 = LintConfig() + config2 = LintConfig() + config1.foo = u"bår" + self.assertEqual(config1, config2) + config2.foo = u"dūr" + self.assertEqual(config1, config2) + class LintConfigGeneratorTests(BaseTestCase): @staticmethod diff --git a/gitlint/tests/config/test_config_builder.py b/gitlint/tests/config/test_config_builder.py index 051a52f..5a28c9f 100644 --- a/gitlint/tests/config/test_config_builder.py +++ b/gitlint/tests/config/test_config_builder.py @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- +import copy from gitlint.tests.base import BaseTestCase from gitlint.config import LintConfig, LintConfigBuilder, LintConfigError +from gitlint import rules + class LintConfigBuilderTests(BaseTestCase): def test_set_option(self): @@ -88,12 +91,13 @@ class LintConfigBuilderTests(BaseTestCase): # 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): + with self.assertRaisesMessage(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." + # We only match the start of the message here, since the exact message can vary depending on platform with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(path) @@ -102,7 +106,7 @@ class LintConfigBuilderTests(BaseTestCase): 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): + with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # non-existing general option @@ -110,7 +114,7 @@ class LintConfigBuilderTests(BaseTestCase): 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): + with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # non-existing option @@ -118,7 +122,7 @@ class LintConfigBuilderTests(BaseTestCase): 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): + with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # invalid option value @@ -127,7 +131,7 @@ class LintConfigBuilderTests(BaseTestCase): 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): + with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() def test_set_config_from_string_list(self): @@ -150,27 +154,27 @@ class LintConfigBuilderTests(BaseTestCase): # 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'"): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(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): + with self.assertRaisesMessage(LintConfigError, expected_msg): config_builder.set_config_from_string_list([u'föobar=1']) def test_rebuild_config(self): @@ -201,3 +205,60 @@ class LintConfigBuilderTests(BaseTestCase): # 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) + + def test_named_rules(self): + # Store a copy of the default rules from the config, so we can reference it later + config_builder = LintConfigBuilder() + config = config_builder.build() + default_rules = copy.deepcopy(config.rules) + self.assertEqual(default_rules, config.rules) # deepcopy should be equal + + # Add a named rule by setting an option in the config builder that follows the named rule pattern + # Assert that whitespace in the rule name is stripped + rule_qualifiers = [u'T7:my-extra-rüle', u' T7 : my-extra-rüle ', u'\tT7:\tmy-extra-rüle\t', + u'T7:\t\n \tmy-extra-rüle\t\n\n', u"title-match-regex:my-extra-rüle"] + for rule_qualifier in rule_qualifiers: + config_builder = LintConfigBuilder() + config_builder.set_option(rule_qualifier, 'regex', u"föo") + + expected_rules = copy.deepcopy(default_rules) + my_rule = rules.TitleRegexMatches({'regex': u"föo"}) + my_rule.id = rules.TitleRegexMatches.id + u":my-extra-rüle" + my_rule.name = rules.TitleRegexMatches.name + u":my-extra-rüle" + expected_rules._rules[u'T7:my-extra-rüle'] = my_rule + self.assertEqual(config_builder.build().rules, expected_rules) + + # assert that changing an option on the newly added rule is passed correctly to the RuleCollection + # we try this with all different rule qualifiers to ensure they all are normalized and map + # to the same rule + for other_rule_qualifier in rule_qualifiers: + cb = config_builder.clone() + cb.set_option(other_rule_qualifier, 'regex', other_rule_qualifier + u"bōr") + # before setting the expected rule option value correctly, the RuleCollection should be different + self.assertNotEqual(cb.build().rules, expected_rules) + # after setting the option on the expected rule, it should be equal + my_rule.options['regex'].set(other_rule_qualifier + u"bōr") + self.assertEqual(cb.build().rules, expected_rules) + my_rule.options['regex'].set(u"wrong") + + def test_named_rules_negative(self): + # T7 = title-match-regex + # Invalid rule name + for invalid_name in ["", " ", " ", "\t", "\n", u"å b", u"å:b", u"åb:", u":åb"]: + config_builder = LintConfigBuilder() + config_builder.set_option(u"T7:{0}".format(invalid_name), 'regex', u"tëst") + expected_msg = u"The rule-name part in 'T7:{0}' cannot contain whitespace, colons or be empty" + with self.assertRaisesMessage(LintConfigError, expected_msg.format(invalid_name)): + config_builder.build() + + # Invalid parent rule name + config_builder = LintConfigBuilder() + config_builder.set_option(u"Ž123:foöbar", u"fåke-option", u"fåke-value") + with self.assertRaisesMessage(LintConfigError, u"No such rule 'Ž123' (named rule: 'Ž123:foöbar')"): + config_builder.build() + + # Invalid option name (this is the same as with regular rules) + config_builder = LintConfigBuilder() + config_builder.set_option(u"T7:foöbar", u"blå", u"my-rëgex") + with self.assertRaisesMessage(LintConfigError, u"Rule 'T7:foöbar' has no option 'blå'"): + config_builder.build() diff --git a/gitlint/tests/config/test_config_precedence.py b/gitlint/tests/config/test_config_precedence.py index 9689e55..a0eeccd 100644 --- a/gitlint/tests/config/test_config_precedence.py +++ b/gitlint/tests/config/test_config_precedence.py @@ -25,40 +25,48 @@ 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") + @patch('gitlint.cli.get_stdin_data', return_value=u"WIP:fö\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 + # 2. environment variables + # 3. commandline -c flags + # 4. config file + # 5. 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") + self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") - # 2. commandline -c flags + # 2. environment variables + with patch('gitlint.display.stderr', new=StringIO()) as stderr: + result = self.cli.invoke(cli.cli, ["-c", "general.verbosity=2", "--config", config_path], + env={"GITLINT_VERBOSITY": "3"}) + self.assertEqual(result.output, "") + self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") + + # 3. 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 + # 4. 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 + # 5. 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") + self.assertEqual(stderr.getvalue(), u"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP:fö\"\n") @patch('gitlint.cli.get_stdin_data', return_value=u"WIP: This is å test") def test_ignore_precedence(self, get_stdin_data): |