From 35016e79c5d994c8b107931b9d1454403afa1892 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Tue, 1 Aug 2023 14:38:15 +0530 Subject: [PATCH 01/10] Ensure that single space between tasks are preserved when using --write --- src/ansiblelint/yaml_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 38ad069699..a76bbf3406 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -695,6 +695,7 @@ def write_comment( ruamel.yaml.events.CollectionEndEvent, ruamel.yaml.events.DocumentEndEvent, ruamel.yaml.events.StreamEndEvent, + ruamel.yaml.events.MappingStartEvent, ), ) ): From 36bbe0870459ec488d985620ea32ffa649f0dae9 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Thu, 3 Aug 2023 16:22:01 +0530 Subject: [PATCH 02/10] Fix for test_formatted_yaml_loader_dumper --- src/ansiblelint/yaml_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index a76bbf3406..9fd1487448 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -702,6 +702,12 @@ def write_comment( # 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. From bf5449b28e10eab0175a1b37cdaaa1c26c5a9fd1 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Fri, 4 Aug 2023 11:12:05 +0530 Subject: [PATCH 03/10] Add tests for formatting task files --- .../task-after-formatting.yml | 19 ++++++++++ .../task-before-formatting.yml | 20 +++++++++++ test/test_yaml_utils.py | 35 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 test/fixtures/formatting-tasks/task-after-formatting.yml create mode 100644 test/fixtures/formatting-tasks/task-before-formatting.yml diff --git a/test/fixtures/formatting-tasks/task-after-formatting.yml b/test/fixtures/formatting-tasks/task-after-formatting.yml new file mode 100644 index 0000000000..26dba8dc49 --- /dev/null +++ b/test/fixtures/formatting-tasks/task-after-formatting.yml @@ -0,0 +1,19 @@ +--- +- 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-tasks/task-before-formatting.yml b/test/fixtures/formatting-tasks/task-before-formatting.yml new file mode 100644 index 0000000000..8636e49961 --- /dev/null +++ b/test/fixtures/formatting-tasks/task-before-formatting.yml @@ -0,0 +1,20 @@ +--- +- 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..12bcc7f5f1 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -21,6 +21,7 @@ formatting_before_fixtures_dir = fixtures_dir / "formatting-before" formatting_prettier_fixtures_dir = fixtures_dir / "formatting-prettier" formatting_after_fixtures_dir = fixtures_dir / "formatting-after" +formatting_tasks_dir = fixtures_dir / "formatting-tasks" @pytest.fixture(name="empty_lintable") @@ -264,6 +265,40 @@ def test_formatted_yaml_loader_dumper( assert not list(run_yamllint(after_content, config)) +@pytest.fixture(name="task_file_formatting_fixtures") +def fixture_tasks_formatting_fixtures() -> tuple[str, str]: + """Get the contents for the formatting fixture files.""" + before_path = formatting_tasks_dir / "task-before-formatting.yml" + after_path = formatting_tasks_dir / "task-after-formatting.yml" + before_content = before_path.read_text() + formatted_content = after_path.read_text() + return before_content, formatted_content + + +@pytest.mark.parametrize( + "fixture_filename", + ("task-before-formatting.yml",), +) +def test_formatted_task( + task_file_formatting_fixtures: tuple[str, str], + fixture_filename: str, # noqa: ARG001 +) -> None: + """Ensure that FormattedYAML loads/dumps formatting fixtures consistently.""" + # pylint: disable=unused-argument + before_content, after_content = task_file_formatting_fixtures + assert before_content != after_content + + yaml = ansiblelint.yaml_utils.FormattedYAML() + + data_before = yaml.loads(before_content) + dump_from_before = yaml.dumps(data_before) + data_after = yaml.loads(after_content) + dump_from_after = yaml.dumps(data_after) + + assert dump_from_before == after_content + assert dump_from_after == after_content + + @pytest.fixture(name="lintable") def fixture_lintable(file_path: str) -> Lintable: """Return a playbook Lintable for use in ``get_path_to_*`` tests.""" From 2305c552f4f8535ce42cfcde76a2ad23be0ccdf3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Aug 2023 05:44:03 +0000 Subject: [PATCH 04/10] chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test/fixtures/formatting-tasks/task-before-formatting.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/test/fixtures/formatting-tasks/task-before-formatting.yml b/test/fixtures/formatting-tasks/task-before-formatting.yml index 8636e49961..26dba8dc49 100644 --- a/test/fixtures/formatting-tasks/task-before-formatting.yml +++ b/test/fixtures/formatting-tasks/task-before-formatting.yml @@ -13,7 +13,6 @@ state: stopped when: '"ufw" in services' - - name: Remove file (delete file) ansible.builtin.file: path: /etc/foo.txt From 597ef3e0efca9411140efd9943bb49cfd76b94fa Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Fri, 4 Aug 2023 12:00:27 +0530 Subject: [PATCH 05/10] Fix the tests and add more description --- test/test_yaml_utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_yaml_utils.py b/test/test_yaml_utils.py index 12bcc7f5f1..8907e7646e 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -283,10 +283,14 @@ def test_formatted_task( task_file_formatting_fixtures: tuple[str, str], fixture_filename: str, # noqa: ARG001 ) -> None: - """Ensure that FormattedYAML loads/dumps formatting fixtures consistently.""" + """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. + + Compares two identical task files and tests that the single space + between block of tasks is preserved. + """ # pylint: disable=unused-argument before_content, after_content = task_file_formatting_fixtures - assert before_content != after_content + assert before_content == after_content yaml = ansiblelint.yaml_utils.FormattedYAML() From 72975d781ab06c51f0d693e2c2ed6ecbc0edaead Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Fri, 4 Aug 2023 12:38:23 +0530 Subject: [PATCH 06/10] Change test_formatted_task to check single task file, before and after formatting --- .github/workflows/tox.yml | 2 +- .../task-before-formatting.yml | 19 ---------- ...task-after-formatting.yml => task-fmt.yml} | 0 test/test_yaml_utils.py | 38 ++++--------------- 4 files changed, 9 insertions(+), 50 deletions(-) delete mode 100644 test/fixtures/formatting-tasks/task-before-formatting.yml rename test/fixtures/formatting-tasks/{task-after-formatting.yml => task-fmt.yml} (100%) diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 7500fd8a0d..38d27eb471 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: 810 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/test/fixtures/formatting-tasks/task-before-formatting.yml b/test/fixtures/formatting-tasks/task-before-formatting.yml deleted file mode 100644 index 26dba8dc49..0000000000 --- a/test/fixtures/formatting-tasks/task-before-formatting.yml +++ /dev/null @@ -1,19 +0,0 @@ ---- -- 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-tasks/task-after-formatting.yml b/test/fixtures/formatting-tasks/task-fmt.yml similarity index 100% rename from test/fixtures/formatting-tasks/task-after-formatting.yml rename to test/fixtures/formatting-tasks/task-fmt.yml diff --git a/test/test_yaml_utils.py b/test/test_yaml_utils.py index 8907e7646e..cb47a48de3 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -265,42 +265,20 @@ def test_formatted_yaml_loader_dumper( assert not list(run_yamllint(after_content, config)) -@pytest.fixture(name="task_file_formatting_fixtures") -def fixture_tasks_formatting_fixtures() -> tuple[str, str]: - """Get the contents for the formatting fixture files.""" - before_path = formatting_tasks_dir / "task-before-formatting.yml" - after_path = formatting_tasks_dir / "task-after-formatting.yml" - before_content = before_path.read_text() - formatted_content = after_path.read_text() - return before_content, formatted_content - - -@pytest.mark.parametrize( - "fixture_filename", - ("task-before-formatting.yml",), -) -def test_formatted_task( - task_file_formatting_fixtures: tuple[str, str], - fixture_filename: str, # noqa: ARG001 -) -> None: +def test_formatted_task() -> None: """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. - Compares two identical task files and tests that the single space - between block of tasks is preserved. + Compares the data of task file before and after formatting, + to ensure that the single space between blocks is preserved. """ - # pylint: disable=unused-argument - before_content, after_content = task_file_formatting_fixtures - assert before_content == after_content + task_file_path = formatting_tasks_dir / "task-fmt.yml" + task_file_content = task_file_path.read_text() yaml = ansiblelint.yaml_utils.FormattedYAML() - data_before = yaml.loads(before_content) - dump_from_before = yaml.dumps(data_before) - data_after = yaml.loads(after_content) - dump_from_after = yaml.dumps(data_after) - - assert dump_from_before == after_content - assert dump_from_after == after_content + before_formatting = yaml.loads(task_file_content) + dump_from_formatting = yaml.dumps(before_formatting) + assert task_file_content == dump_from_formatting @pytest.fixture(name="lintable") From 8e9e848673975f2f6bcdf57b963cfd3dc708f722 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Fri, 4 Aug 2023 14:43:10 +0530 Subject: [PATCH 07/10] Add test for playbook scenario --- .github/workflows/tox.yml | 2 +- .../formatting-playbook/playbook-fmt.yml | 22 +++++++++++++++++++ test/test_yaml_utils.py | 17 ++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/formatting-playbook/playbook-fmt.yml diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 38d27eb471..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: 810 + PYTEST_REQPASS: 811 steps: - name: Activate WSL1 if: "contains(matrix.shell, 'wsl')" diff --git a/test/fixtures/formatting-playbook/playbook-fmt.yml b/test/fixtures/formatting-playbook/playbook-fmt.yml new file mode 100644 index 0000000000..ed12891893 --- /dev/null +++ b/test/fixtures/formatting-playbook/playbook-fmt.yml @@ -0,0 +1,22 @@ +--- +- name: Test this playbook + hosts: all + tasks: + - 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 cb47a48de3..ab9f306c21 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -22,6 +22,7 @@ formatting_prettier_fixtures_dir = fixtures_dir / "formatting-prettier" formatting_after_fixtures_dir = fixtures_dir / "formatting-after" formatting_tasks_dir = fixtures_dir / "formatting-tasks" +formatting_playbook_dir = fixtures_dir / "formatting-playbook" @pytest.fixture(name="empty_lintable") @@ -281,6 +282,22 @@ def test_formatted_task() -> None: assert task_file_content == dump_from_formatting +def test_formatted_playbook() -> None: + """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. + + Compares the data of playbook file before and after formatting, + to ensure that the single space between blocks is preserved. + """ + playbook_file_path = formatting_playbook_dir / "playbook-fmt.yml" + playbook_file_content = playbook_file_path.read_text() + + yaml = ansiblelint.yaml_utils.FormattedYAML() + + before_formatting = yaml.loads(playbook_file_content) + dump_from_formatting = yaml.dumps(before_formatting) + assert playbook_file_content == dump_from_formatting + + @pytest.fixture(name="lintable") def fixture_lintable(file_path: str) -> Lintable: """Return a playbook Lintable for use in ``get_path_to_*`` tests.""" From 93eb0cb385c017693823c2cd2e3b4af6650b7e85 Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Tue, 8 Aug 2023 19:12:55 +0530 Subject: [PATCH 08/10] Changes in the test --- .../fmt-4.yml} | 0 .../fmt-5.yml} | 0 test/fixtures/formatting-before/fmt-4.yml | 20 +++++++ test/fixtures/formatting-before/fmt-5.yml | 23 ++++++++ test/test_yaml_utils.py | 54 ++++++++++--------- 5 files changed, 73 insertions(+), 24 deletions(-) rename test/fixtures/{formatting-tasks/task-fmt.yml => formatting-after/fmt-4.yml} (100%) rename test/fixtures/{formatting-playbook/playbook-fmt.yml => formatting-after/fmt-5.yml} (100%) create mode 100644 test/fixtures/formatting-before/fmt-4.yml create mode 100644 test/fixtures/formatting-before/fmt-5.yml diff --git a/test/fixtures/formatting-tasks/task-fmt.yml b/test/fixtures/formatting-after/fmt-4.yml similarity index 100% rename from test/fixtures/formatting-tasks/task-fmt.yml rename to test/fixtures/formatting-after/fmt-4.yml diff --git a/test/fixtures/formatting-playbook/playbook-fmt.yml b/test/fixtures/formatting-after/fmt-5.yml similarity index 100% rename from test/fixtures/formatting-playbook/playbook-fmt.yml rename to test/fixtures/formatting-after/fmt-5.yml diff --git a/test/fixtures/formatting-before/fmt-4.yml b/test/fixtures/formatting-before/fmt-4.yml new file mode 100644 index 0000000000..8636e49961 --- /dev/null +++ b/test/fixtures/formatting-before/fmt-4.yml @@ -0,0 +1,20 @@ +--- +- 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..a3e055c007 --- /dev/null +++ b/test/fixtures/formatting-before/fmt-5.yml @@ -0,0 +1,23 @@ +--- +- name: Test this playbook + hosts: all + tasks: + - 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 ab9f306c21..a800b51fbc 100644 --- a/test/test_yaml_utils.py +++ b/test/test_yaml_utils.py @@ -21,8 +21,6 @@ formatting_before_fixtures_dir = fixtures_dir / "formatting-before" formatting_prettier_fixtures_dir = fixtures_dir / "formatting-prettier" formatting_after_fixtures_dir = fixtures_dir / "formatting-after" -formatting_tasks_dir = fixtures_dir / "formatting-tasks" -formatting_playbook_dir = fixtures_dir / "formatting-playbook" @pytest.fixture(name="empty_lintable") @@ -266,36 +264,44 @@ def test_formatted_yaml_loader_dumper( assert not list(run_yamllint(after_content, config)) -def test_formatted_task() -> None: - """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. - - Compares the data of task file before and after formatting, - to ensure that the single space between blocks is preserved. - """ - task_file_path = formatting_tasks_dir / "task-fmt.yml" - task_file_content = task_file_path.read_text() - - yaml = ansiblelint.yaml_utils.FormattedYAML() - - before_formatting = yaml.loads(task_file_content) - dump_from_formatting = yaml.dumps(before_formatting) - assert task_file_content == dump_from_formatting +@pytest.fixture(name="task_and_playbook_formatting_fixtures") +def fixture_task_and_playbook_formatting_fixtures( + fixture_filename: str, +) -> tuple[str, str]: + """Get the contents for the formatting fixture files.""" + before_path = formatting_before_fixtures_dir / fixture_filename + after_path = formatting_after_fixtures_dir / fixture_filename + before_content = before_path.read_text() + formatted_content = after_path.read_text() + return before_content, formatted_content -def test_formatted_playbook() -> None: +@pytest.mark.parametrize( + "fixture_filename", + ("fmt-4.yml", "fmt-5.yml"), +) +def test_spaces_between_block_of_tasks( + task_and_playbook_formatting_fixtures: tuple[str, str], + fixture_filename: str, # noqa: ARG001 +) -> None: """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. - Compares the data of playbook file before and after formatting, - to ensure that the single space between blocks is preserved. + Compare files to check that the single space between block of tasks is + preserved and spaces are formatted correctly. """ - playbook_file_path = formatting_playbook_dir / "playbook-fmt.yml" - playbook_file_content = playbook_file_path.read_text() + # pylint: disable=unused-argument + before_content, after_content = task_and_playbook_formatting_fixtures + assert before_content != after_content yaml = ansiblelint.yaml_utils.FormattedYAML() - before_formatting = yaml.loads(playbook_file_content) - dump_from_formatting = yaml.dumps(before_formatting) - assert playbook_file_content == dump_from_formatting + data_before = yaml.loads(before_content) + dump_from_before = yaml.dumps(data_before) + data_after = yaml.loads(after_content) + dump_from_after = yaml.dumps(data_after) + + assert dump_from_before == after_content + assert dump_from_after == after_content @pytest.fixture(name="lintable") From b2818943861b71fa6ad0e520b055ad97cab906ce Mon Sep 17 00:00:00 2001 From: shatakshiiii Date: Wed, 9 Aug 2023 14:52:38 +0530 Subject: [PATCH 09/10] Improvements in the tests --- test/fixtures/formatting-after/fmt-4.yml | 3 +++ test/fixtures/formatting-after/fmt-5.yml | 3 +++ test/fixtures/formatting-before/fmt-4.yml | 5 +++++ test/fixtures/formatting-before/fmt-5.yml | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/test/fixtures/formatting-after/fmt-4.yml b/test/fixtures/formatting-after/fmt-4.yml index 26dba8dc49..5ded596bfc 100644 --- a/test/fixtures/formatting-after/fmt-4.yml +++ b/test/fixtures/formatting-after/fmt-4.yml @@ -1,4 +1,7 @@ --- +- 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 diff --git a/test/fixtures/formatting-after/fmt-5.yml b/test/fixtures/formatting-after/fmt-5.yml index ed12891893..b259e0e895 100644 --- a/test/fixtures/formatting-after/fmt-5.yml +++ b/test/fixtures/formatting-after/fmt-5.yml @@ -2,6 +2,9 @@ - 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 diff --git a/test/fixtures/formatting-before/fmt-4.yml b/test/fixtures/formatting-before/fmt-4.yml index 8636e49961..579231ff17 100644 --- a/test/fixtures/formatting-before/fmt-4.yml +++ b/test/fixtures/formatting-before/fmt-4.yml @@ -1,4 +1,7 @@ --- +- 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 @@ -6,6 +9,7 @@ modification_time: now access_time: now + - name: Disable ufw service ansible.builtin.service: name: ufw @@ -14,6 +18,7 @@ when: '"ufw" in services' + - name: Remove file (delete file) ansible.builtin.file: path: /etc/foo.txt diff --git a/test/fixtures/formatting-before/fmt-5.yml b/test/fixtures/formatting-before/fmt-5.yml index a3e055c007..a9145e6f16 100644 --- a/test/fixtures/formatting-before/fmt-5.yml +++ b/test/fixtures/formatting-before/fmt-5.yml @@ -2,6 +2,9 @@ - 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 @@ -9,6 +12,7 @@ modification_time: now access_time: now + - name: Disable ufw service ansible.builtin.service: name: ufw @@ -17,6 +21,7 @@ when: '"ufw" in services' + - name: Remove file (delete file) ansible.builtin.file: path: /etc/foo.txt From ebb7e13b105291748c5e55b147fb8155ff53fb19 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 21 Aug 2023 11:09:39 +0100 Subject: [PATCH 10/10] Simplify test --- test/fixtures/formatting-prettier/fmt-4.yml | 22 ++++++++++ test/fixtures/formatting-prettier/fmt-5.yml | 25 +++++++++++ test/test_yaml_utils.py | 48 +++------------------ 3 files changed, 52 insertions(+), 43 deletions(-) create mode 100644 test/fixtures/formatting-prettier/fmt-4.yml create mode 100644 test/fixtures/formatting-prettier/fmt-5.yml 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 a800b51fbc..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( @@ -264,46 +266,6 @@ def test_formatted_yaml_loader_dumper( assert not list(run_yamllint(after_content, config)) -@pytest.fixture(name="task_and_playbook_formatting_fixtures") -def fixture_task_and_playbook_formatting_fixtures( - fixture_filename: str, -) -> tuple[str, str]: - """Get the contents for the formatting fixture files.""" - before_path = formatting_before_fixtures_dir / fixture_filename - after_path = formatting_after_fixtures_dir / fixture_filename - before_content = before_path.read_text() - formatted_content = after_path.read_text() - return before_content, formatted_content - - -@pytest.mark.parametrize( - "fixture_filename", - ("fmt-4.yml", "fmt-5.yml"), -) -def test_spaces_between_block_of_tasks( - task_and_playbook_formatting_fixtures: tuple[str, str], - fixture_filename: str, # noqa: ARG001 -) -> None: - """Ensure that FormattedYAML loads/dumps formatting fixtures consistently. - - Compare files to check that the single space between block of tasks is - preserved and spaces are formatted correctly. - """ - # pylint: disable=unused-argument - before_content, after_content = task_and_playbook_formatting_fixtures - assert before_content != after_content - - yaml = ansiblelint.yaml_utils.FormattedYAML() - - data_before = yaml.loads(before_content) - dump_from_before = yaml.dumps(data_before) - data_after = yaml.loads(after_content) - dump_from_after = yaml.dumps(data_after) - - assert dump_from_before == after_content - assert dump_from_after == after_content - - @pytest.fixture(name="lintable") def fixture_lintable(file_path: str) -> Lintable: """Return a playbook Lintable for use in ``get_path_to_*`` tests."""