inspecstyle 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/Gemfile +1 -0
  4. data/Gemfile.lock +12 -7
  5. data/README.md +39 -1
  6. data/Rakefile +3 -0
  7. data/config/default.yml +95 -2
  8. data/doc/RuboCop.html +128 -0
  9. data/doc/RuboCop/Cop.html +117 -0
  10. data/doc/RuboCop/Cop/InSpecStyle.html +117 -0
  11. data/doc/RuboCop/Cop/InSpecStyle/AzureGenericResource.html +249 -0
  12. data/doc/RuboCop/Cop/InSpecStyle/DeprecatedAttributes.html +310 -0
  13. data/doc/RuboCop/Cop/InSpecStyle/FirstCop.html +345 -0
  14. data/doc/RuboCop/InSpecStyle.html +140 -0
  15. data/doc/RuboCop/InSpecStyle/Error.html +124 -0
  16. data/doc/RuboCop/InSpecStyle/Inject.html +195 -0
  17. data/doc/_index.html +211 -0
  18. data/doc/class_list.html +51 -0
  19. data/doc/css/common.css +1 -0
  20. data/doc/css/full_list.css +58 -0
  21. data/doc/css/style.css +496 -0
  22. data/doc/file.README.html +131 -0
  23. data/doc/file_list.html +56 -0
  24. data/doc/frames.html +17 -0
  25. data/doc/index.html +131 -0
  26. data/doc/js/app.js +314 -0
  27. data/doc/js/full_list.js +216 -0
  28. data/doc/js/jquery.js +4 -0
  29. data/doc/method_list.html +99 -0
  30. data/doc/top-level-namespace.html +110 -0
  31. data/lib/rubocop/cop/inspecstyle/apache.rb +57 -0
  32. data/lib/rubocop/cop/inspecstyle/aws_iam_user_property.rb +96 -0
  33. data/lib/rubocop/cop/inspecstyle/azure_generic_resource.rb +35 -0
  34. data/lib/rubocop/cop/inspecstyle/deprecated_attributes.rb +16 -16
  35. data/lib/rubocop/cop/inspecstyle/file_be_mounted.rb +65 -0
  36. data/lib/rubocop/cop/inspecstyle/file_size.rb +77 -0
  37. data/lib/rubocop/cop/inspecstyle/host_proto.rb +71 -0
  38. data/lib/rubocop/cop/inspecstyle/iis_website.rb +36 -0
  39. data/lib/rubocop/cop/inspecstyle/linux_kernel_parameter.rb +41 -0
  40. data/lib/rubocop/cop/inspecstyle/mssql_session_pass.rb +59 -0
  41. data/lib/rubocop/cop/inspecstyle/oracle_db_session_pass.rb +59 -0
  42. data/lib/rubocop/cop/inspecstyle/ppa_resource.rb +36 -0
  43. data/lib/rubocop/cop/inspecstyle/processes_list.rb +77 -0
  44. data/lib/rubocop/cop/inspecstyle/script_resource.rb +48 -0
  45. data/lib/rubocop/cop/inspecstyle/shadow_properties.rb +83 -0
  46. data/lib/rubocop/cop/inspecstyle/users_resource_matchers.rb +98 -0
  47. data/lib/rubocop/cop/inspecstyle/windows_registry_key.rb +36 -0
  48. data/lib/rubocop/cop/inspecstyle/wmi_wmis_class.rb +37 -0
  49. data/lib/rubocop/cop/inspecstyle_cops.rb +17 -1
  50. data/lib/rubocop/inspecstyle/version.rb +1 -1
  51. data/notes-for-development.md +10 -0
  52. data/tasks/cops_documentation.rake +300 -0
  53. metadata +43 -3
  54. data/lib/rubocop/cop/inspecstyle/first_cop.rb +0 -69
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # windows_registry_key has been deprecated as a resource. Use registry_key instead
8
+ #
9
+ class WindowsRegistryKey < Cop
10
+ MSG = 'Use `registry_key` instead of `windows_registry_key`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :windows_registry_key?, <<~PATTERN
14
+ (send _ :windows_registry_key ...)
15
+ PATTERN
16
+
17
+ def on_send(node)
18
+ return unless windows_registry_key?(node)
19
+ add_offense(node, location: :selector)
20
+ end
21
+
22
+ def autocorrect(node)
23
+ lambda do |corrector|
24
+ corrector.replace(node.loc.selector, preferred_replacement)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def preferred_replacement
31
+ cop_config.fetch('PreferredReplacement')
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InSpecStyle
6
+ # @example EnforcedStyle: InSpecStyle (default)
7
+ # # wmi_wmis_class has been deprecated as a resource. Use iis_site instead
8
+ #
9
+ class WmiWmisClass < Cop
10
+ MSG = 'Use `iis_site` instead of `wmi_wmis_class`. '\
11
+ 'This resource will be removed in InSpec 5.'
12
+
13
+ def_node_matcher :wmi_wmis_class?, <<~PATTERN
14
+ (send _ :wmi
15
+ (str "wmisclass") ...)
16
+ PATTERN
17
+
18
+ def on_send(node)
19
+ return unless wmi_wmis_class?(node)
20
+ add_offense(node)
21
+ end
22
+
23
+ def autocorrect(node)
24
+ lambda do |corrector|
25
+ corrector.replace(node.source_range, preferred_replacement)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def preferred_replacement
32
+ cop_config.fetch('PreferredReplacement')
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,19 @@
1
1
  # frozen_string_literal: true
