Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid exceptions post syntax errors #1310

Merged
merged 1 commit into from Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/playbooks/syntax-error.yml
@@ -0,0 +1,6 @@
---
- name: This should raise syntax-error
hosts: localhost
tasks:
debug:
msg: "Note that `tasks` is not entered as a list."
8 changes: 6 additions & 2 deletions src/ansiblelint/runner.py
Expand Up @@ -110,9 +110,13 @@ def worker(lintable: Lintable) -> List[MatchError]:
for data in return_list:
matches.extend(data)

matches.extend(self._emit_matches(files))
# -- phase 2 ---
if not matches: # do our processing only when ansible syntax check passed
if not matches:

# do our processing only when ansible syntax check passed in order
# to avoid causing runtime exceptions. Our processing is not as
# relisient to be able process garbage.
matches.extend(self._emit_matches(files))

# remove duplicates from files list
files = [value for n, value in enumerate(files) if value not in files[:n]]
Expand Down
17 changes: 13 additions & 4 deletions test/TestExamples.py
@@ -1,4 +1,6 @@
"""Assure samples produced desire outcomes."""
import pytest

from ansiblelint.runner import Runner


Expand All @@ -10,11 +12,18 @@ def test_example(default_rules_collection):
assert len(result) == 15


def test_example_plain_string(default_rules_collection):
@pytest.mark.parametrize(
"filename",
(
pytest.param(
'examples/playbooks/syntax-error-string.yml', id='syntax-error-string'
),
pytest.param('examples/playbooks/syntax-error.yml', id='syntax-error'),
),
)
def test_example_syntax_error(default_rules_collection, filename):
"""Validates that loading valid YAML string produce error."""
result = Runner(
'examples/playbooks/plain_string.yml', rules=default_rules_collection
).run()
result = Runner(filename, rules=default_rules_collection).run()
assert len(result) >= 1
passed = False
for match in result:
Expand Down