Skip to content

Commit 2003d51

Browse files
authored
Merge pull request #108 from olehermanse/str
cfengine lint: Added support for deprecated str attribute in vars promises
2 parents 92ef827 + 2dbf9f8 commit 2003d51

62 files changed

Lines changed: 159 additions & 123 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/cfengine_cli/lint.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"rlist",
5151
"slist",
5252
"string",
53+
"str", # deprecated shorthand for string
5354
}
5455
PROMISE_BLOCK_ATTRIBUTES = ("path", "interpreter")
5556

@@ -775,29 +776,35 @@ def _lint_attribute_name(
775776
"""Check an attribute name for deprecations and validity according to the
776777
surrounding promise type."""
777778
assert node.type == "attribute_name"
778-
if state.strict and _text(node) == "ifvarclass":
779+
attribute_name = _text(node)
780+
assert attribute_name == state.attribute_name
781+
if state.strict and state.promise_type == "vars" and attribute_name == "str":
782+
raise ValidationError(
783+
f"Deprecation: Use 'string' instead of 'str' {location}", node
784+
)
785+
if state.strict and attribute_name == "ifvarclass":
779786
raise ValidationError(
780787
f"Deprecation: Use 'if' instead of 'ifvarclass' {location}", node
781788
)
782-
if state.promise_type and state.attribute_name:
789+
if state.promise_type and attribute_name:
783790
promise_type_data = syntax_data.BUILTIN_PROMISE_TYPES.get(
784791
state.promise_type, {}
785792
)
786793
if not promise_type_data:
787794
# Custom promise type - we cannot validate attribute name here.
788795
return
789796
promise_type_attrs = promise_type_data.get("attributes", {})
790-
if state.attribute_name not in promise_type_attrs:
797+
if attribute_name not in promise_type_attrs:
791798
raise ValidationError(
792-
f"Error: Invalid attribute '{state.attribute_name}' for promise type '{state.promise_type}' {location}",
799+
f"Error: Invalid attribute '{attribute_name}' for promise type '{state.promise_type}' {location}",
793800
node,
794801
)
795-
if state.block_keyword == "promise" and state.attribute_name not in (
802+
if state.block_keyword == "promise" and attribute_name not in (
796803
None,
797804
*PROMISE_BLOCK_ATTRIBUTES,
798805
):
799806
raise ValidationError(
800-
f"Error: Invalid attribute name '{state.attribute_name}' in '{state.block_name}' custom promise type definition {location}",
807+
f"Error: Invalid attribute name '{attribute_name}' in '{state.block_name}' custom promise type definition {location}",
801808
node,
802809
)
803810

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
{
3+
defaults:
4+
^-------^
5+
Deprecation: Promise type 'defaults' is deprecated at tests/lint/002_deprecations.x.cf:3:3
6+
7+
vars:
8+
"x" str => "value";
9+
^-^
10+
Deprecation: Use 'string' instead of 'str' at tests/lint/002_deprecations.x.cf:6:9
11+
12+
"Hello, CFEngine"
13+
ifvarclass => "cfengine";
14+
^--------^
15+
Deprecation: Use 'if' instead of 'ifvarclass' at tests/lint/002_deprecations.x.cf:9:7
16+
FAIL: tests/lint/002_deprecations.x.cf (3 errors)
17+
Failure, 3 errors in total.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
bundle agent main
22
{
3+
defaults:
4+
"x" string => "value";
5+
vars:
6+
"x" str => "value";
37
reports:
48
"Hello, CFEngine"
59
ifvarclass => "cfengine";

tests/lint/002_ifvarclass.expected.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/lint/003_deprecated_promise_type.cf

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/lint/003_deprecated_promise_type.expected.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/lint/003_deprecated_promise_type.x.cf

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/lint/003_name_lowercase.cf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bundle agent my_bundle
2+
{
3+
reports:
4+
"Hello";
5+
}
6+
7+
body perms my_body
8+
{
9+
owners => { "root" };
10+
}
11+
12+
promise agent my_promise_type
13+
{
14+
path => "/bin/true";
15+
interpreter => "/bin/bash";
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
bundle agent MyBundle
3+
^------^
4+
Convention: Bundle name should be lowercase at tests/lint/003_name_lowercase.x.cf:1:14
5+
6+
7+
body perms MyBody
8+
^----^
9+
Convention: Body name should be lowercase at tests/lint/003_name_lowercase.x.cf:7:12
10+
11+
12+
promise agent MyPromiseType
13+
^-----------^
14+
Convention: Promise type should be lowercase at tests/lint/003_name_lowercase.x.cf:12:15
15+
FAIL: tests/lint/003_name_lowercase.x.cf (3 errors)
16+
Failure, 3 errors in total.

tests/lint/003_name_lowercase.x.cf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bundle agent MyBundle
2+
{
3+
reports:
4+
"Hello";
5+
}
6+
7+
body perms MyBody
8+
{
9+
owners => { "root" };
10+
}
11+
12+
promise agent MyPromiseType
13+
{
14+
path => "/bin/true";
15+
interpreter => "/bin/bash";
16+
}

0 commit comments

Comments
 (0)