summaryrefslogtreecommitdiffstats
path: root/test/TestImportWithMalformed.py
blob: 4f5425cc58603241e07c7238e3cf328c1b5c71f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import namedtuple

import pytest

from ansiblelint.runner import Runner

PlayFile = namedtuple('PlayFile', ['name', 'content'])


IMPORT_TASKS_MAIN = PlayFile('import-tasks-main.yml', '''
- oops this is invalid
''')

IMPORT_SHELL_PIP = PlayFile('import-tasks-main.yml', '''
- shell: pip
''')

PLAY_IMPORT_TASKS = PlayFile('playbook.yml', '''
- hosts: all
  tasks:
    - import_tasks: import-tasks-main.yml
''')


@pytest.fixture
def play_file_path(tmp_path):
    p = tmp_path / 'playbook.yml'
    return str(p)


@pytest.fixture
def runner(play_file_path, default_rules_collection):
    return Runner(default_rules_collection, play_file_path, [], [], [])


@pytest.fixture
def _play_files(tmp_path, request):
    if request.param is None:
        return
    for play_file in request.param:
        p = tmp_path / play_file.name
        p.write_text(play_file.content)


@pytest.mark.parametrize(
    '_play_files',
    (
        pytest.param([IMPORT_SHELL_PIP, PLAY_IMPORT_TASKS], id='Import shell w/ pip'),
        pytest.param(
            [IMPORT_TASKS_MAIN, PLAY_IMPORT_TASKS],
            id='import_tasks w/ malformed import',
            marks=pytest.mark.xfail(
                reason='Garbage non-tasks sequence is not being '
                'properly processed. Ref: '
                'https://github.com/ansible/ansible-lint/issues/707',
                raises=AttributeError,
            ),
        ),
    ),
    indirect=['_play_files']
)
@pytest.mark.usefixtures('_play_files')
def test_import_tasks_with_malformed_import(runner):
    results = str(runner.run())
    assert 'only when shell functionality is required' in results