summaryrefslogtreecommitdiffstats
path: root/gitlint/tests/contrib/test_contrib_rules.py
blob: 3fa40481b773e5068967daf71a594282d40672ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
import os

from gitlint.tests.base import BaseTestCase
from gitlint.contrib import rules as contrib_rules
from gitlint.tests import contrib as contrib_tests
from gitlint import rule_finder, rules

from gitlint.utils import ustr


class ContribRuleTests(BaseTestCase):

    CONTRIB_DIR = os.path.dirname(os.path.realpath(contrib_rules.__file__))

    def test_contrib_tests_exist(self):
        """ Tests that every contrib rule file has an associated test file.
            While this doesn't guarantee that every contrib rule has associated tests (as we don't check the content
            of the tests file), it's a good leading indicator. """

        contrib_tests_dir = os.path.dirname(os.path.realpath(contrib_tests.__file__))
        contrib_test_files = os.listdir(contrib_tests_dir)

        # Find all python files in the contrib dir and assert there's a corresponding test file
        for filename in os.listdir(self.CONTRIB_DIR):
            if filename.endswith(".py") and filename not in ["__init__.py"]:
                expected_test_file = ustr(u"test_" + filename)
                error_msg = u"Every Contrib Rule must have associated tests. " + \
                            "Expected test file {0} not found.".format(os.path.join(contrib_tests_dir,
                                                                                    expected_test_file))
                self.assertIn(expected_test_file, contrib_test_files, error_msg)

    def test_contrib_rule_naming_conventions(self):
        """ Tests that contrib rules follow certain naming conventions.
            We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
            because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
            again.
        """
        rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)

        for clazz in rule_classes:
            # Contrib rule names start with "contrib-"
            self.assertTrue(clazz.name.startswith("contrib-"))

            # Contrib line rules id's start with "CL"
            if issubclass(clazz, rules.LineRule):
                if clazz.target == rules.CommitMessageTitle:
                    self.assertTrue(clazz.id.startswith("CT"))
                elif clazz.target == rules.CommitMessageBody:
                    self.assertTrue(clazz.id.startswith("CB"))

    def test_contrib_rule_uniqueness(self):
        """ Tests that all contrib rules have unique identifiers.
            We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
            because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
            again.
        """
        rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)

        # Not very efficient way of checking uniqueness, but it works :-)
        class_names = [rule_class.name for rule_class in rule_classes]
        class_ids = [rule_class.id for rule_class in rule_classes]
        self.assertEqual(len(set(class_names)), len(class_names))
        self.assertEqual(len(set(class_ids)), len(class_ids))

    def test_contrib_rule_instantiated(self):
        """ Tests that all contrib rules can be instantiated without errors. """
        rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)

        # No exceptions = what we want :-)
        for rule_class in rule_classes:
            rule_class()