Ejemplo n.º 1
0
    def test_path_rules_specifier(self):
        for _, path_rules in PATH_RULES_SPECIFIER:
            validate_filter_rules(path_rules, self._all_categories())

        config = FilterConfiguration(path_specific=PATH_RULES_SPECIFIER)

        def assert_check(path, category):
            """Assert that the given category should be checked."""
            self.assertTrue(config.should_check(category, path))

        def assert_no_check(path, category):
            """Assert that the given category should not be checked."""
            message = ('Should not check category "%s" for path "%s".' %
                       (category, path))
            self.assertFalse(config.should_check(category, path), message)

        assert_check("random_path.cpp", "build/include")
        assert_check("random_path.cpp", "readability/naming")

        # Third-party Python code: blinkpy/third_party
        path = "tools/blinkpy/third_party/mock.py"
        assert_no_check(path, "build/include")
        assert_no_check(path, "pep8/E401")  # A random pep8 category.
        assert_check(path, "pep8/W191")
        assert_check(path, "pep8/W291")
        assert_check(path, "whitespace/carriage_return")
Ejemplo n.º 2
0
    def parse(self, args):
        """Parse the command line arguments to check_blink_style.py.

        Args:
          args: A list of command-line arguments as returned by sys.argv[1:].

        Returns:
          A tuple of (paths, options)

          paths: The list of paths to check.
          options: A CommandOptionValues instance.
        """
        (options, paths) = self._parser.parse_args(args=args)

        filter_value = options.filter_value
        git_commit = options.git_commit
        diff_files = options.diff_files
        is_verbose = options.is_verbose
        min_confidence = options.min_confidence
        output_format = options.output_format

        if filter_value is not None and not filter_value:
            # Then the user explicitly passed no filter, for
            # example "-f ''" or "--filter=".
            self._exit_with_categories()

        # Validate user-provided values.

        min_confidence = int(min_confidence)
        if (min_confidence < 1) or (min_confidence > 5):
            self._parse_error(
                'option --min-confidence: invalid integer: '
                '%s: value must be between 1 and 5' % min_confidence)

        if filter_value:
            filter_rules = self._parse_filter_flag(filter_value)
        else:
            filter_rules = []

        try:
            validate_filter_rules(filter_rules, self._all_categories)
        except ValueError as err:
            self._parse_error(err)

        options = CommandOptionValues(
            filter_rules=filter_rules,
            git_commit=git_commit,
            diff_files=diff_files,
            is_verbose=is_verbose,
            min_confidence=min_confidence,
            output_format=output_format)

        return (paths, options)
Ejemplo n.º 3
0
 def test_blink_base_filter_rules(self):
     base_filter_rules = _BASE_FILTER_RULES
     already_seen = []
     validate_filter_rules(base_filter_rules, self._all_categories())
     # Also do some additional checks.
     for rule in base_filter_rules:
         # Check no leading or trailing white space.
         self.assertEqual(rule, rule.strip())
         # All categories are on by default, so defaults should
         # begin with -.
         self.assertTrue(rule.startswith('-'))
         # Check no rule occurs twice.
         self.assertNotIn(rule, already_seen)
         already_seen.append(rule)
Ejemplo n.º 4
0
    def test_validate_filter_rules(self):
        all_categories = ["tabs", "whitespace", "build/include"]

        bad_rules = [
            "tabs",
            "*tabs",
            " tabs",
            " +tabs",
            "+whitespace/newline",
            "+xxx",
        ]

        good_rules = ["+tabs", "-tabs", "+build"]

        for rule in bad_rules:
            with self.assertRaises(ValueError):
                validate_filter_rules([rule], all_categories)

        for rule in good_rules:
            # This works: no error.
            validate_filter_rules([rule], all_categories)