cookstyle 5.10.13 → 5.11.0

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: d7a4dfc3bad7b5f35e0665fc7c9f0d255c74729faa3de74fb126962f4f41b65f
4
- data.tar.gz: 285f0eff32d8f1c528328f266136bc4b5899d35d567b0ee185440c8d0438e362
3
+ metadata.gz: 23367383d5c8736b9ab2f8bfbf580f79fb41584840e5f7c8bc00fe473cbb0ef7
4
+ data.tar.gz: fc2c67a465da5575f311f2eebcb658a2aa161d55fb2a41c2ddfe234fb9303ce7
5
5
  SHA512:
6
- metadata.gz: fbebffebb05c03de047144b6f38dd5d35724b80c240347f8b939ce092cbed4c91d2cca3cf6c9104846852886d84e26067afe8ae6778698b1a745694ee7282dca
7
- data.tar.gz: dac2940dba58d62d900e01d071b068f455455e08278766f425c329c51508d145f1916df38b9e90fa3b9a7c7590a2618ad463eae627ef63e96fd869413ef006dc
6
+ metadata.gz: 38e8cbb19de77adcb8eb239749fd729e2aced72b20ba31b8e6cc81b186c3125fcbf774ce2c88a00b03da779f477f8247e3d219cf024d9ac9e2e05799ea3003a2
7
+ data.tar.gz: 6a29d736057c956ec4b420af00f3f84db703ccc3c959ec05f30ab6d195c7d7dac738cfe768f3705539d158211232626a74ea5b7e876f2f4674596dfc29963b1b
data/config/cookstyle.yml CHANGED
@@ -556,6 +556,32 @@ ChefDeprecations/VerifyPropertyUsesFileExpansion:
556
556
  Exclude:
557
557
  - '**/metadata.rb'
558
558
 
559
+ ChefDeprecations/PoiseArchiveUsage:
560
+ Description: The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+.
561
+ Enabled: true
562
+ VersionAdded: '5.11.0'
563
+
564
+ ChefDeprecations/PartialSearchHelperUsage:
565
+ Description: Legacy partial_search usage should be updated to use :filter_result in the search helper instead.
566
+ Enabled: true
567
+ VersionAdded: '5.11.0'
568
+ Exclude:
569
+ - '**/metadata.rb'
570
+
571
+ ChefDeprecations/SearchUsesPositionalParameters:
572
+ Description: Don't use deprecated positional parameters in cookbook search queries.
573
+ Enabled: true
574
+ VersionAdded: '5.11.0'
575
+ Exclude:
576
+ - '**/metadata.rb'
577
+
578
+ ChefDeprecations/PartialSearchClassUsage:
579
+ Description: Legacy Chef::PartialSearch class usage should be updated to use the search helper instead with the filter_result key.
580
+ Enabled: true
581
+ VersionAdded: '5.11.0'
582
+ Exclude:
583
+ - '**/metadata.rb'
584
+
559
585
  ###############################
560
586
  # ChefModernize: Cleaning up legacy code and using new built-in resources
561
587
  ###############################
@@ -823,6 +849,13 @@ ChefModernize/ResourceNameFromInitialize:
823
849
  - '**/providers/*.rb'
824
850
  - '**/libraries/*.rb'
825
851
 
852
+ ChefModernize/Definitions:
853
+ Description: Legacy Chef Infra definitions should be rewritten as custom resources to take full advantage of the Chef Infra feature set.
854
+ Enabled: true
855
+ VersionAdded: '5.11.0'
856
+ Include:
857
+ - '**/definitions/*.rb'
858
+
826
859
  ###############################
827
860
  # Migrating to new patterns
828
861
  ###############################
@@ -831,26 +864,43 @@ ChefEffortless/CookbookUsesSearch:
831
864
  Description: Cookbook uses search, which cannot be used in the Effortless Infra pattern
832
865
  Enabled: false
833
866
  VersionAdded: '5.1.0'
867
+ Exclude:
868
+ - '**/metadata.rb'
834
869
 
835
870
  ChefEffortless/CookbookUsesDatabags:
836
871
  Description: Cookbook uses data bags, which cannot be used in the Effortless Infra pattern
837
872
  Enabled: false
838
873
  VersionAdded: '5.1.0'
874
+ Exclude:
875
+ - '**/metadata.rb'
839
876
 
840
877
  ChefEffortless/CookbookUsesEnvironmments:
