Beispiel #1
0
def _run(buffer, conf, filepath):
    assert hasattr(buffer, '__getitem__'), \
        '_run() argument must be a buffer, not a stream'

    first_line = next(parser.line_generator(buffer)).content
    if re.match(r'^#\s*yamllint disable-file\s*$', first_line):
        return

    # If the document contains a syntax error, save it and yield it at the
    # right line
    syntax_error = get_syntax_error(buffer)

    for problem in get_cosmetic_problems(buffer, conf, filepath):
        # Insert the syntax error (if any) at the right place...
        if (syntax_error and syntax_error.line <= problem.line
                and syntax_error.column <= problem.column):
            yield syntax_error

            # If there is already a yamllint error at the same place, discard
            # it as it is probably redundant (and maybe it's just a 'warning',
            # in which case the script won't even exit with a failure status).
            if (syntax_error.line == problem.line
                    and syntax_error.column == problem.column):
                syntax_error = None
                continue

            syntax_error = None

        yield problem

    if syntax_error:
        yield syntax_error
Beispiel #2
0
    def test_line_generator(self):
        e = list(line_generator(''))
        self.assertEqual(len(e), 1)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].start, 0)
        self.assertEqual(e[0].end, 0)

        e = list(line_generator('\n'))
        self.assertEqual(len(e), 2)

        e = list(line_generator(' \n'))
        self.assertEqual(len(e), 2)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].start, 0)
        self.assertEqual(e[0].end, 1)

        e = list(line_generator('\n\n'))
        self.assertEqual(len(e), 3)

        e = list(line_generator('---\n'
                                'this is line 1\n'
                                'line 2\n'
                                '\n'
                                '3\n'))
        self.assertEqual(len(e), 6)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].content, '---')
        self.assertEqual(e[2].content, 'line 2')
        self.assertEqual(e[3].content, '')
        self.assertEqual(e[5].line_no, 6)

        e = list(line_generator('test with\n'
                                'no newline\n'
                                'at the end'))
        self.assertEqual(len(e), 3)
        self.assertEqual(e[2].line_no, 3)
        self.assertEqual(e[2].content, 'at the end')
Beispiel #3
0
    def test_line_generator(self):
        e = list(line_generator(''))
        self.assertEqual(len(e), 1)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].start, 0)
        self.assertEqual(e[0].end, 0)

        e = list(line_generator('\n'))
        self.assertEqual(len(e), 2)

        e = list(line_generator(' \n'))
        self.assertEqual(len(e), 2)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].start, 0)
        self.assertEqual(e[0].end, 1)

        e = list(line_generator('\n\n'))
        self.assertEqual(len(e), 3)

        e = list(line_generator('---\n'
                                'this is line 1\n'
                                'line 2\n'
                                '\n'
                                '3\n'))
        self.assertEqual(len(e), 6)
        self.assertEqual(e[0].line_no, 1)
        self.assertEqual(e[0].content, '---')
        self.assertEqual(e[2].content, 'line 2')
        self.assertEqual(e[3].content, '')
        self.assertEqual(e[5].line_no, 6)

        e = list(line_generator('test with\n'
                                'no newline\n'
                                'at the end'))
        self.assertEqual(len(e), 3)
        self.assertEqual(e[2].line_no, 3)
        self.assertEqual(e[2].content, 'at the end')