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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cbe0d726d24711da59599a15be6d1a6b9904245cb68ad17aa07f390bbad1537
4
- data.tar.gz: ec624080babc3e2f6295ed610c6700e69d5fae4a383696638d0344ee1e5eaf86
3
+ metadata.gz: 0bebad52b67940ef2ea18a69715758710d78e83d08e6fd3629647c44df467d79
4
+ data.tar.gz: 48af992c7bf5f18d5b3a948912ff57386439bbc57029ec3a6b962d5192b168f6
5
5
  SHA512:
6
- metadata.gz: 5dac69031554a13e6f8df40ebb38dd816c3f7fd87dc32020d5387af42374066433704ac95db00f692ce1dc1970d2b4b11061fb1ea34a6ec2be459486626c25ec
7
- data.tar.gz: 2b6f78c5b4859c6fe62f588725da64b40b12c08184a8ca8ce45bac215c771c744ae3ee5c99171a65e05e10177f1e188d0b457067ef1af5964039b333fddeff9f
6
+ metadata.gz: 31950bedb48fc769d8359183b7a7f8a95bb109cd446ed55748bcb90c97688735b4dc063d12660a3573e8a299eb145ca3f912100a2f0086038eaedf714194dce5
7
+ data.tar.gz: 721544b1d118f01f29be8d025e927df03fd588e5379f58e7c4748cbc4b3325c225b90a32d44f15c914794bb2faf0fd93c8c214c721d3341e02689fa92b1a3187
data/Gemfile CHANGED
@@ -15,6 +15,15 @@ group :docs do
15
15
  gem 'yard'
16
16
  end
17
17
 
18
+ group :development do
19
+ gem 'adamantium'
20
+ gem 'anima'
21
+ gem 'concord'
22
+ gem 'rake'
23
+ gem 'rspec', '>= 3.4'
24
+ gem 'simplecov'
25
+ end
26
+
18
27
  instance_eval(ENV['GEMFILE_MOD']) if ENV['GEMFILE_MOD']
19
28
 
20
29
  # If you want to load debugging tools into the bundle exec sandbox,
data/bin/cookstyle CHANGED
@@ -5,6 +5,13 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), %w(.. lib))
5
5
 
6
6
  require 'cookstyle'
7
7
 
8
+ # force the fail level to :convention so that we can set all our new rules to
9
+ # the lowest level of :refactor without failing everyone's CI jobs
10
+ unless ARGV.include?('--fail-level')
11
+ ARGV << '--fail-level'
12
+ ARGV << 'C'
13
+ end
14
+
8
15
  if ARGV.size == 1 && %w(-v --version).include?(ARGV.first)
9
16
  puts "Cookstyle #{Cookstyle::VERSION}"
10
17
  print ' * RuboCop '
data/config/cookstyle.yml CHANGED
@@ -25,37 +25,225 @@ AllCops:
25
25
  Patterns:
26
26
  - resources/.*\.rb
27
27
 
28
+ ###############################
29
+ # Making cookbooks look better
30
+ ###############################
31
+
28
32
  Chef/AttributeKeys:
29
33
  Description: Check which style of keys are used to access node attributes.
30
34
  Enabled: true
31
35
  EnforcedStyle: strings
36
+ VersionAdded: '5.0.0'
32
37
  SupportedStyles:
33
38
  - strings
34
39
  - symbols
35
40
 
41
+ Chef/CopyrightCommentFormat:
42
+ Description: Properly format copyright dates in comment blocks and ensure dates are up to date
43
+ Enabled: false
44
+ VersionAdded: '5.0.0'
45
+
46
+ Chef/CommentSentenceSpacing:
47
+ Description: Use a single space after sentences in comments
48
+ Enabled: false
49
+ VersionAdded: '5.1.0'
50
+
51
+ Chef/CommentFormat:
52
+ Description: Use Chef's unique format for comment headers
53
+ Enabled: true
54
+ VersionAdded: '5.0.0'
55
+
56
+ ###############################
57
+ # Avoiding potential problems
58
+ ###############################
59
+
36
60
  Chef/FileMode:
37
61
  Description: Use strings to represent file modes in Chef resources
38
62
  Enabled: true
63
+ VersionAdded: '5.0.0'
39
64
 
40
65
  Chef/ServiceResource:
41
66
  Description: Use a service resource to start and stop services instead of execute resources
42
67
  Enabled: true
68
+ VersionAdded: '5.0.0'
43
69
 
