cookstyle 5.7.0 → 5.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/config/cookstyle.yml +43 -0
  3. data/cookstyle.gemspec +0 -2
  4. data/lib/cookstyle/version.rb +1 -1
  5. data/lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb +1 -2
  6. data/lib/rubocop/cop/chef/correctness/default_maintainer_metadata.rb +1 -2
  7. data/lib/rubocop/cop/chef/correctness/empty_metadata_field.rb +46 -0
  8. data/lib/rubocop/cop/chef/correctness/invalid_license_string.rb +1 -2
  9. data/lib/rubocop/cop/chef/correctness/invalid_version_metadata.rb +48 -0
  10. data/lib/rubocop/cop/chef/correctness/metadata_missing_name.rb +2 -2
  11. data/lib/rubocop/cop/chef/correctness/unnecessary_name_property.rb +53 -0
  12. data/lib/rubocop/cop/chef/deprecation/attribute_metadata.rb +1 -0
  13. data/lib/rubocop/cop/chef/deprecation/chefspec_coverage_report.rb +52 -0
  14. data/lib/rubocop/cop/chef/deprecation/chefspec_legacy_runner.rb +64 -0
  15. data/lib/rubocop/cop/chef/deprecation/deprecated_mixins.rb +1 -1
  16. data/lib/rubocop/cop/chef/deprecation/long_description_metadata.rb +1 -7
  17. data/lib/rubocop/cop/chef/deprecation/use_inline_resources.rb +2 -1
  18. data/lib/rubocop/cop/chef/deprecation/user_supports_property.rb +11 -0
  19. data/lib/rubocop/cop/chef/modernize/cron_manage_resource.rb +0 -2
  20. data/lib/rubocop/cop/chef/modernize/macos_user_defaults.rb +0 -2
  21. data/lib/rubocop/cop/chef/modernize/openssl_rsa_key_resource.rb +0 -2
  22. data/lib/rubocop/cop/chef/modernize/openssl_x509_resource.rb +0 -2
  23. data/lib/rubocop/cop/chef/modernize/osx_config_profile_resource.rb +0 -2
  24. data/lib/rubocop/cop/chef/modernize/resource_with_allowed_actions.rb +3 -19
  25. data/lib/rubocop/cop/chef/style/simplify_platform_major_version_check.rb +87 -0
  26. metadata +8 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e79627357ca125484e3ab900401f64dfef191b650282118279129b9fc3e1e65c
4
- data.tar.gz: c6633c204e5cf76711518d7f25cbb03c49814eb4a0534bf108fba69bd58c4f61
3
+ metadata.gz: 3125819fab3d282926b73935652abee3c33badfdd12bbb257d983df77f575968
4
+ data.tar.gz: 74522c27e329921082c8f04e4ba0570ae45e39ae897f13f87ffa5c54c8658974
5
5
  SHA512:
6
- metadata.gz: 04db3d494e31f545987ee9a5d74b5b238fb22e5a3c46904cee0956f3903c5402a0cedb1f922270a54eec3fa44691af0e79767e0fa2cf643bdd727e5d28abf30b
7
- data.tar.gz: b7459b727077fc0079872053a7f7dd4c07822e254a6c9476d99390d22dcf4b7c9284ca5aa1b908ba66673fd9f2116495eacfb19706b3cf69ce6d0c57d282f12b
6
+ metadata.gz: 1c0daefee8ecb8249105e48d6c9ff1578fd150847fa5abf55bb07e175367c923481dcfa1e57339ae757f61bc94a5f23a7856ac3b320ca5f13c9c554f56be12fa
7
+ data.tar.gz: 8128f87b453e15356b7814dec8e7b87e6ba73f60dde690de6db480b856711fbbd73b77acec3ebaae078251b4d74ad96fb422c53f3a401d0bbdfd8e8865240113
@@ -71,6 +71,13 @@ ChefStyle/UsePlatformHelpers:
71
71
  - '**/metadata.rb'
72
72
  - '**/libraries/*'
73
73
 
74
+ ChefStyle/SimplifyPlatformMajorVersionCheck:
75
+ Description: Use node['platform_version'].to_i instead of node['platform_version'].split('.').first or node['platform_version'].split('.')[0]
76
+ Enabled: true
77
+ VersionAdded: '5.8.0'
78
+ Exclude:
79
+ - '**/metadata.rb'
80
+
74
81
  ###############################
75
82
  # ChefCorrectness: Avoiding potential problems
76
83
  ###############################