2
- require_relative 'inspecstyle/first_cop'
3
2
  require_relative 'inspecstyle/deprecated_attributes'
3
+ require_relative 'inspecstyle/azure_generic_resource'
4
+ require_relative 'inspecstyle/shadow_properties'
5
+ require_relative 'inspecstyle/oracle_db_session_pass'
6
+ require_relative 'inspecstyle/file_size'
7
+ require_relative 'inspecstyle/apache'
8
+ require_relative 'inspecstyle/script_resource'
9
+ require_relative 'inspecstyle/users_resource_matchers'
10
+ require_relative 'inspecstyle/iis_website'
11
+ require_relative 'inspecstyle/host_proto'
12
+ require_relative 'inspecstyle/linux_kernel_parameter'
13
+ require_relative 'inspecstyle/mssql_session_pass'
14
+ require_relative 'inspecstyle/ppa_resource'
15
+ require_relative 'inspecstyle/processes_list'
16
+ require_relative 'inspecstyle/windows_registry_key'
17
+ require_relative 'inspecstyle/aws_iam_user_property'
18
+ require_relative 'inspecstyle/file_be_mounted'
19
+ require_relative 'inspecstyle/wmi_wmis_class'
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module InSpecStyle
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -42,4 +42,14 @@ node = source.ast
42
42
  node.type
43
43
  node.children
44
44
  node.source
