Skip to content

Commit

Permalink
Hide stacktrace when loading invalid yaml (ansible#3844)
Browse files Browse the repository at this point in the history
  • Loading branch information
audgirka authored Oct 16, 2023
1 parent 0b52844 commit 721d1d5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
11 changes: 11 additions & 0 deletions examples/playbooks/invalid-transform.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 721d1d5

Please sign in to comment.