summaryrefslogtreecommitdiffstats
path: root/test/TestIncludeMissingFileRule.py
blob: 7248fdc791870c200484af54b845343d7d0ca083 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from collections import namedtuple

import pytest

from ansiblelint.runner import Runner

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


PLAY_INCLUDING_PLAIN = PlayFile('playbook.yml', u'''
- hosts: all
  tasks:
    - include: some_file.yml
''')

PLAY_INCLUDING_JINJA2 = PlayFile('playbook.yml', u'''
- hosts: all
  tasks:
    - include: "{{ some_path }}/some_file.yml"
''')

PLAY_INCLUDING_NOQA = PlayFile('playbook.yml', u'''
- hosts: all
  tasks:
    - include: some_file.yml # noqa 505
''')

PLAY_INCLUDED = PlayFile('some_file.yml', u'''
- debug:
    msg: 'was found & included'
''')

PLAY_HAVING_TASK = PlayFile('playbook.yml', u'''
- name: Play
  hosts: all
  pre_tasks:
  tasks:
    - name: Ping
      ping:
''')


@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([PLAY_INCLUDING_PLAIN], id='referenced file missing'), ),
    indirect=['_play_files']
)
@pytest.mark.usefixtures('_play_files')
def test_include_file_missing(runner):
    results = str(runner.run())
    assert 'referenced missing file in' in results
    assert 'playbook.yml' in results
    assert 'some_file.yml' in results


@pytest.mark.parametrize(
    '_play_files',
    (
        pytest.param([PLAY_INCLUDING_PLAIN, PLAY_INCLUDED], id='File Exists'),
        pytest.param([PLAY_INCLUDING_JINJA2], id='JINJA2 in reference'),
        pytest.param([PLAY_INCLUDING_NOQA], id='NOQA was used'),
        pytest.param([PLAY_HAVING_TASK], id='Having a task')
    ),
    indirect=['_play_files']
)
@pytest.mark.usefixtures('_play_files')
def test_cases_that_do_not_report(runner):
    results = str(runner.run())
    assert 'referenced missing file in' not in results