841
878
  Description: Cookbook uses environments, which cannot be used in the Effortless Infra pattern
842
879
  Enabled: false
843
880
  VersionAdded: '5.10.0'
881
+ Exclude:
882
+ - '**/metadata.rb'
844
883
 
845
884
  ChefEffortless/CookbookUsesPolicygroups:
846
885
  Description: Cookbook uses Policy Groups, which cannot be used in the Effortless Infra pattern
847
886
  Enabled: false
848
887
  VersionAdded: '5.10.0'
888
+ Exclude:
889
+ - '**/metadata.rb'
849
890
 
850
891
  ChefEffortless/CookbookUsesRoles:
851
892
  Description: Cookbook uses Roles, which cannot be used in the Effortless Infra pattern
852
893
  Enabled: false
853
894
  VersionAdded: '5.10.0'
895
+ Exclude:
896
+ - '**/metadata.rb'
897
+
898
+ ChefEffortless/SearchForEnvironmentsOrRoles:
899
+ Description: Cookbook uses search with a node query that looks for a role or environment
900
+ Enabled: false
901
+ VersionAdded: '5.11.0'
902
+ Exclude:
903
+ - '**/metadata.rb'
854
904
 
855
905
  #### The base rubocop 0.37 enabled.yml file we started with ####
856
906
 
@@ -1288,6 +1338,19 @@ Metrics/CyclomaticComplexity:
1288
1338
  Metrics/PerceivedComplexity:
1289
1339
  Enabled: false
1290
1340
 
1341
+ #### COPS FOR THE NEXT MAJOR RELEASE
1342
+
1343
+ # This updates how we send helpers into the Chef recipe/resource classes and makes WAY more sense
1344
+ # Chef::Recipe.send(:include, ::Apt::Helpers) -> Chef::Recipe.include ::Apt::Helpers
1345
+ # Lint/SendWithMixinArgument:
1346
+ # Enabled: true
1347
+
1348
+ #### END COPS FOR THE NEXT MAJOR RELEASE
1349
+
1350
+ # this migrates old # rubocop: comments to use the latest namespaces, which prevents a ton of spam during cookstyle runs
1351
+ Migration/DepartmentName:
1352
+ Enabled: true
1353
+
1291
1354
  # https://github.com/chef/cookstyle/pull/5
1292
1355
  Style/TrailingCommaInHashLiteral:
1293
1356
  Enabled: true
@@ -1,4 +1,6 @@
1
1
  ---
2
+ Migration/DepartmentName:
3
+ Enabled: false
2
4
  Bundler/DuplicatedGem:
3
5
  Enabled: false
4
6
  Bundler/GemComment:
@@ -303,6 +305,8 @@ Lint/SafeNavigationWithEmpty:
303
305
  Enabled: false
304
306
  Lint/ScriptPermission:
305
307
  Enabled: false
308
+ Lint/SendWithMixinArgument:
309
+ Enabled: false
306
310
  Lint/ShadowedArgument:
307
311
  Enabled: false
308
312
  Lint/ShadowedException:
@@ -465,6 +469,8 @@ Style/DocumentationMethod:
465
469
  Enabled: false
466
470
  Style/Documentation:
467
471
  Enabled: false
472
+ Style/DoubleCopDisableDirective:
473
+ Enabled: false
468
474
  Style/DoubleNegation:
469
475
  Enabled: false
470
476
  Style/EachForSimpleLoop:
@@ -569,6 +575,8 @@ Style/MultilineMemoization:
569
575
  Enabled: false
570
576
  Style/MultilineTernaryOperator:
571
577
  Enabled: false
578
+ Style/MultilineWhenThen:
579
+ Enabled: false
572
580
  Style/MultipleComparison:
573
581
  Enabled: false
574
582
  Style/MutableConstant:
data/config/upstream.yml CHANGED
@@ -816,6 +816,8 @@ Layout/LeadingCommentSpace:
816
816
  StyleGuide: '#hash-space'
817
817
  Enabled: true
818
818
  VersionAdded: '0.49'
819
+ VersionChanged: '0.73'
820
+ AllowDoxygenCommentStyle: false
819
821
 
820
822
  Layout/MultilineArrayBraceLayout:
821
823
  Description: >-
@@ -1589,6 +1591,11 @@ Lint/ScriptPermission:
1589
1591
  VersionAdded: '0.49'
1590
1592
  VersionChanged: '0.50'