@@ -208,6 +215,28 @@ ChefCorrectness/IncludingOhaiDefaultRecipe:
208
215
  Exclude:
209
216
  - '**/metadata.rb'
210
217
 
218
+ ChefCorrectness/UnnecessaryNameProperty:
219
+ Description: There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
220
+ Enabled: true
221
+ VersionAdded: '5.8.0'
222
+ Include:
223
+ - '**/resources/*.rb'
224
+ - '**/libraries/*.rb'
225
+
226
+ ChefCorrectness/EmptyMetadataField:
227
+ Description: metadata.rb should not include fields with an empty string. Either don't include the field or add a value.
228
+ Enabled: true
229
+ VersionAdded: '5.8.0'
230
+ Include:
231
+ - '**/metadata.rb'
232
+
233
+ ChefCorrectness/InvalidVersionMetadata:
234
+ Description: Cookbook metadata.rb version field should follow X.Y.Z version format.
235
+ Enabled: true
236
+ VersionAdded: '5.8.0'
237
+ Include:
238
+ - '**/metadata.rb'
239
+
211
240
  ###############################
212
241
  # ChefDeprecations: Resolving Deprecations that block upgrading Chef Infra Client
213
242
  ###############################
@@ -456,6 +485,20 @@ ChefDeprecations/ResourceUsesProviderBaseMethod:
456
485
  - '**/libraries/*.rb'
457
486
  - '**/resources/*.rb'
458
487
 
488
+ ChefDeprecations/ChefSpecCoverageReport:
489
+ Description: Don't use the deprecated ChefSpec coverage report functionality in your specs.
490
+ Enabled: true
491
+ VersionAdded: '5.8.0'
492
+ Include:
493
+ - '**/spec/*.rb'
494
+
495
+ ChefDeprecations/ChefSpecLegacyRunner:
496
+ Description: Use ChefSpec::SoloRunner or ChefSpec::ServerRunner instead of the deprecated ChefSpec::Runner.
497
+ Enabled: true
498
+ VersionAdded: '5.8.0'
499
+ Include:
500
+ - '**/spec/*.rb'
501
+
459
502
  ###############################
460
503
  # ChefModernize: Cleaning up legacy code and using new built-in resources
461
504
  ###############################
@@ -19,7 +19,5 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = %w(cookstyle)
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec'
24
22
  spec.add_dependency('rubocop', Cookstyle::RUBOCOP_VERSION)
25
23
  end
@@ -1,4 +1,4 @@
1
1
  module Cookstyle
2
- VERSION = "5.7.0".freeze # rubocop: disable Style/StringLiterals
2
+ VERSION = "5.8.1".freeze # rubocop: disable Style/StringLiterals
3
3
  RUBOCOP_VERSION = '0.72.0'.freeze
4
4
  end
@@ -18,8 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefCorrectness
21
- # A resource guard (not_if/only_if) that is a string should not be wrapped in {}.
22
- # Wrapping a guard string in {} causes it be executed as Ruby code which will always returns true instead of a shell command that will actually run.
21
+ # A resource guard (not_if/only_if) that is a string should not be wrapped in {}. Wrapping a guard string in {} causes it be executed as Ruby code which will always returns true instead of a shell command that will actually run.
23
22
  #
24
23
  # @example
25
24
  #
@@ -19,8 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefCorrectness
22
- # Metadata contains default maintainer information from the `chef generate cookbook`
23
- # command. This should be updated to reflect that actual maintainer of the cookbook.
22
+ # Metadata contains default maintainer information from the `chef generate cookbook` command. This should be updated to reflect that actual maintainer of the cookbook.
24
23
  #
25
24
  # @example
26
25
  #
@@ -0,0 +1,46 @@
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
+ module ChefCorrectness
22
+ # metadata.rb should not include fields with an empty string. Either don't include the field or add a value.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # license ''
28
+ #
29
+ # # good
30
+ # license 'Apache-2.0'
31
+ #
32
+ class EmptyMetadataField < Cop
33
+ MSG = 'Cookbook metadata.rb contains an field with an empty string.'.freeze
34
+
35
+ def_node_matcher :field?, '(send nil? _ $str ...)'
36
+
37
+ def on_send(node)
38
+ field?(node) do |str|
39
+ add_offense(str, location: :expression, message: MSG, severity: :refactor) if str.value.empty?
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -19,8 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefCorrectness
22
- # metadata.rb license field should include a SPDX compliant string
23
- # or "all right reserved" (not case sensitive)
22
+ # metadata.rb license field should include a SPDX compliant string or "all right reserved" (not case sensitive)
24
23
  #
