Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that single space between tasks is preserved when using --write #3641

Merged
merged 10 commits into from
Aug 21, 2023
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
7 changes: 7 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
cognifloyd marked this conversation as resolved.
Show resolved Hide resolved
elif pre:
# preserve content in pre comment with at least one newline,
# but no extra blank lines.
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/formatting-after/fmt-4.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/fixtures/formatting-after/fmt-5.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/fixtures/formatting-before/fmt-4.yml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions test/fixtures/formatting-before/fmt-5.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions test/fixtures/formatting-prettier/fmt-4.yml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/fixtures/formatting-prettier/fmt-5.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down