1591
1593
 
1594
+ Lint/SendWithMixinArgument:
1595
+ Description: 'Checks for `send` method when using mixin.'
1596
+ Enabled: true
1597
+ VersionAdded: '0.75'
1598
+
1592
1599
  Lint/ShadowedArgument:
1593
1600
  Description: 'Avoid reassigning arguments before they were used.'
1594
1601
  Enabled: true
@@ -1660,6 +1667,7 @@ Lint/UnneededSplatExpansion:
1660
1667
  Description: 'Checks for splat unnecessarily being called on literals.'
1661
1668
  Enabled: true
1662
1669
  VersionAdded: '0.43'
1670
+ VersionChanged: '0.74'
1663
1671
 
1664
1672
  Lint/UnreachableCode:
1665
1673
  Description: 'Unreachable code.'
@@ -1743,7 +1751,7 @@ Metrics/AbcSize:
1743
1751
  branches, and conditions.
1744
1752
  Reference:
1745
1753
  - http://c2.com/cgi/wiki?AbcMetric
1746
- - https://en.wikipedia.org/wiki/ABC_Software_Metric'
1754
+ - https://en.wikipedia.org/wiki/ABC_Software_Metric
1747
1755
  Enabled: true
1748
1756
  VersionAdded: '0.27'
1749
1757
  VersionChanged: '0.66'
@@ -1807,7 +1815,7 @@ Metrics/LineLength:
1807
1815
  - https
1808
1816
  # The IgnoreCopDirectives option causes the LineLength rule to ignore cop
1809
1817
  # directives like '# rubocop: enable ...' when calculating a line's length.
1810
- IgnoreCopDirectives: false
1818
+ IgnoreCopDirectives: true
1811
1819
  # The IgnoredPatterns option is a list of !ruby/regexp and/or string
1812
1820
  # elements. Strings will be converted to Regexp objects. A line that matches
1813
1821
  # any regular expression listed in this option will be ignored by LineLength.
@@ -1846,6 +1854,14 @@ Metrics/PerceivedComplexity:
1846
1854
  VersionAdded: '0.25'
1847
1855
  Max: 7
1848
1856
 
1857
+ ################## Migration #############################
1858
+
1859
+ Migration/DepartmentName:
1860
+ Description: >-
1861
+ Check that cop names in rubocop:disable (etc) comments are
1862
+ given with department name.
1863
+ Enabled: false
1864
+
1849
1865
  #################### Naming ##############################
1850
1866
 
1851
1867
  Naming/AccessorMethodName:
@@ -1981,6 +1997,13 @@ Naming/MethodName:
1981
1997
  SupportedStyles:
1982
1998
  - snake_case
1983
1999
  - camelCase
2000
+ # Method names matching patterns are always allowed.
2001
+ #
2002
+ # IgnoredPatterns:
2003
+ # - '\A\s*onSelectionBulkChange\s*'
2004
+ # - '\A\s*onSelectionCleared\s*'
2005
+ #
2006
+ IgnoredPatterns: []
1984
2007
 
1985
2008
  Naming/PredicateName:
1986
2009
  Description: 'Check the names of predicate methods.'
@@ -2344,7 +2367,7 @@ Style/ClassAndModuleChildren:
2344
2367
  StyleGuide: '#namespace-definition'
2345
2368
  # Moving from compact to nested children requires knowledge of whether the
2346
2369
  # outer parent is a module or a class. Moving from nested to compact requires
2347
- # verification that the outer parent is defined elsewhere. RuboCop does not
2370
+ # verification that the outer parent is defined elsewhere. Rubocop does not
2348
2371
  # have the knowledge to perform either operation safely and thus requires
2349
2372
  # manual oversight.
2350
2373
  SafeAutoCorrect: false
@@ -2555,6 +2578,11 @@ Style/DocumentationMethod:
2555
2578
  - 'test/**/*'
2556
2579
  RequireForNonPublicMethods: false
2557
2580
 
2581
+ Style/DoubleCopDisableDirective:
2582
+ Description: 'Checks for double rubocop:disable comments on a single line.'
2583
+ Enabled: true
2584
+ VersionAdded: '0.73'
2585
+
2558
2586
  Style/DoubleNegation:
2559
2587
  Description: 'Checks for uses of double negation (!!).'
2560
2588
  StyleGuide: '#no-bang-bang'
@@ -2698,7 +2726,7 @@ Style/FormatStringToken:
2698
2726
  - template
