Пример #1
0
 def test_failed_module_import(self):
     # test importing a bogus module
     user_rule_path = self.get_sample_path("user_rules/import_exception")
     # We don't check the entire error message because that is different based on the python version and underlying
     # operating system
     expected_msg = "Error while importing extra-path module 'invalid_python'"
     with self.assertRaisesRegex(UserRuleError, expected_msg):
         find_rule_classes(user_rule_path)
Пример #2
0
 def test_failed_module_import(self):
     # test importing a bogus module
     user_rule_path = self.get_sample_path("user_rules/import_exception")
     # We don't check the entire error message because that is different based on the python version and underlying
     # operating system
     expected_msg = "Error while importing extra-path module 'invalid_python'"
     with self.assertRaisesRegex(UserRuleError, expected_msg):
         find_rule_classes(user_rule_path)
Пример #3
0
 def test_assert_valid_rule_class_negative(self):
     # general test to make sure that incorrect rules will raise an exception
     user_rule_path = self.get_sample_path("user_rules/incorrect_linerule")
     with self.assertRaisesRegex(
             UserRuleError,
             "User-defined rule class 'MyUserLineRule' must have a 'validate' method"
     ):
         find_rule_classes(user_rule_path)
Пример #4
0
    def extra_path(self, value):
        try:
            if self.extra_path:
                self._extra_path.set(value)
            else:
                self._extra_path = options.PathOption(
                    'extra-path', value,
                    "Path to a directory or module with extra user-defined rules",
                    type='both'
                )

            # Make sure we unload any previously loaded extra-path rules
            for rule in self.rules:
                if hasattr(rule, 'user_defined') and rule.user_defined:
                    del self._rules[rule.id]

            # Find rules in the new extra-path
            rule_classes = user_rules.find_rule_classes(self.extra_path)

            # Add the newly found rules to the existing rules
            for rule_class in rule_classes:
                rule_obj = rule_class()
                rule_obj.user_defined = True
                self._rules[rule_class.id] = rule_obj

        except (options.RuleOptionError, user_rules.UserRuleError) as e:
            raise LintConfigError(ustr(e))
Пример #5
0
    def extra_path(self, value):
        try:
            if self.extra_path:
                self._extra_path.set(value)
            else:
                self._extra_path = options.DirectoryOption('extra-path', value,
                                                           "Path to a directory with extra user-defined rules")

            # Make sure we unload any previously loaded extra-path rules
            for rule in self.rules:
                if hasattr(rule, 'user_defined') and rule.user_defined:
                    del self._rules[rule.id]

            # Find rules in the new extra-path
            rule_classes = user_rules.find_rule_classes(self.extra_path)

            # Add the newly found rules to the existing rules
            for rule_class in rule_classes:
                rule_obj = rule_class()
                rule_obj.user_defined = True
                self._rules[rule_class.id] = rule_obj

        except options.RuleOptionError as e:
            raise LintConfigError(str(e))
        except Exception as e:
            # TODO(joris.roovers): better error message
            raise e
Пример #6
0
    def test_empty_user_classes(self):
        # Test that we don't find rules if we scan a different directory
        user_rule_path = self.get_sample_path("config")
        classes = find_rule_classes(user_rule_path)
        self.assertListEqual(classes, [])

        # Importantly, ensure that the directory is not added to the syspath as this happens only when we actually
        # find modules
        self.assertNotIn(user_rule_path, sys.path)
Пример #7
0
    def test_empty_user_classes(self):
        # Test that we don't find rules if we scan a different directory
        user_rule_path = self.get_sample_path("config")
        classes = find_rule_classes(user_rule_path)
        self.assertListEqual(classes, [])

        # Importantly, ensure that the directory is not added to the syspath as this happens only when we actually
        # find modules
        self.assertNotIn(user_rule_path, sys.path)
Пример #8
0
    def test_extra_path_specified_by_file(self):
        # Test that find_rule_classes can handle an extra path given as a file name instead of a directory
        user_rule_path = self.get_sample_path("user_rules")
        user_rule_module = os.path.join(user_rule_path, "my_commit_rules.py")
        classes = find_rule_classes(user_rule_module)

        rule_class = classes[0]()
        violations = rule_class.validate("false-commit-object (ignored)")
        self.assertListEqual(violations, [
            rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)
        ])
Пример #9
0
    def extra_path(self, value):
        try:
            self._extra_path.set(value)
            rule_classes = user_rules.find_rule_classes(self.extra_path)

            # Add the newly found rules to the existing rules
            for rule_class in rule_classes:
                self.rule_classes.append(rule_class)
                self._rules[rule_class.id] = rule_class()

        except options.RuleOptionError as e:
            raise LintConfigError(str(e))
        except Exception as e:
            # TODO(joris.roovers): better error message
            raise e