44
- Chef/CopyrightCommentFormat:
45
- Description: Properly format copyright dates in comment blocks and ensure dates are up to date
46
- Enabled: false
70
+ Chef/NodeNormal:
71
+ Description: Do not use the node.normal method
72
+ Enabled: true
73
+ VersionAdded: '5.1.0'
47
74
 
48
- Chef/CommentFormat:
49
- Description: Use Chef's unique format for comment headers
75
+ Chef/NodeNormalUnless:
76
+ Description: Do not use the node.normal_unless method
50
77
  Enabled: true
78
+ VersionAdded: '5.1.0'
79
+
80
+ Chef/TmpPath:
81
+ Description: Use file_cache_path rather than hard-coding tmp paths
82
+ Enabled: true
83
+ VersionAdded: '5.0.0'
84
+
85
+ Chef/InsecureCookbookURL:
86
+ Description: Insecure http Github or Gitlab URLs for metadata source_url/issues_url fields
87
+ Enabled: true
88
+ VersionAdded: '5.1.0'
89
+ Include:
90
+ - '**/metadata.rb'
91
+
92
+ Chef/NamePropertyIsRequired:
93
+ Description: Resource properties marked as name properties should not also be required properties
94
+ Enabled: true
95
+ VersionAdded: '5.1.0'
96
+
97
+ ###############################
98
+ # Resolving Deprecations
99
+ ###############################
51
100
 
52
101
  Chef/NodeSet:
53
102
  Description: Do not use the deprecated node.set method
54
103
  Enabled: true
104
+ VersionAdded: '5.0.0'
55
105
 
56
- Chef/TmpPath:
57
- Description: Use file_cache_path rather than hard-coding tmp paths
106
+ Chef/NodeSetUnless:
107
+ Description: Do not use the deprecated node.set_unless method
108
+ Enabled: true
109
+ VersionAdded: '5.1.0'
110
+
111
+ Chef/EpicFail:
112
+ Description: Use ignore_failure method instead of the deprecated epic_fail method
113
+ Enabled: true
114
+ VersionAdded: '5.1.0'
115
+
116
+ Chef/CookbookDependsOnPoise:
117
+ Description: Cookbooks should not depend on the deprecated Poise framework
118
+ Enabled: true
119
+ VersionAdded: '5.1.0'
120
+ Include:
121
+ - '**/metadata.rb'
122
+
123
+ Chef/ConflictsMetadata:
124
+ Description: Don't use the deprecated 'conflicts' metadata value
58
125
  Enabled: true