2699
2727
  - unannotated
2700
2728
  VersionAdded: '0.49'
2701
- VersionChanged: '0.52'
2729
+ VersionChanged: '0.75'
2702
2730
 
2703
2731
  Style/FrozenStringLiteralComment:
2704
2732
  Description: >-
@@ -2891,6 +2919,7 @@ Style/MethodCallWithArgsParentheses:
2891
2919
  VersionChanged: '0.61'
2892
2920
  IgnoreMacros: true
2893
2921
  IgnoredMethods: []
2922
+ IgnoredPatterns: []
2894
2923
  IncludedMacros: []
2895
2924
  AllowParenthesesInMultilineCall: false
2896
2925
  AllowParenthesesInChaining: false
@@ -3041,6 +3070,12 @@ Style/MultilineTernaryOperator:
3041
3070
  Enabled: true
3042
3071
  VersionAdded: '0.9'
3043
3072
 
3073
+ Style/MultilineWhenThen:
3074
+ Description: 'Do not use then for multi-line when statement.'
3075
+ StyleGuide: '#no-then'
3076
+ Enabled: true
3077
+ VersionAdded: '0.73'
3078
+
3044
3079
  Style/MultipleComparison:
3045
3080
  Description: >-
3046
3081
  Avoid comparing a variable with multiple items in a conditional,
@@ -3572,6 +3607,8 @@ Style/StringHashKeys:
3572
3607
  StyleGuide: '#symbols-as-keys'
3573
3608
  Enabled: false
3574
3609
  VersionAdded: '0.52'
3610
+ VersionChanged: '0.75'
3611
+ Safe: false
3575
3612
 
3576
3613
  Style/StringLiterals:
3577
3614
  Description: 'Checks if uses of quotes match the configured preference.'
@@ -3890,8 +3927,9 @@ Style/YodaCondition:
3890
3927
  - require_for_all_comparison_operators
3891
3928
  # enforce yoda only for equality operators: `!=` and `==`
3892
3929
  - require_for_equality_operators_only
3930
+ Safe: false
3893
3931
  VersionAdded: '0.49'
3894
- VersionChanged: '0.63'
3932
+ VersionChanged: '0.75'
3895
3933
 
3896
3934
  Style/ZeroLengthPredicate:
3897
3935
  Description: 'Use #empty? when testing for objects of length 0.'
@@ -1,4 +1,4 @@
1
1
  module Cookstyle
2
- VERSION = "5.10.13".freeze # rubocop: disable Style/StringLiterals
3
- RUBOCOP_VERSION = '0.72.0'.freeze
2
+ VERSION = "5.11.0".freeze # rubocop: disable Style/StringLiterals
3
+ RUBOCOP_VERSION = '0.75.1'.freeze
4
4
  end
@@ -2,24 +2,35 @@ module RuboCop
2
2
  module Chef
3
3
  # Common node helpers used for matching against Chef Infra Cookbooks
4
4
  module CookbookHelpers
5
+ def resource_block_name_if_string(node)
6
+ if looks_like_resource?(node) && node.children.first.arguments.first.respond_to?(:value)
7
+ node.children.first.arguments.first.value
8
+ end
9
+ end
10
+
11
+ # Match a particular resource
12
+ #
13
+ # @param [String] resource_name The name of the resource to match
14
+ # @param [RuboCop::AST::Node] node The rubocop ast node to search
15
+ #
16
+ # @yield
17
+ #
18
+ def match_resource_type?(resource_name, node)
19
+ return unless looks_like_resource?(node)
20
+ # bail out if we're not in the resource we care about or nil was passed (all resources)
21
+ yield(node) if node.children.first.method?(resource_name.to_sym)
22
+ end
23
+
5
24
  # Match particular properties within a resource
6
25
  #
7
26
  # @param [String] resource_name The name of the resource to match
8
27
  # @param [String] property_name The name of the property to match (or action)
9
- # @param [<Type>] node The rubocop ast node to search
28
+ # @param [RuboCop::AST::Node] node The rubocop ast node to search
10
29
  #
11
30
  # @yield
12
31
  #
13
32
  def match_property_in_resource?(resource_name, property_name, node)
