From 867e2d23f3934dd474f473736d789fcd4c883ff4 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 22 Apr 2026 13:49:24 +0200 Subject: [PATCH 1/2] Added test for moving comments inside attributes out Signed-off-by: Ole Herman Schumacher Elgesem --- tests/format/004_comments.expected.cf | 11 +++++++++++ tests/format/004_comments.input.cf | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/format/004_comments.expected.cf b/tests/format/004_comments.expected.cf index 9b9e4e2..e8e39cb 100644 --- a/tests/format/004_comments.expected.cf +++ b/tests/format/004_comments.expected.cf @@ -34,3 +34,14 @@ bundle agent main # Inside promise, next to attributes string => "value"; } + +bundle agent check +{ + methods: + "any" + # Verify that the custom failsafe.cf did run and created the + # file that we removed earlier. + usebundle => dcs_passif_fileexists( + "$(sys.inputdir)/failsafe_output.txt", "$(this.promise_filename)" + ); +} diff --git a/tests/format/004_comments.input.cf b/tests/format/004_comments.input.cf index 93b6b05..8e7341a 100644 --- a/tests/format/004_comments.input.cf +++ b/tests/format/004_comments.input.cf @@ -33,3 +33,13 @@ if => "something", # Inside promise, next to attributes string => "value"; } + +bundle agent check +{ + methods: + "any" usebundle => + # Verify that the custom failsafe.cf did run and created the + # file that we removed earlier. + dcs_passif_fileexists("$(sys.inputdir)/failsafe_output.txt", + "$(this.promise_filename)"); +} From b03ee7ed3635ed7bafbaeebc6155d257f20d9535 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Wed, 22 Apr 2026 13:50:20 +0200 Subject: [PATCH 2/2] cfengine format: Moved comments out of attributes Co-authored-by: Claude Opus 4.6 (1M context) Signed-off-by: Ole Herman Schumacher Elgesem --- src/cfengine_cli/format.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/cfengine_cli/format.py b/src/cfengine_cli/format.py index c571003..a111fce 100644 --- a/src/cfengine_cli/format.py +++ b/src/cfengine_cli/format.py @@ -281,18 +281,33 @@ def maybe_split_rval( def attempt_split_attribute(node: Node, indent: int, line_length: int) -> list[str]: """Split an attribute node, wrapping the rval if it's a list or call.""" - assert len(node.children) == 3 - lval = node.children[0] - arrow = node.children[1] - rval = node.children[2] + assert len(node.children) >= 3 # lval + arrow + rval + optionally comments + + # Separate comments from the 3 structural children (lval, arrow, rval). + # Comments are moved to appear before the attribute. + children = [] + comments = [] + for child in node.children: + if child.type == "comment": + comments.append(child) + else: + children.append(child) + + assert len(children) == 3 + + lval = children[0] + arrow = children[1] + rval = children[2] + + comment_lines = [" " * indent + text(c) for c in comments] if rval.type == "list" or rval.type == "call": prefix = " " * indent + text(lval) + " " + text(arrow) + " " offset = len(prefix) lines = maybe_split_rval(rval, indent, offset, line_length) lines[0] = prefix + lines[0] - return lines - return [" " * indent + stringify_single_line_node(node)] + return comment_lines + lines + return comment_lines + [" " * indent + stringify_single_line_node(node)] def stringify(node: Node, indent: int, line_length: int) -> list[str]: