Skip to content

Commit 71bf087

Browse files
authored
Merge pull request #148 from olehermanse/main
Fixed formatter to indent trailing comments in bodies and add empty lines according to test expectations
2 parents ad72e76 + 1b57797 commit 71bf087

6 files changed

Lines changed: 167 additions & 3 deletions

File tree

src/cfengine_cli/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def _format_dirname(directory: str, line_length: int, check: bool) -> int:
7474
name.endswith(".x.cf")
7575
or name.endswith(".input.cf")
7676
or name.endswith(".output.cf")
77+
or name.endswith(".expected.cf")
7778
):
7879
continue # Test files skipped during directory traversal
7980
if name.endswith(

src/cfengine_cli/format.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,16 +733,31 @@ def _needs_blank_line_before(child: Node, indent: int, line_length: int) -> bool
733733
)
734734
return not both_single
735735

736+
if child.type == "class_guarded_promise_block_attributes":
737+
return prev.type in {"attribute", "class_guarded_promise_block_attributes"}
738+
736739
if child.type in CLASS_GUARD_TYPES:
737740
return prev.type in {"promise", "half_promise", "class_guarded_promises"}
738741

739742
if child.type == "comment":
740743
if prev.type not in {"promise", "half_promise"} | CLASS_GUARD_TYPES:
741744
return False
742745
parent = child.parent
743-
return bool(
744-
parent and parent.type in {"bundle_section", "class_guarded_promises"}
745-
)
746+
if parent and parent.type in {"bundle_section", "class_guarded_promises"}:
747+
return True
748+
# Inside a body/promise block, a comment between two class guards
749+
# only gets a blank-line separator when the preceding class guard
750+
# already has interior comments (i.e. the visual block is rich
751+
# enough that running it together with the next would look dense).
752+
if parent and parent.type in {"body_block_body", "promise_block_body"}:
753+
if _skip_comments(child.next_named_sibling, "next") is None:
754+
return False
755+
if prev.type in CLASS_GUARD_TYPES and any(
756+
c.type == "comment" for c in prev.children
757+
):
758+
return True
759+
return False
760+
return False
746761

747762
return False
748763

@@ -775,8 +790,13 @@ def _skip_comments(sibling: Node | None, direction: str = "next") -> Node | None
775790
def _comment_indent(node: Node, indent: int) -> int:
776791
"""Compute indentation for a leaf comment based on its nearest non-comment neighbor."""
777792
nearest = _skip_comments(node.next_named_sibling, "next")
793+
# A trailing comment whose previous sibling is a class-guarded group
794+
# lines up with that group's contents (one extra indent level), as if
795+
# it were the last comment inside the class guard.
778796
if nearest is None:
779797
nearest = _skip_comments(node.prev_named_sibling, "prev")
798+
if nearest and nearest.type in CLASS_GUARD_TYPES:
799+
return indent + 4
780800
if nearest and nearest.type in INDENTED_TYPES:
781801
return indent + 2
782802
# No indented sibling found — if we're directly inside a block body,

tests/format/002_basics.expected.cf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ body common control
1212
promise agent example
1313
{
1414
interpreter => "/usr/bin/python3";
15+
1516
linux::
17+
interpreter => "/usr/bin/python3";
1618
path => "/var/cfengine/inputs/modules/promises/git.py";
19+
1720
windows::
1821
path => "C:\Program files\Cfengine\inputs\modules\promises\git.py";
1922
}