14
- return unless node.block_type? # resources are blocks if they have properties
15
- return unless node.children.first.receiver.nil? # resource blocks don't have a receiver
16
- return if node.send_node.arguments.first.is_a?(RuboCop::AST::SymbolNode) # resources have a string name. resource actions have symbols
17
-
18
- # bail if the block doesn't have a name a resource *generally* has a name.
19
- # This isn't 100% true with things like apt_update and build_essential, but we'll live
20
- # with that for now to avoid the false positives of getting stuck in generic blocks in resources
21
- return if node.children.first.arguments.empty?
22
-
33
+ return unless looks_like_resource?(node)
23
34
  # bail out if we're not in the resource we care about or nil was passed (all resources)
24
35
  return unless resource_name.nil? || node.children.first.method?(resource_name.to_sym) # see if we're in the right resource
25
36
 
@@ -48,6 +59,28 @@ module RuboCop
48
59
 
49
60
  private
50
61
 
62
+ #
63
+ # given a node object does it look like a chef resource or not?
64
+ # warning: currently this requires a resource with properties since we key off blocks and property-less resources look like methods
65
+ #
66
+ # @param [RuboCop::AST::Node] node AST object to test
67
+ #
68
+ # @return [boolean]
69
+ #
70
+ def looks_like_resource?(node)
71
+ return false unless node.block_type? # resources are blocks if they have properties
72
+ return false unless node.children.first.receiver.nil? # resource blocks don't have a receiver
73
+ return false if node.send_node.arguments.first.is_a?(RuboCop::AST::SymbolNode) # resources have a string name. resource actions have symbols
74
+
75
+ # bail if the block doesn't have a name a resource *generally* has a name.
76
+ # This isn't 100% true with things like apt_update and build_essential, but we'll live
77
+ # with that for now to avoid the false positives of getting stuck in generic blocks in resources
78
+ return false if node.children.first.arguments.empty?
79
+
80
+ # if we made it this far we're probably in a resource
81
+ true
82
+ end
83
+
51
84
  def extract_send_types(node)
52
85
  return if node.nil? # there are cases we can be passed an empty node
53
86
  case node.type
