rubocop 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +42 -0
  4. data/README.md +59 -54
  5. data/config/default.yml +124 -95
  6. data/config/disabled.yml +4 -0
  7. data/config/enabled.yml +286 -264
  8. data/lib/rubocop.rb +10 -3
  9. data/lib/rubocop/cli.rb +10 -171
  10. data/lib/rubocop/cop/cop.rb +14 -11
  11. data/lib/rubocop/cop/lint/debugger.rb +35 -0
  12. data/lib/rubocop/cop/lint/syntax.rb +34 -0
  13. data/lib/rubocop/cop/offence.rb +15 -23
  14. data/lib/rubocop/cop/style/align_array.rb +1 -10
  15. data/lib/rubocop/cop/style/align_hash.rb +14 -2
  16. data/lib/rubocop/cop/style/align_parameters.rb +1 -11
  17. data/lib/rubocop/cop/style/and_or.rb +13 -11
  18. data/lib/rubocop/cop/style/attr.rb +1 -1
  19. data/lib/rubocop/cop/style/autocorrect_alignment.rb +31 -6
  20. data/lib/rubocop/cop/style/blocks.rb +14 -6
  21. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  22. data/lib/rubocop/cop/style/class_length.rb +38 -0
  23. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  24. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  25. data/lib/rubocop/cop/style/configurable_naming.rb +47 -0
  26. data/lib/rubocop/cop/style/def_parentheses.rb +2 -2
  27. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  28. data/lib/rubocop/cop/style/even_odd.rb +7 -3
  29. data/lib/rubocop/cop/style/hash_methods.rb +1 -1
  30. data/lib/rubocop/cop/style/hash_syntax.rb +36 -7
  31. data/lib/rubocop/cop/style/lambda_call.rb +36 -11
  32. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  33. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +41 -0
  34. data/lib/rubocop/cop/style/method_length.rb +2 -13
  35. data/lib/rubocop/cop/style/method_name.rb +42 -0
  36. data/lib/rubocop/cop/style/numeric_literals.rb +4 -0
  37. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  38. data/lib/rubocop/cop/style/raise_args.rb +60 -0
  39. data/lib/rubocop/cop/style/redundant_exception.rb +32 -0
  40. data/lib/rubocop/cop/style/redundant_return.rb +14 -5
  41. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  42. data/lib/rubocop/cop/style/semicolon.rb +13 -4
  43. data/lib/rubocop/cop/style/signal_exception.rb +63 -14
  44. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  45. data/lib/rubocop/cop/style/string_help.rb +4 -1
  46. data/lib/rubocop/cop/style/string_literals.rb +34 -11
  47. data/lib/rubocop/cop/style/surrounding_space.rb +64 -17
  48. data/lib/rubocop/cop/style/tab.rb +1 -1
  49. data/lib/rubocop/cop/style/variable_name.rb +44 -0
  50. data/lib/rubocop/cop/style/when_then.rb +1 -1
  51. data/lib/rubocop/cop/style/while_until_do.rb +1 -1
  52. data/lib/rubocop/cop/team.rb +4 -7
  53. data/lib/rubocop/cop/util.rb +12 -0
  54. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  55. data/lib/rubocop/formatter/emacs_style_formatter.rb +5 -2
  56. data/lib/rubocop/formatter/json_formatter.rb +1 -0
  57. data/lib/rubocop/formatter/progress_formatter.rb +9 -13
  58. data/lib/rubocop/formatter/simple_text_formatter.rb +63 -15
  59. data/lib/rubocop/options.rb +184 -0
  60. data/lib/rubocop/processed_source.rb +4 -0
  61. data/lib/rubocop/version.rb +1 -1
  62. data/rubocop.gemspec +1 -1
  63. data/spec/rubocop/cli_spec.rb +870 -1001
  64. data/spec/rubocop/config_spec.rb +13 -13
  65. data/spec/rubocop/config_store_spec.rb +38 -37
  66. data/spec/rubocop/cop/commissioner_spec.rb +42 -46
  67. data/spec/rubocop/cop/cop_spec.rb +72 -77
  68. data/spec/rubocop/cop/corrector_spec.rb +51 -55
  69. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +100 -106
  70. data/spec/rubocop/cop/lint/block_alignment_spec.rb +403 -409
  71. data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
  72. data/spec/rubocop/cop/lint/empty_ensure_spec.rb +19 -25
  73. data/spec/rubocop/cop/lint/end_alignment_spec.rb +50 -56
  74. data/spec/rubocop/cop/lint/end_in_method_spec.rb +20 -26
  75. data/spec/rubocop/cop/lint/ensure_return_spec.rb +30 -36
  76. data/spec/rubocop/cop/lint/eval_spec.rb +25 -31
  77. data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +22 -28
  78. data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +51 -57
  79. data/spec/rubocop/cop/lint/loop_spec.rb +17 -23
  80. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +51 -57
  81. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +105 -111
  82. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +230 -236
  83. data/spec/rubocop/cop/lint/syntax_spec.rb +33 -0
  84. data/spec/rubocop/cop/lint/unreachable_code_spec.rb +51 -57
  85. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1544 -1550
  86. data/spec/rubocop/cop/lint/useless_comparison_spec.rb +21 -27
  87. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +119 -125
  88. data/spec/rubocop/cop/lint/void_spec.rb +50 -56
  89. data/spec/rubocop/cop/offence_spec.rb +101 -131
  90. data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +6 -12
  91. data/spec/rubocop/cop/rails/read_attribute_spec.rb +6 -12
  92. data/spec/rubocop/cop/rails/validation_spec.rb +13 -19
  93. data/spec/rubocop/cop/style/access_control_spec.rb +146 -152
  94. data/spec/rubocop/cop/style/alias_spec.rb +30 -36
  95. data/spec/rubocop/cop/style/align_array_spec.rb +57 -56
  96. data/spec/rubocop/cop/style/align_hash_spec.rb +256 -237
  97. data/spec/rubocop/cop/style/align_parameters_spec.rb +216 -202
  98. data/spec/rubocop/cop/style/and_or_spec.rb +51 -55
  99. data/spec/rubocop/cop/style/ascii_comments_spec.rb +14 -20
  100. data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +26 -32
  101. data/spec/rubocop/cop/style/attr_spec.rb +11 -17
  102. data/spec/rubocop/cop/style/begin_block_spec.rb +6 -12
  103. data/spec/rubocop/cop/style/block_comments_spec.rb +13 -19
  104. data/spec/rubocop/cop/style/block_nesting_spec.rb +134 -140
  105. data/spec/rubocop/cop/style/blocks_spec.rb +81 -84
  106. data/spec/rubocop/cop/style/case_equality_spec.rb +5 -11
  107. data/spec/rubocop/cop/style/case_indentation_spec.rb +71 -77
  108. data/spec/rubocop/cop/style/character_literal_spec.rb +31 -37
  109. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +31 -37
  110. data/spec/rubocop/cop/style/class_length_spec.rb +65 -0
  111. data/spec/rubocop/cop/style/class_methods_spec.rb +35 -41
  112. data/spec/rubocop/cop/style/class_vars_spec.rb +11 -17
  113. data/spec/rubocop/cop/style/collection_methods_spec.rb +41 -47
  114. data/spec/rubocop/cop/style/colon_method_call_spec.rb +45 -51
  115. data/spec/rubocop/cop/style/comment_annotation_spec.rb +79 -85
  116. data/spec/rubocop/cop/style/constant_name_spec.rb +45 -51
  117. data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +28 -34
  118. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +23 -29
  119. data/spec/rubocop/cop/style/documentation_spec.rb +60 -66
  120. data/spec/rubocop/cop/style/dot_position_spec.rb +50 -56
  121. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +109 -110
  122. data/spec/rubocop/cop/style/empty_lines_spec.rb +28 -34
  123. data/spec/rubocop/cop/style/empty_literal_spec.rb +80 -86
  124. data/spec/rubocop/cop/style/encoding_spec.rb +50 -56
  125. data/spec/rubocop/cop/style/end_block_spec.rb +6 -12
  126. data/spec/rubocop/cop/style/end_of_line_spec.rb +13 -19
  127. data/spec/rubocop/cop/style/even_odd_spec.rb +69 -41
  128. data/spec/rubocop/cop/style/favor_join_spec.rb +21 -27
  129. data/spec/rubocop/cop/style/favor_modifier_spec.rb +180 -176
  130. data/spec/rubocop/cop/style/favor_sprintf_spec.rb +41 -47
  131. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +63 -69
  132. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +32 -38
  133. data/spec/rubocop/cop/style/final_newline_spec.rb +16 -22
  134. data/spec/rubocop/cop/style/for_spec.rb +18 -24
  135. data/spec/rubocop/cop/style/global_vars_spec.rb +23 -29
  136. data/spec/rubocop/cop/style/hash_methods_spec.rb +39 -45
  137. data/spec/rubocop/cop/style/hash_syntax_spec.rb +93 -65
  138. data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +11 -17
  139. data/spec/rubocop/cop/style/indentation_width_spec.rb +377 -383
  140. data/spec/rubocop/cop/style/lambda_call_spec.rb +40 -22
  141. data/spec/rubocop/cop/style/lambda_spec.rb +30 -34
  142. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +44 -50
  143. data/spec/rubocop/cop/style/line_length_spec.rb +11 -17
  144. data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +14 -20
  145. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
  146. data/spec/rubocop/cop/style/method_length_spec.rb +127 -133
  147. data/spec/rubocop/cop/style/method_name_spec.rb +103 -0
  148. data/spec/rubocop/cop/style/module_function_spec.rb +16 -22
  149. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +64 -70
  150. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +74 -80
  151. data/spec/rubocop/cop/style/nil_comparison_spec.rb +20 -26
  152. data/spec/rubocop/cop/style/not_spec.rb +13 -19
  153. data/spec/rubocop/cop/style/numeric_literals_spec.rb +36 -35
  154. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +5 -11
  155. data/spec/rubocop/cop/style/op_method_spec.rb +61 -67
  156. data/spec/rubocop/cop/style/parameter_lists_spec.rb +31 -37
  157. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +62 -68
  158. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +9 -15
  159. data/spec/rubocop/cop/style/proc_spec.rb +13 -19
  160. data/spec/rubocop/cop/style/raise_args_spec.rb +55 -0
  161. data/spec/rubocop/cop/style/reduce_arguments_spec.rb +49 -55
  162. data/spec/rubocop/cop/style/redundant_begin_spec.rb +47 -53
  163. data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
  164. data/spec/rubocop/cop/style/redundant_return_spec.rb +164 -72
  165. data/spec/rubocop/cop/style/redundant_self_spec.rb +109 -115
  166. data/spec/rubocop/cop/style/regexp_literal_spec.rb +57 -63
  167. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +99 -105
  168. data/spec/rubocop/cop/style/semicolon_spec.rb +95 -90
  169. data/spec/rubocop/cop/style/signal_exception_spec.rb +248 -95
  170. data/spec/rubocop/cop/style/single_line_methods_spec.rb +45 -51
  171. data/spec/rubocop/cop/style/space_after_colon_spec.rb +14 -20
  172. data/spec/rubocop/cop/style/space_after_comma_spec.rb +16 -22
  173. data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +48 -54
  174. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +49 -55
  175. data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +10 -16
  176. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +118 -50
  177. data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +18 -24
  178. data/spec/rubocop/cop/style/space_around_operators_spec.rb +204 -210
  179. data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +36 -42
  180. data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +39 -45
  181. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +62 -68
  182. data/spec/rubocop/cop/style/space_inside_parens_spec.rb +18 -24
  183. data/spec/rubocop/cop/style/special_global_vars_spec.rb +40 -46
  184. data/spec/rubocop/cop/style/string_literals_spec.rb +148 -70
  185. data/spec/rubocop/cop/style/symbol_array_spec.rb +28 -34
  186. data/spec/rubocop/cop/style/symbol_name_spec.rb +132 -138
  187. data/spec/rubocop/cop/style/tab_spec.rb +9 -15
  188. data/spec/rubocop/cop/style/ternary_operator_spec.rb +25 -31
  189. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +10 -16
  190. data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +14 -20
  191. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +374 -380
  192. data/spec/rubocop/cop/style/unless_else_spec.rb +17 -23
  193. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +36 -42
  194. data/spec/rubocop/cop/style/variable_name_spec.rb +83 -0
  195. data/spec/rubocop/cop/style/when_then_spec.rb +31 -37
  196. data/spec/rubocop/cop/style/while_until_do_spec.rb +47 -53
  197. data/spec/rubocop/cop/style/word_array_spec.rb +49 -55
  198. data/spec/rubocop/cop/team_spec.rb +125 -127
  199. data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +176 -180
  200. data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +580 -586
  201. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +140 -146
  202. data/spec/rubocop/cop/variable_inspector/variable_spec.rb +52 -58
  203. data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +217 -223
  204. data/spec/rubocop/cop/variable_inspector_spec.rb +15 -19
  205. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +35 -36
  206. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -2
  207. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +24 -1
  208. data/spec/rubocop/formatter/file_list_formatter_spec.rb +1 -1
  209. data/spec/rubocop/formatter/formatter_set_spec.rb +3 -3
  210. data/spec/rubocop/formatter/json_formatter_spec.rb +7 -2
  211. data/spec/rubocop/formatter/offence_count_formatter_spec.rb +1 -1
  212. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  213. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +55 -6
  214. data/spec/rubocop/options_spec.rb +233 -0
  215. data/spec/rubocop/processed_source_spec.rb +85 -42
  216. data/spec/rubocop/source_parser_spec.rb +108 -110
  217. data/spec/rubocop/target_finder_spec.rb +125 -127
  218. data/spec/rubocop/token_spec.rb +15 -17
  219. metadata +32 -8
  220. data/lib/rubocop/backports/bsearch.rb +0 -39
  221. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +0 -77
  222. data/spec/rubocop/cop/style/method_and_variable_snake_case_spec.rb +0 -102
