Skip to content

Commit 9021516

Browse files
Ensure that single space between tasks is preserved when using --write (#3641)
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
1 parent 566b036 commit 9021516

9 files changed

Lines changed: 160 additions & 4 deletions

File tree

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
7272
# Number of expected test passes, safety measure for accidental skip of
7373
# tests. Update value if you add/remove tests.
74-
PYTEST_REQPASS: 809
74+
PYTEST_REQPASS: 811
7575
steps:
7676
- name: Activate WSL1
7777
if: "contains(matrix.shell, 'wsl')"

src/ansiblelint/yaml_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,19 @@ def write_comment(
695695
ruamel.yaml.events.CollectionEndEvent,
696696
ruamel.yaml.events.DocumentEndEvent,
697697
ruamel.yaml.events.StreamEndEvent,
698+
ruamel.yaml.events.MappingStartEvent,
698699
),
699700
)
700701
):
701702
# drop pure whitespace pre comments
702703
# does not apply to End events since they consume one of the newlines.
703704
value = ""
705+
elif (
706+
pre
707+
and not value.strip()
708+
and isinstance(self.event, ruamel.yaml.events.MappingStartEvent)
709+
):
710+
value = self._re_repeat_blank_lines.sub("", value)
704711
elif pre:
705712
# preserve content in pre comment with at least one newline,
706713
# but no extra blank lines.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
- name: Gather all legacy facts
3+
cisco.ios.ios_facts:
4+
5+
- name: Update modification and access time of given file
6+
ansible.builtin.file:
7+
path: /etc/some_file
8+
state: file
9+
modification_time: now
10+
access_time: now
11+
12+
- name: Disable ufw service
13+
ansible.builtin.service:
14+
name: ufw
15+
enabled: false
16+
state: stopped
17+
when: '"ufw" in services'
18+
19+
- name: Remove file (delete file)
20+
ansible.builtin.file:
21+
path: /etc/foo.txt
22+
state: absent
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- name: Test this playbook
3+
hosts: all
4+
tasks:
5+
- name: Gather all legacy facts
6+
cisco.ios.ios_facts:
7+
8+
- name: Update modification and access time of given file
9+
ansible.builtin.file:
10+
path: /etc/some_file
11+
state: file
12+
modification_time: now
13+
access_time: now
14+
15+
- name: Disable ufw service
16+
ansible.builtin.service:
17+
name: ufw
18+
enabled: false
19+
state: stopped
20+
when: '"ufw" in services'
21+
22+
- name: Remove file (delete file)
23+
ansible.builtin.file:
24+
path: /etc/foo.txt
25+
state: absent
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- name: Gather all legacy facts
3+
cisco.ios.ios_facts:
4+
5+
- name: Update modification and access time of given file
6+
ansible.builtin.file:
7+
path: /etc/some_file
8+
state: file
9+
modification_time: now
10+
access_time: now
11+
12+
13+
- name: Disable ufw service
14+
ansible.builtin.service:
15+
name: ufw
16+
enabled: false
17+
state: stopped
18+
when: '"ufw" in services'
19+
20+
21+
22+
- name: Remove file (delete file)
23+
ansible.builtin.file:
24+
path: /etc/foo.txt
25+
state: absent
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
- name: Test this playbook
3+
hosts: all
4+
tasks:
5+
- name: Gather all legacy facts
6+
cisco.ios.ios_facts:
7+
8+
- name: Update modification and access time of given file
9+
ansible.builtin.file:
10+
path: /etc/some_file
11+
state: file
12+
modification_time: now
13+
access_time: now
14+
15+
16+
- name: Disable ufw service
17+
ansible.builtin.service:
18+
name: ufw
19+
enabled: false
20+
state: stopped
21+
when: '"ufw" in services'
22+
23+
24+
25+
- name: Remove file (delete file)
26+
ansible.builtin.file:
27+
path: /etc/foo.txt
28+
state: absent
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
- name: Gather all legacy facts
3+
cisco.ios.ios_facts:
4+
5+
- name: Update modification and access time of given file
6+
ansible.builtin.file:
7+
path: /etc/some_file
8+
state: file
9+
modification_time: now
10+
access_time: now
11+
12+
- name: Disable ufw service
13+
ansible.builtin.service:
14+
name: ufw
15+
enabled: false
16+
state: stopped
17+
when: '"ufw" in services'
18+
19+
- name: Remove file (delete file)
20+
ansible.builtin.file:
21+
path: /etc/foo.txt
22+
state: absent
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
- name: Test this playbook
3+
hosts: all
4+
tasks:
5+
- name: Gather all legacy facts
6+
cisco.ios.ios_facts:
7+
8+
- name: Update modification and access time of given file
9+
ansible.builtin.file:
10+
path: /etc/some_file
11+
state: file
12+
modification_time: now
13+
access_time: now
14+
15+
- name: Disable ufw service
16+
ansible.builtin.service:
17+
name: ufw
18+
enabled: false
19+
state: stopped
20+
when: '"ufw" in services'
21+
22+
- name: Remove file (delete file)
23+
ansible.builtin.file:
24+
path: /etc/foo.txt
25+
state: absent

test/test_yaml_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ def fixture_yaml_formatting_fixtures(fixture_filename: str) -> tuple[str, str, s
222222
@pytest.mark.parametrize(
223223
"fixture_filename",
224224
(
225-
"fmt-1.yml",
226-
"fmt-2.yml",
227-
"fmt-3.yml",
225+
pytest.param("fmt-1.yml", id="1"),
226+
pytest.param("fmt-2.yml", id="2"),
227+
pytest.param("fmt-3.yml", id="3"),
228+
pytest.param("fmt-4.yml", id="4"),
229+
pytest.param("fmt-5.yml", id="5"),
228230
),
229231
)
230232
def test_formatted_yaml_loader_dumper(

0 commit comments

Comments
 (0)