25
24
  # @example
26
25
  #
@@ -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
+
18
+ module RuboCop
19
+ module Cop
20
+ module Chef
21
+ module ChefCorrectness
22
+ # Cookbook metadata.rb version field should follow X.Y.Z version format.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ # version '1.2.3.4'
28
+ #
29
+ # # good
30
+ # version '1.2.3'
31
+ #
32
+ class InvalidVersionMetadata < Cop
33
+ MSG = 'Cookbook metadata.rb version field should follow X.Y.Z version format.'.freeze
34
+
35
+ def_node_matcher :version?, '(send nil? :version $str ...)'
36
+
37
+ def on_send(node)
38
+ version?(node) do |ver|
39
+ if ver.value !~ /\A\d+\.\d+(\.\d+)?\z/ # entirely borrowed from Foodcritic.
40
+ add_offense(ver, location: :expression, message: MSG, severity: :refactor)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -18,7 +18,7 @@
18
18
  module RuboCop
19
19
  module Cop
20
20
  module Chef
21
- # metadata.rb needs to include the name method
21
+ # metadata.rb needs to include the name method or it will fail on Chef Infra Client 12 and later.
22
22
  #
23
23
  # @example
24
24
  #
@@ -29,7 +29,7 @@ module RuboCop
29
29
  class MetadataMissingName < Cop
30
30
  include RangeHelp
31
31
 
32
- MSG = 'metadata.rb needs to include the name method'.freeze
32
+ MSG = 'metadata.rb needs to include the name method or it will fail on Chef Infra Client 12 and later.'.freeze
33
33
 
34
34
  def investigate(processed_source)
35
35
  return if processed_source.blank?
@@ -0,0 +1,53 @@
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
+ module ChefCorrectness
21
+ # There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # property :name, String
27
+ # property :name, String, name_property: true
28
+ #
29
+ class UnnecessaryNameProperty < Cop
30
+ MSG = 'There is no need to define a property named :name in a resource as Chef Infra defines that property for all resources out of the box.'.freeze
31
+
32
+ def_node_matcher :name_property?, <<-PATTERN
33
+ (send nil? :property (sym :name) (const nil? :String) $...)
34
+ PATTERN
35
+
36
+ def on_send(node)
37
+ name_property?(node) do |hash_vals|
38
+ if hash_vals.empty? || (hash_vals.first.keys.count == 1 && hash_vals.first.keys.first.source == 'name_property')
39
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
40
+ end
41
+ end
42
+ end
43
+
44
+ def autocorrect(node)
45
+ lambda do |corrector|
46
+ corrector.remove(node.source_range)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -31,6 +31,7 @@ module RuboCop
31
31
  # type: 'string',
32
32
  # required: 'optional',
33
33
  # default: '"127.0.0.1:2181"'
34
+ #
34
35
  class AttributeMetadata < Cop
35
36
  MSG = "Don't use the deprecated 'attribute' metadata value".freeze
36
37
 
@@ -0,0 +1,52 @@
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
+ module ChefDeprecations
22
+ # Don't use the deprecated ChefSpec Coverage report functionality in your specs. This feature has been removed as coverage reports encourage cookbook authors to write ineffective specs. Focus on testing your logic instead of achieving 100% code coverage.
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ #
28
+ # at_exit { ChefSpec::Coverage.report! }
29
+ #
30
+ class ChefSpecCoverageReport < Cop
31
+ MSG = "Don't use the deprecated ChefSpec coverage report functionality in your specs.".freeze
32
+
33
+ def_node_matcher :coverage_reporter?, <<-PATTERN
34
+ (block (send nil? :at_exit ) (args) (send (const (const nil? :ChefSpec) :Coverage) :report!))
35
+ PATTERN
36
+
37
+ def on_block(node)
38
+ coverage_reporter?(node) do
39
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
40
+ end
41
+ end
42
+
43
+ def autocorrect(node)
44
+ lambda do |corrector|
45
+ corrector.remove(node.loc.expression)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,64 @@
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
+ module ChefDeprecations
22
+ # Use ChefSpec::SoloRunner or ChefSpec::ServerRunner instead of the deprecated ChefSpec::Runner. These new runners were introduced in ChefSpec 4.1 (Oct 2014).
23
+ #
24
+ # @example
25
+ #
26
+ # # bad
27
+ #
28
+ # describe 'foo::default' do
29
+ # subject { ChefSpec::Runner.new.converge(described_recipe) }
30
+ #
31
+ # # some spec code
32
+ # end
33
+ #
34
+ # # good
35
+ #
36
+ # describe 'foo::default' do
37
+ # subject { ChefSpec::ServerRunner.new.converge(described_recipe) }
38
+ #
39
+ # # some spec code
40
+ # end
41
+ #
42
+ class ChefSpecLegacyRunner < Cop
43
+ MSG = 'Use ChefSpec::SoloRunner or ChefSpec::ServerRunner instead of the deprecated ChefSpec::Runner.'.freeze
44
+
45
+ def_node_matcher :chefspec_runner?, <<-PATTERN
46
+ (const (const nil? :ChefSpec) :Runner)
47
+ PATTERN
48
+
49
+ def on_const(node)
50
+ chefspec_runner?(node) do
51
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
52
+ end
53
+ end
54
+
55
+ def autocorrect(node)
56
+ lambda do |corrector|
57
+ corrector.replace(node.loc.expression, 'ChefSpec::ServerRunner')
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -32,7 +32,7 @@ module RuboCop
32
32
  # require 'chef/mixin/language'