@@ -2,30 +2,26 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- module Rubocop
6
- module Cop
7
- describe VariableInspector do
8
- include AST::Sexp
5
+ describe Rubocop::Cop::VariableInspector do
6
+ include AST::Sexp
9
7
 
10
- class ExampleInspector
11
- include VariableInspector
12
- end
8
+ class ExampleInspector
9
+ include Rubocop::Cop::VariableInspector
10
+ end
13
11
 
14
- subject(:inspector) { ExampleInspector.new }
12
+ subject(:inspector) { ExampleInspector.new }
15
13
 
16
- describe '#process_node' do
17
- before do
18
- inspector.variable_table.push_scope(s(:def))
19
- end
14
+ describe '#process_node' do
15
+ before do
16
+ inspector.variable_table.push_scope(s(:def))
17
+ end
20
18
 
21
- context 'when processing lvar node' do
22
- let(:node) { s(:lvar, :foo) }
19
+ context 'when processing lvar node' do
20
+ let(:node) { s(:lvar, :foo) }
23
21
 
24
- context 'when the variable is not yet declared' do
25
- it 'does not raise error' do
26
- expect { inspector.process_node(node) }.not_to raise_error
27
- end
28
- end
22
+ context 'when the variable is not yet declared' do
23
+ it 'does not raise error' do
24
+ expect { inspector.process_node(node) }.not_to raise_error
29
25
  end
