Skip to content

Commit e451097

Browse files
authored
Merge pull request #46 from SimonThalvorsen/main
ENT-13824/13826: Errors on userdefined bundles that shadow built-ins & function-calls inside vars that are not built-in
2 parents d6d871d + 08ab598 commit e451097

7 files changed

Lines changed: 101 additions & 0 deletions

src/cfengine_cli/lint.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,15 @@ def _lint_node(node: Node, policy_file: PolicyFile, state: State) -> int:
492492
f"Error: Bundle type must be one of ({', '.join(ALLOWED_BUNDLE_TYPES)}), not '{_text(node)}' {location}"
493493
)
494494
return 1
495+
if state.strict and (
496+
node.type in ("bundle_block_name", "body_block_name")
497+
and _text(node) in BUILTIN_FUNCTIONS
498+
):
499+
_highlight_range(node, lines)
500+
print(
501+
f"Error: {"Bundle" if "bundle" in node.type else "Body"} '{_text(node)}' conflicts with built-in function with the same name {location}"
502+
)
503+
return 1
495504
if node.type == "calling_identifier":
496505
name = _text(node)
497506
qualified_name = _qualify(name, state.namespace)
@@ -515,6 +524,26 @@ def _lint_node(node: Node, policy_file: PolicyFile, state: State) -> int:
515524
f"Error: Call to unknown function / bundle / body '{name}' {location}"
516525
)
517526
return 1
527+
if (
528+
name not in BUILTIN_FUNCTIONS
529+
and state.promise_type == "vars"
530+
and state.attribute_name not in ("action", "classes")
531+
):
532+
_highlight_range(node, lines)
533+
print(
534+
f"Error: Call to unknown function '{name}' inside 'vars'-promise {location}"
535+
)
536+
return 1
537+
if (
538+
state.promise_type == "vars"
539+
and state.attribute_name in ("action", "classes")
540+
and qualified_name not in state.bodies
541+
):
542+
_highlight_range(node, lines)
543+
print(
544+
f"Error: '{name}' is not a defined body. Only bodies may be called with '{state.attribute_name}' {location}"
545+
)
546+
return 1
518547
return 0
519548

520549

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent isvariab
2+
{
3+
reports:
4+
"isvariab is not a built-in function";
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
bundle agent isvariable
3+
^--------^
4+
Error: Bundle 'isvariable' conflicts with built-in function with the same name at tests/lint/009_bundle_shadows_function.x.cf:1:14
5+
FAIL: tests/lint/009_bundle_shadows_function.x.cf (1 error)
6+
Failure, 1 error in total.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent isvariable
2+
{
3+
reports:
4+
"isvariable is a built-in function";
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body classes bundle_class(a)
2+
{
3+
scope => "bundle";
4+
promise_kept => { "$(a):bundle_context" };
5+
}
6+
7+
bundle agent target(a)
8+
{
9+
reports:
10+
"Hello, $(a)";
11+
}
12+
13+
bundle agent helper
14+
{
15+
vars:
16+
"x" string => isvariable("arg");
17+
"y"
18+
classes => bundle_class("arg"),
19+
string => "";
20+
methods:
21+
"x" usebundle => target("arg");
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
vars:
3+
"x" string => target("arg");
4+
^----^
5+
Error: Call to unknown function 'target' inside 'vars'-promise at tests/lint/010_unknown_function_inside_vars.x.cf:16:19
6+
7+
"y"
8+
classes => isvariable("arg"),
9+
^--------^
10+
Error: 'isvariable' is not a defined body. Only bodies may be called with 'classes' at tests/lint/010_unknown_function_inside_vars.x.cf:18:18
11+
FAIL: tests/lint/010_unknown_function_inside_vars.x.cf (2 errors)
12+
Failure, 2 errors in total.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body classes bundle_class(a)
2+
{
3+
scope => "bundle";
4+
promise_kept => { "$(a):bundle_context" };
5+
}
6+
7+
bundle agent target(a)
8+
{
9+
reports:
10+
"Hello, $(a)";
11+
}
12+
13+
bundle agent helper
14+
{
15+
vars:
16+
"x" string => target("arg");
17+
"y"
18+
classes => isvariable("arg"),
19+
string => "";
20+
methods:
21+
"x" usebundle => target("arg");
22+
}

0 commit comments

Comments
 (0)