diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 7500fd8a0d..0842b85372 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: 809 + PYTEST_REQPASS: 811 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 38ad069699..9fd1487448 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -695,12 +695,19 @@ def write_comment( ruamel.yaml.events.CollectionEndEvent, ruamel.yaml.events.DocumentEndEvent, ruamel.yaml.events.StreamEndEvent, + ruamel.yaml.events.MappingStartEvent, ), ) ): # drop pure whitespace pre comments # does not apply to End events since they consume one of the newlines. value = "" + elif ( + pre + and not value.strip() + and isinstance(self.event, ruamel.yaml.events.MappingStartEvent) + ): + value = self._re_repeat_blank_lines.sub("", value) elif pre: # preserve content in pre comment with at least one newline, # but no extra blank lines. diff --git a/test/fixtures/formatting-after/fmt-4.yml b/test/fixtures/formatting-after/fmt-4.yml new file mode 100644 index 0000000000..5ded596bfc --- /dev/null +++ b/test/fixtures/formatting-after/fmt-4.yml @@ -0,0 +1,22 @@ +--- +- name: Gather all legacy facts + cisco.ios.ios_facts: + +- name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + +- name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + +- name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/fixtures/formatting-after/fmt-5.yml b/test/fixtures/formatting-after/fmt-5.yml new file mode 100644 index 0000000000..b259e0e895 --- /dev/null +++ b/test/fixtures/formatting-after/fmt-5.yml @@ -0,0 +1,25 @@ +--- +- name: Test this playbook + hosts: all + tasks: + - name: Gather all legacy facts + cisco.ios.ios_facts: + + - name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + + - name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + + - name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/fixtures/formatting-before/fmt-4.yml b/test/fixtures/formatting-before/fmt-4.yml new file mode 100644 index 0000000000..579231ff17 --- /dev/null +++ b/test/fixtures/formatting-before/fmt-4.yml @@ -0,0 +1,25 @@ +--- +- name: Gather all legacy facts + cisco.ios.ios_facts: + +- name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + + +- name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + + + +- name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/fixtures/formatting-before/fmt-5.yml b/test/fixtures/formatting-before/fmt-5.yml new file mode 100644 index 0000000000..a9145e6f16 --- /dev/null +++ b/test/fixtures/formatting-before/fmt-5.yml @@ -0,0 +1,28 @@ +--- +- name: Test this playbook + hosts: all + tasks: + - name: Gather all legacy facts + cisco.ios.ios_facts: + + - name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + + + - name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + + + + - name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/fixtures/formatting-prettier/fmt-4.yml b/test/fixtures/formatting-prettier/fmt-4.yml new file mode 100644 index 0000000000..5ded596bfc --- /dev/null +++ b/test/fixtures/formatting-prettier/fmt-4.yml @@ -0,0 +1,22 @@ +--- +- name: Gather all legacy facts + cisco.ios.ios_facts: + +- name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + +- name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + +- name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/fixtures/formatting-prettier/fmt-5.yml b/test/fixtures/formatting-prettier/fmt-5.yml new file mode 100644 index 0000000000..b259e0e895 --- /dev/null +++ b/test/fixtures/formatting-prettier/fmt-5.yml @@ -0,0 +1,25 @@ +--- +- name: Test this playbook + hosts: all + tasks: + - name: Gather all legacy facts + cisco.ios.ios_facts: + + - name: Update modification and access time of given file + ansible.builtin.file: + path: /etc/some_file + state: file + modification_time: now + access_time: now + + - name: Disable ufw service + ansible.builtin.service: + name: ufw + enabled: false + state: stopped + when: '"ufw" in services' + + - name: Remove file (delete file) + ansible.builtin.file: + path: /etc/foo.txt + state: absent diff --git a/test/test_yaml_utils.py b/test/test_yaml_utils.py index 5546e582cc..6a6c7e7635 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -222,9 +222,11 @@ def fixture_yaml_formatting_fixtures(fixture_filename: str) -> tuple[str, str, s @pytest.mark.parametrize( "fixture_filename", ( - "fmt-1.yml", - "fmt-2.yml", - "fmt-3.yml", + pytest.param("fmt-1.yml", id="1"), + pytest.param("fmt-2.yml", id="2"), + pytest.param("fmt-3.yml", id="3"), + pytest.param("fmt-4.yml", id="4"), + pytest.param("fmt-5.yml", id="5"), ), ) def test_formatted_yaml_loader_dumper(