30
26
  end
31
27
  end
@@ -6,43 +6,9 @@ require 'stringio'
6
6
  module Rubocop
7
7
  module Formatter
8
8
  describe ClangStyleFormatter do
9
- subject(:formatter) { ClangStyleFormatter.new(output) }
9
+ subject(:formatter) { described_class.new(output) }
10
10
  let(:output) { StringIO.new }
11
11
 
12
- describe '#report_summary' do
13
- context 'when no files inspected' do
14
- it 'handles pluralization correctly' do
15
- formatter.report_summary(0, 0)
16
- expect(output.string).to eq(
17
- "\n0 files inspected, no offences detected\n")
18
- end
19
- end
20
-
21
- context 'when a file inspected and no offences detected' do
22
- it 'handles pluralization correctly' do
23
- formatter.report_summary(1, 0)
24
- expect(output.string).to eq(
25
- "\n1 file inspected, no offences detected\n")
26
- end
27
- end
28
-
29
- context 'when a offence detected' do
30
- it 'handles pluralization correctly' do
31
- formatter.report_summary(1, 1)
32
- expect(output.string).to eq(
33
- "\n1 file inspected, 1 offence detected\n")
34
- end
35
- end
36
-
37
- context 'when 2 offences detected' do
38
- it 'handles pluralization correctly' do
39
- formatter.report_summary(2, 2)
40
- expect(output.string).to eq(
41
- "\n2 files inspected, 2 offences detected\n")
42
- end
43
- end
44
- end
45
-
46
12
  describe '#report_file' do
