コード例 #1
0
def test_statement_to_comment(context_gen):
    comment_statments = (
        line_in_context('# a comment line', context_gen(1)),
        line_in_context('#', context_gen(2)),
        line_in_context('', context_gen(3)),
    )
    expected_objects = (
        comment(' a comment line', context_gen(1)),
        comment('', context_gen(2)),
        comment('', context_gen(3)),
    )
    for sttmt, expected in _it.izip(comment_statments, expected_objects):
        result = statement_to_comment(sttmt)
        assert expected == result

    bad_statements = (
        line_in_context('  # indented comment', context_gen(4)),
        line_in_context('command arg1:arg2', context_gen(5)),
        line_in_context('command', context_gen(6)),
        line_in_context('command args  # not a comment', context_gen(7)),
    )
    exception_contexts = tuple(context_gen(i) for i in range(4, 8))
    for sttmt, exp_ctx in _it.izip(bad_statements, exception_contexts):
        with pytest.raises(VirtSyntaxError) as err:
            statement_to_comment(sttmt)
        assert exp_ctx == err.value.context
コード例 #2
0
def test_comment(some_context):
    txt = 'some comment text'
    cmnt = comment(text=txt, context=some_context)
    assert cmnt.text == txt
    assert cmnt.context == some_context
    cmnt = comment(txt, some_context)
    assert cmnt.text == txt
    assert cmnt.context == some_context
コード例 #3
0
def test_statement_to_object(file_name):
    statements = add_context(file_name, (
        '# some comment',
        'a_command with:args',
    ))
    expected_objects = (
        comment(' some comment', context(file_name, 1)),
        command('a_command', 'with:args', context(file_name, 2)),
    )
    for sttmt, expected in _it.izip(statements, expected_objects):
        result = statement_to_object(sttmt)
        assert expected == result
コード例 #4
0
def test_lines_to_ast(file_name):
    lines = (
        '# some comment\n',
        'a_command with:multi\\\n',
        'line args\n',
        'a_command with:args\n',
    )
    expected = (
        comment(' some comment', context(file_name, 7)),
        command('a_command', 'with:multi\nline args', context(file_name, 8)),
        command('a_command', 'with:args', context(file_name, 10)),
    )
    result = tuple(
        lines_to_ast(lines, context_file=file_name, context_start_at=7)
    )
    assert expected == result
コード例 #5
0
def test_perform_ast_includes(file_name, fake_open):
    files = {
        'file1.inc': dedent(
            """
            run /file1/arg1
            write /file1/arg2:long\\
            input
            """
        ).lstrip(),
        'incdir/file2.inc': dedent(
            """
            # file2 comment
            run /file2/script
            commands-from-file file3.inc
            commands-from-shell \\
                echo "# shell ctx: $VIRT_COMMAND_FILE ($VIRT_COMMAND_LINE)" \\
                echo "root-password password:foo"
            write /last/file2:command
            """
        ).lstrip(),
        'incdir/file3.inc': dedent(
            """
            # file3 comment
            run file3_command
            """
        ).lstrip(),
        'file4': dedent(
            """
            # will be included from script
            """
        ).lstrip(),
    }
    ast = lines_to_ast(StringIO(dedent(
        """
        # The main AST
        commands-from-file file1.inc
        commands-from-file incdir/file2.inc
        commands-from-shell \\
            echo "# shell comment" \\
            echo commands-from-file file4
        selinux-relabel
        """
    ).lstrip()), context_file=file_name)
    expected = (
        comment(' The main AST', context(file_name, 1)),
        # commands-from-file file1.inc
        command('run', '/file1/arg1', context('file1.inc', 1)),
        command('write', '/file1/arg2:long\ninput', context('file1.inc', 2)),
        # commands-from-file file2.inc
        comment(' file2 comment', context('incdir/file2.inc', 1)),
        command('run', '/file2/script', context('incdir/file2.inc', 2)),
        # commands-from-file file3.inc (in file2)
        comment(' file3 comment', context('incdir/file3.inc', 1)),
        command('run', 'file3_command', context('incdir/file3.inc', 2)),
        # commands-from-shell (in file2)
        comment(
            " shell ctx: incdir/file2.inc (4)",
            nested_context(
                'script<incdir/file2.inc (4)>', 1,
                context('incdir/file2.inc', 4)
            )
        ),
        command(
            'root-password', 'password:foo',
            nested_context(
                'script<incdir/file2.inc (4)>', 2,
                context('incdir/file2.inc', 4)
            )
        ),
        # back to file2
        command(
            'write', '/last/file2:command',
            context('incdir/file2.inc', 7)
        ),
        # commands-from-shell (in main ast)
        comment(
            " shell comment",
            nested_context(
                'script<{0} (4)>'.format(file_name), 1,
                context(file_name, 4)
            ),
        ),
        # commands-from-file file4
        comment(' will be included from script', context('file4', 1)),
        # back to main
        command('selinux-relabel', None, context(file_name, 7)),
    )
    with fake_open(files):
        result = tuple(perform_ast_includes(ast))
    assert expected == result