diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 45edc4dc32..c82a865eaf 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -70,7 +70,7 @@ jobs: env: # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 832 + PYTEST_REQPASS: 833 steps: - uses: actions/checkout@v4 with: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac49de5e31..5f76c92059 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,6 +44,7 @@ repos: examples/other/some.j2.yaml| examples/playbooks/collections/.*| examples/playbooks/example.yml| + examples/playbooks/invalid-transform.yml| examples/playbooks/multiline-brackets.*| examples/playbooks/templates/not-valid.yaml| examples/playbooks/vars/empty.transformed.yml| diff --git a/examples/playbooks/invalid-transform.yml b/examples/playbooks/invalid-transform.yml new file mode 100644 index 0000000000..3a1d50a54f --- /dev/null +++ b/examples/playbooks/invalid-transform.yml @@ -0,0 +1,11 @@ +# yamllint disable-file +--- +- name: Test + hosts: localhost + gather_facts: false + + tasks: + - name: Print hello message + ansible.builtin.debug: + msg: "Hello!" + register: vm_output diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 851f96ca13..98318ec94d 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -21,6 +21,7 @@ # Module 'ruamel.yaml' does not explicitly export attribute 'YAML'; implicit reexport disabled # To make the type checkers happy, we import from ruamel.yaml.main instead. from ruamel.yaml.main import YAML +from ruamel.yaml.parser import ParserError from ruamel.yaml.scalarint import ScalarInt from yamllint.config import YamlLintConfig @@ -953,6 +954,9 @@ def loads(self, stream: str) -> Any: data = self.load(stream=text) except ComposerError: data = self.load_all(stream=text) + except ParserError: + data = None + _logger.error("Invalid yaml, verify the file contents and try again.") if preamble_comment is not None and isinstance( data, (CommentedMap, CommentedSeq), diff --git a/test/test_transformer.py b/test/test_transformer.py index 525e37278b..759f9cbf80 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -162,6 +162,13 @@ def fixture_runner_result( False, id="github-workflow", ), + pytest.param( + "examples/playbooks/invalid-transform.yml", + 1, + False, + True, + id="invalid_transform", + ), ), ) @mock.patch.dict(os.environ, {"ANSIBLE_LINT_WRITE_TMP": "1"}, clear=True)