47
13
  it 'displays text containing the offending source line' do
48
14
  cop = Cop::Cop.new
@@ -73,7 +39,7 @@ module Rubocop
73
39
  Parser::Source::Range.new(source_buffer, 0, 2),
74
40
  'message 1')
75
41
  cop.add_offence(:fatal, nil,
76
- Parser::Source::Range.new(source_buffer, 6, 4),
42
+ Parser::Source::Range.new(source_buffer, 6, 10),
77
43
  'message 2')
78
44
 
79
45
  formatter.report_file('test', cop.offences)
@@ -83,6 +49,39 @@ module Rubocop
83
49
  '^^^^',
84
50
  ''].join("\n")
85
51
  end
52
+
53
+ let(:file) { '/path/to/file' }
54
+
55
+ let(:offence) do
56
+ Cop::Offence.new(:convention, location,
57
+ 'This is a message.', 'CopName', corrected)
58
+ end
59
+
60
+ let(:location) do
61
+ source_buffer = Parser::Source::Buffer.new('test', 1)
62
+ source_buffer.source = "a\n"
63
+ Parser::Source::Range.new(source_buffer, 0, 1)
64
+ end
65
+
66
+ context 'when the offence is not corrected' do
67
+ let(:corrected) { false }
68
+
69
+ it 'prints message as-is' do
70
+ formatter.report_file(file, [offence])
71
+ expect(output.string)
72
+ .to include(': This is a message.')
73
+ end
74
+ end
75
+
76
+ context 'when the offence is automatically corrected' do
77
+ let(:corrected) { true }
78
+
79
+ it 'prints [Corrected] along with message' do
80
+ formatter.report_file(file, [offence])
81
+ expect(output.string)
82
+ .to include(': [Corrected] This is a message.')
83
+ end
84
+ end
86
85
  end
87
86
  end
88
87
  end
@@ -7,7 +7,7 @@ require 'ostruct'
7
7
  module Rubocop
8
8
  module Formatter
9
9
  describe DisabledConfigFormatter do
10
- subject(:formatter) { DisabledConfigFormatter.new(output) }
10
+ subject(:formatter) { described_class.new(output) }
11
11
  let(:output) do
12
12
  o = StringIO.new
13
13
  def o.path
@@ -26,7 +26,7 @@ module Rubocop
26
26
  it 'displays YAML configuration disabling all cops with offences' do
27
27
  formatter.file_finished('test.rb', offences)
28
28
  formatter.finished(['test.rb'])
29
- expect(output.string).to eq(DisabledConfigFormatter::HEADING +
29
+ expect(output.string).to eq(described_class::HEADING +
30
30
  ['',
31
31
  '',
32
32
  'Cop1:',
@@ -6,7 +6,7 @@ require 'stringio'
6
6
  module Rubocop
7
7
  module Formatter
8
8
  describe EmacsStyleFormatter do
9
- subject(:formatter) { EmacsStyleFormatter.new(output) }
9
+ subject(:formatter) { described_class.new(output) }
10
10
  let(:output) { StringIO.new }
11
11
 
12
12
  describe '#file_finished' do
@@ -26,6 +26,29 @@ module Rubocop
26
26
  expect(output.string).to eq ['test:1:1: C: message 1',
27
27
  "test:3:6: F: message 2\n"].join("\n")
28
28
  end
29
+
30
+ context 'when the offence is automatically corrected' do
31
+ let(:file) { '/path/to/file' }
32
+
33
+ let(:offence) do
34
+ Cop::Offence.new(:convention, location,
35
+ 'This is a message.', 'CopName', corrected)
36
+ end
37
+
38
+ let(:location) do
39
+ source_buffer = Parser::Source::Buffer.new('test', 1)
40
+ source_buffer.source = "a\n"
41
+ Parser::Source::Range.new(source_buffer, 0, 1)
42
+ end
43
+
44
+ let(:corrected) { true }
45
+
46
+ it 'prints [Corrected] along with message' do
47
+ formatter.file_finished(file, [offence])
48
+ expect(output.string)
49
+ .to include(': [Corrected] This is a message.')
50
+ end
51
+ end
29
52
  end
30
53
 
31
54
  describe '#finished' do
@@ -6,7 +6,7 @@ require 'stringio'
6
6
  module Rubocop
7
7
  module Formatter
8
8
  describe FileListFormatter do
9
- subject(:formatter) { FileListFormatter.new(output) }
9
+ subject(:formatter) { described_class.new(output) }
10
10
  let(:output) { StringIO.new }
11
11
 
12
12
  describe '#file_finished' do
@@ -6,7 +6,7 @@ require 'tempfile'
6
6
  module Rubocop
7
7
  module Formatter
8
8
  describe FormatterSet do
9
- subject(:formatter_set) { FormatterSet.new }
9
+ subject(:formatter_set) { described_class.new }
10
10
 
11
11
  it 'responds to all formatter API methods' do
12
12
  [:started, :file_started, :file_finished, :finished].each do |method|
@@ -88,7 +88,7 @@ module Rubocop
88
88
 
89
89
  describe '#builtin_formatter_class' do
90
90
  def builtin_formatter_class(string)
91
- FormatterSet.new.send(:builtin_formatter_class, string)
91
+ described_class.new.send(:builtin_formatter_class, string)
92
92
  end
93
93
 
94
94
  it 'returns class which matches passed alias name exactly' do
@@ -105,7 +105,7 @@ module Rubocop
105
105
 
106
106
  describe '#custom_formatter_class' do
107
107
  def custom_formatter_class(string)
108
- FormatterSet.new.send(:custom_formatter_class, string)
108
+ described_class.new.send(:custom_formatter_class, string)
109
109
  end
110
110
 
111
111
  it 'returns constant represented by the passed string' do
@@ -5,7 +5,7 @@ require 'stringio'
5
5
 
6
6
  module Rubocop
7
7
  describe Formatter::JSONFormatter do
8
- subject(:formatter) { Formatter::JSONFormatter.new(output) }
8
+ subject(:formatter) { described_class.new(output) }
9
9
  let(:output) { StringIO.new }
10
10
  let(:files) { %w(/path/to/file1 /path/to/file2) }
11
11
  let(:location) do
@@ -14,7 +14,8 @@ module Rubocop
14
14
  Parser::Source::Range.new(source_buffer, 9, 10)
15
15
  end
16
16
  let(:offence) do
17
- Cop::Offence.new(:convention, location, 'This is message', 'CopName')
17
+ Cop::Offence.new(:convention, location,
18
+ 'This is message', 'CopName', true)
18
19
  end
19
20
 
20
21
  describe '#started' do
@@ -116,6 +117,10 @@ module Rubocop
116
117
  expect(hash[:cop_name]).to eq('CopName')
117
118
  end
118
119
 
120
+ it 'sets Offence#corrected? value for :corrected key' do
121
+ expect(hash[:corrected]).to be_true
122
+ end
123
+
119
124
  before do
120
125
  formatter.stub(:hash_for_location).and_return(location_hash)
121
126
  end
@@ -7,7 +7,7 @@ require 'tempfile'
7
7
  module Rubocop
8
8
  module Formatter
9
9
  describe OffenceCountFormatter do
10
- subject(:formatter) { Formatter::OffenceCountFormatter.new(output) }
10
+ subject(:formatter) { described_class.new(output) }
11
11
  let(:output) { StringIO.new }
12
12
 
13
13
  let(:files) do
@@ -5,7 +5,7 @@ require 'stringio'
5
5
 
6
6
  module Rubocop
7
7
  describe Formatter::ProgressFormatter do
8
- subject(:formatter) { Formatter::ProgressFormatter.new(output) }
8
+ subject(:formatter) { described_class.new(output) }
9
9
  let(:output) { StringIO.new }
10
10
 
11
11
  let(:files) do
@@ -7,14 +7,29 @@ require 'tempfile'
7
7
  module Rubocop
8
8
  module Formatter
9
9
  describe SimpleTextFormatter do
10
- subject(:formatter) { SimpleTextFormatter.new(output) }
10
+ subject(:formatter) { described_class.new(output) }
11
11
  let(:output) { StringIO.new }
12
12
 
13
13
  describe '#report_file' do
14
14
  before do
15
- formatter.report_file(file, [])
15
+ formatter.report_file(file, [offence])
16
16
  end
17
17
 
18
+ let(:file) { '/path/to/file' }
19
+
20
+ let(:offence) do
21
+ Cop::Offence.new(:convention, location,
22
+ 'This is a message.', 'CopName', corrected)
23
+ end
24
+
25
+ let(:location) do
26
+ source_buffer = Parser::Source::Buffer.new('test', 1)
27
+ source_buffer.source = "a\n"
28
+ Parser::Source::Range.new(source_buffer, 0, 1)
29
+ end
30
+
31
+ let(:corrected) { false }
32
+
18
33
  context 'the file is under the current working directory' do
19
34
  let(:file) { File.expand_path('spec/spec_helper.rb') }
20
35
 
@@ -34,12 +49,30 @@ module Rubocop
34
49
  expect(output.string).to include("== #{file} ==")
35
50
  end
36
51
  end
52
+
53
+ context 'when the offence is not corrected' do
54
+ let(:corrected) { false }
55
+
56
+ it 'prints message as-is' do
57
+ expect(output.string)
58
+ .to include(': This is a message.')
59
+ end
60
+ end
61
+
62
+ context 'when the offence is automatically corrected' do
63
+ let(:corrected) { true }
64
+
65
+ it 'prints [Corrected] along with message' do
66
+ expect(output.string)
67
+ .to include(': [Corrected] This is a message.')
68
+ end
69
+ end
37
70
  end
38
71
 
39
72
  describe '#report_summary' do
40
73
  context 'when no files inspected' do
41
74
  it 'handles pluralization correctly' do
42
- formatter.report_summary(0, 0)
75
+ formatter.report_summary(0, 0, 0)
43
76
  expect(output.string).to eq(
44
77
  "\n0 files inspected, no offences detected\n")
45
78
  end
@@ -47,7 +80,7 @@ module Rubocop
47
80
 
48
81
  context 'when a file inspected and no offences detected' do
49
82
  it 'handles pluralization correctly' do
50
- formatter.report_summary(1, 0)
83
+ formatter.report_summary(1, 0, 0)
51
84
  expect(output.string).to eq(
52
85
  "\n1 file inspected, no offences detected\n")
53
86
  end
@@ -55,7 +88,7 @@ module Rubocop
55
88
 
56
89
  context 'when a offence detected' do
57
90
  it 'handles pluralization correctly' do
58
- formatter.report_summary(1, 1)
91
+ formatter.report_summary(1, 1, 0)
59
92
  expect(output.string).to eq(
60
93
  "\n1 file inspected, 1 offence detected\n")
61
94
  end
@@ -63,11 +96,27 @@ module Rubocop
63
96
 
64
97
  context 'when 2 offences detected' do
65
98
  it 'handles pluralization correctly' do
66
- formatter.report_summary(2, 2)
99
+ formatter.report_summary(2, 2, 0)
67
100
  expect(output.string).to eq(
68
101
  "\n2 files inspected, 2 offences detected\n")
69
102
  end
70
103
  end
104
+
105
+ context 'when an offence is corrected' do
106
+ it 'prints about correction' do
107
+ formatter.report_summary(1, 1, 1)
108
+ expect(output.string).to eq(
109
+ "\n1 file inspected, 1 offence detected, 1 offence corrected\n")
110
+ end
111
+ end
112
+
113
+ context 'when 2 offences are corrected' do
114
+ it 'handles pluralization correctly' do
115
+ formatter.report_summary(1, 1, 2)
116
+ expect(output.string).to eq(
117
+ "\n1 file inspected, 1 offence detected, 2 offences corrected\n")
118
+ end
119
+ end
71
120
  end
72
121
  end
73
122
  end
@@ -0,0 +1,233 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Options, :isolated_environment do
6
+ include FileHelper
7
+
8
+ subject(:options) { described_class.new(config_store) }
9
+ let(:config_store) { Rubocop::ConfigStore.new }
10
+
11
+ before(:each) do
12
+ $stdout = StringIO.new
13
+ $stderr = StringIO.new
14
+ end
15
+
16
+ after(:each) do
17
+ $stdout = STDOUT
18
+ $stderr = STDERR
19
+ end
20
+
21
+ def abs(path)
22
+ File.expand_path(path)
23
+ end
24
+
25
+ describe 'option' do
26
+ describe '-h/--help' do
27
+ it 'exits cleanly' do
28
+ expect { options.parse ['-h'] }.to exit_with_code(0)
29
+ expect { options.parse ['--help'] }.to exit_with_code(0)
30
+ end
31
+
32
+ it 'shows help text' do
33
+ begin
34
+ options.parse(['--help'])
35
+ rescue SystemExit # rubocop:disable HandleExceptions
36
+ end
37
+
38
+ expected_help = <<-END
39
+ Usage: rubocop [options] [file1, file2, ...]
40
+ -d, --debug Display debug info.
41
+ -c, --config FILE Specify configuration file.
42
+ --only COP Run just one cop.
43
+ --auto-gen-config Generate a configuration file acting as a
44
+ TODO list.
45
+ --show-cops Shows cops and their config for the
46
+ current directory.
47
+ -f, --format FORMATTER Choose an output formatter. This option
48
+ can be specified multiple times to enable
49
+ multiple formatters at the same time.
50
+ [p]rogress (default)
51
+ [s]imple
52
+ [c]lang
53
+ [e]macs
54
+ [j]son
55
+ [f]iles
56
+ [o]ffences
57
+ custom formatter class name
58
+ -o, --out FILE Write output to a file instead of STDOUT.
59
+ This option applies to the previously
60
+ specified --format, or the default format
61
+ if no format is specified.
62
+ -r, --require FILE Require Ruby file.
63
+ -R, --rails Run extra Rails cops.
64
+ -l, --lint Run only lint cops.
65
+ -a, --auto-correct Auto-correct offences.
66
+ -n, --no-color Disable color output.
67
+ -v, --version Display version.
68
+ -V, --verbose-version Display verbose version.
69
+ END
70
+
71
+ expect($stdout.string).to eq(expected_help)
72
+ end
73
+
74
+ it 'lists all builtin formatters' do
75
+ begin
76
+ options.parse(['--help'])
77
+ rescue SystemExit # rubocop:disable HandleExceptions
78
+ end
79
+
80
+ option_sections = $stdout.string.lines.slice_before(/^\s*-/)
81
+
82
+ format_section = option_sections.find do |lines|
83
+ lines.first =~ /^\s*-f/
84
+ end
85
+
86
+ formatter_keys = format_section.reduce([]) do |keys, line|
87
+ match = line.match(/^[ ]{39}(\[[a-z\]]+)/)
88
+ next keys unless match
89
+ keys << match.captures.first.gsub(/\[|\]/, '')
90
+ end.sort
91
+
92
+ expected_formatter_keys =
93
+ Rubocop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS
94
+ .keys.sort
95
+
96
+ expect(formatter_keys).to eq(expected_formatter_keys)
97
+ end
98
+ end
99
+
100
+ describe '--version' do
101
+ it 'exits cleanly' do
102
+ expect { options.parse ['-v'] }.to exit_with_code(0)
103
+ expect { options.parse ['--version'] }.to exit_with_code(0)
104
+ expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
105
+ end
106
+ end
107
+
108
+ describe '--only' do
109
+ it 'exits with error if an incorrect cop name is passed' do
110
+ expect { options.parse(%w(--only 123)) }
111
+ .to raise_error(ArgumentError, /Unrecognized cop name: 123./)
112
+ end
113
+ end
114
+
115
+ describe '--show-cops' do
116
+ let(:cops) { Rubocop::Cop::Cop.all }
117
+
118
+ let(:global_conf) do
119
+ config_path = Rubocop::Config.configuration_file_for(Dir.pwd.to_s)
120
+ Rubocop::Config.configuration_from_file(config_path)
121
+ end
122
+
123
+ let(:stdout) { $stdout.string }
124
+
125
+ before do
126
+ expect { options.parse ['--show-cops'] }.to exit_with_code(0)
127
+ end
128
+
129
+ # Extracts the first line out of the description
130
+ def short_description_of_cop(cop)
131
+ desc = full_description_of_cop(cop)
132
+ desc ? desc.lines.first.strip : ''
133
+ end
134
+
135
+ # Gets the full description of the cop or nil if no description is set.
136
+ def full_description_of_cop(cop)
137
+ cop_config = global_conf.for_cop(cop)
138
+ cop_config['Description']
139
+ end
140
+
141
+ it 'prints all available cops and their description' do
142
+ cops.each do |cop|
143
+ expect(stdout).to include cop.cop_name
144
+ expect(stdout).to include short_description_of_cop(cop)
145
+ end
146
+ end
147
+
148
+ it 'prints all types' do
149
+ cops
150
+ .types
151
+ .map(&:to_s)
152
+ .map(&:capitalize)
153
+ .each { |type| expect(stdout).to include(type) }
154
+ end
155
+
156
+ it 'prints all cops in their right type listing' do
157
+ lines = stdout.lines
158
+ lines.slice_before(/Type /).each do |slice|
159
+ types = cops.types.map(&:to_s).map(&:capitalize)
160
+ current = types.delete(slice.shift[/Type '(?<c>[^'']+)'/, 'c'])
161
+ # all cops in their type listing
162
+ cops.with_type(current).each do |cop|
163
+ expect(slice.any? { |l| l.include? cop.cop_name }).to be_true
164
+ end
165
+
166
+ # no cop in wrong type listing
167
+ types.each do |type|
168
+ cops.with_type(type).each do |cop|
169
+ expect(slice.any? { |l| l.include? cop.cop_name }).to be_false
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ it 'prints the current configuration' do
176
+ out = stdout.lines.to_a
177
+ cops.each do |cop|
178
+ conf = global_conf[cop.cop_name].dup
179
+ confstrt =
180
+ out.find_index { |i| i.include?("- #{cop.cop_name}") } + 1
181
+ c = out[confstrt, conf.keys.size].to_s
182
+ conf.delete('Description')
183
+ expect(c).to include(short_description_of_cop(cop))
184
+ conf.each do |k, v|
185
+ # ugly hack to get hash/array content tested
186
+ if v.kind_of?(Hash) || v.kind_of?(Array)
187
+ expect(c).to include "#{k}: #{v.to_s.dump[2, -2]}"
188
+ else
189
+ expect(c).to include "#{k}: #{v}"
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ describe '--require' do
197
+ let(:required_file_path) { './path/to/required_file.rb' }
198
+
199
+ before do
200
+ create_file('example.rb', '# encoding: utf-8')
201
+
202
+ create_file(required_file_path, [
203
+ '# encoding: utf-8',
204
+ "puts 'Hello from required file!'"
205
+ ])
206
+ end
207
+
208
+ it 'requires the passed path' do
209
+ options.parse(['--require', required_file_path, 'example.rb'])
210
+ expect($stdout.string).to start_with('Hello from required file!')
211
+ end
212
+ end
213
+ end
214
+
215
+ unless Rubocop::Version::STRING.start_with?('0')
216
+ describe '-e/--emacs option' do
217
+ it 'is dropped in RuboCop 1.0.0' do
218
+ # This spec can be removed once the option is dropped.
219
+ expect { options.parse(['--emacs']) }
220
+ .to raise_error(OptionParser::InvalidOption)
221
+ end
222
+ end
223
+
224
+ describe '-s/--silent option' do
225
+ it 'raises error in RuboCop 1.0.0' do
226
+ # This spec can be removed
227
+ # once Options#ignore_dropped_options is removed.
228
+ expect { options.parse(['--silent']) }
229
+ .to raise_error(OptionParser::InvalidOption)
230
+ end
231
+ end
232
+ end
233
+ end