33
33
  # require 'chef/mixin/language_include_attribute'
34
34
  # require 'chef/mixin/language_include_recipe'
35
-
35
+ #
36
36
  class UsesDeprecatedMixins < Cop
37
37
  MSG = "Don't use deprecated Mixins no longer included in Chef Infra Client 14 and later.".freeze
38
38
 
@@ -32,14 +32,8 @@ module RuboCop
32
32
 
33
33
  MSG = 'The long_description metadata.rb method is not used and is unnecessary in cookbooks'.freeze
34
34
 
35
- def_node_matcher :long_description?, <<-PATTERN
36
- (send nil? :long_description ... )
37
- PATTERN
38
-
39
35
  def on_send(node)
40
- long_description?(node) do
41
- add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :long_description
42
- end
36
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :long_description
43
37
  end
44
38
 
45
39
  def autocorrect(node)
@@ -26,6 +26,7 @@ module RuboCop
26
26
  # # bad
27
27
  # use_inline_resources
28
28
  # use_inline_resources if defined?(use_inline_resources)
29
+ # use_inline_resources if respond_to?(:use_inline_resources)
29
30
  #
30
31
  class UseInlineResourcesDefined < Cop
31
32
  MSG = 'use_inline_resources is now the default for resources in Chef Infra Client 13+ and does not need to be specified.'.freeze
@@ -37,7 +38,7 @@ module RuboCop
37
38
  return if node.parent && node.parent.defined_type?
38
39
 
39
40
  # catch the full offense if the method is gated like this: use_inline_resources if defined?(use_inline_resources)
40
- if node.parent && node.parent.if_type? && node.parent.children.first.method_name == :defined?
41
+ if node.parent && node.parent.if_type? && %i(defined? respond_to?).include?(node.parent.children.first.method_name)
41
42
  node = node.parent
42
43
  end
43
44
  add_offense(node, location: :expression, message: MSG, severity: :refactor)
@@ -46,6 +46,17 @@ module RuboCop
46
46
  add_offense(property, location: :expression, message: MSG, severity: :refactor)
47
47
  end
48
48
  end
49
+
50
+ def autocorrect(node)
51
+ lambda do |corrector|
52
+ new_text = []
53
+ node.arguments.first.each_pair do |k, v|
54
+ new_text << "#{k.source} #{v.source}"
55
+ end
56
+
57
+ corrector.replace(node.loc.expression, new_text.join("\n "))
58
+ end
59
+ end
49
60
  end
50
61
  end
51
62
  end
@@ -29,8 +29,6 @@ module RuboCop
29
29
  # cron_access 'mike'
30
30
  #
31
31
  class CronManageResource < Cop
32
- include RuboCop::Chef::CookbookHelpers
33
-
34
32
  MSG = 'The cron_manage resource was renamed to cron_access in the 6.1 release of the cron cookbook and later shipped in Chef Infra Client 14.4. The new resource name should be used.'.freeze
35
33
 
36
34
  def on_send(node)
@@ -36,8 +36,6 @@ module RuboCop
36
36
  # end
37
37
  #
38
38
  class MacOsXUserdefaults < Cop
39
- include RuboCop::Chef::CookbookHelpers
40
-
41
39
  MSG = 'The mac_os_x_userdefaults resource was renamed to macos_userdefaults when it was added to Chef Infra Client 14.0. The new resource name should be used.'.freeze