126
+ VersionAdded: '5.1.0'
127
+ Include:
128
+ - '**/metadata.rb'
129
+
130
+ Chef/SuggestsMetadata:
131
+ Description: Don't use the deprecated 'suggests' metadata value
132
+ Enabled: true
133
+ VersionAdded: '5.1.0'
134
+ Include:
135
+ - '**/metadata.rb'
136
+
137
+ Chef/ProvidesMetadata:
138
+ Description: Don't use the deprecated 'provides' metadata value
139
+ Enabled: true
140
+ VersionAdded: '5.1.0'
141
+ Include:
142
+ - '**/metadata.rb'
143
+
144
+ Chef/ReplacesMetadata:
145
+ Description: Don't use the deprecated 'replaces' metadata value
146
+ Enabled: true
147
+ VersionAdded: '5.1.0'
148
+ Include:
149
+ - '**/metadata.rb'
150
+
151
+ Chef/AttributeMetadata:
152
+ Description: Don't use the deprecated 'attribute' metadata value
153
+ Enabled: true
154
+ VersionAdded: '5.1.0'
155
+ Include:
156
+ - '**/metadata.rb'
157
+
158
+ Chef/CookbookDependsOnCompatResource:
159
+ Description: Don't depend on the deprecated compat_resource cookbook made obsolete by Chef 12.19+
160
+ Enabled: true
161
+ VersionAdded: '5.1.0'
162
+ Include:
163
+ - '**/metadata.rb'
164
+
165
+ Chef/CookbookDependsOnPartialSearch:
166
+ Description: Don't depend on the deprecated partial_search cookbook made obsolete by Chef 13+
167
+ Enabled: true
168
+ VersionAdded: '5.1.0'
169
+ Include:
170
+ - '**/metadata.rb'
171
+
172
+ Chef/EasyInstallResource:
173
+ Description: Don't use the deprecated easy_install resource resource removed in Chef 13
174
+ Enabled: true
175
+ VersionAdded: '5.1.0'
176
+
177
+ Chef/ErlCallResource:
178
+ Description: Don't use the deprecated erl_call resource removed in Chef 13
179
+ Enabled: true
180
+ VersionAdded: '5.1.0'
181
+
182
+ ###############################
183
+ # Cleaning up Legacy Code
184
+ ###############################
185
+
186
+ Chef/LegacyBerksfileSource:
187
+ Description: Do not use legacy Berksfile community sources. Use Chef Supermarket instead.
188
+ Enabled: true
189
+ VersionAdded: '5.1.0'
190
+ Include:
191
+ - '**/Berksfile'
192
+
193
+ Chef/WhyRunSupportedTrue:
194
+ Description: why_run_supported? no longer needs to be set to true as it is the default in Chef 13+
195
+ Enabled: true
196
+ VersionAdded: '5.1.0'
197
+ Include:
198
+ - '**/resources/.*\.rb'
199
+ - '**/providers/.*\.rb'
200
+ - '**/libraries/.*\.rb'
201
+
202
+ PropertyWithNameAttribute:
203
+ Description: Resource property sets name_attribute not name_property
204
+ Enabled: true
205
+ VersionAdded: '5.1.0'
206
+ Include:
207
+ - '**/resources/.*\.rb'
208
+ - '**/libraries/.*\.rb'
209
+
210
+ PropertyWithRequiredAndDefault:
211
+ Description: Resource property should not be both required and have a default value
212
+ Enabled: true
213
+ VersionAdded: '5.1.0'
214
+ Include:
215
+ - '**/resources/.*\.rb'
216
+ - '**/libraries/.*\.rb'
217
+
218
+ Chef/UnnecessaryDependsChef14:
219
+ Description: Don't depend on cookbooks made obsolete by Chef 14
220
+ Enabled: true
221
+ VersionAdded: '5.1.0'
222
+ Include:
223
+ - '**/metadata.rb'
224
+
225
+ ###############################
226
+ # Utilize new built-in resources
227
+ ###############################
228
+
229
+ Chef/UseBuildEssentialResource:
230
+ Description: Use the build_essential resource instead of the legacy build-essential recipe
231
+ Enabled: false
232
+ VersionAdded: '5.1.0'
233
+
234
+ ###############################
235
+ # Migrating to new patterns
236
+ ###############################
237
+
238
+ Chef/CookbookUsesSearch:
239
+ Description: Cookbook uses search, which cannot be used in the Effortless Infra pattern
240
+ Enabled: false
241
+ VersionAdded: '5.1.0'
242
+
243
+ Chef/CookbookUsesDatabags:
244
+ Description: Cookbook uses data bags, which cannot be used in the Effortless Infra pattern
245
+ Enabled: false
246
+ VersionAdded: '5.1.0'
59
247
 
60
248
  #### The base rubocop 0.37 enabled.yml file we started with ####
61
249
 
data/config/upstream.yml CHANGED
@@ -2344,7 +2344,7 @@ Style/ClassAndModuleChildren:
2344
2344
  StyleGuide: '#namespace-definition'
2345
2345
  # Moving from compact to nested children requires knowledge of whether the
2346
2346
  # 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
2347
+ # verification that the outer parent is defined elsewhere. RuboCop does not
2348
2348
  # have the knowledge to perform either operation safely and thus requires
2349
2349
  # manual oversight.
2350
2350
  SafeAutoCorrect: false
data/lib/cookstyle.rb CHANGED
@@ -36,5 +36,6 @@ require 'rubocop/chef/cookbook_only'
36
36
  # Chef specific cops
37
37
  Dir.glob(File.dirname(__FILE__) + '/rubocop/cop/chef/**/*.rb') do |file|
38
38
  next if File.directory?(file)
39
+
39
40
  require_relative file # not actually relative but require_relative is faster
40
41
  end
@@ -1,4 +1,4 @@
1
1
  module Cookstyle
2
- VERSION = "5.0.4".freeze # rubocop: disable Style/StringLiterals
2
+ VERSION = "5.1.19".freeze # rubocop: disable Style/StringLiterals
3
3
  RUBOCOP_VERSION = '0.72.0'.freeze
4
4
  end
@@ -4,15 +4,15 @@ module RuboCop
4
4
  module Chef
5
5
  # Mixin for cops that skips non-cookbook files
6
6
  #
7
- # The criteria for whether rubocop-rspec analyzes a certain ruby file
8
- # is configured via `AllCops/RSpec`. For example, if you want to
7
+ # The criteria for whether cookstyle analyzes a certain ruby file
8
+ # is configured via `AllCops/Chef`. For example, if you want to
9
9
  # customize your project to scan all files within a `test/` directory
10
10
  # then you could add this to your configuration:
11
11
  #
12
12
  # @example configuring analyzed paths
13
13
  #