@@ -0,0 +1,65 @@
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 ChefDeprecations
21
+ # Legacy Chef::PartialSearch class usage should be updated to use the `search` helper instead with the `filter_result` key.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # ::Chef::PartialSearch.new.search((:node, 'role:web',
27
+ # keys: { 'name' => [ 'name' ],
28
+ # 'ip' => [ 'ipaddress' ],
29
+ # 'kernel_version' => %w(kernel version),
30
+ # }
31
+ # ).each do |result|
32
+ # puts result['name']
33
+ # puts result['ip']
34
+ # puts result['kernel_version']
35
+ # end
36
+ #
37
+ # # good
38
+ # search(:node, 'role:web',
39
+ # filter_result: { 'name' => [ 'name' ],
40
+ # 'ip' => [ 'ipaddress' ],
41
+ # 'kernel_version' => %w(kernel version),
42
+ # }
43
+ # ).each do |result|
44
+ # puts result['name']
45
+ # puts result['ip']
46
+ # puts result['kernel_version']
47
+ # end
48
+ #
49
+ class PartialSearchClassUsage < Cop
50
+ MSG = 'Legacy Chef::PartialSearch class usage should be updated to use the search helper instead with the filter_result key.'.freeze
51
+
52
+ def_node_matcher :partial_search_class?, <<-PATTERN
53
+ (send (const (const ... :Chef) :PartialSearch) :new)
54
+ PATTERN
55
+
56
+ def on_send(node)
57
+ partial_search_class?(node) do
58
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,59 @@
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 ChefDeprecations
21
+ # Legacy partial_search usage should be updated to use :filter_result in the search helper instead
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # partial_search(:node, 'role:web',
27
+ # keys: { 'name' => [ 'name' ],
28
+ # 'ip' => [ 'ipaddress' ],
29
+ # 'kernel_version' => %w(kernel version),
30
+ # }
31
+ # ).each do |result|
32
+ # puts result['name']
33
+ # puts result['ip']
34
+ # puts result['kernel_version']
35
+ # end
36
+ #
37
+ # # good
38
+ # search(:node, 'role:web',
39
+ # filter_result: { 'name' => [ 'name' ],
40
+ # 'ip' => [ 'ipaddress' ],
41
+ # 'kernel_version' => %w(kernel version),
42
+ # }
43
+ # ).each do |result|
44
+ # puts result['name']
45
+ # puts result['ip']
46
+ # puts result['kernel_version']
47
+ # end
48
+ #
49
+ class PartialSearchHelperUsage < Cop
50
+ MSG = 'Legacy partial_search usage should be updated to use :filter_result in the search helper instead'.freeze
51
+
52
+ def on_send(node)
53
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :partial_search
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,59 @@
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 ChefDeprecations
21
+ # The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # poise_archive 'https://example.com/myapp.tgz' do
27
+ # destination '/opt/myapp'
28
+ # end
29
+ #
30
+ # # good
31
+ # archive_file 'https://example.com/myapp.tgz' do
32
+ # destination '/opt/myapp'
33
+ # end
34
+ #
35
+ class PoiseArchiveUsage < Cop
36
+ include RuboCop::Chef::CookbookHelpers
37
+
38
+ MSG = 'The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+'.freeze
39
+
40
+ def_node_matcher :depends_poise_archive?, <<-PATTERN
41
+ (send nil? :depends (str "poise-archive"))
42
+ PATTERN
43
+
44
+ def on_send(node)
45
+ depends_poise_archive?(node) do
46
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
47
+ end
48
+ end
49
+
50
+ def on_block(node)
51
+ match_resource_type?(:poise_archive, node) do
52
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,134 @@
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 ChefDeprecations
21
+ # In the cookbook search helper you need to use named parameters (key/value style) other than the first (type) and second (query string) values.
22
+ #
23
+ # @example
24
+ #
25
+ # bad:
26
+ # search(:node, '*:*', 0, 1000, { :ip_address => ["ipaddress"] })
27
+ # search(:node, '*:*', 0, 1000)
28
+ # search(:node, '*:*', 0)
29
+
30
+ # good
31
+ #
32
+ # query(:node, '*:*')
33
+ # search(:node, '*:*', start: 0, rows: 1000, filter_result: { :ip_address => ["ipaddress"] })
34
+ # search(:node, '*:*', start: 0, rows: 1000)
35
+ # search(:node, '*:*', start: 0)
36
+ #
37
+ class SearchUsesPositionalParameters < Cop
38
+ MSG = "Don't use deprecated positional parameters in cookbook search queries.".freeze
39
+
40
+ NAMED_PARAM_LOOKUP_TABLE = [nil, nil, 'start', 'rows', 'filter_result'].freeze
41
+
42
+ def_node_matcher :search_method?, <<-PATTERN
43
+ (send nil? :search ... )
44
+ PATTERN
45
+
46
+ def on_send(node)
47
+ search_method?(node) do
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if positional_arguments?(node)
49
+ end
50
+ end
51
+
52
+ def autocorrect(node)
53
+ lambda do |corrector|
54
+ corrector.replace(node.loc.expression, corrected_string(node))
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ #
61
+ # Are the arguments in the passed node object positional
62
+ #
63
+ # @param [RuboCop::AST::Node] node
64
+ #
65
+ # @return [Boolean]
66
+ #
67
+ def positional_arguments?(node)
68
+ return false if node.arguments.count < 3
69
+ node.arguments[2..-1].each do |arg|
70
+ # hashes, blocks, or variable/methods are valid. Anything else is not
71
+ return true unless %i(send hash block_pass).include?(arg.type)
72
+ end
73
+ false
74
+ end
75
+
76
+ #
77
+ # Return the corrected search string
78
+ #
79
+ # @param [RuboCop::AST::Node] node
80
+ #
81
+ # @return [String]
82
+ #
83
+ def corrected_string(node)
84
+ args = node.arguments
85
+
86
+ # If the 2nd argument is a String and not an Integer as a String
87
+ # then it's the old sort field and we need to delete it. Same thing
88
+ # goes for nil values here.
89
+ args.delete_at(2) if (args[2].str_type? && !integer_like_val?(args[2])) || args[2].nil_type?
90
+
91
+ "search(#{args.collect.with_index { |arg, i| hashify_argument(arg, i) }.join(', ')})"
92
+ end
93
+
94
+ #
95
+ # lookup the position in NAMED_PARAM_LOOKUP_TABLE to create a new
96
+ # hashified version of the query. Also convert Integer like Strings into Integers
97
+ #
98
+ # @param [RuboCop::AST::Node] arg
99
+ # @param [Integer] position
100
+ #
101
+ # @return [String]
102
+ #
103
+ def hashify_argument(arg, position)
104
+ hash_key = NAMED_PARAM_LOOKUP_TABLE[position]
105
+ if hash_key
106
+ # convert Integers stored as Strings into plain Integers
107
+ if integer_like_val?(arg)
108
+ "#{hash_key}: #{Integer(arg.value)}"
109
+ else
110
+ "#{hash_key}: #{arg.source}"
111
+ end
112
+ else
113
+ arg.source
114
+ end
115
+ end
116
+
117
+ #
118
+ # Does this value look like an Integer (it's an integer or a string)
119
+ #
120
+ # @param [RuboCop::AST::Node] val
121
+ #
122
+ # @return [Boolean]
123
+ #
124
+ def integer_like_val?(val)
125
+ Integer(val.value)
126
+ true
127
+ rescue
128
+ false
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,41 @@
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 ChefEffortless
21
+ # Policyfiles (and Effortless) do not use environments or roles so searching for those will need to be refactored before migrating to Policyfiles and the Effortless pattern.
22
+ #
23
+ # @example
24
+ #
25
+ # # bad
26
+ # search(:node, 'chef_environment:foo')
27
+ # search(:node, 'role:bar')
28
+ #
29
+ class SearchForEnvironmentsOrRoles < Cop
30
+ MSG = 'Cookbook uses search with a node query that looks for a role or environment'.freeze
31
+
32
+ def on_send(node)
33
+ if node.method_name == :search && node.arguments[1]&.value&.match?(/chef_environment|role/)
34
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -18,7 +18,7 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefModernize
21
- # Use secure Github and Gitlab URLs for source_url and issues_url
21
+ # Use the build_essential resource from the build-essential cookbook 5.0+ or Chef Infra Client 14+ instead of using the build-essential::default recipe.
22
22
  #
