Example #1
0
    def test_named_rules(self):
        """ Test that when named rules are present, both them and the original (non-named) rules executed """

        lint_config = LintConfig()
        for rule_name in [u"my-ïd", u"another-rule-ïd"]:
            rule_id = TitleMustNotContainWord.id + ":" + rule_name
            lint_config.rules.add_rule(TitleMustNotContainWord, rule_id)
            lint_config.set_rule_option(rule_id, "words", [u"Föo"])
            linter = GitLinter(lint_config)

        violations = [
            RuleViolation("T5",
                          u"Title contains the word 'WIP' (case-insensitive)",
                          u"WIP: Föo bar", 1),
            RuleViolation(u"T5:another-rule-ïd",
                          u"Title contains the word 'Föo' (case-insensitive)",
                          u"WIP: Föo bar", 1),
            RuleViolation(u"T5:my-ïd",
                          u"Title contains the word 'Föo' (case-insensitive)",
                          u"WIP: Föo bar", 1)
        ]
        self.assertListEqual(
            violations,
            linter.lint(
                self.gitcommit(u"WIP: Föo bar\n\nFoo bår hur dur bla bla")))
Example #2
0
    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(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)
Example #4
0
    def test_lint_configuration_rule(self):
        # Test that all rules are ignored because of matching regex
        lint_config = LintConfig()
        lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")

        linter = GitLinter(lint_config)
        violations = linter.lint(
            self.gitcommit(self.get_sample("commit_message/sample2")))
        self.assertListEqual(violations, [])

        # Test ignoring only certain rules
        lint_config = LintConfig()
        lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")
        lint_config.set_rule_option("I1", "ignore", "B6")

        linter = GitLinter(lint_config)
        violations = linter.lint(
            self.gitcommit(self.get_sample("commit_message/sample2")))

        # Normally we'd expect a B6 violation, but that one is skipped because of the specific ignore set above
        expected = [
            RuleViolation("T5",
                          "Title contains the word 'WIP' (case-insensitive)",
                          u"Just a title contåining WIP", 1)
        ]

        self.assertListEqual(violations, expected)

        # Test ignoring body lines
        lint_config = LintConfig()
        linter = GitLinter(lint_config)
        lint_config.set_rule_option("I3", "regex", u"(.*)tråiling(.*)")
        violations = linter.lint(
            self.gitcommit(self.get_sample("commit_message/sample1")))
        expected_errors = [
            RuleViolation(
                "T3", "Title has trailing punctuation (.)",
                u"Commit title contåining 'WIP', as well as trailing punctuation.",
                1),
            RuleViolation(
                "T5", "Title contains the word 'WIP' (case-insensitive)",
                u"Commit title contåining 'WIP', as well as trailing punctuation.",
                1),
            RuleViolation("B4", "Second line is not empty",
                          "This line should be empty", 2),
            RuleViolation(
                "B1", "Line exceeds max length (135>80)",
                "This is the first line of the commit message body and it is meant to test "
                +
                "a line that exceeds the maximum line length of 80 characters.",
                3),
            RuleViolation("B2", "Line has trailing whitespace",
                          "This line has a trailing tab.\t", 4),
            RuleViolation("B3", "Line contains hard tab characters (\\t)",
                          "This line has a trailing tab.\t", 4)
        ]

        self.assertListEqual(violations, expected_errors)
Example #5
0
    def test_lint_regex_rules(self):
        """ Additional test for title-match-regex, body-match-regex """
        commit = self.gitcommit(
            self.get_sample("commit_message/no-violations"))
        lintconfig = LintConfig()
        linter = GitLinter(lintconfig)
        violations = linter.lint(commit)
        # No violations by default
        self.assertListEqual(violations, [])

        # Matching regexes shouldn't be a problem
        rule_regexes = [("title-match-regex", u"Tïtle$"),
                        ("body-match-regex", u"Sïgned-Off-By: (.*)$")]
        for rule_regex in rule_regexes:
            lintconfig.set_rule_option(rule_regex[0], "regex", rule_regex[1])
            violations = linter.lint(commit)
            self.assertListEqual(violations, [])

        # Non-matching regexes should return violations
        rule_regexes = [("title-match-regex", ), ("body-match-regex", )]
        lintconfig.set_rule_option("title-match-regex", "regex", u"^Tïtle")
        lintconfig.set_rule_option("body-match-regex", "regex",
                                   u"Sügned-Off-By: (.*)$")
        expected_violations = [
            RuleViolation("T7", u"Title does not match regex (^Tïtle)",
                          u"Normal Commit Tïtle", 1),
            RuleViolation("B8",
                          u"Body does not match regex (Sügned-Off-By: (.*)$)",
                          None, 6)
        ]
        violations = linter.lint(commit)
        self.assertListEqual(violations, expected_violations)
Example #6
0
    def test_set_rule_option_negative(self):
        config = LintConfig()

        # non-existing rule
        expected_error_msg = "No such rule 'foobar'"
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('foobar', 'line-length', 60)

        # non-existing option
        expected_error_msg = "Rule 'title-max-length' has no option 'foobar'"
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('title-max-length', 'foobar', 60)

        # invalid option value
        expected_error_msg = "'foo' is not a valid value for option 'title-max-length.line-length'. " + \
                             "Option 'line-length' must be a positive integer \(current value: 'foo'\)."
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('title-max-length', 'line-length', "foo")

        # invalid verbosity
        with self.assertRaisesRegexp(LintConfigError,
                                     "verbosity must be set between 0 and 3"):
            config.verbosity = -1
        with self.assertRaisesRegexp(LintConfigError,
                                     "verbosity must be set between 0 and 3"):
            config.verbosity = 4
Example #7
0
    def test_lint_configuration_rule(self):
        # Test that all rules are ignored because of matching regex
        lint_config = LintConfig()
        lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")

        linter = GitLinter(lint_config)
        violations = linter.lint(
            self.gitcommit(self.get_sample("commit_message/sample2")))
        self.assertListEqual(violations, [])

        # Test ignoring only certain rules
        lint_config = LintConfig()
        lint_config.set_rule_option("I1", "regex", "^Just a title(.*)")
        lint_config.set_rule_option("I1", "ignore", "B6")

        linter = GitLinter(lint_config)
        violations = linter.lint(
            self.gitcommit(self.get_sample("commit_message/sample2")))

        # Normally we'd expect a B6 violation, but that one is skipped because of the specific ignore set above
        expected = [
            RuleViolation("T5",
                          "Title contains the word 'WIP' (case-insensitive)",
                          u"Just a title contåining WIP", 1)
        ]

        self.assertListEqual(violations, expected)
Example #8
0
    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_rule_option_negative(self):
        config = LintConfig()

        # non-existing rule
        expected_error_msg = "No such rule 'foobar'"
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('foobar', 'line-length', 60)

        # non-existing option
        expected_error_msg = "Rule 'title-max-length' has no option 'foobar'"
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('title-max-length', 'foobar', 60)

        # invalid option value
        expected_error_msg = "'foo' is not a valid value for option 'title-max-length.line-length'. " + \
                             r"Option 'line-length' must be a positive integer \(current value: 'foo'\)."
        with self.assertRaisesRegexp(LintConfigError, expected_error_msg):
            config.set_rule_option('title-max-length', 'line-length', "foo")