42
40
 
43
41
  def on_send(node)
@@ -32,8 +32,6 @@ module RuboCop
32
32
  # end
33
33
  #
34
34
  class OpensslRsaKeyResource < Cop
35
- include RuboCop::Chef::CookbookHelpers
36
-
37
35
  MSG = 'The openssl_rsa_key resource was renamed to openssl_rsa_private_key in Chef Infra Client 14.0. The new resource name should be used.'.freeze
38
36
 
39
37
  def on_send(node)
@@ -38,8 +38,6 @@ module RuboCop
38
38
  # end
39
39
  #
40
40
  class OpensslX509Resource < Cop
41
- include RuboCop::Chef::CookbookHelpers
42
-
43
41
  MSG = 'The openssl_x509 resource was renamed to openssl_x509_certificate in Chef Infra Client 14.4. The new resource name should be used.'.freeze
44
42
 
45
43
  def on_send(node)
@@ -32,8 +32,6 @@ module RuboCop
32
32
  # end
33
33
  #
34
34
  class OsxConfigProfileResource < Cop
35
- include RuboCop::Chef::CookbookHelpers
36
-
37
35
  MSG = 'The osx_config_profile resource was renamed to osx_profile. The new resource name should be used.'.freeze
38
36
 
39
37
  def on_send(node)
@@ -19,9 +19,7 @@ module RuboCop
19
19
  module Cop
20
20
  module Chef
21
21
  module ChefModernize
22
- # In HWRPs and LWRPs it was necessary to define the allowed actions within the resource.
23
- # In custom resources this is no longer necessary as Chef will determine it based on the
24
- # actions defined in the resource.
22
+ # In Chef Infra Client releases after 12.5 it is no longer necessary to set `actions` or `allowed_actions` as Chef Infra Client determines these automatically from the set of all actions defined in the resource.
25
23
  #
26
24
  # @example
27
25
  #
@@ -29,39 +27,25 @@ module RuboCop
29
27
  # property :something, String
30
28
  #
31
29
  # allowed_actions [:create, :remove]
32
- # action :create do
33
- # # some action code because we're in a custom resource
34
- # end
35
30
  #
36
31
  # # also bad
37
32
  # property :something, String
38
33
  #
39
34
  # actions [:create, :remove]
40
- # action :create do
41
- # # some action code because we're in a custom resource
42
- # end
43
35
  #
44
36
  # # good
45
37
  # property :something, String
46
38
  #
47
- # action :create do
48
- # # some action code because we're in a custom resource
49
- # end
50
- #
51
39
  class CustomResourceWithAllowedActions < Cop
52
- MSG = "Custom Resources don't need to define the allowed actions with allowed_actions or actions methods".freeze
40
+ MSG = 'Resources no longer need to define the allowed actions with allowed_actions or actions methods.'.freeze
53
41
 
54
42
  def_node_matcher :allowed_actions?, <<-PATTERN
55
43
  (send nil? {:allowed_actions :actions} ... )
56
44
  PATTERN
57
45
 
58
- def_node_search :resource_actions?, <<-PATTERN
59
- (block (send nil? :action ... ) ... )
60
- PATTERN
61
-
62
46
  def on_send(node)
63
47
  allowed_actions?(node) do
64
- add_offense(node, location: :expression, message: MSG, severity: :refactor) if resource_actions?(processed_source.ast)
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
65
49
  end
66
50
  end
67
51
 
