Skip to content

Commit

Permalink
Avoid exceptions post syntax errors
Browse files Browse the repository at this point in the history
Fixes: #1012
  • Loading branch information
ssbarnea committed Feb 6, 2021
1 parent d1d4322 commit d43509b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
File renamed without changes.
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

0 comments on commit d43509b

Please sign in to comment.