Пример #10
0
    def test_find_rule_classes(self):
        # Let's find some user classes!
        user_rule_path = self.get_sample_path("user_rules")
        classes = find_rule_classes(user_rule_path)

        # Compare string representations because we can't import MyUserCommitRule here since samples/user_rules is not
        # a proper python package
        # Note that the following check effectively asserts that:
        # - There is only 1 rule recognized and it is MyUserCommitRule
        # - Other non-python files in the directory are ignored
        # - Other members of the my_commit_rules module are ignored
        #  (such as func_should_be_ignored, global_variable_should_be_ignored)
        # - Rules are loaded non-recursively (user_rules/import_exception directory is ignored)
        self.assertEqual("[<class 'my_commit_rules.MyUserCommitRule'>]",
                         ustr(classes))

        # Assert that we added the new user_rules directory to the system path and modules
        self.assertIn(user_rule_path, sys.path)
        self.assertIn("my_commit_rules", sys.modules)

        # # Do some basic asserts on our user rule
        self.assertEqual(classes[0].id, "UC1")
        self.assertEqual(classes[0].name, u"my-üser-commit-rule")
        expected_option = options.IntOption('violation-count', 1,
                                            u"Number of violåtions to return")
        self.assertListEqual(classes[0].options_spec, [expected_option])
        self.assertTrue(hasattr(classes[0], "validate"))

        # Test that we can instantiate the class and can execute run the validate method and that it returns the
        # expected result
        rule_class = classes[0]()
        violations = rule_class.validate("false-commit-object (ignored)")
        self.assertListEqual(violations, [
            rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1)
        ])

        # Have it return more violations
        rule_class.options['violation-count'].value = 2
        violations = rule_class.validate("false-commit-object (ignored)")
        self.assertListEqual(violations, [
            rules.RuleViolation("UC1", u"Commit violåtion 1", u"Contënt 1", 1),
            rules.RuleViolation("UC1", u"Commit violåtion 2", u"Contënt 2", 2)
        ])
Пример #11
0
    def test_find_rule_classes(self):
        # Let's find some user classes!
        user_rule_path = self.get_sample_path("user_rules")
        classes = find_rule_classes(user_rule_path)

        # Compare string representations because we can't import MyUserCommitRule here since samples/user_rules is not
        # a proper python package
        # Note that the following check effectively asserts that:
        # - There is only 1 rule recognized and it is MyUserCommitRule
        # - Other non-python files in the directory are ignored
        # - Other members of the my_commit_rules module are ignored
        #  (such as func_should_be_ignored, global_variable_should_be_ignored)
        # - Rules are loaded non-recursively (user_rules/import_exception directory is ignored)
        self.assertEqual("[<class 'my_commit_rules.MyUserCommitRule'>]", str(classes))

        # Assert that we added the new user_rules directory to the system path and modules
        self.assertIn(user_rule_path, sys.path)
        self.assertIn("my_commit_rules", sys.modules)

        # Do some basic asserts on our user rule
        self.assertEqual(classes[0].id, "TUC1")
        self.assertEqual(classes[0].name, "my-user-commit-rule")
        expected_option = options.IntOption('violation-count', 1, "Number of violations to return")
        self.assertListEqual(classes[0].options_spec, [expected_option])
        self.assertTrue(hasattr(classes[0], "validate"))

        # Test that we can instantiate the class and can execute run the validate method and that it returns the
        # expected result
        rule_class = classes[0]()
        violations = rule_class.validate("false-commit-object (ignored)")
        self.assertListEqual(violations, [rules.RuleViolation("TUC1", "Commit violation 1", "Content 1", 1)])

        # Have it return more violations
        rule_class.options['violation-count'].value = 2
        violations = rule_class.validate("false-commit-object (ignored)")
        self.assertListEqual(violations, [rules.RuleViolation("TUC1", "Commit violation 1", "Content 1", 1),
                                          rules.RuleViolation("TUC1", "Commit violation 2", "Content 2", 2)])
Пример #12
0
 def test_find_rule_classes_nonexisting_path(self):
     with self.assertRaisesRegex(UserRuleError,
                                 u"Invalid extra-path: föo/bar"):
         find_rule_classes(u"föo/bar")
Пример #13
0
 def test_find_rule_classes_nonexisting_path(self):
     # When searching an non-existing path, we expect an OSError. That's fine because this case will be caught by
     # the CLI (you cannot specify a non-existing directory). What we do here is just assert that we indeed
     # get an OSError (so we guard against regressions).
     with self.assertRaisesRegex(OSError, "No such file or directory"):
         find_rule_classes(u"föo/bar")
Пример #14
0
 def test_find_rule_classes_nonexisting_path(self):
     # When searching an non-existing path, we expect an OSError. That's fine because this case will be caught by
     # the CLI (you cannot specify a non-existing directory). What we do here is just assert that we indeed
     # get an OSError (so we guard against regressions).
     with self.assertRaisesRegex(OSError, "No such file or directory"):
         find_rule_classes("foo/bar")