diff options
Diffstat (limited to 'gitlint/tests/config')
-rw-r--r-- | gitlint/tests/config/test_config.py | 263 | ||||
-rw-r--r-- | gitlint/tests/config/test_config_builder.py | 203 | ||||
-rw-r--r-- | gitlint/tests/config/test_config_precedence.py | 100 | ||||
-rw-r--r-- | gitlint/tests/config/test_rule_collection.py | 64 |
4 files changed, 630 insertions, 0 deletions
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") |