cookstyle 5.0.4 → 5.1.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +9 -0
  3. data/bin/cookstyle +7 -0
  4. data/config/cookstyle.yml +195 -7
  5. data/config/upstream.yml +1 -1
  6. data/lib/cookstyle.rb +1 -0
  7. data/lib/cookstyle/version.rb +1 -1
  8. data/lib/rubocop/chef/cookbook_only.rb +4 -3
  9. data/lib/rubocop/cop/chef/correctness/insecure_cookbook_url.rb +60 -0
  10. data/lib/rubocop/cop/chef/correctness/name_property_and_required.rb +94 -0
  11. data/lib/rubocop/cop/chef/correctness/node_normal.rb +53 -0
  12. data/lib/rubocop/cop/chef/correctness/node_normal_unless.rb +53 -0
  13. data/lib/rubocop/cop/chef/correctness/property_with_default_and_required.rb +67 -0
  14. data/lib/rubocop/cop/chef/correctness/property_with_name_attribute.rb +59 -0
  15. data/lib/rubocop/cop/chef/{service_resource.rb → correctness/service_resource.rb} +1 -1
  16. data/lib/rubocop/cop/chef/{tmp_path.rb → correctness/tmp_path.rb} +1 -1
  17. data/lib/rubocop/cop/chef/deprecation/attribute_metadata.rb +49 -0
  18. data/lib/rubocop/cop/chef/deprecation/conflicts_metadata.rb +44 -0
  19. data/lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb +48 -0
  20. data/lib/rubocop/cop/chef/deprecation/depends_partial_search.rb +42 -0
  21. data/lib/rubocop/cop/chef/deprecation/depends_poise.rb +42 -0
  22. data/lib/rubocop/cop/chef/deprecation/easy_install.rb +39 -0
  23. data/lib/rubocop/cop/chef/deprecation/epic_fail.rb +50 -0
  24. data/lib/rubocop/cop/chef/deprecation/erl_call.rb +39 -0
  25. data/lib/rubocop/cop/chef/{node_set.rb → deprecation/node_set.rb} +2 -2
  26. data/lib/rubocop/cop/chef/deprecation/node_set_unless.rb +53 -0
  27. data/lib/rubocop/cop/chef/deprecation/provides_metadata.rb +44 -0
  28. data/lib/rubocop/cop/chef/deprecation/replaces_metadata.rb +44 -0
  29. data/lib/rubocop/cop/chef/deprecation/suggests_metadata.rb +44 -0
  30. data/lib/rubocop/cop/chef/effortless/data_bags.rb +36 -0
  31. data/lib/rubocop/cop/chef/effortless/search_used.rb +36 -0
  32. data/lib/rubocop/cop/chef/modernize/berksfile_source.rb +59 -0
  33. data/lib/rubocop/cop/chef/modernize/build_essential.rb +52 -0
  34. data/lib/rubocop/cop/chef/modernize/chef_14_resources.rb +54 -0
  35. data/lib/rubocop/cop/chef/modernize/why_run_supported_true.rb +46 -0
  36. data/lib/rubocop/cop/chef/{attribute_keys.rb → style/attribute_keys.rb} +2 -2
  37. data/lib/rubocop/cop/chef/style/comment_sentence_spacing.rb +42 -0
  38. data/lib/rubocop/cop/chef/{comments_copyright_format.rb → style/comments_copyright_format.rb} +5 -2
  39. data/lib/rubocop/cop/chef/{comments_format.rb → style/comments_format.rb} +4 -2
  40. data/lib/rubocop/cop/chef/{file_mode.rb → style/file_mode.rb} +1 -3
  41. metadata +34 -9
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright:: Copyright 2019-2019, Chef Software Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ module RuboCop
17
+ module Cop
18
+ module Chef
19
+ # Normal attributes are discouraged since their semantics differ importantly from the
20
+ # default and override levels. Their values persist in the node object even after
21
+ # all code referencing them has been deleted, unlike default and override.
22
+ #
23
+ # Code should be updated to use default or override levels, but this will change
24
+ # attribute merging behavior so needs to be validated manually and force_default or
25
+ # force_override levels may need to be used in recipe code.
26
+ #
27
+ # @example
28
+ #
29
+ # # bad
30
+ # node.normal['foo'] = true
31
+ #
32
+ # # good
33
+ # node.default['foo'] = true
34
+ # node.override['foo'] = true
35
+ # node.force_default['foo'] = true
36
+ # node.force_override['foo'] = true
37
+ #
38
+ class NodeNormal < Cop
39
+ MSG = 'Do not use node.normal. Replace with default/override/force_default/force_override attribute levels.'.freeze
40
+
41
+ def_node_matcher :node_normal?, <<-PATTERN
42
+ (send (send _ :node) :normal)
43
+ PATTERN
44
+
45
+ def on_send(node)
46
+ node_normal?(node) do
47
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright:: Copyright 2019-2019, Chef Software Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ module RuboCop
17
+ module Cop
18
+ module Chef
19
+ # Normal attributes are discouraged since their semantics differ importantly from the
20
+ # default and override levels. Their values persist in the node object even after
21
+ # all code referencing them has been deleted, unlike default and override.
22
+ #
23
+ # Code should be updated to use default or override levels, but this will change
24
+ # attribute merging behavior so needs to be validated manually and force_default or
25
+ # force_override levels may need to be used in recipe code.
26
+ #
27
+ # @example
28
+ #
29
+ # # bad
30
+ # node.normal_unless['foo'] = true
31
+ #
32
+ # # good
33
+ # node.default_unless['foo'] = true
34
+ # node.override_unless['foo'] = true
35
+ # node.force_default_unless['foo'] = true
36
+ # node.force_override_unless['foo'] = true
37
+ #
38
+ class NodeNormalUnless < Cop
39
+ MSG = 'Do not use node.normal_unless. Replace with default/override/force_default/force_override attribute levels.'.freeze
40
+
41
+ def_node_matcher :node_normal_unless?, <<-PATTERN
42
+ (send (send _ :node) :normal_unless)
43
+ PATTERN
44
+
45
+ def on_send(node)
46
+ node_normal_unless?(node) do
47
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ #
2
+ # Copyright:: Copyright 2019, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ # When using properties in a custom resource you shouldn't set a property to
21
+ # required and then provide a default value. If a property is required the
22
+ # user will always pass in a value and the default will never be used.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # property :bob, String, required: true, default: 'foo'
28
+ #
29
+ # # good
30
+ # property :bob, String, required: true
31
+ #
32
+ class PropertyWithRequiredAndDefault < Cop
33
+ MSG = 'Resource property should not be both required and have a default value'.freeze
34
+
35
+ def on_send(node)
36
+ if required_property?(node) && property_has_default?(node)
37
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def required_property?(node)
44
+ if node.method_name == :property
45
+ node.arguments.each do |arg|
46
+ if arg.type == :hash
47
+ return true if arg.source.match?(/required:\s*true/)
48
+ end
49
+ end
50
+ false # no required: true found
51
+ end
52
+ end
53
+
54
+ def property_has_default?(node)
55
+ if node.method_name == :property
56
+ node.arguments.each do |arg|
57
+ if arg.type == :hash
58
+ return true if arg.source.match?(/default:/)
59
+ end
60
+ end
61
+ false # no default: found
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,59 @@
1
+ #
2
+ # Copyright:: Copyright 2019, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ # When using properties in a custom resource you should use name_property not
21
+ # the legacy name_attribute from the days of attributes
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # property :bob, String, name_attribute: true
27
+ #
28
+ # # good
29
+ # property :bob, String, name_property: true
30
+ #
31
+ class PropertyWithNameAttribute < Cop
32
+ MSG = 'Resource property sets name_attribute not name_property'.freeze
33
+
34
+ def on_send(node)
35
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if attribute_method_mix?(node)
36
+ end
37
+
38
+ def autocorrect(node)
39
+ lambda do |corrector|
40
+ corrector.replace(node.loc.expression, node.source.gsub('name_attribute', 'name_property'))
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def attribute_method_mix?(node)
47
+ if node.method_name == :property
48
+ node.arguments.each do |arg|
49
+ if arg.type == :hash
50
+ return true if arg.source.match?(/name_attribute:/)
51
+ end
52
+ end
53
+ false # no name_attribute found
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -35,7 +35,7 @@ module RuboCop
35
35
  def on_send(node)