14
14
  # AllCops:
15
- # RSpec:
15
+ # Chef:
16
16
  # Patterns:
17
17
  # - '_spec.rb$'
18
18
  # - '(?:^|/)spec/'
@@ -31,6 +31,7 @@ module RuboCop
31
31
  patterns = []
32
32
  COOKBOOK_SEGMENTS.each do |segment|
33
33
  next unless self.class.cookbook_only_segments[segment.to_sym]
34
+
34
35
  cookbook_pattern_config(segment).each do |pattern|
35
36
  patterns << Regexp.new(pattern)
36
37
  end
@@ -0,0 +1,60 @@
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
+ # Use secure Github and Gitlab URLs for source_url and issues_url
21
+ #
22
+ # @example
23
+ #
24
+ # # bad
25
+ # source_url 'http://github.com/something/something'
26
+ # source_url 'http://www.github.com/something/something'
27
+ # source_url 'http://www.gitlab.com/something/something'
28
+ # source_url 'http://gitlab.com/something/something'
29
+ #
30
+ # # good
31
+ # source_url 'http://github.com/something/something'
32
+ # source_url 'http://gitlab.com/something/something'
33
+ #
34
+ class InsecureCookbookURL < Cop
35
+ MSG = 'Insecure http Github or Gitlab URLs for metadata source_url/issues_url fields'.freeze
36
+
37
+ def_node_matcher :insecure_cb_url?, <<-PATTERN
38
+ (send nil? {:source_url :issues_url} (str #insecure_url?))
39
+ PATTERN
40
+
41
+ def insecure_url?(url)
42
+ # https://rubular.com/r/dS6L6bQZvwWxWq
43
+ url.match?(%r{http://(www.)*git(hub|lab)})
44
+ end
45
+
46
+ def on_send(node)
47
+ insecure_cb_url?(node) do
48
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
49
+ end
50
+ end
51
+
52
+ def autocorrect(node)
53
+ lambda do |corrector|
54
+ corrector.replace(node.loc.expression, node.source.gsub(%r{http://(www.)*}, 'https://'))
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,94 @@
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
+ # be both required and a name_property. Name properties are a way to optionally
22
+ # override the name given to the resource block in cookbook code. In your resource
23
+ # code you use the name_property and if the user doesn't pass in anything to that
24
+ # property its value will be populated with resource block's name. This
25
+ # allows users to provide more friendly resource names for logging that give
26
+ # additional context on the change being made.
27
+ #
28
+ # How about a nice example! Here we have a resource called ntp_config that has a
29
+ # name_property of config_file. All throughout the code of this resource we'd
30
+ # use new_resource.config_file when referencing the path to the config.
31
+ #
32
+ # We can use a friendly name for the block and specific a value to config_file
33
+ # ntp_config 'Configure the main config file' do
34
+ # config_file '/etc/ntp/ntp.conf'
35
+ # action :create
36
+ # end
37
+ #
38
+ # We can also just set the config path as the resource block and Chef will
39
+ # make sure to pass this in as new_resource.config_file as well.
40
+ # ntp_config '/etc/ntp/ntp.conf' do
41
+ # action :create
42
+ # end
43
+ #
44
+ # The core tenant of the name property feature is that these properties are optional
45
+ # and making them required effectively turns off the functionality provided by name
46
+ # properties. If the goal is to always require the user to pass the config_file property
47
+ # then it should just be made a required property and not a name_property.
48
+ #
49
+ #
50
+ # @example
51
+ #
52
+ #
53
+ # # bad
54
+ # property :config_file, String, required: true, name_property: true
55
+ #
56
+ #
57
+ # # good
58
+ # property :config_file, String, required: true
59
+ class NamePropertyIsRequired < Cop
60
+ MSG = 'Resource properties marked as name properties should not also be required properties'.freeze
61
+
62
+ def on_send(node)
63
+ if required_property?(node) && property_is_name_property?(node)
64
+ add_offense(node, location: :expression, message: MSG, severity: :refactor)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def property_is_name_property?(node)
71
+ if node.method_name == :property
72
+ node.arguments.each do |arg|
73
+ if arg.type == :hash
74
+ return true if arg.source.match?(/name_property:\s*true/)
75
+ end
76
+ end
77
+ false # no required: true found
78
+ end
79
+ end
80
+
81
+ def required_property?(node)
82
+ if node.method_name == :property
83
+ node.arguments.each do |arg|
84
+ if arg.type == :hash
85
+ return true if arg.source.match?(/required:\s*true/)
86
+ end
87
+ end
88
+ false # no default: found
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end