rubocop-minitest 0.6.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +0 -3
  3. data/.gitattributes +1 -0
  4. data/.rubocop.yml +2 -1
  5. data/.rubocop_todo.yml +0 -7
  6. data/CHANGELOG.md +65 -0
  7. data/Gemfile +1 -1
  8. data/README.md +5 -1
  9. data/Rakefile +29 -0
  10. data/config/default.yml +103 -18
  11. data/docs/antora.yml +7 -0
  12. data/docs/modules/ROOT/nav.adoc +6 -0
  13. data/docs/modules/ROOT/pages/cops.adoc +37 -0
  14. data/docs/modules/ROOT/pages/cops_minitest.adoc +1014 -0
  15. data/docs/modules/ROOT/pages/index.adoc +5 -0
  16. data/docs/modules/ROOT/pages/installation.adoc +15 -0
  17. data/docs/modules/ROOT/pages/usage.adoc +32 -0
  18. data/{manual → legacy-docs}/cops.md +1 -0
  19. data/{manual → legacy-docs}/cops_minitest.md +64 -41
  20. data/{manual → legacy-docs}/index.md +0 -0
  21. data/{manual → legacy-docs}/installation.md +0 -0
  22. data/{manual → legacy-docs}/usage.md +0 -0
  23. data/lib/rubocop/cop/generator.rb +56 -0
  24. data/lib/rubocop/cop/minitest/assert_empty.rb +4 -30
  25. data/lib/rubocop/cop/minitest/assert_empty_literal.rb +15 -0
  26. data/lib/rubocop/cop/minitest/assert_equal.rb +2 -31
  27. data/lib/rubocop/cop/minitest/assert_in_delta.rb +27 -0
  28. data/lib/rubocop/cop/minitest/assert_includes.rb +4 -4
  29. data/lib/rubocop/cop/minitest/assert_instance_of.rb +4 -38
  30. data/lib/rubocop/cop/minitest/assert_kind_of.rb +25 -0
  31. data/lib/rubocop/cop/minitest/assert_match.rb +4 -39
  32. data/lib/rubocop/cop/minitest/assert_nil.rb +2 -2
  33. data/lib/rubocop/cop/minitest/assert_output.rb +49 -0
  34. data/lib/rubocop/cop/minitest/assert_path_exists.rb +58 -0
  35. data/lib/rubocop/cop/minitest/assert_respond_to.rb +10 -45
  36. data/lib/rubocop/cop/minitest/assert_silent.rb +45 -0
  37. data/lib/rubocop/cop/minitest/assert_truthy.rb +2 -2
  38. data/lib/rubocop/cop/minitest/assertion_in_lifecycle_hook.rb +43 -0
  39. data/lib/rubocop/cop/minitest/global_expectations.rb +95 -0
  40. data/lib/rubocop/cop/minitest/literal_as_actual_argument.rb +52 -0
  41. data/lib/rubocop/cop/minitest/multiple_assertions.rb +63 -0
  42. data/lib/rubocop/cop/minitest/refute_empty.rb +4 -30
  43. data/lib/rubocop/cop/minitest/refute_false.rb +3 -3
  44. data/lib/rubocop/cop/minitest/refute_in_delta.rb +27 -0
  45. data/lib/rubocop/cop/minitest/refute_includes.rb +4 -4
  46. data/lib/rubocop/cop/minitest/refute_instance_of.rb +4 -38
  47. data/lib/rubocop/cop/minitest/refute_kind_of.rb +25 -0
  48. data/lib/rubocop/cop/minitest/refute_match.rb +4 -39
  49. data/lib/rubocop/cop/minitest/refute_nil.rb +2 -2
  50. data/lib/rubocop/cop/minitest/refute_path_exists.rb +58 -0
  51. data/lib/rubocop/cop/minitest/refute_respond_to.rb +10 -45
  52. data/lib/rubocop/cop/minitest/test_method_name.rb +70 -0
  53. data/lib/rubocop/cop/minitest/unspecified_exception.rb +36 -0
  54. data/lib/rubocop/cop/minitest_cops.rb +17 -1
  55. data/lib/rubocop/cop/mixin/argument_range_helper.rb +10 -0
  56. data/lib/rubocop/cop/mixin/in_delta_mixin.rb +50 -0
  57. data/lib/rubocop/cop/mixin/minitest_cop_rule.rb +102 -0
  58. data/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +84 -0
  59. data/lib/rubocop/minitest/version.rb +1 -1
  60. data/mkdocs.yml +2 -2
  61. data/relnotes/v0.10.0.md +21 -0
  62. data/relnotes/v0.6.2.md +5 -0
  63. data/relnotes/v0.7.0.md +13 -0
  64. data/relnotes/v0.8.0.md +12 -0
  65. data/relnotes/v0.8.1.md +5 -0
  66. data/relnotes/v0.9.0.md +10 -0
  67. data/rubocop-minitest.gemspec +4 -4
  68. data/tasks/cops_documentation.rake +83 -52
  69. data/tasks/cut_release.rake +16 -0
  70. metadata +45 -15
  71. data/lib/rubocop/cop/mixin/includes_cop_rule.rb +0 -78
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ # Helper methods for different explorations against test files and test cases.
8
+ module MinitestExplorationHelpers
9
+ extend NodePattern::Macros
10
+
11
+ ASSERTION_PREFIXES = %w[assert refute].freeze
12
+
13
+ LIFECYCLE_HOOK_METHODS = %i[
14
+ before_setup
15
+ setup
16
+ after_setup
17
+ before_teardown
18
+ teardown
19
+ after_teardown
20
+ ].to_set.freeze
21
+
22
+ private
23
+
24
+ def test_class?(class_node)
25
+ class_node.parent_class && class_node.identifier.source.end_with?('Test')
26
+ end
27
+
28
+ def test_case?(node)
29
+ return false unless node.def_type? && test_case_name?(node.method_name)
30
+
31
+ class_ancestor = node.each_ancestor(:class).first
32
+ test_class?(class_ancestor)
33
+ end
34
+
35
+ def test_cases(class_node)
36
+ class_def_nodes(class_node)
37
+ .select { |def_node| test_case_name?(def_node.method_name) }
38
+ end
39
+
40
+ def lifecycle_hooks(class_node)
41
+ class_def_nodes(class_node)
42
+ .select { |def_node| lifecycle_hook_method?(def_node) }
43
+ end
44
+
45
+ def class_def_nodes(class_node)
46
+ class_def = class_node.body
47
+ return [] unless class_def
48
+
49
+ if class_def.def_type?
50
+ [class_def]
51
+ else
52
+ class_def.each_child_node(:def).to_a
53
+ end
54
+ end
55
+
56
+ def test_case_name?(name)
57
+ name.to_s.start_with?('test_')
58
+ end
59
+
60
+ def assertions(def_node)
61
+ method_def = def_node.body
62
+ return [] unless method_def
63
+
64
+ send_nodes =
65
+ if method_def.send_type?
66
+ [method_def]
67
+ else
68
+ method_def.each_child_node(:send)
69
+ end
70
+
71
+ send_nodes.select { |send_node| assertion?(send_node) }
72
+ end
73
+
74
+ def assertion?(node)
75
+ node.send_type? &&
76
+ ASSERTION_PREFIXES.any? { |prefix| node.method_name.to_s.start_with?(prefix) }
77
+ end
78
+
79
+ def lifecycle_hook_method?(node)
80
+ node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Minitest
5
- VERSION = '0.6.2'
5
+ VERSION = '0.10.0'
6
6
  end
