Exemplo n.º 1
0
    def matchyaml(self, file: str, text: str) -> List[Match]:
        matches: List[Match] = []
        if not self.matchplay:
            return matches

        yaml = ansiblelint.utils.parse_yaml_linenumbers(text, file['path'])
        if not yaml:
            return matches

        if isinstance(yaml, dict):
            yaml = [yaml]

        yaml = ansiblelint.skip_utils.append_skipped_rules(
            yaml, text, file['type'])

        for play in yaml:
            if self.id in play.get('skipped_rules', ()):
                continue

            result = self.matchplay(file, play)
            if not result:
                continue

            if isinstance(result, tuple):
                result = [result]

            if not isinstance(result, list):
                raise TypeError("{} is not a list".format(result))

            for section, message, *optional_linenumber in result:
                linenumber = self._matchplay_linenumber(
                    play, optional_linenumber)
                matches.append(
                    Match(linenumber, section, file['path'], self, message))
        return matches
Exemplo n.º 2
0
    def matchtasks(self, file: str, text: str) -> List[Match]:
        matches: List[Match] = []
        if not self.matchtask:
            return matches

        if file['type'] == 'meta':
            return matches

        yaml = ansiblelint.utils.parse_yaml_linenumbers(text, file['path'])
        if not yaml:
            return matches

        yaml = append_skipped_rules(yaml, text, file['type'])

        for task in ansiblelint.utils.get_normalized_tasks(yaml, file):
            if self.id in task.get('skipped_rules', ()):
                continue

            if 'action' not in task:
                continue
            result = self.matchtask(file, task)
            if not result:
                continue

            message = None
            if isinstance(result, str):
                message = result
            task_msg = "Task/Handler: " + ansiblelint.utils.task_to_str(task)
            matches.append(
                Match(task[ansiblelint.utils.LINE_NUMBER_KEY], task_msg,
                      file['path'], self, message))
        return matches
Exemplo n.º 3
0
    def matchyaml(self, file, text):
        matches = []
        if not self.matchplay:
            return matches

        yaml = ansiblelint.utils.parse_yaml_linenumbers(text, file['path'])
        if not yaml:
            return matches

        if isinstance(yaml, dict):
            yaml = [yaml]

        yaml = ansiblelint.skip_utils.append_skipped_rules(yaml, text, file['type'])

        for play in yaml:
            if self.id in play.get('skipped_rules', ()):
                continue

            result = self.matchplay(file, play)
            if not result:
                continue

            if isinstance(result, tuple):
                result = [result]

            if not isinstance(result, list):
                raise TypeError("{} is not a list".format(result))

            for section, message in result:
                matches.append(Match(play[ansiblelint.utils.LINE_NUMBER_KEY],
                                     section, file['path'], self, message))
        return matches
Exemplo n.º 4
0
    def matchlines(self, file, text) -> List[Match]:
        matches: List[Match] = []
        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
            matches.append(
                Match(prev_line_no + 1, line, file['path'], self, message))
        return matches
Exemplo n.º 5
0
 def test_dict_format_line(self):
     match = Match(1, {'hello': 'world'}, "filename.yml", self.rule, "xyz")
     self.formatter.format(match, True)
Exemplo n.º 6
0
 def test_unicode_format_string(self):
     match = Match(1, "hello", "filename.yml", self.rule, u'\U0001f427')
     self.formatter.format(match, False)
Exemplo n.º 7
0
 def test_format_coloured_string(self):
     match = Match(1, "hello", "filename.yml", self.rule, "message")
     self.formatter.format(match, True)