Skip to content

Commit c19ea88

Browse files
authored
Merge pull request #67 from SimonThalvorsen/ent-27/28/32
ENT-13827/13828/13832: Checks for validity of attribute names and number of arguments
2 parents 1dc9856 + 883a894 commit c19ea88

6 files changed

Lines changed: 100 additions & 0 deletions

src/cfengine_cli/lint.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,47 @@ def _lint_node(
634634
f"Error: '{name}' is not a defined body. Only bodies may be called with '{state.attribute_name}' {location}"
635635
)
636636
return 1
637+
if node.type == "attribute_name" and state.promise_type and state.attribute_name:
638+
promise_type_data = syntax_data.BUILTIN_PROMISE_TYPES.get(
639+
state.promise_type, {}
640+
)
641+
promise_type_attrs = promise_type_data.get("attributes", {})
642+
if state.attribute_name not in promise_type_attrs:
643+
_highlight_range(node, lines)
644+
print(
645+
f"Error: Invalid attribute '{state.attribute_name}' for promise type '{state.promise_type}' {location}"
646+
)
647+
return 1
648+
if (
649+
state.block_keyword == "promise"
650+
and node.type == "attribute_name"
651+
and state.attribute_name
652+
not in (
653+
None,
654+
"path",
655+
"interpreter",
656+
)
657+
):
658+
_highlight_range(node, lines)
659+
print(
660+
f"Error: Invalid attribute name '{state.attribute_name}' in '{state.block_name}' custom promise type definition {location}"
661+
)
662+
return 1
663+
if node.type == "call":
664+
call, _, *args, _ = node.children # f ( a1 , a2 , a..N )
665+
call = _text(call)
666+
args = list(filter(",".__ne__, iter(_text(x) for x in args)))
667+
668+
if call in syntax_data.BUILTIN_FUNCTIONS:
669+
variadic = syntax_data.BUILTIN_FUNCTIONS.get(call, {}).get("variadic", True)
670+
params = syntax_data.BUILTIN_FUNCTIONS.get(call, {}).get("parameters", {})
671+
if not variadic and (len(params) != len(args)):
672+
_highlight_range(node, lines)
673+
print(
674+
f"Error: Expected {len(params)} arguments, received {len(args)} {location}"
675+
)
676+
return 1
677+
# TODO: Handle variadic functions with varying number of required arguments (0-N, 1-N, 2-N and so on)
637678
return 0
638679

639680

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
path => "/var/cfengine/inputs/modules/promises/git.py";
3+
blah => "something";
4+
^--^
5+
Error: Invalid attribute name 'blah' in 'git' custom promise type definition at tests/lint/012_invalid_attributes.x.cf:5:3
6+
7+
slist => {},
8+
bar => "";
9+
^-^
10+
Error: Invalid attribute 'bar' for promise type 'vars' at tests/lint/012_invalid_attributes.x.cf:12:7
11+
FAIL: tests/lint/012_invalid_attributes.x.cf (2 errors)
12+
Failure, 2 errors in total.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
promise agent git
2+
{
3+
interpreter => "/usr/bin/python3";
4+
path => "/var/cfengine/inputs/modules/promises/git.py";
5+
blah => "something";
6+
}
7+
bundle agent foo
8+
{
9+
vars:
10+
"error"
11+
slist => {},
12+
bar => "";
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
bundle agent main
2+
{
3+
vars:
4+
"string"
5+
string => canonify("test");
6+
reports:
7+
"Test => $(string)";
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
"string1"
3+
string => canonify();
4+
^--------^
5+
Error: Expected 1 arguments, received 0 at tests/lint/013_function_call_arg_count.x.cf:5:15
6+
7+
"string3"
8+
string => canonify("test", "test");
9+
^----------------------^
10+
Error: Expected 1 arguments, received 2 at tests/lint/013_function_call_arg_count.x.cf:9:15
11+
FAIL: tests/lint/013_function_call_arg_count.x.cf (2 errors)
12+
Failure, 2 errors in total.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
bundle agent main
2+
{
3+
vars:
4+
"string1"
5+
string => canonify();
6+
"string2"
7+
string => canonify("test");
8+
"string3"
9+
string => canonify("test", "test");
10+
reports:
11+
"Test1 => $(string1)";
12+
"Test2 => $(string2)";
13+
"Test3 => $(string3)";
14+
}

0 commit comments

Comments
 (0)