From c1b41f27edcc5aa38676c4310b1560c6e923d152 Mon Sep 17 00:00:00 2001 From: Shatakshi Mishra Date: Wed, 23 Aug 2023 17:27:52 +0530 Subject: [PATCH] Use yaml.load_all for multiple yaml document (#3679) --- .github/workflows/tox.yml | 2 +- .../playbooks/multi_yaml_doc.transformed.yml | 23 +++++++++++++++++++ examples/playbooks/multi_yaml_doc.yml | 23 +++++++++++++++++++ src/ansiblelint/yaml_utils.py | 6 ++++- test/test_transformer.py | 6 +++++ 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 examples/playbooks/multi_yaml_doc.transformed.yml create mode 100644 examples/playbooks/multi_yaml_doc.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index a2f62e5ed5..204145a44d 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -71,7 +71,7 @@ jobs: WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY # Number of expected test passes, safety measure for accidental skip of # tests. Update value if you add/remove tests. - PYTEST_REQPASS: 812 + PYTEST_REQPASS: 813 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/examples/playbooks/multi_yaml_doc.transformed.yml b/examples/playbooks/multi_yaml_doc.transformed.yml new file mode 100644 index 0000000000..ab1e02f5c6 --- /dev/null +++ b/examples/playbooks/multi_yaml_doc.transformed.yml @@ -0,0 +1,23 @@ +--- +- name: First problematic play + hosts: localhost + tasks: + - name: Echo a message + ansible.builtin.shell: echo hello # <-- command-instead-of-shell + changed_when: false +--- +- name: second problematic play # <-- name[casing] + hosts: localhost + tasks: + - name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent +--- +- name: Third problematic play + hosts: localhost + tasks: + - name: Remove file (delete file) + file: # <-- fqcn[action-core] + path: /etc/foo.txt + state: absent diff --git a/examples/playbooks/multi_yaml_doc.yml b/examples/playbooks/multi_yaml_doc.yml new file mode 100644 index 0000000000..ab1e02f5c6 --- /dev/null +++ b/examples/playbooks/multi_yaml_doc.yml @@ -0,0 +1,23 @@ +--- +- name: First problematic play + hosts: localhost + tasks: + - name: Echo a message + ansible.builtin.shell: echo hello # <-- command-instead-of-shell + changed_when: false +--- +- name: second problematic play # <-- name[casing] + hosts: localhost + tasks: + - name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent +--- +- name: Third problematic play + hosts: localhost + tasks: + - name: Remove file (delete file) + file: # <-- fqcn[action-core] + path: /etc/foo.txt + state: absent diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 9fd1487448..8d159c818d 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -14,6 +14,7 @@ import ruamel.yaml.events from ruamel.yaml.comments import CommentedMap, CommentedSeq, Format +from ruamel.yaml.composer import ComposerError from ruamel.yaml.constructor import RoundTripConstructor from ruamel.yaml.emitter import Emitter, ScalarAnalysis @@ -935,7 +936,10 @@ def loads(self, stream: str) -> Any: # https://sourceforge.net/p/ruamel-yaml/tickets/460/ text, preamble_comment = self._pre_process_yaml(stream) - data = self.load(stream=text) + try: + data = self.load(stream=text) + except ComposerError: + data = self.load_all(stream=text) if preamble_comment is not None and isinstance( data, (CommentedMap, CommentedSeq), diff --git a/test/test_transformer.py b/test/test_transformer.py index f66c96bbcd..61e8112948 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -84,6 +84,12 @@ def fixture_runner_result( pytest.param("examples/playbooks/vars/empty.yml", 1, False, id="empty"), pytest.param("examples/playbooks/name-case.yml", 1, True, id="name_case"), pytest.param("examples/playbooks/fqcn.yml", 3, True, id="fqcn"), + pytest.param( + "examples/playbooks/multi_yaml_doc.yml", + 1, + False, + id="multi_yaml_doc", + ), pytest.param( "examples/playbooks/transform_command_instead_of_shell.yml", 3,