From 5d944979077694f52a6b62579663e221db6bcf06 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 2 Jun 2021 13:55:10 +0100 Subject: [PATCH] Remove include missing file rule As current versions of Ansible do correctly detect missing imports and raise load-failure errors, we no longer need our own rule for it. --- .../rules/IncludeMissingFileRule.py | 82 ------------------- test/TestIncludeMissingFileRule.py | 80 ------------------ test/TestRulesCollection.py | 2 +- 3 files changed, 1 insertion(+), 163 deletions(-) delete mode 100644 src/ansiblelint/rules/IncludeMissingFileRule.py delete mode 100644 test/TestIncludeMissingFileRule.py diff --git a/src/ansiblelint/rules/IncludeMissingFileRule.py b/src/ansiblelint/rules/IncludeMissingFileRule.py deleted file mode 100644 index aa34938ffe..0000000000 --- a/src/ansiblelint/rules/IncludeMissingFileRule.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright (c) 2020, Joachim Lusiardi -# Copyright (c) 2020, Ansible Project - -import os.path -from typing import TYPE_CHECKING, List - -import ansible.parsing.yaml.objects - -from ansiblelint.rules import AnsibleLintRule - -if TYPE_CHECKING: - from typing import Any - - from ansiblelint.constants import odict - from ansiblelint.errors import MatchError - from ansiblelint.file_utils import Lintable - - -class IncludeMissingFileRule(AnsibleLintRule): - id = 'missing-import' - shortdesc = 'referenced files must exist' - description = ( - 'All files referenced by include / import tasks ' - 'must exist. The check excludes files with jinja2 ' - 'templates in the filename.' - ) - severity = 'MEDIUM' - tags = ['idiom'] - version_added = 'v4.3.0' - - def matchplay( - self, file: "Lintable", data: "odict[str, Any]" - ) -> List["MatchError"]: - results = [] - - # avoid failing with a playbook having tasks: null - for task in data.get('tasks', []) or []: - - # ignore None tasks or - # if the id of the current rule is not in list of skipped rules for this play - if not task or self.id in task.get('skipped_rules', ()): - continue - - # collect information which file was referenced for include / import - referenced_file = None - for key, val in task.items(): - if not ( - key.startswith('include_') - or key.startswith('import_') - or key == 'include' - ): - continue - if isinstance(val, ansible.parsing.yaml.objects.AnsibleMapping): - referenced_file = val.get('file', None) - else: - referenced_file = val - # take the file and skip the remaining keys - if referenced_file: - break - - if referenced_file is None or file.dir is None: - continue - - # make sure we have a absolute path here and check if it is a file - referenced_file = os.path.join(file.dir, referenced_file) - - # skip if this is a jinja2 templated reference - if '{{' in referenced_file: - continue - - # existing files do not produce any error - if os.path.isfile(referenced_file): - continue - - results.append( - self.create_matcherror( - filename=task['__file__'], - linenumber=task['__line__'], - details=referenced_file, - ) - ) - return results diff --git a/test/TestIncludeMissingFileRule.py b/test/TestIncludeMissingFileRule.py deleted file mode 100644 index 00534424d0..0000000000 --- a/test/TestIncludeMissingFileRule.py +++ /dev/null @@ -1,80 +0,0 @@ -import pytest - -from ansiblelint.file_utils import Lintable -from ansiblelint.runner import Runner - -PLAY_INCLUDING_PLAIN = Lintable( - 'playbook.yml', - u'''\ -- hosts: all - tasks: - - include: some_file.yml -''', -) - -PLAY_INCLUDING_JINJA2 = Lintable( - 'playbook.yml', - u'''\ -- hosts: all - tasks: - - include: "{{ some_path }}/some_file.yml" -''', -) - -PLAY_INCLUDING_NOQA = Lintable( - 'playbook.yml', - u'''\ -- hosts: all - tasks: - - include: some_file.yml # noqa missing-import -''', -) - -PLAY_INCLUDED = Lintable( - 'some_file.yml', - u'''\ -- debug: - msg: 'was found & included' -''', -) - -PLAY_HAVING_TASK = Lintable( - 'playbook.yml', - u'''\ -- name: Play - hosts: all - pre_tasks: - tasks: - - name: Ping - ping: -''', -) - - -@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: Runner) -> None: - results = str(runner.run()) - assert 'referenced files must exist' 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: Runner) -> None: - results = str(runner.run()) - assert 'referenced missing file in' not in results diff --git a/test/TestRulesCollection.py b/test/TestRulesCollection.py index 4d29362019..2b15671616 100644 --- a/test/TestRulesCollection.py +++ b/test/TestRulesCollection.py @@ -144,4 +144,4 @@ def test_rules_id_format() -> None: assert rule_id_re.match( rule.id ), f"R rule id {rule.id} did not match our required format." - assert len(rules) == 41 + assert len(rules) == 40