36
36
  execute_command?(node) do |command|
37
37
  if starts_service?(command)
38
- add_offense(command, location: :expression, message: MSG, severity: :warning)
38
+ add_offense(command, location: :expression, message: MSG, severity: :refactor)
39
39
  end
40
40
  end
41
41
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  def on_send(node)
39
39
  remote_file?(node) do |command|
40
40
  if hardcoded_tmp?(command) && !file_cache_path?(command)
41
- add_offense(command, location: :expression, message: MSG, severity: :warning)
41
+ add_offense(command, location: :expression, message: MSG, severity: :refactor)
42
42
  end
43
43
  end
44
44
  end
@@ -0,0 +1,49 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # Don't use the deprecated 'attribute' metadata value
22
+ #
23
+ # @example
24
+ #
25
+ # # bad in metadata.rb:
26
+ #
27
+ # attribute 'zookeeper_bridge/server',
28
+ # display_name: 'zookeeper server',
29
+ # description: 'Zookeeper server address.',
30
+ # type: 'string',
31
+ # required: 'optional',
32
+ # default: '"127.0.0.1:2181"'
33
+
34
+ class AttributeMetadata < Cop
35
+ MSG = "Don't use the deprecated 'attribute' metadata value".freeze
36
+
37
+ def on_send(node)
38
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :attribute
39
+ end
40
+
41
+ def autocorrect(node)
42
+ lambda do |corrector|
43
+ corrector.remove(node.loc.expression)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ # Don't use the deprecated 'conflicts' metadata value
22
+ #
23
+ # @example
24
+ #
25
+ # # bad in metadata.rb:
26
+ #
27
+ # conflicts "another_cookbook"
28
+ #
29
+ class ConflictsMetadata < Cop
30
+ MSG = "Don't use the deprecated 'conflicts' metadata value".freeze
31
+
32
+ def on_send(node)
33
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :conflicts
34
+ end
35
+
36
+ def autocorrect(node)
37
+ lambda do |corrector|
38
+ corrector.remove(node.loc.expression)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software, Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ # Don't depend on the deprecated compat_resource cookbook made obsolete by Chef 12.19+
21
+ #
22
+ # @example
23
+ #
24
+ # # bad
25
+ # depends 'compat_resource'
26
+ #
27
+ class CookbookDependsOnCompatResource < Cop
28
+ MSG = "Don't depend on the deprecated compat_resource cookbook made obsolete by Chef 12.19+".freeze
29
+
30
+ def_node_matcher :depends_compat_resource?, <<-PATTERN
31
+ (send nil? :depends (str {"compat_resource"}))
32
+ PATTERN
33
+
34
+ def on_send(node)
35
+ depends_compat_resource?(node) do
36
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
37
+ end
38
+ end
39
+
40
+ def autocorrect(node)
41
+ lambda do |corrector|
42
+ corrector.remove(node.loc.expression)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ #
2
+ # Copyright:: 2019, Chef Software, Inc.
3
+ # Author:: Tim Smith (<tsmith@chef.io>)
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module RuboCop
18
+ module Cop
19
+ module Chef
20
+ # Don't depend on the partial_search cookbook made obsolete by Chef 13
21
+ #
22
+ # @example
23
+ #
24
+ # # bad
25
+ # depends 'partial_search'
26
+ #
27
+ class CookbookDependsOnPartialSearch < Cop
28
+ MSG = "Don't depend on the deprecated partial_search cookbook made obsolete by Chef 13".freeze
29
+
30
+ def_node_matcher :depends_partial_search?, <<-PATTERN
31
+ (send nil? :depends (str {"partial_search"}))
32
+ PATTERN
33
+
34
+ def on_send(node)
35
+ depends_partial_search?(node) do
36
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end