tests/format/002_basics.input.cf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ inputs => {};
1212
promise agent example{
1313
interpreter => "/usr/bin/python3";
1414
linux::
15+
interpreter => "/usr/bin/python3";
1516
path => "/var/cfengine/inputs/modules/promises/git.py";
1617
windows::
1718
path => "C:\Program files\Cfengine\inputs\modules\promises\git.py";

tests/format/004_comments.expected.cf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,73 @@ bundle agent win_services
7474
# Windows Time
7575
};
7676
}
77+
78+
body package_method apt
79+
{
80+
any::
81+
# ii acpi 0.09-3ubuntu1
82+
package_changes => "bulk";
83+
package_list_command => "/usr/bin/dpkg -l";
84+
package_list_name_regex => "ii\s+([^\s]+).*";
85+
package_list_version_regex => "ii\s+[^\s]+\s+([^\s]+).*";
86+
# package_list_arch_regex => "none";
87+
package_installed_regex => ".*";
88+
# all reported are installed:
89+
# package_name_convention => "$(name)_$(version)_$(arch)";
90+
package_name_convention => "$(name)";
91+
# Use these only if not using a separate version/arch string:
92+
# package_version_regex => "";
93+
# package_name_regex => "";
94+
# package_arch_regex => "";
95+
package_add_command => "/usr/bin/apt-get --yes install";
96+
package_delete_command => "/usr/bin/apt-get --yes remove";
97+
package_update_command => "/usr/bin/apt-get --yes dist-upgrade";
98+
# package_verify_command => "/bin/rpm -V";
99+
}
100+
101+
body package_method ca
102+
{
103+
# foo
104+
package_changes => "bulk";
105+
}
106+
107+
body package_method ac
108+
{
109+
package_changes => "bulk";
110+
# bar
111+
}
112+
113+
body package_method cac
114+
{
115+
# foo
116+
package_changes => "bulk";
117+
# bar
118+
}
119+
120+
body package_method ga
121+
{
122+
linux::
123+
package_changes => "bulk";
124+
}
125+
126+
body package_method cgcac
127+
{
128+
# foo
129+
linux::
130+
# bar
131+
package_changes => "bulk";
132+
# baz
133+
}
134+
135+
body package_method cgcacgcac
136+
{
137+
# foo
138+
linux::
139+
# bar
140+
package_changes => "bulk";
141+
142+
# baz
143+
!linux::
144+
# bar
145+
package_changes => "bulk";
146+
}

tests/format/004_comments.input.cf

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,72 @@ bundle agent win_services
6464
# Windows Time
6565
};
6666
}
67+
68+
body package_method apt
69+
{
70+
any::
71+
# ii acpi 0.09-3ubuntu1
72+
package_changes => "bulk";
73+
package_list_command => "/usr/bin/dpkg -l";
74+
package_list_name_regex => "ii\s+([^\s]+).*";
75+
package_list_version_regex => "ii\s+[^\s]+\s+([^\s]+).*";
76+
# package_list_arch_regex => "none";
77+
package_installed_regex => ".*";
78+
# all reported are installed:
79+
# package_name_convention => "$(name)_$(version)_$(arch)";
80+
package_name_convention => "$(name)";
81+
# Use these only if not using a separate version/arch string:
82+
# package_version_regex => "";
83+
# package_name_regex => "";
84+
# package_arch_regex => "";
85+
package_add_command => "/usr/bin/apt-get --yes install";
86+
package_delete_command => "/usr/bin/apt-get --yes remove";
87+
package_update_command => "/usr/bin/apt-get --yes dist-upgrade";
88+
# package_verify_command => "/bin/rpm -V";
89+
}
90+
91+
body package_method ca
92+
{
93+
# foo
94+
package_changes => "bulk";
95+
}
96+
97+
body package_method ac
98+
{
99+
package_changes => "bulk";
100+
# bar
101+
}
102+
103+
body package_method cac
104+
{
105+
# foo
106+
package_changes => "bulk";
107+
# bar
108+
}
109+
110+
body package_method ga
111+
{
112+
linux::
113+
package_changes => "bulk";
114+
}
115+
116+
body package_method cgcac
117+
{
118+
# foo
119+
linux::
120+
# bar
121+
package_changes => "bulk";
122+
# baz
123+
}
124+
125+
body package_method cgcacgcac
126+
{
127+
# foo
128+
linux::
129+
# bar
130+
package_changes => "bulk";
131+
# baz
132+
!linux::
133+
# bar
134+
package_changes => "bulk";
135+
}

0 commit comments

Comments
 (0)