diff --git a/tests/test_rules.py b/tests/test_rules.py
new file mode 100644
index 0000000..6114c8c
--- /dev/null
+++ b/tests/test_rules.py
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2020 Satoru SATOH
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+import unittest
+
+try:
+ from unittest import mock
+except ImportError: # for python 2.7
+ mock = False
+
+from tests.plugins import example
+
+import yamllint.rules
+
+
+RULE_NEVER_EXISTS = "rule_never_exists"
+PLUGIN_RULES = example.RULES_MAP
+
+
+class TestCase(unittest.TestCase):
+ """Test cases for yamllint.rules.__init__.*.
+ """
+ def test_get_default_rule(self):
+ self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
+ yamllint.rules.braces)
+
+ def test_get_rule_does_not_exist(self):
+ with self.assertRaises(ValueError):
+ yamllint.rules.get(RULE_NEVER_EXISTS)
+
+
+@unittest.skipIf(not mock, "unittest.mock is not available")
+class TestCaseUsingMock(unittest.TestCase):
+ """Test cases for yamllint.rules.__init__.* using mock.
+ """
+ def test_get_default_rule_with_plugins(self):
+ with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
+ self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
+ yamllint.rules.braces)
+
+ def test_get_plugin_rules(self):
+ plugin_rule_id = example.override_comments.ID
+ plugin_rule_mod = example.override_comments
+
+ with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
+ self.assertEqual(yamllint.rules.get(plugin_rule_id),
+ plugin_rule_mod)
+
+ def test_get_rule_does_not_exist_with_plugins(self):
+ with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
+ with self.assertRaises(ValueError):
+ yamllint.rules.get(RULE_NEVER_EXISTS)
diff --git a/yamllint/rules/__init__.py b/yamllint/rules/__init__.py
index a084d6e..84d07f5 100644
--- a/yamllint/rules/__init__.py
+++ b/yamllint/rules/__init__.py
@@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
+import yamllint.plugins
from yamllint.rules import (
braces,
brackets,
@@ -62,9 +63,14 @@ _RULES = {
truthy.ID: truthy,
}
+_EXTERNAL_RULES = yamllint.plugins.get_plugin_rules_map()
-def get(id):
- if id not in _RULES:
- raise ValueError('no such rule: "%s"' % id)
- return _RULES[id]
+def get(rule_id):
+ if rule_id in _RULES:
+ return _RULES[rule_id]
+
+ if rule_id in _EXTERNAL_RULES:
+ return _EXTERNAL_RULES[rule_id]
+
+ raise ValueError('no such rule: "%s"' % rule_id)