Example #1
0
    def test_assert_valid_rule_class_negative_parent(self):
        # rule class must extend from LineRule or CommitRule
        class MyRuleClass(object):
            pass

        expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule " + \
                       "or gitlint.rules.CommitRule"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)
Example #2
0
    def test_assert_valid_rule_class_negative_name(self):
        class MyRuleClass(rules.LineRule):
            id = "UC1"

        # Rule class must have an name
        expected_msg = "User-defined rule class 'MyRuleClass' must have a 'name' attribute"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # Rule names must be non-empty
        MyRuleClass.name = ""
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)
Example #3
0
    def test_assert_valid_rule_class_negative_option_spec(self):
        class MyRuleClass(rules.LineRule):
            id = "UC1"
            name = u"my-rüle-class"

        # if set, option_spec must be a list of gitlint options
        MyRuleClass.options_spec = u"föo"
        expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \
                       "of gitlint.options.RuleOption"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # option_spec is a list, but not of gitlint options
        MyRuleClass.options_spec = [u"föo", 123]  # pylint: disable=bad-option-value,redefined-variable-type
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)
Example #4
0
    def test_assert_valid_rule_class_negative_validate(self):
        class MyRuleClass(rules.LineRule):
            id = "UC1"
            name = u"my-rüle-class"

        with self.assertRaisesRegex(
                UserRuleError,
                "User-defined rule class 'MyRuleClass' must have a 'validate' method"
        ):
            assert_valid_rule_class(MyRuleClass)

        # validate attribute - not a method
        MyRuleClass.validate = u"föo"
        with self.assertRaisesRegex(
                UserRuleError,
                "User-defined rule class 'MyRuleClass' must have a 'validate' method"
        ):
            assert_valid_rule_class(MyRuleClass)
Example #5
0
    def test_assert_valid_rule_class(self):
        class MyLineRuleClass(rules.LineRule):
            id = 'UC1'
            name = u'my-lïne-rule'
            target = rules.CommitMessageTitle

            def validate(self):
                pass

        class MyCommitRuleClass(rules.CommitRule):
            id = 'UC2'
            name = u'my-cömmit-rule'

            def validate(self):
                pass

        # Just assert that no error is raised
        self.assertIsNone(assert_valid_rule_class(MyLineRuleClass))
        self.assertIsNone(assert_valid_rule_class(MyCommitRuleClass))
Example #6
0
    def test_assert_valid_rule_class_negative_target(self):
        class MyRuleClass(rules.LineRule):
            id = "UC1"
            name = u"my-rüle-class"

            def validate(self):
                pass

        # no target
        expected_msg = "The target attribute of the user-defined LineRule class 'MyRuleClass' must be either " + \
                       "gitlint.rules.CommitMessageTitle or gitlint.rules.CommitMessageBody"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # invalid target
        MyRuleClass.target = u"föo"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # valid target, no exception should be raised
        MyRuleClass.target = rules.CommitMessageTitle  # pylint: disable=bad-option-value,redefined-variable-type
        self.assertIsNone(assert_valid_rule_class(MyRuleClass))
Example #7
0
    def test_assert_valid_rule_class_negative_id(self):
        class MyRuleClass(rules.LineRule):
            pass

        # Rule class must have an id
        expected_msg = "User-defined rule class 'MyRuleClass' must have an 'id' attribute"
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # Rule ids must be non-empty
        MyRuleClass.id = ""
        with self.assertRaisesRegex(UserRuleError, expected_msg):
            assert_valid_rule_class(MyRuleClass)

        # Rule ids must not start with one of the reserved id letters
        for letter in ["T", "R", "B", "M"]:
            MyRuleClass.id = letter + "1"
            expected_msg = "The id '{0}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M"
            with self.assertRaisesRegex(UserRuleError,
                                        expected_msg.format(letter)):
                assert_valid_rule_class(MyRuleClass)
Example #8
0
    def test_assert_valid_rule_class_negative(self):
        class MyNormalClass(object):
            pass

        self.assertFalse(assert_valid_rule_class(MyNormalClass))
Example #9
0
    def test_assert_valid_rule_class(self):
        class MyRuleClass(rules.Rule):
            pass

        self.assertTrue(assert_valid_rule_class(MyRuleClass))