7
7
  end
data/mkdocs.yml CHANGED
@@ -1,8 +1,8 @@
1
1
  site_name: "A RuboCop extension focused on enforcing Minitest best practices and coding conventions."
2
2
  repo_url: https://github.com/rubocop-hq/rubocop-minitest
3
- edit_uri: edit/master/manual/
3
+ edit_uri: edit/master/legacy-docs/
4
4
  copyright: "Copyright © 2019 Bozhidar Batsov, Jonas Arvidsson, Koichi ITO, and RuboCop contributors"
5
- docs_dir: manual
5
+ docs_dir: legacy-docs
6
6
  pages:
7
7
  - Home: index.md
8
8
  - Installation: installation.md
@@ -0,0 +1,21 @@
1
+ ### New features
2
+
3
+ * [#92](https://github.com/rubocop-hq/rubocop-minitest/pull/92): Add new `Minitest/LiteralAsActualArgument` cop. ([@fatkodima][], [@tsmmark][])
4
+ * [#95](https://github.com/rubocop-hq/rubocop-minitest/pull/95): Add new `Minitest/AssertionInLifecycleHook` cop. ([@fatkodima][])
5
+ * [#91](https://github.com/rubocop-hq/rubocop-minitest/pull/91): Add new `Minitest/AssertInDelta` and `Minitest/RefuteInDelta` cops. ([@fatkodima][])
6
+ * [#89](https://github.com/rubocop-hq/rubocop-minitest/pull/89): Add new `Minitest/TestMethodName` cop. ([@fatkodima][])
7
+ * [#83](https://github.com/rubocop-hq/rubocop-minitest/pull/83): New cops `AssertPathExists` and `RefutePathExists` check for use of `assert_path_exists`/`refute_path_exists` instead of `assert(File.exist?(path))`/`refute(File.exist?(path))`. ([@fatkodima][])
8
+ * [#88](https://github.com/rubocop-hq/rubocop-minitest/pull/88): Add new `Minitest/MultipleAssertions` cop. ([@fatkodima][])
9
+ * [#87](https://github.com/rubocop-hq/rubocop-minitest/pull/87): Add new `Minitest/AssertSilent` cop. ([@fatkodima][])
10
+ * [#96](https://github.com/rubocop-hq/rubocop-minitest/pull/96): Add new `Minitest/UnspecifiedException` cop. ([@fatkodima][])
11
+ * [#98](https://github.com/rubocop-hq/rubocop-minitest/pull/98): Add new `Minitest/AssertOutput` cop. ([@fatkodima][])
12
+ * [#84](https://github.com/rubocop-hq/rubocop-minitest/pull/84): New cops `AssertKindOf` and `RefuteKindOf` check for use of `assert_kind_of`/`refute_kind_of` instead of `assert(foo.kind_of?(Class))`/`refute(foo.kind_of?(Class))`. ([@fatkodima][])
13
+ * [#85](https://github.com/rubocop-hq/rubocop-minitest/pull/85): Add autocorrect to `Rails/AssertEmptyLiteral` cop. ([@fatkodima][])
14
+
15
+ ### Changes
16
+
17
+ * [#104](https://github.com/rubocop-hq/rubocop-minitest/pull/104): Require RuboCop 0.87 or higher. ([@koic][])
18
+
19
+ [@fatkodima]: https://github.com/fatkodima
20
+ [@tsmmark]: https://github.com/tsmmark
21
+ [@koic]: https://github.com/koic
@@ -0,0 +1,5 @@
1
+ ### Bug fixes
2
+
3
+ * [#55](https://github.com/rubocop-hq/rubocop-minitest/issues/55): Fix an error for `Minitest/AssertIncludes` when using local variable argument. ([@koic][])
4
+
5
+ [@koic]: https://github.com/koic
@@ -0,0 +1,13 @@
1
+ ### New features
2
+
3
+ * [#60](https://github.com/rubocop-hq/rubocop-minitest/issues/60): Add new cop `Minitest/GlobalExpectations` to check for deprecated global expectations. ([@tejasbubane][])
4
+
5
+ ### Bug fixes
6
+
7
+ * [#58](https://github.com/rubocop-hq/rubocop-minitest/pull/58): Fix a false negative for `Minitest/AssertMatch` and `Minitest/RefuteMatch` when an argument is enclosed in redundant parentheses. ([@koic][])
8
+ * [#59](https://github.com/rubocop-hq/rubocop-minitest/pull/59): Fix a false negative for `Minitest/AssertRespondTo` and `Minitest/RefuteRespondTo` when an argument is enclosed in redundant parentheses. ([@koic][])
9
+ * [#61](https://github.com/rubocop-hq/rubocop-minitest/pull/61): Fix a false negative for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` when an argument is enclosed in redundant parentheses. ([@koic][])
10
+ * [#62](https://github.com/rubocop-hq/rubocop-minitest/pull/62): Fix a false negative for `Minitest/AssertEmpty` and `Minitest/RefuteEmpty` when an argument is enclosed in redundant parentheses. ([@koic][])
11
+
12
+ [@tejasbubane]: https://github.com/tejasbubane
13
+ [@koic]: https://github.com/koic
@@ -0,0 +1,12 @@
1
+ ### New features
2
+
3
+ * [#66](https://github.com/rubocop-hq/rubocop-minitest/issues/66): Support all expectations of `Minitest::Expectations` for `Minitest/GlobalExpectations` cop. ([@koic][])
4
+
5
+ ### Bug fixes
6
+
7
+ * [#60](https://github.com/rubocop-hq/rubocop-minitest/issues/60): Fix `Minitest/GlobalExpectations` autocorrection for chained methods. ([@tejasbubane][])
8
+ * [#69](https://github.com/rubocop-hq/rubocop-minitest/pull/69): Fix a false negative for `Minitest/GlobalExpectations` cop when using a variable or a hash index for receiver. ([@koic][])
9
+ * [#71](https://github.com/rubocop-hq/rubocop-minitest/pull/71): Fix a false negative for `Minitest/AssertEqual` when an argument is enclosed in redundant parentheses. ([@koic][])
10
+
11
+ [@koic]: https://github.com/koic
12
+ [@tejasbubane]: https://github.com/tejasbubane
@@ -0,0 +1,5 @@
1
+ ### Bug fixes
2
+
3
+ * [#72](https://github.com/rubocop-hq/rubocop-minitest/pull/72): Fix some false negatives for `Minitest/GlobalExpectations`. ([@andrykonchin][])
4
+
5
+ [@andrykonchin]: https://github.com/andrykonchin
@@ -0,0 +1,10 @@
1
+ ### Bug fixes
2
+
3
+ * [#75](https://github.com/rubocop-hq/rubocop-minitest/issues/75): Fix a false negative for `Minitest/GlobalExpectations` when using global expectation methods with no arguments. ([@koic][])
4
+
5
+ ### Changes
6
+
7
+ * [#73](https://github.com/rubocop-hq/rubocop-minitest/issues/73): The Minitest department works on file names end with `_test.rb` by default. ([@koic][])
8
+ * [#77](https://github.com/rubocop-hq/rubocop-minitest/pull/77): **(BREAKING)** Drop support for Ruby 2.3. ([@koic][])
9
+
10
+ [@koic]: https://github.com/koic
@@ -16,12 +16,12 @@ Gem::Specification.new do |spec|
16
16
  DESCRIPTION
17
17
  spec.license = 'MIT'
18
18
 
19
- spec.required_ruby_version = '>= 2.3.0'
19
+ spec.required_ruby_version = '>= 2.4.0'
20
20
  spec.metadata = {
21
- 'homepage_uri' => 'https://docs.rubocop.org/projects/minitest/',
21
+ 'homepage_uri' => 'https://docs.rubocop.org/rubocop-minitest/',
22
22
  'changelog_uri' => 'https://github.com/rubocop-hq/rubocop-minitest/blob/master/CHANGELOG.md',
23
23
  'source_code_uri' => 'https://github.com/rubocop-hq/rubocop-minitest',
24
- 'documentation_uri' => 'https://docs.rubocop.org/projects/minitest/',
24
+ 'documentation_uri' => 'https://docs.rubocop.org/rubocop-minitest/',
25
25
  'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop-minitest/issues'
26
26
  }
27
27
 
@@ -34,6 +34,6 @@ Gem::Specification.new do |spec|
34
34
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
35
  spec.require_paths = ['lib']
36
36
 
37
- spec.add_runtime_dependency 'rubocop', '>= 0.74'
37
+ spec.add_runtime_dependency 'rubocop', '>= 0.87'
38
38
  spec.add_development_dependency 'minitest', '~> 5.11'
39
39
  end
@@ -17,7 +17,8 @@ task generate_cops_documentation: :yard_for_generate_documentation do
17
17
 
18
18
  def cops_body(config, cop, description, examples_objects, pars)
19
19
  content = h2(cop.cop_name)
20
- content << properties(config, cop)
20
+ content << required_ruby_version(cop)
21
+ content << properties(cop.new(config))
21
22
  content << "#{description}\n"
22
23
  content << examples(examples_objects) if examples_objects.count.positive?
23
24
  content << configurations(pars)
@@ -27,30 +28,36 @@ task generate_cops_documentation: :yard_for_generate_documentation do
27
28
 
28
29
  def examples(examples_object)
29
30
  examples_object.each_with_object(h3('Examples').dup) do |example, content|
31
+ content << "\n" unless content.end_with?("\n\n")
30
32
  content << h4(example.name) unless example.name == ''
31
33
  content << code_example(example)
32
34
  end
33
35
  end
34
36
 
37
+ def required_ruby_version(cop)
38
+ return '' unless cop.respond_to?(:required_minimum_ruby_version)
39
+
40
+ "NOTE: Required Ruby version: #{cop.required_minimum_ruby_version}\n\n"
41
+ end
42
+
35
43
  # rubocop:disable Metrics/MethodLength
36
- def properties(config, cop)
44
+ def properties(cop_instance)
37
45
  header = [
38
46
  'Enabled by default', 'Safe', 'Supports autocorrection', 'VersionAdded',
39
47
  'VersionChanged'
40
48
  ]
41
- config = config.for_cop(cop)
42
- safe_auto_correct = config.fetch('SafeAutoCorrect', true)
43
- autocorrect = if cop.new.support_autocorrect?
44
- "Yes #{'(Unsafe)' unless safe_auto_correct}"
49
+ autocorrect = if cop_instance.support_autocorrect?
50
+ "Yes#{' (Unsafe)' unless cop_instance.safe_autocorrect?}"
45
51
  else
46
52
  'No'
47
53
  end
54
+ cop_config = cop_instance.cop_config
48
55
  content = [[
49
- config.fetch('Enabled') ? 'Enabled' : 'Disabled',
50
- config.fetch('Safe', true) ? 'Yes' : 'No',
56
+ cop_status(cop_config.fetch('Enabled')),
57
+ cop_config.fetch('Safe', true) ? 'Yes' : 'No',
51
58
  autocorrect,
52
- config.fetch('VersionAdded', '-'),
53
- config.fetch('VersionChanged', '-')
59
+ cop_config.fetch('VersionAdded', '-'),
60
+ cop_config.fetch('VersionChanged', '-')
54
61
  ]]
55
62
  to_table(header, content) + "\n"
56
63
  end
@@ -58,29 +65,29 @@ task generate_cops_documentation: :yard_for_generate_documentation do
58
65
 
59
66
  def h2(title)
60
67
  content = +"\n"
61
- content << "## #{title}\n"
68
+ content << "== #{title}\n"
62
69
  content << "\n"
63
70
  content
64
71
  end
65
72
 
66
73
  def h3(title)
67
74
  content = +"\n"
68
- content << "### #{title}\n"
75
+ content << "=== #{title}\n"
69
76
  content << "\n"
70
77
  content
71
78
  end
72
79
 
73
80
  def h4(title)
74
- content = +"#### #{title}\n"
81
+ content = +"==== #{title}\n"
75
82
  content << "\n"
76
83
  content
77
84
  end
78
85
 
79
86
  def code_example(ruby_code)
80
- content = +"```ruby\n"
81
- content << ruby_code.text
82
- .gsub('@good', '# good').gsub('@bad', '# bad').strip
83
- content << "\n```\n"
87
+ content = +"[source,ruby]\n----\n"
88
+ content << ruby_code.text.gsub('@good', '# good')
89
+ .gsub('@bad', '# bad').strip
90
+ content << "\n----\n"
84
91
  content
85
92
  end
86
93
 
@@ -88,7 +95,10 @@ task generate_cops_documentation: :yard_for_generate_documentation do
88
95
  return '' if pars.empty?
89
96
 
90
97
  header = ['Name', 'Default value', 'Configurable values']
91
- configs = pars.each_key.reject { |key| key.start_with?('Supported') }
98
+ configs = pars
99
+ .each_key
100
+ .reject { |key| key.start_with?('Supported') }
101
+ .reject { |key| key.start_with?('AllowMultipleStyles') }
92
102
  content = configs.map do |name|
93
103
  configurable = configurable_values(pars, name)
94
104
  default = format_table_value(pars[name])
@@ -106,8 +116,6 @@ task generate_cops_documentation: :yard_for_generate_documentation do
106
116
  format_table_value(pars[supported_style_name])
107
117
  when 'IndentationWidth'
108
118
  'Integer'
109
- when 'Database'
110
- format_table_value(pars['SupportedDatabases'])
111
119
  else
112
120
  case pars[name]
113
121
  when String
@@ -129,11 +137,14 @@ task generate_cops_documentation: :yard_for_generate_documentation do
129
137
 
130
138
  def to_table(header, content)
131
139
  table = [
132
- header.join(' | '),
133
- Array.new(header.size, '---').join(' | ')
134
- ]
135
- table.concat(content.map { |c| c.join(' | ') })
136
- table.join("\n") + "\n"
140
+ '|===',
141
+ "| #{header.join(' | ')}\n\n"
142
+ ].join("\n")
143
+ marked_contents = content.map do |plain_content|
144
+ plain_content.map { |c| "| #{c}" }.join("\n")
145
+ end
146
+ table << marked_contents.join("\n\n")
147
+ table << "\n|===\n"
137
148
  end
138
149
 
139
150
  def format_table_value(val)
@@ -146,11 +157,20 @@ task generate_cops_documentation: :yard_for_generate_documentation do
146
157
  val.map { |config| format_table_value(config) }.join(', ')
147
158
  end
148
159
  else
149
- "`#{val.nil? ? '<none>' : val}`"
160
+ wrap_backtick(val.nil? ? '<none>' : val)
150
161
  end
151
162
  value.gsub("#{Dir.pwd}/", '').rstrip
152
163
  end
153
164
 
165
+ def wrap_backtick(value)
166
+ if value.is_a?(String)
167
+ # Use `+` to prevent text like `**/*.gemspec` from being bold.
168
+ value.start_with?('*') ? "`+#{value}+`" : "`#{value}`"
169
+ else
170
+ "`#{value}`"
171
+ end
172
+ end
173
+
154
174
  def references(config, cop)
155
175
  cop_config = config.for_cop(cop)
156
176
  urls = RuboCop::Cop::MessageAnnotator.new(
@@ -159,27 +179,30 @@ task generate_cops_documentation: :yard_for_generate_documentation do
159
179
  return '' if urls.empty?
160
180
 
161
181
  content = h3('References')
162
- content << urls.map { |url| "* [#{url}](#{url})" }.join("\n")
182
+ content << urls.map { |url| "* #{url}" }.join("\n")
163
183
  content << "\n"
164
184
  content
165
185
  end
166
186
 
187
+ # rubocop:disable Metrics/AbcSize
167
188
  def print_cops_of_department(cops, department, config)
168
189
  selected_cops = cops_of_department(cops, department).select do |cop|
169
190
  cop.to_s.start_with?('RuboCop::Cop::Minitest')
170
191
  end
171
192
  return if selected_cops.empty?
172
193
 
173
- content = +"# #{department}\n"
194
+ selected_cops = cops_of_department(cops, department)
195
+ content = +"= #{department}\n"
174
196
  selected_cops.each do |cop|
175
197
  content << print_cop_with_doc(cop, config)
176
198
  end
177
- file_name = "#{Dir.pwd}/manual/cops_#{department.downcase}.md"
199
+ file_name = "#{Dir.pwd}/docs/modules/ROOT/pages/cops_#{department.downcase}.adoc"
178
200
  File.open(file_name, 'w') do |file|
179
201
  puts "* generated #{file_name}"
180
202
  file.write(content.strip + "\n")
181
203
  end
182
204
  end
205
+ # rubocop:enable Metrics/AbcSize
183
206
 
184
207
  def print_cop_with_doc(cop, config)
185
208
  t = config.for_cop(cop)
@@ -190,15 +213,21 @@ task generate_cops_documentation: :yard_for_generate_documentation do
190
213
  pars = t.reject { |k| non_display_keys.include? k }
191
214
  description = 'No documentation'
192
215
  examples_object = []
193
- YARD::Registry.all(:class).detect do |code_object|
194
- next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
195
-
216
+ cop_code(cop) do |code_object|
196
217
  description = code_object.docstring unless code_object.docstring.blank?
197
218
  examples_object = code_object.tags('example')
198
219
  end
199
220
  cops_body(config, cop, description, examples_object, pars)
200
221
  end
201
222
 
223
+ def cop_code(cop)
224
+ YARD::Registry.all(:class).detect do |code_object|
225
+ next unless RuboCop::Cop::Badge.for(code_object.to_s) == cop.badge
226
+
227
+ yield code_object
228
+ end
229
+ end
230
+
202
231
  # rubocop:disable Metrics/AbcSize
203
232
  def table_of_content_for_department(cops, department)
204
233
  selected_cops = cops_of_department(cops, department.to_sym).select do |cop|
@@ -207,11 +236,11 @@ task generate_cops_documentation: :yard_for_generate_documentation do
207
236
  return if selected_cops.empty?
208
237
 
209
238
  type_title = department[0].upcase + department[1..-1]
210
- filename = "cops_#{department.downcase}.md"
211
- content = +"#### Department [#{type_title}](#{filename})\n\n"
212
- selected_cops.each do |cop|
239
+ filename = "cops_#{department.downcase}.adoc"
240
+ content = +"= Department xref:#{filename}[#{type_title}]\n\n"
241
+ cops_of_department(cops, department.to_sym).each do |cop|
213
242
  anchor = cop.cop_name.sub('/', '').downcase
214
- content << "* [#{cop.cop_name}](#{filename}##{anchor})\n"
243
+ content << "* xref:#{filename}##{anchor}[#{cop.cop_name}]\n"
215
244
  end
216
245
 
217
246
  content
@@ -219,21 +248,17 @@ task generate_cops_documentation: :yard_for_generate_documentation do
219
248
  # rubocop:enable Metrics/AbcSize
220
249
 
221
250
  def print_table_of_contents(cops)
222
- path = "#{Dir.pwd}/manual/cops.md"
251
+ path = "#{Dir.pwd}/docs/modules/ROOT/pages/cops.adoc"
223
252
  original = File.read(path)
224
- content = +"<!-- START_COP_LIST -->\n"
253
+ content = +"// START_COP_LIST\n\n"
225
254
 
226
255
  content << table_contents(cops)
227
256
 
228
- content << "\n<!-- END_COP_LIST -->"
257
+ content << "\n// END_COP_LIST"
229
258
 
230
- content = if original.empty?
231
- content
232
- else
233
- original.sub(
234
- /<!-- START_COP_LIST -->.+<!-- END_COP_LIST -->/m, content
235
- )
236
- end
259
+ content = original.sub(
260
+ %r{// START_COP_LIST.+// END_COP_LIST}m, content
261
+ )
237
262
  File.write(path, content)
238
263
  end
239
264
 
@@ -243,19 +268,25 @@ task generate_cops_documentation: :yard_for_generate_documentation do
243
268
  .map(&:to_s)
244
269
  .sort
245
270
  .map { |department| table_of_content_for_department(cops, department) }
246
- .reject(&:nil?)
271
+ .compact
247
272
  .join("\n")
248
273
  end
249
274
 
250
- def assert_manual_synchronized
275
+ def cop_status(status)
276
+ return 'Disabled' unless status
277
+
278
+ status == 'pending' ? 'Pending' : 'Enabled'
279
+ end
280
+
281
+ def assert_docs_synchronized
251
282
  # Do not print diff and yield whether exit code was zero
252
- sh('git diff --quiet manual') do |outcome, _|
283
+ sh('git diff --quiet docs') do |outcome, _|
253
284
  return if outcome
254
285
 
255
286
  # Output diff before raising error
256
- sh('GIT_PAGER=cat git diff manual')
287
+ sh('GIT_PAGER=cat git diff docs')
257
288
 
258
- warn 'The manual directory is out of sync. ' \
289
+ warn 'The docs directory is out of sync. ' \
259
290
  'Run `rake generate_cops_documentation` and commit the results.'
260
291
  exit!
261
292
  end
@@ -272,7 +303,7 @@ task generate_cops_documentation: :yard_for_generate_documentation do
272
303
 
273
304
  print_table_of_contents(cops)
274
305
 
275
- assert_manual_synchronized if ENV['CI'] == 'true'
306
+ assert_docs_synchronized if ENV['CI'] == 'true'
276
307
  ensure
277
308
  RuboCop::ConfigLoader.default_configuration = nil
278
309
  end