cookstyle 5.17.4 → 5.18.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9542dd1a3bbc3a2f0f4db8c2108e67ef70cec17da89557746489ccc9dd89d4f5
4
- data.tar.gz: b2a67152f01ef162bfe42b833e15b627fe361403a2ad450b0f591bc490a1b551
3
+ metadata.gz: 56fb1dda6ccaeeb4a3f521c52407e12becf9f1b1625a89b4c893f415820e40c4
4
+ data.tar.gz: 46c9bf893653173d77d7282056dc81cb385ef2b6f7828cc5f76e0e4e8e51a3f4
5
5
  SHA512:
6
- metadata.gz: 1432f849f9464fabd9e9b17c559889f1c537671175b5690fb48d2f311030dd51c12746fc1dbad60dfd084fde4377e48ea5ee4c8a3bdd991f486f46ffafa8627e
7
- data.tar.gz: a0f4c2a88b75f3049967747a8fc183d922800ff0b84d0d11bcb853444d1755bc83c7ee90e02c70a0bdb04b4d65b208ee438093010fbcab184c5bbeb817f7ec62
6
+ metadata.gz: f3e1c5acbb968e895c0a8190c28635baa023574e797d0b56ba54bba776f97f24b7d0f718bb02b92080175f46f475500a02254db3bd6f92090fd89f5b2239ec4e
7
+ data.tar.gz: 44d2c68635670c76ac81c4941dd70a3fd99fa53cdf40654576584784fd88b256f6c94066716cdff8c8cb7b63b29f68b17524b654e7de4cba514d89803993941b
@@ -2,8 +2,8 @@ AllCops:
2
2
  TargetRubyVersion: 2.4
3
3
  TargetChefVersion: ~
4
4
  Exclude:
5
- - files/**/*
6
- - vendor/**/*
5
+ - '**/files/**/*'
6
+ - '**/vendor/**/*'
7
7
  - Guardfile
8
8
  ChefAttributes:
9
9
  Patterns:
@@ -1064,8 +1064,26 @@ ChefModernize/DslIncludeInResource:
1064
1064
  - '**/resources/*.rb'
1065
1065
  - '**/providers/*.rb'
1066
1066
 
1067
+ ChefModernize/ResourceForcingCompileTime:
1068
+ Description: The hostname, build_essential, chef_gem, and ohai_hint resources include 'compile_time' properties, which should be used to force the resources to run at compile time by setting `compile_time true`.
1069
+ Enabled: true
1070
+ VersionAdded: '5.18.0'
1071
+ Exclude:
1072
+ - '**/metadata.rb'
1073
+ - '**/attributes/*.rb'
1074
+ - '**/Berksfile'
1075
+
1076
+ ChefModernize/ExecuteSysctl:
1077
+ Description: Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
1078
+ Enabled: true
1079
+ VersionAdded: '5.18.0'
1080
+ Exclude:
1081
+ - '**/metadata.rb'
1082
+ - '**/attributes/*.rb'
1083
+ - '**/Berksfile'
1084
+
1067
1085
  ###############################
1068
- # ChefRedundantCode: Cleanup unncessary code in your cookbooks regardless of chef-client release
1086
+ # ChefRedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
1069
1087
  ###############################
1070
1088
 
1071
1089
  ChefRedundantCode/ConflictsMetadata:
@@ -1892,7 +1910,7 @@ Lint/BigDecimalNew:
1892
1910
  Lint/UnneededCopEnableDirective:
1893
1911
  Enabled: true
1894
1912
 
1895
- # get people on a much simpler ruby 2.4 way of doing things
1913
+ # get people on a much simpler ruby 2.4+ way of doing things
1896
1914
  Style/UnpackFirst:
1897
1915
  Enabled: true
1898
1916
 
@@ -1,4 +1,4 @@
1
1
  module Cookstyle
2
- VERSION = "5.17.4".freeze # rubocop: disable Style/StringLiterals
2
+ VERSION = "5.18.4".freeze # rubocop: disable Style/StringLiterals
3
3
  RUBOCOP_VERSION = '0.75.1'.freeze
4
4
  end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Copyright:: 2020, 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
