Beispiel #1
0
    def matchlines(self, file, text) -> List[MatchError]:
        matches: List[MatchError] = []
        if not self.match:
            return matches
        # arrays are 0-based, line numbers are 1-based
        # so use prev_line_no as the counter
        for (prev_line_no, line) in enumerate(text.split("\n")):
            if line.lstrip().startswith('#'):
                continue

            rule_id_list = get_rule_skips_from_line(line)
            if self.id in rule_id_list:
                continue

            result = self.match(file, line)
            if not result:
                continue
            message = None
            if isinstance(result, str):
                message = result
            m = MatchError(message=message,
                           linenumber=prev_line_no + 1,
                           details=line,
                           filename=file['path'],
                           rule=self)
            matches.append(m)
        return matches
Beispiel #2
0
    def matchyaml(self, file: Lintable) -> List["MatchError"]:
        """Return matches found for a specific YAML text."""
        matches: List["MatchError"] = []
        filtered_matches = []
        if file.kind == 'role':
            return matches

        if YamllintRule.config:
            for p in run_yamllint(file.content, YamllintRule.config):
                matches.append(
                    self.create_matcherror(
                        message=p.desc,
                        linenumber=p.line,
                        details="",
                        filename=str(file.path),
                        tag=p.rule,
                    )
                )

        if matches:
            lines = file.content.splitlines()
            for match in matches:
                # rule.linenumber starts with 1, not zero
                skip_list = get_rule_skips_from_line(lines[match.linenumber - 1])
                # print(skip_list)
                if match.rule.id not in skip_list and match.tag not in skip_list:
                    filtered_matches.append(match)
        return filtered_matches
    def matchyaml(self, file: Lintable) -> List["MatchError"]:
        """Return matches found for a specific YAML text."""
        matches: List["MatchError"] = []
        filtered_matches: List["MatchError"] = []
        if file.base_kind != 'text/yaml':
            return matches

        if YamllintRule.config:
            for p in run_yamllint(file.content,
                                  YamllintRule.config,
                                  filepath=file.path):
                self.severity = 'VERY_LOW'
                if p.level == "error":
                    self.severity = 'MEDIUM'
                if p.desc.endswith("(syntax)"):
                    self.severity = 'VERY_HIGH'
                matches.append(
                    self.create_matcherror(
                        message=p.desc,
                        linenumber=p.line,
                        details="",
                        filename=str(file.path),
                        tag=p.rule,
                    ))

        if matches:
            lines = file.content.splitlines()
            for match in matches:
                # rule.linenumber starts with 1, not zero
                skip_list = get_rule_skips_from_line(lines[match.linenumber -
                                                           1])
                # print(skip_list)
                if match.rule.id not in skip_list and match.tag not in skip_list:
                    filtered_matches.append(match)
        return filtered_matches
def test_get_rule_skips_from_line(line: str, expected: str) -> None:
    """Validate get_rule_skips_from_line."""
    x = get_rule_skips_from_line(line)
    assert x == [expected]
Beispiel #5
0
def test_get_rule_skips_from_line(line, expected):
    """Validate get_rule_skips_from_line."""
    x = get_rule_skips_from_line(line)
    assert x == [expected]