Skip to content

Commit 2f36e13

Browse files
committed
cfengine format: Fixed issue with multilining function calls inside macros
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
1 parent 93ccbf0 commit 2f36e13

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

src/cfengine_cli/format.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,11 @@ def split_generic_list(
256256
# (one per branch); without, fix only the last non-comment element.
257257
if has_macros:
258258
for i, e in enumerate(elements):
259-
if not e.lstrip().startswith(("@", "#")):
260-
elements[i] = _set_trailing_comma(e, trailing_comma)
259+
# Skip macros, comments, and the opening line of a wrapped element
260+
# (e.g. "regcmp(") which is a continuation start, not a value.
261+
if e.lstrip().startswith(("@", "#")) or e.rstrip().endswith(("(", "{")):
262+
continue
263+
elements[i] = _set_trailing_comma(e, trailing_comma)
261264
else:
262265
for i in reversed(range(len(elements))):
263266
if not elements[i].lstrip().startswith("#"):

tests/format/011_macros.expected.cf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,23 @@ bundle agent test(a)
200200
"$(a)";
201201
}
202202
@endif
203+
bundle agent test_macro_call_param
204+
{
205+
classes:
206+
"_control_agent_environment_vars_validated" -> { "CFE-3927" }
207+
and => {
208+
# The variable must be defined
209+
isvariable("default:def.control_agent_environment_vars"),
210+
# The length of the variable must be greater than 0 (can't be an empty list)
211+
isgreaterthan(length("default:def.control_agent_environment_vars"), 0),
212+
# Each element of the list must be of the form KEY=VALUE
213+
every(".+=.+", "default:def.control_agent_environment_vars"),
214+
# In 3.18 and greater we can validate the type of variable in use
215+
@if minimum_version(3.18.0)
216+
regcmp(
217+
"(policy slist|data array)",
218+
type("default:def.control_agent_environment_vars", "true"),
219+
),
220+
@endif
221+
};
222+
}

tests/format/011_macros.input.cf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,21 @@ reports:
193193
"$(a)";
194194
}
195195
@endif
196+
197+
bundle agent test_macro_call_param
198+
{
199+
classes:
200+
"_control_agent_environment_vars_validated" -> { "CFE-3927" }
201+
and => {
202+
# The variable must be defined
203+
isvariable("default:def.control_agent_environment_vars"),
204+
# The length of the variable must be greater than 0 (can't be an empty list)
205+
isgreaterthan(length("default:def.control_agent_environment_vars"), 0),
206+
# Each element of the list must be of the form KEY=VALUE
207+
every(".+=.+", "default:def.control_agent_environment_vars"),
208+
# In 3.18 and greater we can validate the type of variable in use
209+
@if minimum_version(3.18.0)
210+
regcmp( "(policy slist|data array)", type( "default:def.control_agent_environment_vars", "true" ) ),
211+
@endif
212+
};
213+
}

0 commit comments

Comments
 (0)