+ module ChefModernize
21
+ # The hostname, build_essential, chef_gem, and ohai_hint resources include 'compile_time' properties, which should be used to force the resources to run at compile time by setting `compile_time true`.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # build_essential 'install build tools' do
27
+ # action :nothing
28
+ # end.run_action(:install)
29
+ #
30
+ # # good
31
+ # build_essential 'install build tools' do
32
+ # compile_time true
33
+ # end
34
+ #
35
+ class ResourceForcingCompileTime < Cop
36
+ MSG = "Set 'compile_time true' in resources when available instead of forcing resources to run at compile time by setting an action on the block.".freeze
37
+
38
+ def_node_matcher :compile_time_resource?, <<-PATTERN
39
+ (send (block (send nil? {:build_essential :chef_gem :hostname :ohai_hint} (...)) (args) (...)) $:run_action (sym ...))
40
+ PATTERN
41
+
42
+ def on_send(node)
43
+ compile_time_resource?(node) do
44
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,73 @@
1
+ #
2
+ # Copyright:: 2020, 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
+ module ChefModernize
21
+ # Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.
22
+ #
23
+ # # bad
24
+ # file '/etc/sysctl.d/ipv4.conf' do
25
+ # owner 'root'
26
+ # group 'root'
27
+ # mode '0755'
28
+ # notifies :run, 'service[sysctl -p /etc/sysctl.d/ipv4.conf]', :immediately
29
+ # content '9000 65500'
30
+ # end
31
+ #
32
+ # execute 'sysctl -p /etc/sysctl.d/ipv4.conf' do
33
+ # action :nothing
34
+ # end
35
+ #
36
+ # # good
37
+ # sysctl 'net.ipv4.ip_local_port_range' do
38
+ # value '9000 65500'
39
+ # end
40
+ #
41
+ class ExecuteSysctl < Cop
42
+ include RuboCop::Chef::CookbookHelpers
43
+ extend TargetChefVersion
44
+
45
+ minimum_target_chef_version '14.0'
46
+
47
+ MSG = 'Chef Infra Client 14.0 and later includes a sysctl resource that should be used to idempotently load sysctl values instead of templating files and using execute to load them.'.freeze
48
+
49
+ # non block execute resources
50
+ def on_send(node)
51
+ return unless node.method_name == :execute
52
+
53
+ # use a regex on source instead of .value in case there's string interpolation which adds a complex dstr type
54
+ # with a nested string and a begin. Source allows us to avoid a lot of defensive programming here
55
+ if node&.arguments.first&.source&.match?(/^("|')sysctl -p/)
56
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
57
+ end
58
+ end
59
+
60
+ # block execute resources
61
+ def on_block(node)
62
+ match_property_in_resource?(:execute, 'command', node) do |code_property|
63
+ property_data = method_arg_ast_to_string(code_property)
64
+ if property_data && property_data.match?(/^sysctl -p/i)
65
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2019, Chef Software Inc.
2
+ # Copyright:: Copyright 2019-2020, Chef Software Inc.
3
3
  # Author:: Tim Smith (<tsmith@chef.io>)
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -61,35 +61,18 @@ module RuboCop
61
61
  class NamePropertyIsRequired < Cop
62
62
  MSG = 'Resource properties marked as name properties should not also be required properties'.freeze
63
63
 
64
+ # match on a property or attribute that has any name and any type and a hash that
65
+ # contains name_property: true and required: true. These are wrapped in <> which means
66
+ # the order doesn't matter in the hash.
67
+ def_node_matcher :name_property_and_required?, <<-PATTERN
68
+ (send nil? {:property :attribute} (sym _) ... (hash <(pair (sym {:name_property :name_attribute}) (true)) $(pair (sym :required) (true) ) ...>))
69
+ PATTERN
70
+
64
71
  def on_send(node)
65
- if required_property?(node) && property_is_name_property?(node)
72
+ name_property_and_required?(node) do
66
73
  add_offense(node, location: :expression, message: MSG, severity: :refactor)
67
74
  end
68
75
  end
69
-
70
- private
71
-
72
- def property_is_name_property?(node)
73
- if %i(property attribute).include?(node.method_name)
74
- node.arguments.each do |arg|
75
- if arg.type == :hash
76
- return true if arg.source.match?(/name_(property|attribute):\s*true/)
77
- end
78
- end
79
- false # no required: true found
80
- end
81
- end
82
-
83
- def required_property?(node)
84
- if %i(property attribute).include?(node.method_name)
85
- node.arguments.each do |arg|
86
- if arg.type == :hash
87
- return true if arg.source.match?(/required:\s*true/)
88
- end
89
- end
90
- false # no default: found
91
- end
92
- end
93
76
  end
94
77
  end
95
78
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright 2019, Chef Software Inc.
2
+ # Copyright:: Copyright 2019-2020, Chef Software Inc.
3
3
  # Author:: Tim Smith (<tsmith@chef.io>)
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,35 +32,29 @@ module RuboCop
32
32
  # property :bob, String, required: true
33
33
  #
34
34
  class PropertyWithRequiredAndDefault < Cop
35
- MSG = 'Resource property should not be both required and have a default value. This will fail on Chef Infra Client 13+'.freeze
35
+ include RangeHelp
36
36
 
37
- def on_send(node)
38
- if required_property?(node) && property_has_default?(node)
39
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
40
- end
41
- end
37
+ MSG = 'Resource properties should not be both required and have a default value. This will fail on Chef Infra Client 13+'.freeze
42
38
 
43
- private
39
+ # match on a property or attribute that has any name and any type and a hash that
40
+ # contains default: true and required: true. These are wrapped in <> which means
41
+ # the order doesn't matter in the hash.
42
+ def_node_matcher :required_and_default?, <<-PATTERN
43
+ (send nil? {:property :attribute} (sym _) ... (hash <$(pair (sym :default) ...) (pair (sym :required) true) ...>))
44
+ PATTERN
44
45
 
45
- def required_property?(node)
46
- if node.method_name == :property
47
- node.arguments.each do |arg|
48
- if arg.type == :hash
49
- return true if arg.source.match?(/required:\s*true/)
50
- end
51
- end
52
- false # no required: true found
46
+ def on_send(node)
47
+ required_and_default?(node) do
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
53
49
  end
54
50
  end
55
51
 
56
- def property_has_default?(node)
57
- if node.method_name == :property
58
- node.arguments.each do |arg|
59
- if arg.type == :hash
60
- return true if arg.source.match?(/default:/)
61
- end
52
+ def autocorrect(node)
53
+ lambda do |corrector|
54
+ required_and_default?(node) do |default|
55
+ range = range_with_surrounding_comma(range_with_surrounding_space(range: default.loc.expression, side: :left), :left)
56
+ corrector.remove(range)
62
57
  end
63
- false # no default: found
64
58
  end
65
59
  end
66
60
  end
@@ -24,28 +24,39 @@ module RuboCop
24
24
  #
25
25
  # # bad
26
26
  # property :foo, [TrueClass, FalseClass]
27
+ # attribute :foo, kind_of: [TrueClass, FalseClass]
27
28
  #
28
29
  # # good
29
30
  # property :foo, [true, false]
31
+ # attribute :foo, kind_of: [true, false]
30
32
  #
31
33
  class TrueClassFalseClassResourceProperties < Cop
32
34
  MSG = "When setting the allowed types for a resource to accept either true or false values it's much simpler to use true and false instead of TrueClass and FalseClass.".freeze
33
35
 
36
+ def_node_matcher :trueclass_falseclass_array?, <<-PATTERN
37
+ (array (const nil? :TrueClass) (const nil? :FalseClass))
38
+ PATTERN
39
+
40
+ def_node_matcher :tf_in_kind_of_hash?, <<-PATTERN
41
+ (hash (pair (sym :kind_of) #trueclass_falseclass_array? ))
42
+ PATTERN
43
+
34
44
  def_node_matcher :trueclass_falseclass_property?, <<-PATTERN
35
- (send nil? {:property :attribute} (sym _) $(array (const nil? :TrueClass) (const nil? :FalseClass)) ... )
45
+ (send nil? {:property :attribute} (sym _)
46
+
47
+ ${#tf_in_kind_of_hash? #trueclass_falseclass_array? } ... )
36
48
  PATTERN
37
49
 
38
50
  def on_send(node)
39
- trueclass_falseclass_property?(node) do
40
- add_offense(node, location: :expression, message: MSG, severity: :refactor)
51
+ trueclass_falseclass_property?(node) do |tf_match|
52
+ add_offense(tf_match, location: :expression, message: MSG, severity: :refactor)
41
53
  end
42
54
  end
43
55
 
44
56
  def autocorrect(node)
45
57
  lambda do |corrector|
46
- trueclass_falseclass_property?(node) do |types|
47
- corrector.replace(types.loc.expression, '[true, false]')
48
- end
58
+ replacement_text = node.hash_type? ? 'kind_of: [true, false]' : '[true, false]'
59
+ corrector.replace(node.loc.expression, replacement_text)
49
60
  end
50
61
  end
51
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cookstyle
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.17.4
4
+ version: 5.18.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thom May
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-12-20 00:00:00.000000000 Z
12
+ date: 2020-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubocop
@@ -129,6 +129,7 @@ files:
129
129
  - lib/rubocop/cop/chef/modernize/build_essential.rb
130
130
  - lib/rubocop/cop/chef/modernize/chef_14_resources.rb
131
131
  - lib/rubocop/cop/chef/modernize/chef_gem_nokogiri.rb
132
+ - lib/rubocop/cop/chef/modernize/compile_time_resources.rb
132
133
  - lib/rubocop/cop/chef/modernize/cron_manage_resource.rb
133
134
  - lib/rubocop/cop/chef/modernize/default_action_initializer.rb
134
135
  - lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb
@@ -139,6 +140,7 @@ files:
139
140
  - lib/rubocop/cop/chef/modernize/execute_apt_update.rb
140
141
  - lib/rubocop/cop/chef/modernize/execute_sc_exe.rb
141
142
  - lib/rubocop/cop/chef/modernize/execute_sleep.rb
143
+ - lib/rubocop/cop/chef/modernize/execute_sysctl.rb
142
144
  - lib/rubocop/cop/chef/modernize/execute_tzutil.rb
143
145
  - lib/rubocop/cop/chef/modernize/foodcritic_comments.rb
144
146
  - lib/rubocop/cop/chef/modernize/if_provides_default_action.rb