45
+ NodePattern.new('(send ...)').match(node) # => true
45
46
  ```
47
+
48
+ - Implement CI
49
+
50
+ Correction docs at rubocop's: lib/rubocop/cop/corrector.rb
51
+
52
+ ### Great Repos for cop examples
53
+ - rubocop-ast
54
+ - rubocop-rspec
55
+ - rubocop
@@ -0,0 +1,300 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yard'
4
+ require 'rubocop'
5
+
6
+ YARD::Rake::YardocTask.new(:yard_for_generate_documentation) do |task|
7
+ task.files = ['lib/rubocop/cop/*/*.rb']
8
+ task.options = ['--no-output']
9
+ end
10
+
11
+ desc 'Generate docs of all cops departments'
12
+ task generate_cops_documentation: :yard_for_generate_documentation do
13
+ def cops_of_department(cops, department)
14
+ cops.with_department(department).sort!
15
+ end
16
+
17
+ # rubocop:disable Metrics/AbcSize
18
+ def cops_body(config, cop, description, examples_objects, pars)
19
+ content = h2(cop.cop_name)
20
+ content << required_ruby_version(cop)
21
+ content << properties(cop.new(config))
22
+ content << "#{description}\n"
23
+ content << examples(examples_objects) if examples_objects.count.positive?
24
+ content << configurations(pars)
25
+ content << references(config, cop)
26
+ content
27
+ end
28
+ # rubocop:enable Metrics/AbcSize
29
+
30
+ def examples(examples_object)
31
+ examples_object.each_with_object(h3('Examples').dup) do |example, content|
32
+ content << "\n" unless content.end_with?("\n\n")
33
+ content << h4(example.name) unless example.name == ''
34
+ content << code_example(example)
35
+ end
36
+ end
37
+
38
+ def required_ruby_version(cop)
39
+ return '' unless cop.respond_to?(:required_minimum_ruby_version)
40
+
41
+ "NOTE: Required Ruby version: #{cop.required_minimum_ruby_version}\n\n"
42
+ end
43
+
44
+ # rubocop:disable Metrics/MethodLength
45
+ def properties(cop_instance)
46
+ header = [
47
+ 'Enabled by default', 'Safe', 'Supports autocorrection', 'VersionAdded',
48
+ 'VersionChanged'
49
+ ]
50
+ autocorrect = if cop_instance.support_autocorrect?
51
+ "Yes#{' (Unsafe)' unless cop_instance.safe_autocorrect?}"
52
+ else
53
+ 'No'
54
+ end
55
+ cop_config = cop_instance.cop_config
56
+ content = [[
57
+ cop_status(cop_config.fetch('Enabled')),
58
+ cop_config.fetch('Safe', true) ? 'Yes' : 'No',
59
+ autocorrect,
60
+ cop_config.fetch('VersionAdded', '-'),
61
+ cop_config.fetch('VersionChanged', '-')
62
+ ]]
63
+ to_table(header, content) + "\n"
64
+ end
65
+ # rubocop:enable Metrics/MethodLength
66
+
67
+ def h2(title)
68
+ content = +"\n"
69
+ content << "== #{title}\n"
70
+ content << "\n"
71
+ content
72
+ end
73
+
74
+ def h3(title)
75
+ content = +"\n"
76
+ content << "=== #{title}\n"
77
+ content << "\n"
78
+ content
79
+ end
80
+
81
+ def h4(title)
82
+ content = +"==== #{title}\n"
83
+ content << "\n"
84
+ content
85
+ end
86
+
87
+ def code_example(ruby_code)
88
+ content = +"[source,ruby]\n----\n"
89
+ content << ruby_code.text.gsub('@good', '# good')
90
+ .gsub('@bad', '# bad').strip
91
+ content << "\n----\n"
92
+ content
93
+ end
94
+
95
+ def configurations(pars)
96
+ return '' if pars.empty?
97
+
98
+ header = ['Name', 'Default value', 'Configurable values']
99
+ configs = pars
100
+ .each_key
101
+ .reject { |key| key.start_with?('Supported') }
102
+ .reject { |key| key.start_with?('AllowMultipleStyles') }
103
+ content = configs.map do |name|
104
+ configurable = configurable_values(pars, name)
105
+ default = format_table_value(pars[name])
106
+ [name, default, configurable]
107
+ end
108
+
109
+ h3('Configurable attributes') + to_table(header, content)
110
+ end
111
+
112
+ # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
113
+ def configurable_values(pars, name)
114
+ case name
115
+ when /^Enforced/
116
+ supported_style_name = RuboCop::Cop::Util.to_supported_styles(name)
117
+ format_table_value(pars[supported_style_name])
118
+ when 'IndentationWidth'
119
+ 'Integer'
120
+ when 'Database'
121
+ format_table_value(pars['SupportedDatabases'])
122
+ else
123
+ case pars[name]
124
+ when String
125
+ 'String'
126
+ when Integer
127
+ 'Integer'
128
+ when Float
129
+ 'Float'
130
+ when true, false
131
+ 'Boolean'
132
+ when Array
133
+ 'Array'
134
+ else
135
+ ''
136
+ end
137
+ end
138
+ end
139
+ # rubocop:enable Metrics/CyclomaticComplexity,Metrics/MethodLength
140
+
141
+ def to_table(header, content)
142
+ table = [
143
+ '|===',
144
+ "| #{header.join(' | ')}\n\n"
145
+ ].join("\n")
146
+ marked_contents = content.map do |plain_content|
147
+ plain_content.map { |c| "| #{c}" }.join("\n")
148
+ end
149
+ table << marked_contents.join("\n\n")
150
+ table << "\n|===\n"
151
+ end
152
+
153
+ def format_table_value(val)
154
+ value =
155
+ case val
156
+ when Array
157
+ if val.empty?
158
+ '`[]`'
159
+ else
160
+ val.map { |config| format_table_value(config) }.join(', ')
161
+ end
162
+ else
163
+ wrap_backtick(val.nil? ? '<none>' : val)
164
+ end
165
+ value.gsub("#{Dir.pwd}/", '').rstrip
166
+ end
167
+
168
+ def wrap_backtick(value)
169
+ if value.is_a?(String)
170
+ # Use `+` to prevent text like `**/*.gemspec` from being bold.
171
+ value.start_with?('*') ? "`+#{value}+`" : "`#{value}`"
172
+ else
173
+ "`#{value}`"
174
+ end
175
+ end
176
+
177
+ def references(config, cop)
178
+ cop_config = config.for_cop(cop)
179
+ urls = RuboCop::Cop::MessageAnnotator.new(
180
+ config, cop.name, cop_config, {}
181
+ ).urls
182
+ return '' if urls.empty?
183
+
184
+ content = h3('References')
185
+ content << urls.map { |url| "* #{url}" }.join("\n")
186
+ content << "\n"
187
+ content
188
+ end
189
+
190
+ def print_cops_of_department(cops, department, config)
191
+ selected_cops = cops_of_department(cops, department)
192
+ content = +"= #{department}\n"
193
+ selected_cops.each do |cop|
194
+ content << print_cop_with_doc(cop, config)
195
+ end
196
+ file_name = "#{Dir.pwd}/docs/modules/ROOT/pages/cops_#{department.downcase}.adoc"
197
+ File.open(file_name, 'w') do |file|
198
+ puts "* generated #{file_name}"
199
+ file.write(content.strip + "\n")
200
+ end
201
+ end
202
+
203
+ def print_cop_with_doc(cop, config)
204
+ t = config.for_cop(cop)
205
+ non_display_keys = %w[
206
+ Description Enabled StyleGuide Reference Safe SafeAutoCorrect VersionAdded
207
+ VersionChanged
208
+ ]
209
+ pars = t.reject { |k| non_display_keys.include? k }
210
+ description = 'No documentation'
211
+ examples_object = []
212
+ cop_code(cop) do |code_object|
213
+ description = code_object.docstring unless code_object.docstring.blank?
214
+ examples_object = code_object.tags('example')
215
+ end
216
+ cops_body(config, cop, description, examples_object, pars)
217
+ end
218
+
219
+ def cop_code(cop)
220
+ YARD::Registry.all(:class).detect do |code_object|
221
+ next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
222
+
223
+ yield code_object
224
+ end
225
+ end
226
+
227
+ def table_of_content_for_department(cops, department)
228
+ type_title = department[0].upcase + department[1..-1]
229
+ filename = "cops_#{department.downcase}.adoc"
230
+ content = +"=== Department xref:#{filename}[#{type_title}]\n\n"
231
+ cops_of_department(cops, department.to_sym).each do |cop|
232
+ anchor = cop.cop_name.sub('/', '').downcase
233
+ content << "* xref:#{filename}##{anchor}[#{cop.cop_name}]\n"
234
+ end
235
+
236
+ content
237
+ end
238
+
239
+ def print_table_of_contents(cops)
240
+ path = "#{Dir.pwd}/docs/modules/ROOT/pages/cops.adoc"
241
+ original = File.read(path)
242
+ content = +"// START_COP_LIST\n\n"
243
+
244
+ content << table_contents(cops)
245
+
246
+ content << "\n// END_COP_LIST"
247
+
248
+ content = original.sub(
249
+ %r{// START_COP_LIST.+// END_COP_LIST}m, content
250
+ )
251
+ File.write(path, content)
252
+ end
253
+
254
+ def table_contents(cops)
255
+ cops
256
+ .departments
257
+ .map(&:to_s)
258
+ .sort
259
+ .map { |department| table_of_content_for_department(cops, department) }
260
+ .join("\n")
261
+ end
262
+
263
+ def cop_status(status)
264
+ return 'Disabled' unless status
265
+
266
+ status == 'pending' ? 'Pending' : 'Enabled'
267
+ end
268
+
269
+ def assert_docs_synchronized
270
+ # Do not print diff and yield whether exit code was zero
271
+ sh('git diff --quiet docs') do |outcome, _|
272
+ return if outcome
273
+
274
+ # Output diff before raising error
275
+ sh('GIT_PAGER=cat git diff docs')
276
+
277
+ warn 'The docs directory is out of sync. ' \
278
+ 'Run `rake generate_cops_documentation` and commit the results.'
279
+ exit!
280
+ end
281
+ end
282
+
283
+ def main
284
+ cops = RuboCop::Cop::Cop.registry
285
+ config = RuboCop::ConfigLoader.default_configuration
286
+
287
+ YARD::Registry.load!
288
+ cops.departments.sort!.each do |department|
289
+ print_cops_of_department(cops, department, config)
290
+ end
291
+
292
+ print_table_of_contents(cops)
293
+
294
+ assert_docs_synchronized if ENV['CI'] == 'true'
295
+ ensure
296
+ RuboCop::ConfigLoader.default_configuration = nil
297
+ end
298
+
299
+ main
300
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inspecstyle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Schwaderer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-10 00:00:00.000000000 Z
11
+ date: 2020-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -46,15 +46,55 @@ files:
46
46
  - bin/console
47
47
  - bin/setup
48
48
  - config/default.yml
49
+ - doc/RuboCop.html
50
+ - doc/RuboCop/Cop.html
51
+ - doc/RuboCop/Cop/InSpecStyle.html
52
+ - doc/RuboCop/Cop/InSpecStyle/AzureGenericResource.html
53
+ - doc/RuboCop/Cop/InSpecStyle/DeprecatedAttributes.html
54
+ - doc/RuboCop/Cop/InSpecStyle/FirstCop.html
55
+ - doc/RuboCop/InSpecStyle.html
56
+ - doc/RuboCop/InSpecStyle/Error.html
57
+ - doc/RuboCop/InSpecStyle/Inject.html
58
+ - doc/_index.html
59
+ - doc/class_list.html
60
+ - doc/css/common.css
61
+ - doc/css/full_list.css
62
+ - doc/css/style.css
63
+ - doc/file.README.html
64
+ - doc/file_list.html
65
+ - doc/frames.html
66
+ - doc/index.html
67
+ - doc/js/app.js
68
+ - doc/js/full_list.js
69
+ - doc/js/jquery.js
70
+ - doc/method_list.html
71
+ - doc/top-level-namespace.html
49
72
  - inspecstyle.gemspec
50
73
  - lib/inspecstyle.rb
74
+ - lib/rubocop/cop/inspecstyle/apache.rb
75
+ - lib/rubocop/cop/inspecstyle/aws_iam_user_property.rb
76
+ - lib/rubocop/cop/inspecstyle/azure_generic_resource.rb
51
77
  - lib/rubocop/cop/inspecstyle/deprecated_attributes.rb
52
- - lib/rubocop/cop/inspecstyle/first_cop.rb
78
+ - lib/rubocop/cop/inspecstyle/file_be_mounted.rb
79
+ - lib/rubocop/cop/inspecstyle/file_size.rb
80
+ - lib/rubocop/cop/inspecstyle/host_proto.rb
81
+ - lib/rubocop/cop/inspecstyle/iis_website.rb
82
+ - lib/rubocop/cop/inspecstyle/linux_kernel_parameter.rb
83
+ - lib/rubocop/cop/inspecstyle/mssql_session_pass.rb
84
+ - lib/rubocop/cop/inspecstyle/oracle_db_session_pass.rb
85
+ - lib/rubocop/cop/inspecstyle/ppa_resource.rb
86
+ - lib/rubocop/cop/inspecstyle/processes_list.rb
87
+ - lib/rubocop/cop/inspecstyle/script_resource.rb
88
+ - lib/rubocop/cop/inspecstyle/shadow_properties.rb
89
+ - lib/rubocop/cop/inspecstyle/users_resource_matchers.rb
90
+ - lib/rubocop/cop/inspecstyle/windows_registry_key.rb
91
+ - lib/rubocop/cop/inspecstyle/wmi_wmis_class.rb
53
92
  - lib/rubocop/cop/inspecstyle_cops.rb
54
93
  - lib/rubocop/inspecstyle.rb
55
94
  - lib/rubocop/inspecstyle/inject.rb
56
95
  - lib/rubocop/inspecstyle/version.rb
57
96
  - notes-for-development.md
97
+ - tasks/cops_documentation.rake
58
98
  homepage: https://github.com/schwad/inspecstyle
59
99
  licenses:
60
100
  - MIT