@@ -0,0 +1,87 @@
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
+ module ChefStyle
21
+ # When checking the major version number of a platform you can take the node['platform_version'] attribute and transform it to an integer to strip it down to just the major version number. This simple way of determining the major version number of a platform should be used instead of splitting the platform into multiple fields with '.' as the delimiter.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # node['platform_version'].split('.').first
27
+ # node['platform_version'].split('.')[0]
28
+ # node['platform_version'].split('.').first.to_i
29
+ # node['platform_version'].split('.')[0].to_i
30
+ #
31
+ # # good
32
+ #
33
+ # # check to see if we're on RHEL 7 on a RHEL 7.6 node where node['platform_version] is 7.6.1810
34
+ # if node['platform_version'].to_i == 7
35
+ #
36
+ class SimplifyPlatformMajorVersionCheck < Cop
37
+ MSG = "Use node['platform_version'].to_i instead of node['platform_version'].split('.').first or node['platform_version'].split('.')[0]".freeze
38
+
39
+ def_node_matcher :platform_version_check?, <<-PATTERN
40
+ (send (send (send nil? :node) :[] (str "platform_version") ) :split (str ".") )
41
+ PATTERN
42
+
43
+ def on_send(node)
44
+ platform_version_check?(node) do
45
+ if parent_method_equals?(node, :[])
46
+ node = node.parent
47
+ if node&.arguments.count == 1 &&
48
+ node&.arguments&.first&.int_type? &&
49
+ node&.arguments&.first.source == '0'
50
+ add_offense_to_i_if_present(node)
51
+ end
52
+ elsif parent_method_equals?(node, :first)
53
+ node = node.parent
54
+ add_offense_to_i_if_present(node)
55
+ end
56
+ end
57
+ end
58
+
59
+ def autocorrect(node)
60
+ lambda do |corrector|
61
+ corrector.replace(node.loc.expression, "node['platform_version'].to_i")
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ # if the parent is .to_i then we want to alert on that
68
+ def add_offense_to_i_if_present(node)
69
+ node = node.parent if parent_method_equals?(node, :to_i)
70
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
71
+ end
72
+
73
+ # see if the parent is a method and if it equals the passed in name
74
+ #
75
+ # @param [Rubocop::AST:Node] node The rubocop ast node to search
76
+ # @param [Symbol] name The method name
77
+ #
78
+ def parent_method_equals?(node, name)
79
+ return false if node.parent.nil?
80
+ return false unless node.parent.send_type?
81
+ name == node.parent.method_name
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ 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.7.0
4
+ version: 5.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thom May
@@ -9,36 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-04 00:00:00.000000000 Z
12
+ date: 2019-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rake
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: '0'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- version: '0'
28
- - !ruby/object:Gem::Dependency
29
- name: rspec
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
14
  - !ruby/object:Gem::Dependency
43
15
  name: rubocop
44
16
  requirement: !ruby/object:Gem::Requirement
@@ -78,9 +50,11 @@ files:
78
50
  - lib/rubocop/cop/chef/correctness/block_guard_clause_string_only.rb
79
51
  - lib/rubocop/cop/chef/correctness/cb_depends_on_self.rb
80
52
  - lib/rubocop/cop/chef/correctness/default_maintainer_metadata.rb
53
+ - lib/rubocop/cop/chef/correctness/empty_metadata_field.rb
81
54
  - lib/rubocop/cop/chef/correctness/insecure_cookbook_url.rb
82
55
  - lib/rubocop/cop/chef/correctness/invalid_license_string.rb
83
56
  - lib/rubocop/cop/chef/correctness/invalid_platform_metadata.rb
57
+ - lib/rubocop/cop/chef/correctness/invalid_version_metadata.rb
84
58
  - lib/rubocop/cop/chef/correctness/metadata_missing_name.rb
85
59
  - lib/rubocop/cop/chef/correctness/name_property_and_required.rb
86
60
  - lib/rubocop/cop/chef/correctness/node_normal.rb
@@ -94,8 +68,11 @@ files:
94
68
  - lib/rubocop/cop/chef/correctness/resource_with_none_action.rb
95
69
  - lib/rubocop/cop/chef/correctness/service_resource.rb
96
70
  - lib/rubocop/cop/chef/correctness/tmp_path.rb
71
+ - lib/rubocop/cop/chef/correctness/unnecessary_name_property.rb
97
72
  - lib/rubocop/cop/chef/deprecation/attribute_metadata.rb
98
73
  - lib/rubocop/cop/chef/deprecation/chef_rest.rb
74
+ - lib/rubocop/cop/chef/deprecation/chefspec_coverage_report.rb
75
+ - lib/rubocop/cop/chef/deprecation/chefspec_legacy_runner.rb
99
76
  - lib/rubocop/cop/chef/deprecation/chocolatey_package_uninstall_action.rb
100
77
  - lib/rubocop/cop/chef/deprecation/conflicts_metadata.rb
101
78
  - lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb
@@ -167,6 +144,7 @@ files:
167
144
  - lib/rubocop/cop/chef/style/comments_copyright_format.rb
168
145
  - lib/rubocop/cop/chef/style/comments_format.rb
169
146
  - lib/rubocop/cop/chef/style/file_mode.rb
147
+ - lib/rubocop/cop/chef/style/simplify_platform_major_version_check.rb
170
148
  - lib/rubocop/cop/chef/style/use_platform_helpers.rb
171
149
  homepage: https://github.com/chef/cookstyle
172
150
  licenses: