Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/cfengine_cli/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"rlist",
"slist",
"string",
"str", # deprecated shorthand for string
}
PROMISE_BLOCK_ATTRIBUTES = ("path", "interpreter")

Expand Down Expand Up @@ -775,29 +776,35 @@ def _lint_attribute_name(
"""Check an attribute name for deprecations and validity according to the
surrounding promise type."""
assert node.type == "attribute_name"
if state.strict and _text(node) == "ifvarclass":
attribute_name = _text(node)
assert attribute_name == state.attribute_name
if state.strict and state.promise_type == "vars" and attribute_name == "str":
raise ValidationError(
f"Deprecation: Use 'string' instead of 'str' {location}", node
)
if state.strict and attribute_name == "ifvarclass":
raise ValidationError(
f"Deprecation: Use 'if' instead of 'ifvarclass' {location}", node
)
if state.promise_type and state.attribute_name:
if state.promise_type and attribute_name:
promise_type_data = syntax_data.BUILTIN_PROMISE_TYPES.get(
state.promise_type, {}
)
if not promise_type_data:
# Custom promise type - we cannot validate attribute name here.
return
promise_type_attrs = promise_type_data.get("attributes", {})
if state.attribute_name not in promise_type_attrs:
if attribute_name not in promise_type_attrs:
raise ValidationError(
f"Error: Invalid attribute '{state.attribute_name}' for promise type '{state.promise_type}' {location}",
f"Error: Invalid attribute '{attribute_name}' for promise type '{state.promise_type}' {location}",
node,
)
if state.block_keyword == "promise" and state.attribute_name not in (
if state.block_keyword == "promise" and attribute_name not in (
None,
*PROMISE_BLOCK_ATTRIBUTES,
):
raise ValidationError(
f"Error: Invalid attribute name '{state.attribute_name}' in '{state.block_name}' custom promise type definition {location}",
f"Error: Invalid attribute name '{attribute_name}' in '{state.block_name}' custom promise type definition {location}",
node,
)

Expand Down
17 changes: 17 additions & 0 deletions tests/lint/002_deprecations.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

{
defaults:
^-------^
Deprecation: Promise type 'defaults' is deprecated at tests/lint/002_deprecations.x.cf:3:3

vars:
"x" str => "value";
^-^
Deprecation: Use 'string' instead of 'str' at tests/lint/002_deprecations.x.cf:6:9

"Hello, CFEngine"
ifvarclass => "cfengine";
^--------^
Deprecation: Use 'if' instead of 'ifvarclass' at tests/lint/002_deprecations.x.cf:9:7
FAIL: tests/lint/002_deprecations.x.cf (3 errors)
Failure, 3 errors in total.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
bundle agent main
{
defaults:
"x" string => "value";
vars:
"x" str => "value";
reports:
"Hello, CFEngine"
ifvarclass => "cfengine";
Expand Down
7 changes: 0 additions & 7 deletions tests/lint/002_ifvarclass.expected.txt

This file was deleted.

5 changes: 0 additions & 5 deletions tests/lint/003_deprecated_promise_type.cf

This file was deleted.

7 changes: 0 additions & 7 deletions tests/lint/003_deprecated_promise_type.expected.txt

This file was deleted.

5 changes: 0 additions & 5 deletions tests/lint/003_deprecated_promise_type.x.cf

This file was deleted.

16 changes: 16 additions & 0 deletions tests/lint/003_name_lowercase.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bundle agent my_bundle
{
reports:
"Hello";
}

body perms my_body
{
owners => { "root" };
}

promise agent my_promise_type
{
path => "/bin/true";
interpreter => "/bin/bash";
}
16 changes: 16 additions & 0 deletions tests/lint/003_name_lowercase.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

bundle agent MyBundle
^------^
Convention: Bundle name should be lowercase at tests/lint/003_name_lowercase.x.cf:1:14


body perms MyBody
^----^
Convention: Body name should be lowercase at tests/lint/003_name_lowercase.x.cf:7:12


promise agent MyPromiseType
^-----------^
Convention: Promise type should be lowercase at tests/lint/003_name_lowercase.x.cf:12:15
FAIL: tests/lint/003_name_lowercase.x.cf (3 errors)
Failure, 3 errors in total.
16 changes: 16 additions & 0 deletions tests/lint/003_name_lowercase.x.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bundle agent MyBundle
{
reports:
"Hello";
}

body perms MyBody
{
owners => { "root" };
}

promise agent MyPromiseType
{
path => "/bin/true";
interpreter => "/bin/bash";
}
5 changes: 0 additions & 5 deletions tests/lint/004_bundle_name_lowercase.cf

This file was deleted.

6 changes: 0 additions & 6 deletions tests/lint/004_bundle_name_lowercase.expected.txt

This file was deleted.

5 changes: 0 additions & 5 deletions tests/lint/004_bundle_name_lowercase.x.cf

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

bundle notavalidtype my_bundle
^-----------^
Error: Bundle type must be one of (agent, common, edit_line, edit_xml, monitor, server), not 'notavalidtype' at tests/lint/005_bundle_type.x.cf:1:8
FAIL: tests/lint/005_bundle_type.x.cf (1 error)
Error: Bundle type must be one of (agent, common, edit_line, edit_xml, monitor, server), not 'notavalidtype' at tests/lint/004_bundle_type.x.cf:1:8
FAIL: tests/lint/004_bundle_type.x.cf (1 error)
Failure, 1 error in total.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/lint/005_syntax_error.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

{
reports
^-----^
Error: Syntax error at tests/lint/005_syntax_error.x.cf:3:3
FAIL: tests/lint/005_syntax_error.x.cf (1 error)
Failure, 1 error in total.
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/lint/006_empty_file.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error: Empty policy file 'tests/lint/006_empty_file.x.cf'
FAIL: tests/lint/006_empty_file.x.cf (1 error)
Failure, 1 error in total.
File renamed without changes.
7 changes: 0 additions & 7 deletions tests/lint/006_syntax_error.expected.txt

This file was deleted.

3 changes: 0 additions & 3 deletions tests/lint/007_empty_file.expected.txt

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
methods:
"x" usebundle => default:target("arg");
^------------^
Error: Call to unknown bundle 'default:target' at tests/lint/008_namespace.x.cf:15:22
FAIL: tests/lint/008_namespace.x.cf (1 error)
Error: Call to unknown bundle 'default:target' at tests/lint/007_namespace.x.cf:15:22
FAIL: tests/lint/007_namespace.x.cf (1 error)
Failure, 1 error in total.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

bundle agent isvariable
^--------^
Error: Bundle 'isvariable' conflicts with built-in function with the same name at tests/lint/009_bundle_shadows_function.x.cf:1:14
FAIL: tests/lint/009_bundle_shadows_function.x.cf (1 error)
Error: Bundle 'isvariable' conflicts with built-in function with the same name at tests/lint/008_bundle_shadows_function.x.cf:1:14
FAIL: tests/lint/008_bundle_shadows_function.x.cf (1 error)
Failure, 1 error in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
vars:
"x" string => target("arg");
^----^
Error: Call to unknown function 'target' inside 'vars'-promise at tests/lint/010_unknown_function_inside_vars.x.cf:16:19
Error: Call to unknown function 'target' inside 'vars'-promise at tests/lint/009_unknown_function_inside_vars.x.cf:16:19

"y"
classes => isvariable("arg"),
^--------^
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
FAIL: tests/lint/010_unknown_function_inside_vars.x.cf (2 errors)
Error: 'isvariable' is not a defined body. Only bodies may be called with 'classes' at tests/lint/009_unknown_function_inside_vars.x.cf:18:18
FAIL: tests/lint/009_unknown_function_inside_vars.x.cf (2 errors)
Failure, 2 errors in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
policy => "free",
real => "0.5";
^--^
Error: Mutually exclusive attribute values (slist, int, real) for a single promiser inside vars-promise at tests/lint/011_mutually_exclusive_types_vars.x.cf:4:5
Error: Mutually exclusive attribute values (slist, int, real) for a single promiser inside vars-promise at tests/lint/010_mutually_exclusive_types_vars.x.cf:4:5


"missing-error";
^--------------^
Error: Missing value for vars promise "missing-error" at tests/lint/011_mutually_exclusive_types_vars.x.cf:16:5
FAIL: tests/lint/011_mutually_exclusive_types_vars.x.cf (2 errors)
Error: Missing value for vars promise "missing-error" at tests/lint/010_mutually_exclusive_types_vars.x.cf:16:5
FAIL: tests/lint/010_mutually_exclusive_types_vars.x.cf (2 errors)
Failure, 2 errors in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
path => "/var/cfengine/inputs/modules/promises/git.py";
blah => "something";
^--^
Error: Invalid attribute name 'blah' in 'git' custom promise type definition at tests/lint/012_invalid_attributes.x.cf:5:3
Error: Invalid attribute name 'blah' in 'git' custom promise type definition at tests/lint/011_invalid_attributes.x.cf:5:3

slist => {},
bar => "";
^-^
Error: Invalid attribute 'bar' for promise type 'vars' at tests/lint/012_invalid_attributes.x.cf:12:7
FAIL: tests/lint/012_invalid_attributes.x.cf (2 errors)
Error: Invalid attribute 'bar' for promise type 'vars' at tests/lint/011_invalid_attributes.x.cf:12:7
FAIL: tests/lint/011_invalid_attributes.x.cf (2 errors)
Failure, 2 errors in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"string1"
string => canonify();
^--------^
Error: Expected 1 arguments, received 0 for function 'canonify' at tests/lint/013_function_call_arg_count.x.cf:5:15
Error: Expected 1 arguments, received 0 for function 'canonify' at tests/lint/012_function_call_arg_count.x.cf:5:15

"string3"
string => canonify("test", "test");
^----------------------^
Error: Expected 1 arguments, received 2 for function 'canonify' at tests/lint/013_function_call_arg_count.x.cf:9:15
FAIL: tests/lint/013_function_call_arg_count.x.cf (2 errors)
Error: Expected 1 arguments, received 2 for function 'canonify' at tests/lint/012_function_call_arg_count.x.cf:9:15
FAIL: tests/lint/012_function_call_arg_count.x.cf (2 errors)
Failure, 2 errors in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
create => "true",
perms => mog("644");
^--------^
Error: Expected 3 arguments, received 1 for body 'mog' at tests/lint/014_num_args_body.x.cf:13:16
Hint: The body 'mog' is defined at tests/lint/014_num_args_body.x.cf:1:12
Error: Expected 3 arguments, received 1 for body 'mog' at tests/lint/013_num_args_body.x.cf:13:16
Hint: The body 'mog' is defined at tests/lint/013_num_args_body.x.cf:1:12

create => "true",
perms => mog("644", "root");
^----------------^
Error: Expected 3 arguments, received 2 for body 'mog' at tests/lint/014_num_args_body.x.cf:16:16
Hint: The body 'mog' is defined at tests/lint/014_num_args_body.x.cf:1:12
Error: Expected 3 arguments, received 2 for body 'mog' at tests/lint/013_num_args_body.x.cf:16:16
Hint: The body 'mog' is defined at tests/lint/013_num_args_body.x.cf:1:12

create => "true",
perms => mog("644", "root", "root", "root");
^--------------------------------^
Error: Expected 3 arguments, received 4 for body 'mog' at tests/lint/014_num_args_body.x.cf:22:16
Hint: The body 'mog' is defined at tests/lint/014_num_args_body.x.cf:1:12
FAIL: tests/lint/014_num_args_body.x.cf (3 errors)
Error: Expected 3 arguments, received 4 for body 'mog' at tests/lint/013_num_args_body.x.cf:22:16
Hint: The body 'mog' is defined at tests/lint/013_num_args_body.x.cf:1:12
FAIL: tests/lint/013_num_args_body.x.cf (3 errors)
Failure, 3 errors in total.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"test1"
usebundle => test();
^----^
Error: Expected 2 arguments, received 0 for bundle 'test' at tests/lint/014_num_args_bundle.x.cf:5:20
Hint: The bundle 'test' is defined at tests/lint/014_num_args_bundle.x.cf:13:14
Error: Expected 2 arguments, received 0 for bundle 'test' at tests/lint/013_num_args_bundle.x.cf:5:20
Hint: The bundle 'test' is defined at tests/lint/013_num_args_bundle.x.cf:13:14

"test2"
usebundle => test("a");
^-------^
Error: Expected 2 arguments, received 1 for bundle 'test' at tests/lint/014_num_args_bundle.x.cf:7:20
Hint: The bundle 'test' is defined at tests/lint/014_num_args_bundle.x.cf:13:14
Error: Expected 2 arguments, received 1 for bundle 'test' at tests/lint/013_num_args_bundle.x.cf:7:20
Hint: The bundle 'test' is defined at tests/lint/013_num_args_bundle.x.cf:13:14

"test4"
usebundle => test("a", "b", "c");
^-----------------^
Error: Expected 2 arguments, received 3 for bundle 'test' at tests/lint/014_num_args_bundle.x.cf:11:20
Hint: The bundle 'test' is defined at tests/lint/014_num_args_bundle.x.cf:13:14
FAIL: tests/lint/014_num_args_bundle.x.cf (3 errors)
Error: Expected 2 arguments, received 3 for bundle 'test' at tests/lint/013_num_args_bundle.x.cf:11:20
Hint: The bundle 'test' is defined at tests/lint/013_num_args_bundle.x.cf:13:14
FAIL: tests/lint/013_num_args_bundle.x.cf (3 errors)
Failure, 3 errors in total.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"users3" slist => getusers("a", "b");
"users4" slist => getusers("a", "b", "c", "d");
^--------------------------^
Error: Expected 0-2 arguments, received 4 for function 'getusers' at tests/lint/015_variadic_func_arg_count.x.cf:7:23
Error: Expected 0-2 arguments, received 4 for function 'getusers' at tests/lint/014_variadic_func_arg_count.x.cf:7:23

"file1" string => readfile("/tmp/tmp.txt");
"file2" string => readfile("/tmp/tmp.txt", "100", "/tmp/tmp2.txt");
^----------------------------------------------^
Error: Expected 1-2 arguments, received 3 for function 'readfile' at tests/lint/015_variadic_func_arg_count.x.cf:9:23
FAIL: tests/lint/015_variadic_func_arg_count.x.cf (2 errors)
Error: Expected 1-2 arguments, received 3 for function 'readfile' at tests/lint/014_variadic_func_arg_count.x.cf:9:23
FAIL: tests/lint/014_variadic_func_arg_count.x.cf (2 errors)
Failure, 2 errors in total.
14 changes: 14 additions & 0 deletions tests/lint/015_macro_multi_def_bundle.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

"test1"
usebundle => test();
^----^
Error: Expected 1 or 2 arguments, received 0 for bundle 'test' at tests/lint/015_macro_multi_def_bundle.x.cf:20:20
Hint: The bundle 'test' is defined at tests/lint/015_macro_multi_def_bundle.x.cf:2:14 and tests/lint/015_macro_multi_def_bundle.x.cf:8:14

"test2"
usebundle => test("hello", "world", "!");
^-------------------------^
Error: Expected 1 or 2 arguments, received 3 for bundle 'test' at tests/lint/015_macro_multi_def_bundle.x.cf:23:20
Hint: The bundle 'test' is defined at tests/lint/015_macro_multi_def_bundle.x.cf:2:14 and tests/lint/015_macro_multi_def_bundle.x.cf:8:14
FAIL: tests/lint/015_macro_multi_def_bundle.x.cf (2 errors)
Failure, 2 errors in total.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
string => $(bar);
string => $(bar);
^---------------^
Error: Found promise attribute with no parent-promiser outside of a macro at tests/lint/017_half_promises.x.cf:6:7
Error: Found promise attribute with no parent-promiser outside of a macro at tests/lint/016_half_promises.x.cf:6:7

string => $($(baz));
string => $($(baz));
^------------------^
Error: Multiple promise attributes with ending semicolon found inside macro 'minimum_version(3.18)' at tests/lint/017_half_promises.x.cf:9:7
Error: Multiple promise attributes with ending semicolon found inside macro 'minimum_version(3.18)' at tests/lint/016_half_promises.x.cf:9:7

# comment
string => $($(baz));
^------------------^
Error: Multiple promise attributes with ending semicolon found inside macro 'else' at tests/lint/017_half_promises.x.cf:26:7
FAIL: tests/lint/017_half_promises.x.cf (3 errors)
Error: Multiple promise attributes with ending semicolon found inside macro 'else' at tests/lint/016_half_promises.x.cf:26:7
FAIL: tests/lint/016_half_promises.x.cf (3 errors)
Failure, 3 errors in total.
File renamed without changes.
Loading
Loading