23
23
  # @example
24
24
  #
@@ -29,6 +29,7 @@ module RuboCop
29
29
  #
30
30
  # # good
31
31
  # build_essential 'install compilation tools'
32
+ #
32
33
  class UseBuildEssentialResource < Cop
33
34
  MSG = 'Use the build_essential resource instead of the legacy build-essential recipe. This resource ships in the build-essential cookbook v5.0+ and is built into Chef Infra Client 14+'.freeze
34
35
 
@@ -53,7 +53,7 @@ module RuboCop
53
53
  lambda do |corrector|
54
54
  # insert the new default_action call above the initialize method
55
55
  initialize_node = intialize_method(processed_source.ast).first
56
- corrector.insert_before(initialize_node.source_range, "default_action #{node.asgn_rhs.source}\n\n")
56
+ corrector.insert_before(initialize_node.source_range, "default_action #{node.descendants.first.source}\n\n")
57
57
 
58
58
  # remove the variable from the initialize method
59
59
  corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
@@ -0,0 +1,35 @@
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 ChefModernize
21
+ # In 2016 with Chef Infra Client 12.5 Custom Resources were introduced as a way of writing reusable resource code that could be shipped in cookbooks. Custom Resources offer many advantages of legacy Definitions including unit testing with ChefSpec, input validation, actions, commmon properties like not_if/only_if, and resource reporting.
22
+ #
23
+ class Definitions < Cop
24
+ include RuboCop::Chef::CookbookHelpers
25
+
26
+ MSG = 'Legacy Chef Infra definitions should be rewritten as custom resources to take full advantage of the Chef Infra feature set.'.freeze
27
+
28
+ def on_block(node)
29
+ add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.respond_to?(:method_name) && node.method_name == :define
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -53,7 +53,7 @@ module RuboCop
53
53
  lambda do |corrector|
54
54
  # insert the new resource_name call above the initialize method
55
55
  initialize_node = intialize_method(processed_source.ast).first
56
- corrector.insert_before(initialize_node.source_range, "resource_name #{node.asgn_rhs.source}\n\n")
56
+ corrector.insert_before(initialize_node.source_range, "resource_name #{node.descendants.first.source}\n\n")
57
57
 
58
58
  # remove the variable from the initialize method
59
59
  corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
@@ -18,19 +18,20 @@ module RuboCop
18
18
  module Cop
19
19
  module Chef
20
20
  module ChefModernize
21
- # It is not longer necessary respond_to?(:foo) in metadata. This was used to support new metadata
21
+ # It is not longer necessary respond_to?(:foo) or defined?(foo) in metadata. This was used to support new metadata
22
22
  # methods in Chef 11 and early versions of Chef 12.
23
23
  #
24
24
  # @example
