From fe611e793577e4a075dcd7fed593d095b122ac65 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Tue, 23 Jun 2026 16:22:06 +0200 Subject: [PATCH] cfengine format: Added empty lines before comments in bodies Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/format.py | 19 +++++++++++-------- tests/format/004_comments.expected.cf | 7 +++++++ tests/format/011_promises.expected.cf | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/cfengine_cli/format.py b/src/cfengine_cli/format.py index 2595c6b..5fb46ca 100644 --- a/src/cfengine_cli/format.py +++ b/src/cfengine_cli/format.py @@ -777,14 +777,17 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool return True if parent and parent.type in {"bundle_section", "class_guarded_promises"}: return prev.type in {"promise", "half_promise"} | CLASS_GUARD_TYPES - if parent and parent.type in {"body_block_body", "promise_block_body"}: - next_sib = _skip_comments(child.next_named_sibling, "next") - if next_sib is None: - return False - # Leading comment for a class-guarded section preceded by - # content above it. - if next_sib.type in CLASS_GUARD_TYPES: - return prev.type in CLASS_GUARD_TYPES | {"attribute"} + if parent and parent.type in { + "body_block_body", + "promise_block_body", + "class_guarded_body_attributes", + }: + # A comment is separated by a blank line from a preceding + # attribute or a preceding class-guarded block, so it reads as + # a leading comment for what follows (or a trailing comment for + # the block). This mirrors how a comment after a promise is + # separated in bundles. + return prev.type == "attribute" or prev.type in CLASS_GUARD_TYPES return False return False diff --git a/tests/format/004_comments.expected.cf b/tests/format/004_comments.expected.cf index 475cd98..c9f7770 100644 --- a/tests/format/004_comments.expected.cf +++ b/tests/format/004_comments.expected.cf @@ -83,11 +83,14 @@ body package_method apt package_list_command => "/usr/bin/dpkg -l"; package_list_name_regex => "ii\s+([^\s]+).*"; package_list_version_regex => "ii\s+[^\s]+\s+([^\s]+).*"; + # package_list_arch_regex => "none"; package_installed_regex => ".*"; + # all reported are installed: # package_name_convention => "$(name)_$(version)_$(arch)"; package_name_convention => "$(name)"; + # Use these only if not using a separate version/arch string: # package_version_regex => ""; # package_name_regex => ""; @@ -95,6 +98,7 @@ body package_method apt package_add_command => "/usr/bin/apt-get --yes install"; package_delete_command => "/usr/bin/apt-get --yes remove"; package_update_command => "/usr/bin/apt-get --yes dist-upgrade"; + # package_verify_command => "/bin/rpm -V"; } @@ -107,6 +111,7 @@ body package_method ca body package_method ac { package_changes => "bulk"; + # bar } @@ -114,6 +119,7 @@ body package_method cac { # foo package_changes => "bulk"; + # bar } @@ -129,6 +135,7 @@ body package_method cgcac linux:: # bar package_changes => "bulk"; + # baz } diff --git a/tests/format/011_promises.expected.cf b/tests/format/011_promises.expected.cf index e2245f0..deaf545 100644 --- a/tests/format/011_promises.expected.cf +++ b/tests/format/011_promises.expected.cf @@ -120,6 +120,7 @@ body common control any:: ignore_missing_bundles => "$(def.control_common_ignore_missing_bundles)"; ignore_missing_inputs => "$(def.control_common_ignore_missing_inputs)"; + # The number of minutes after which last-seen entries are purged from cf_lastseen.lmdb lastseenexpireafter => "$(def.control_common_lastseenexpireafter)";