25
25
  #
26
26
  # # bad
27
27
  # chef_version '>= 13' if respond_to?(:chef_version)
28
+ # chef_version '>= 13' if defined?(chef_version)
28
29
  #
29
30
  # # good
30
31
  # chef_version '>= 13'
31
32
  #
32
33
  class RespondToInMetadata < Cop
33
- MSG = 'It is no longer necessary to use respond_to? in metadata.rb in Chef Infra Client 12.15 and later'.freeze
34
+ MSG = 'It is no longer necessary to use respond_to? or if_defined? in metadata.rb in Chef Infra Client 12.15 and later'.freeze
34
35
 
35
36
  def on_if(node)
36
37
  if_respond_to?(node) do
@@ -38,6 +39,11 @@ module RuboCop
38
39
  end
39
40
  end
40
41
 
42
+ def on_defined?(node)
43
+ node = node.parent if node.parent.if? # we want the whole if statement
44
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
45
+ end
46
+
41
47
  def_node_matcher :if_respond_to?, <<~PATTERN
42
48
  (if (send nil? :respond_to? _ ) ... )
43
49
  PATTERN
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.10.13
4
+ version: 5.11.0
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-10-25 00:00:00.000000000 Z
12
+ date: 2019-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubocop
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: 0.72.0
20
+ version: 0.75.1
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - '='
26
26
  - !ruby/object:Gem::Version
27
- version: 0.72.0
27
+ version: 0.75.1
28
28
  description:
29
29
  email:
30
30
  - thom@chef.io
@@ -96,6 +96,9 @@ files:
96
96
  - lib/rubocop/cop/chef/deprecation/node_methods_not_attributes.rb
97
97
  - lib/rubocop/cop/chef/deprecation/node_set.rb
98
98
  - lib/rubocop/cop/chef/deprecation/node_set_unless.rb
99
+ - lib/rubocop/cop/chef/deprecation/partial_search_class_usage.rb
100
+ - lib/rubocop/cop/chef/deprecation/partial_search_helper_usage.rb
101
+ - lib/rubocop/cop/chef/deprecation/poise_archive.rb
99
102
  - lib/rubocop/cop/chef/deprecation/provides_metadata.rb
100
103
  - lib/rubocop/cop/chef/deprecation/recipe_metadata.rb
101
104
  - lib/rubocop/cop/chef/deprecation/replaces_metadata.rb
@@ -105,6 +108,7 @@ files:
105
108
  - lib/rubocop/cop/chef/deprecation/resource_uses_provider_base_method.rb
106
109
  - lib/rubocop/cop/chef/deprecation/resource_uses_updated_method.rb
107
110
  - lib/rubocop/cop/chef/deprecation/run_command_helper.rb
111
+ - lib/rubocop/cop/chef/deprecation/search_uses_positional_parameters.rb
108
112
  - lib/rubocop/cop/chef/deprecation/suggests_metadata.rb
109
113
  - lib/rubocop/cop/chef/deprecation/use_inline_resources.rb
110
114
  - lib/rubocop/cop/chef/deprecation/user_supports_property.rb
@@ -116,6 +120,7 @@ files:
116
120
  - lib/rubocop/cop/chef/effortless/node_environment.rb
117
121
  - lib/rubocop/cop/chef/effortless/node_policygroup.rb
118
122
  - lib/rubocop/cop/chef/effortless/node_roles.rb
123
+ - lib/rubocop/cop/chef/effortless/search_for_environments_or_roles.rb
119
124
  - lib/rubocop/cop/chef/effortless/search_used.rb
120
125
  - lib/rubocop/cop/chef/modernize/apt_default_recipe.rb
121
126
  - lib/rubocop/cop/chef/modernize/berksfile_source.rb
@@ -124,6 +129,7 @@ files:
124
129
  - lib/rubocop/cop/chef/modernize/cron_manage_resource.rb
125
130
  - lib/rubocop/cop/chef/modernize/default_action_initializer.rb
126
131
  - lib/rubocop/cop/chef/modernize/defines_chefspec_matchers.rb
132
+ - lib/rubocop/cop/chef/modernize/definitions.rb
127
133
  - lib/rubocop/cop/chef/modernize/depends_zypper_cookbook.rb
128
134
  - lib/rubocop/cop/chef/modernize/execute_apt_update.rb
129
135
  - lib/rubocop/cop/chef/modernize/execute_tzutil.rb