rubocop 0.4.0 → 0.8.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 (190) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/.rubocop.yml +5 -127
  4. data/.travis.yml +7 -1
  5. data/CHANGELOG.md +157 -0
  6. data/CONTRIBUTING.md +13 -6
  7. data/Gemfile +3 -8
  8. data/README.md +160 -9
  9. data/Rakefile +3 -17
  10. data/bin/rubocop +16 -10
  11. data/config/default.yml +46 -0
  12. data/config/disabled.yml +5 -0
  13. data/config/enabled.yml +322 -0
  14. data/lib/rubocop/cli.rb +248 -93
  15. data/lib/rubocop/config.rb +205 -0
  16. data/lib/rubocop/config_store.rb +37 -0
  17. data/lib/rubocop/cop/access_control.rb +41 -0
  18. data/lib/rubocop/cop/alias.rb +17 -0
  19. data/lib/rubocop/cop/align_parameters.rb +20 -95
  20. data/lib/rubocop/cop/and_or.rb +26 -0
  21. data/lib/rubocop/cop/ascii_comments.rb +13 -0
  22. data/lib/rubocop/cop/ascii_identifiers.rb +19 -0
  23. data/lib/rubocop/cop/avoid_class_vars.rb +15 -0
  24. data/lib/rubocop/cop/avoid_for.rb +17 -0
  25. data/lib/rubocop/cop/avoid_global_vars.rb +61 -0
  26. data/lib/rubocop/cop/avoid_perl_backrefs.rb +17 -0
  27. data/lib/rubocop/cop/avoid_perlisms.rb +47 -0
  28. data/lib/rubocop/cop/block_comments.rb +15 -0
  29. data/lib/rubocop/cop/blocks.rb +11 -47
  30. data/lib/rubocop/cop/case_indentation.rb +22 -0
  31. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
  32. data/lib/rubocop/cop/class_methods.rb +15 -0
  33. data/lib/rubocop/cop/collection_methods.rb +16 -16
  34. data/lib/rubocop/cop/colon_method_call.rb +20 -0
  35. data/lib/rubocop/cop/constant_name.rb +24 -0
  36. data/lib/rubocop/cop/cop.rb +34 -47
  37. data/lib/rubocop/cop/def_parentheses.rb +43 -35
  38. data/lib/rubocop/cop/empty_line_between_defs.rb +22 -0
  39. data/lib/rubocop/cop/empty_lines.rb +21 -13
  40. data/lib/rubocop/cop/empty_literal.rb +47 -0
  41. data/lib/rubocop/cop/encoding.rb +3 -3
  42. data/lib/rubocop/cop/end_of_line.rb +3 -3
  43. data/lib/rubocop/cop/ensure_return.rb +19 -0
  44. data/lib/rubocop/cop/eval.rb +19 -0
  45. data/lib/rubocop/cop/favor_join.rb +22 -0
  46. data/lib/rubocop/cop/favor_modifier.rb +38 -48
  47. data/lib/rubocop/cop/favor_percent_r.rb +19 -0
  48. data/lib/rubocop/cop/favor_sprintf.rb +21 -0
  49. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
  50. data/lib/rubocop/cop/handle_exceptions.rb +17 -0
  51. data/lib/rubocop/cop/hash_syntax.rb +29 -14
  52. data/lib/rubocop/cop/if_then_else.rb +32 -29
  53. data/lib/rubocop/cop/leading_comment_space.rb +17 -0
  54. data/lib/rubocop/cop/line_continuation.rb +15 -0
  55. data/lib/rubocop/cop/line_length.rb +4 -4
  56. data/lib/rubocop/cop/loop.rb +33 -0
  57. data/lib/rubocop/cop/method_and_variable_snake_case.rb +41 -17
  58. data/lib/rubocop/cop/method_length.rb +52 -0
  59. data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
  60. data/lib/rubocop/cop/not.rb +21 -0
  61. data/lib/rubocop/cop/numeric_literals.rb +9 -7
  62. data/lib/rubocop/cop/offence.rb +12 -1
  63. data/lib/rubocop/cop/op_method.rb +26 -0
  64. data/lib/rubocop/cop/parameter_lists.rb +12 -6
  65. data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
  66. data/lib/rubocop/cop/percent_r.rb +19 -0
  67. data/lib/rubocop/cop/reduce_arguments.rb +29 -0
  68. data/lib/rubocop/cop/rescue_exception.rb +26 -0
  69. data/lib/rubocop/cop/rescue_modifier.rb +17 -0
  70. data/lib/rubocop/cop/semicolon.rb +31 -0
  71. data/lib/rubocop/cop/single_line_methods.rb +44 -0
  72. data/lib/rubocop/cop/space_after_comma_etc.rb +30 -10
  73. data/lib/rubocop/cop/space_after_control_keyword.rb +29 -0
  74. data/lib/rubocop/cop/string_literals.rb +9 -23
  75. data/lib/rubocop/cop/surrounding_space.rb +223 -83
  76. data/lib/rubocop/cop/symbol_array.rb +31 -0
  77. data/lib/rubocop/cop/symbol_name.rb +23 -0
  78. data/lib/rubocop/cop/syntax.rb +35 -5
  79. data/lib/rubocop/cop/tab.rb +3 -3
  80. data/lib/rubocop/cop/ternary_operator.rb +26 -24
  81. data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
  82. data/lib/rubocop/cop/trivial_accessors.rb +26 -0
  83. data/lib/rubocop/cop/unless_else.rb +11 -7
  84. data/lib/rubocop/cop/util.rb +26 -0
  85. data/lib/rubocop/cop/variable_interpolation.rb +29 -0
  86. data/lib/rubocop/cop/when_then.rb +6 -14
  87. data/lib/rubocop/cop/word_array.rb +37 -0
  88. data/lib/rubocop/report/emacs_style.rb +2 -2
  89. data/lib/rubocop/report/plain_text.rb +1 -1
  90. data/lib/rubocop/version.rb +3 -1
  91. data/lib/rubocop.rb +48 -8
  92. data/rubocop.gemspec +32 -151
  93. data/spec/project_spec.rb +27 -0
  94. data/spec/rubocop/cli_spec.rb +573 -200
  95. data/spec/rubocop/config_spec.rb +409 -0
  96. data/spec/rubocop/config_store_spec.rb +66 -0
  97. data/spec/rubocop/cops/access_control_spec.rb +129 -0
  98. data/spec/rubocop/cops/alias_spec.rb +39 -0
  99. data/spec/rubocop/cops/align_parameters_spec.rb +66 -70
  100. data/spec/rubocop/cops/and_or_spec.rb +37 -0
  101. data/spec/rubocop/cops/ascii_comments_spec.rb +26 -0
  102. data/spec/rubocop/cops/ascii_identifiers_spec.rb +26 -0
  103. data/spec/rubocop/cops/avoid_class_vars_spec.rb +25 -0
  104. data/spec/rubocop/cops/avoid_for_spec.rb +35 -0
  105. data/spec/rubocop/cops/avoid_global_vars_spec.rb +32 -0
  106. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  107. data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
  108. data/spec/rubocop/cops/block_comments_spec.rb +25 -0
  109. data/spec/rubocop/cops/blocks_spec.rb +33 -0
  110. data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +7 -7
  111. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
  112. data/spec/rubocop/cops/class_methods_spec.rb +49 -0
  113. data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
  114. data/spec/rubocop/cops/colon_method_call_spec.rb +53 -0
  115. data/spec/rubocop/cops/constant_name_spec.rb +42 -0
  116. data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
  117. data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
  118. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
  119. data/spec/rubocop/cops/empty_lines_spec.rb +14 -59
  120. data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
  121. data/spec/rubocop/cops/encoding_spec.rb +11 -11
  122. data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
  123. data/spec/rubocop/cops/ensure_return_spec.rb +35 -0
  124. data/spec/rubocop/cops/eval_spec.rb +39 -0
  125. data/spec/rubocop/cops/favor_join_spec.rb +35 -0
  126. data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
  127. data/spec/rubocop/cops/favor_percent_r_spec.rb +29 -0
  128. data/spec/rubocop/cops/favor_sprintf_spec.rb +51 -0
  129. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
  130. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
  131. data/spec/rubocop/cops/handle_exceptions_spec.rb +34 -0
  132. data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
  133. data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
  134. data/spec/rubocop/cops/leading_comment_space_spec.rb +54 -0
  135. data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
  136. data/spec/rubocop/cops/line_length_spec.rb +3 -2
  137. data/spec/rubocop/cops/loop_spec.rb +31 -0
  138. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +55 -9
  139. data/spec/rubocop/cops/method_length_spec.rb +147 -0
  140. data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
  141. data/spec/rubocop/cops/new_lambda_literal_spec.rb +5 -6
  142. data/spec/rubocop/cops/not_spec.rb +31 -0
  143. data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
  144. data/spec/rubocop/cops/offence_spec.rb +13 -0
  145. data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
  146. data/spec/rubocop/cops/op_method_spec.rb +78 -0
  147. data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
  148. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
  149. data/spec/rubocop/cops/percent_r_spec.rb +29 -0
  150. data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
  151. data/spec/rubocop/cops/rescue_exception_spec.rb +125 -0
  152. data/spec/rubocop/cops/rescue_modifier_spec.rb +37 -0
  153. data/spec/rubocop/cops/semicolon_spec.rb +88 -0
  154. data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
  155. data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
  156. data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
  157. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +67 -0
  158. data/spec/rubocop/cops/space_after_semicolon_spec.rb +6 -1
  159. data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
  160. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +12 -2
  161. data/spec/rubocop/cops/space_around_operators_spec.rb +88 -26
  162. data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
  163. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +79 -0
  164. data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
  165. data/spec/rubocop/cops/string_literals_spec.rb +21 -6
  166. data/spec/rubocop/cops/symbol_array_spec.rb +41 -0
  167. data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
  168. data/spec/rubocop/cops/syntax_spec.rb +28 -5
  169. data/spec/rubocop/cops/tab_spec.rb +2 -2
  170. data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
  171. data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
  172. data/spec/rubocop/cops/trivial_accessors_spec.rb +329 -0
  173. data/spec/rubocop/cops/unless_else_spec.rb +8 -8
  174. data/spec/rubocop/cops/variable_interpolation_spec.rb +49 -0
  175. data/spec/rubocop/cops/when_then_spec.rb +14 -14
  176. data/spec/rubocop/cops/word_array_spec.rb +47 -0
  177. data/spec/spec_helper.rb +30 -9
  178. data/spec/support/file_helper.rb +21 -0
  179. data/spec/support/isolated_environment.rb +27 -0
  180. metadata +235 -76
  181. data/.document +0 -5
  182. data/Gemfile.lock +0 -41
  183. data/VERSION +0 -1
  184. data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
  185. data/lib/rubocop/cop/grammar.rb +0 -135
  186. data/lib/rubocop/cop/indentation.rb +0 -44
  187. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
  188. data/spec/rubocop/cops/grammar_spec.rb +0 -71
  189. data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
  190. data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
@@ -1,237 +1,610 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'fileutils'
4
+ require 'tmpdir'
3
5
  require 'spec_helper'
4
6
 
5
7
  module Rubocop
6
- module Report
7
- describe CLI do
8
- let(:cli) { CLI.new }
9
- before(:each) { $stdout = StringIO.new }
10
- after(:each) { $stdout = STDOUT }
11
-
12
- it 'exits cleanly when -h is used' do
13
- expect { cli.run ['-h'] }.to exit_with_code(0)
14
- expect { cli.run ['--help'] }.to exit_with_code(0)
15
- message = ['Usage: rubocop [options] [file1, file2, ...]',
16
- ' -d, --[no-]debug Display debug info',
17
- ' -e, --emacs Emacs style output',
18
- ' -c, --config FILE Configuration file',
19
- ' -s, --silent Silence summary',
20
- ' -v, --version Display version']
21
- expect($stdout.string).to eq((message * 2).join("\n") + "\n")
22
- end
8
+ describe CLI, :isolated_environment do
9
+ include FileHelper
10
+
11
+ let(:cli) { CLI.new }
12
+ before(:each) { $stdout = StringIO.new }
13
+ after(:each) { $stdout = STDOUT }
14
+
15
+ it 'exits cleanly when -h is used' do
16
+ expect { cli.run ['-h'] }.to exit_with_code(0)
17
+ expect { cli.run ['--help'] }.to exit_with_code(0)
18
+ message = ['Usage: rubocop [options] [file1, file2, ...]',
19
+ ' -d, --debug Display debug info',
20
+ ' -e, --emacs Emacs style output',
21
+ ' -c, --config FILE Configuration file',
22
+ ' --only COP Run just one cop',
23
+ ' -s, --silent Silence summary',
24
+ ' -n, --no-color Disable color output',
25
+ ' -v, --version Display version']
26
+ expect($stdout.string).to eq((message * 2).join("\n") + "\n")
27
+ end
28
+
29
+ it 'exits cleanly when -v is used' do
30
+ expect { cli.run ['-v'] }.to exit_with_code(0)
31
+ expect { cli.run ['--version'] }.to exit_with_code(0)
32
+ expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
33
+ end
23
34
 
24
- it 'exits cleanly when -v is used' do
25
- expect { cli.run ['-v'] }.to exit_with_code(0)
26
- expect { cli.run ['--version'] }.to exit_with_code(0)
27
- expect($stdout.string).to eq((Rubocop::VERSION + "\n") * 2)
35
+ describe '#wants_to_quit?' do
36
+ it 'is initially false' do
37
+ expect(cli.wants_to_quit?).to be_false
28
38
  end
39
+ end
29
40
 
30
- it 'checks a given correct file and returns 0' do
31
- File.open('example.rb', 'w') do |f|
32
- f.puts '# encoding: utf-8'
33
- f.puts 'x = 0'
34
- f.puts 'puts x'
35
- end
36
- begin
37
- expect(cli.run(['example.rb'])).to eq(0)
38
- expect($stdout.string.uncolored)
39
- .to eq("\n1 files inspected, 0 offences detected\n")
40
- ensure
41
- File.delete 'example.rb'
41
+ context 'when interrupted with Ctrl-C' do
42
+ before do
43
+ @interrupt_handlers = []
44
+ Signal.stub(:trap).with('INT') do |&block|
45
+ @interrupt_handlers << block
42
46
  end
47
+
48
+ $stderr = StringIO.new
49
+
50
+ create_file('example.rb', '# encoding: utf-8')
43
51
  end
44
52
 
45
- it 'checks a given file with faults and returns 1' do
46
- File.open('example.rb', 'w') do |f|
47
- f.puts '# encoding: utf-8'
48
- f.puts 'x = 0 '
49
- f.puts 'puts x'
50
- end
51
- begin
52
- expect(cli.run(['example.rb'])).to eq(1)
53
- expect($stdout.string.uncolored)
54
- .to eq ['== example.rb ==',
55
- 'C: 2: Trailing whitespace detected.',
56
- '',
57
- '1 files inspected, 1 offences detected',
58
- ''].join("\n")
59
- ensure
60
- File.delete 'example.rb'
61
- end
53
+ after do
54
+ $stderr = STDERR
55
+ @cli_thread.terminate if @cli_thread
62
56
  end
63
57
 
64
- it 'can report in emacs style', ruby: 1.9 do
65
- File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
66
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
67
- begin
68
- expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
69
- expect($stdout.string.uncolored)
70
- .to eq(
71
- ['example1.rb:1: C: Missing encoding comment.',
72
- 'example1.rb:1: C: Trailing whitespace detected.',
73
- "example1.rb:1: C: Surrounding space missing for operator '='.",
74
- 'example1.rb:2: C: Trailing whitespace detected.',
75
- 'example2.rb:1: C: Missing encoding comment.',
76
- 'example2.rb:1: C: Tab detected.',
77
- '',
78
- '2 files inspected, 6 offences detected',
79
- ''].join("\n"))
80
- ensure
81
- File.delete 'example1.rb'
82
- File.delete 'example2.rb'
83
- end
58
+ def interrupt
59
+ @interrupt_handlers.each(&:call)
84
60
  end
85
61
 
86
- it 'can report in emacs style', ruby: 2.0 do
87
- File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
88
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
89
- begin
90
- expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
91
- expect($stdout.string.uncolored)
92
- .to eq(
93
- ['example1.rb:1: C: Trailing whitespace detected.',
94
- "example1.rb:1: C: Surrounding space missing for operator '='.",
95
- 'example1.rb:2: C: Trailing whitespace detected.',
96
- 'example2.rb:1: C: Tab detected.',
97
- '',
98
- '2 files inspected, 4 offences detected',
99
- ''].join("\n"))
100
- ensure
101
- File.delete 'example1.rb'
102
- File.delete 'example2.rb'
62
+ def cli_run_in_thread
63
+ @cli_thread = Thread.new do
64
+ cli.run(['--debug'])
103
65
  end
66
+
67
+ # Wait for start.
68
+ loop { break unless $stdout.string.empty? }
69
+
70
+ @cli_thread
104
71
  end
105
72
 
106
- it 'ommits summary when --silent passed', ruby: 1.9 do
107
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
108
- File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
109
- begin
110
- expect(cli.run(['--emacs',
111
- '--silent',
112
- 'example1.rb',
113
- 'example2.rb'])).to eq(1)
114
- expect($stdout.string).to eq(
115
- ['example1.rb:1: C: Missing encoding comment.',
116
- 'example1.rb:1: C: Trailing whitespace detected.',
117
- 'example2.rb:1: C: Missing encoding comment.',
118
- 'example2.rb:1: C: Tab detected.',
119
- ''].join("\n"))
120
- ensure
121
- File.delete 'example1.rb'
122
- File.delete 'example2.rb'
123
- end
73
+ it 'exits with status 1' do
74
+ cli_thread = cli_run_in_thread
75
+ interrupt
76
+ expect(cli_thread.value).to eq(1)
124
77
  end
125
78
 
126
- it 'ommits summary when --silent passed', ruby: 2.0 do
127
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
128
- File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
129
- begin
130
- expect(cli.run(['--emacs',
131
- '--silent',
132
- 'example1.rb',
133
- 'example2.rb'])).to eq(1)
134
- expect($stdout.string).to eq(
135
- ['example1.rb:1: C: Trailing whitespace detected.',
136
- 'example2.rb:1: C: Tab detected.',
137
- ''].join("\n"))
138
- ensure
139
- File.delete 'example1.rb'
140
- File.delete 'example2.rb'
141
- end
79
+ it 'exits gracefully without dumping backtraces' do
80
+ cli_thread = cli_run_in_thread
81
+ interrupt
82
+ cli_thread.join
83
+ expect($stderr.string).not_to match(/from .+:\d+:in /)
142
84
  end
143
85
 
144
- it 'can be configured with option to disable a certain error' do
145
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
146
- File.open('rubocop.yml', 'w') do |f|
147
- f.puts('Encoding:',
148
- ' Enabled: false',
149
- '',
150
- 'Indentation:',
151
- ' Enabled: false')
152
- end
153
- begin
154
- expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
155
- expect($stdout.string.uncolored).to eq(
156
- ['== example1.rb ==',
157
- 'C: 1: Trailing whitespace detected.',
158
- '',
159
- '1 files inspected, 1 offences detected',
160
- ''].join("\n"))
161
- ensure
162
- File.delete 'example1.rb'
163
- File.delete 'rubocop.yml'
86
+ context 'with Ctrl-C once' do
87
+ it 'reports summary' do
88
+ cli_thread = cli_run_in_thread
89
+ interrupt
90
+ cli_thread.join
91
+ expect($stdout.string).to match(/files? inspected/)
164
92
  end
165
93
  end
166
94
 
167
- it 'can be configured with project config to disable a certain error' do
168
- FileUtils.mkdir 'example_src'
169
- File.open('example_src/example1.rb', 'w') { |f| f.puts 'puts 0 ' }
170
- File.open('example_src/.rubocop.yml', 'w') do |f|
171
- f.puts('Encoding:',
172
- ' Enabled: false',
173
- '',
174
- 'Indentation:',
175
- ' Enabled: false')
176
- end
177
- begin
178
- expect(cli.run(['example_src/example1.rb'])).to eq(1)
179
- expect($stdout.string.uncolored).to eq(
180
- ['== example_src/example1.rb ==',
181
- 'C: 1: Trailing whitespace detected.',
182
- '',
183
- '1 files inspected, 1 offences detected',
184
- ''].join("\n"))
185
- ensure
186
- FileUtils.rm_rf 'example_src'
95
+ context 'with Ctrl-C twice' do
96
+ it 'exits immediately' do
97
+ Object.any_instance.should_receive(:exit!).with(1)
98
+ cli_thread = cli_run_in_thread
99
+ interrupt
100
+ interrupt
101
+ cli_thread.join
187
102
  end
188
103
  end
104
+ end
189
105
 
190
- it 'can use an alternative max line length from a config file' do
191
- FileUtils.mkdir 'example_src'
192
- File.open('example_src/example1.rb', 'w') do |f|
193
- f.puts '# encoding: utf-8'
194
- f.puts '#' * 90
195
- end
196
- File.open('example_src/.rubocop.yml', 'w') do |f|
197
- f.puts('LineLength:',
198
- ' Enabled: true',
199
- ' Max: 100')
200
- end
201
- begin
202
- expect(cli.run(['example_src/example1.rb'])).to eq(0)
203
- expect($stdout.string.uncolored).to eq(
204
- ['', '1 files inspected, 0 offences detected',
205
- ''].join("\n"))
206
- ensure
207
- FileUtils.rm_rf 'example_src'
208
- end
106
+ it 'checks a given correct file and returns 0' do
107
+ create_file('example.rb', [
108
+ '# encoding: utf-8',
109
+ 'x = 0',
110
+ 'puts x'
111
+ ])
112
+ expect(cli.run(['example.rb'])).to eq(0)
113
+ expect($stdout.string)
114
+ .to eq("\n1 file inspected, no offences detected\n")
115
+ end
116
+
117
+ it 'checks a given file with faults and returns 1' do
118
+ create_file('example.rb', [
119
+ '# encoding: utf-8',
120
+ 'x = 0 ',
121
+ 'puts x'
122
+ ])
123
+ expect(cli.run(['example.rb'])).to eq(1)
124
+ expect($stdout.string)
125
+ .to eq ['== example.rb ==',
126
+ 'C: 2: Trailing whitespace detected.',
127
+ '',
128
+ '1 file inspected, 1 offence detected',
129
+ ''].join("\n")
130
+ end
131
+
132
+ it 'can report in emacs style', ruby: 1.9 do
133
+ create_file('example1.rb', [
134
+ 'x= 0 ',
135
+ 'y ',
136
+ 'puts x'
137
+ ])
138
+ create_file('example2.rb', [
139
+ "\tx = 0",
140
+ 'puts x'
141
+ ])
142
+ expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
143
+ expect($stdout.string)
144
+ .to eq(
145
+ ['example1.rb:1: C: Missing utf-8 encoding comment.',
146
+ 'example1.rb:1: C: Trailing whitespace detected.',
147
+ "example1.rb:1: C: Surrounding space missing for operator '='.",
148
+ 'example1.rb:2: C: Trailing whitespace detected.',
149
+ 'example2.rb:1: C: Missing utf-8 encoding comment.',
150
+ 'example2.rb:1: C: Tab detected.',
151
+ '',
152
+ '2 files inspected, 6 offences detected',
153
+ ''].join("\n"))
154
+ end
155
+
156
+ it 'can report in emacs style', ruby: 2.0 do
157
+ create_file('example1.rb', [
158
+ 'x= 0 ',
159
+ 'y ',
160
+ 'puts x'
161
+ ])
162
+ create_file('example2.rb', [
163
+ "\tx = 0",
164
+ 'puts x'
165
+ ])
166
+ expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
167
+ expect($stdout.string)
168
+ .to eq(
169
+ ['example1.rb:1: C: Trailing whitespace detected.',
170
+ "example1.rb:1: C: Surrounding space missing for operator '='.",
171
+ 'example1.rb:2: C: Trailing whitespace detected.',
172
+ 'example2.rb:1: C: Tab detected.',
173
+ '',
174
+ '2 files inspected, 4 offences detected',
175
+ ''].join("\n"))
176
+ end
177
+
178
+ it 'runs just one cop if --only is passed' do
179
+ create_file('example.rb', ['if x== 0 ',
180
+ "\ty",
181
+ 'end'])
182
+ # IfUnlessModifier depends on the configuration of LineLength.
183
+ # That configuration might have been set by other spec examples
184
+ # so we reset it to emulate a start from scratch.
185
+ Cop::LineLength.config = nil
186
+
187
+ expect(cli.run(['--only', 'IfUnlessModifier', 'example.rb'])).to eq(1)
188
+ expect($stdout.string)
189
+ .to eq(['== example.rb ==',
190
+ 'C: 1: Favor modifier if/unless usage when you have a ' +
191
+ 'single-line body. Another good alternative is the usage of ' +
192
+ 'control flow &&/||.',
193
+ '',
194
+ '1 file inspected, 1 offence detected',
195
+ ''].join("\n"))
196
+ end
197
+
198
+ it 'exits with error if an incorrect cop name is passed to --only' do
199
+ expect(cli.run(%w(--only 123))).to eq(1)
200
+ expect($stdout.string).to eq("Unrecognized cop name: 123.\n")
201
+ end
202
+
203
+ it 'ommits summary when --silent passed', ruby: 1.9 do
204
+ create_file('example1.rb', 'puts 0 ')
205
+ create_file('example2.rb', "\tputs 0")
206
+ expect(cli.run(['--emacs',
207
+ '--silent',
208
+ 'example1.rb',
209
+ 'example2.rb'])).to eq(1)
210
+ expect($stdout.string).to eq(
211
+ ['example1.rb:1: C: Missing utf-8 encoding comment.',
212
+ 'example1.rb:1: C: Trailing whitespace detected.',
213
+ 'example2.rb:1: C: Missing utf-8 encoding comment.',
214
+ 'example2.rb:1: C: Tab detected.',
215
+ ''].join("\n"))
216
+ end
217
+
218
+ it 'ommits summary when --silent passed', ruby: 2.0 do
219
+ create_file('example1.rb', 'puts 0 ')
220
+ create_file('example2.rb', "\tputs 0")
221
+ expect(cli.run(['--emacs',
222
+ '--silent',
223
+ 'example1.rb',
224
+ 'example2.rb'])).to eq(1)
225
+ expect($stdout.string).to eq(
226
+ ['example1.rb:1: C: Trailing whitespace detected.',
227
+ 'example2.rb:1: C: Tab detected.',
228
+ ''].join("\n"))
229
+ end
230
+
231
+ it 'shows cop names when --debug is passed', ruby: 2.0 do
232
+ create_file('example1.rb', "\tputs 0")
233
+ expect(cli.run(['--emacs',
234
+ '--silent',
235
+ '--debug',
236
+ 'example1.rb'])).to eq(1)
237
+ expect($stdout.string.lines[-1]).to eq(
238
+ ['example1.rb:1: C: Tab: Tab detected.',
239
+ ''].join("\n"))
240
+ end
241
+
242
+ it 'can be configured with option to disable a certain error' do
243
+ create_file('example1.rb', 'puts 0 ')
244
+ create_file('rubocop.yml', [
245
+ 'Encoding:',
246
+ ' Enabled: false',
247
+ '',
248
+ 'CaseIndentation:',
249
+ ' Enabled: false'
250
+ ])
251
+ expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
252
+ expect($stdout.string).to eq(
253
+ ['== example1.rb ==',
254
+ 'C: 1: Trailing whitespace detected.',
255
+ '',
256
+ '1 file inspected, 1 offence detected',
257
+ ''].join("\n"))
258
+ end
259
+
260
+ it 'can be configured with project config to disable a certain error' do
261
+ create_file('example_src/example1.rb', 'puts 0 ')
262
+ create_file('example_src/.rubocop.yml', [
263
+ 'Encoding:',
264
+ ' Enabled: false',
265
+ '',
266
+ 'CaseIndentation:',
267
+ ' Enabled: false'
268
+ ])
269
+ expect(cli.run(['example_src/example1.rb'])).to eq(1)
270
+ expect($stdout.string).to eq(
271
+ ['== example_src/example1.rb ==',
272
+ 'C: 1: Trailing whitespace detected.',
273
+ '',
274
+ '1 file inspected, 1 offence detected',
275
+ ''].join("\n"))
276
+ end
277
+
278
+ it 'can use an alternative max line length from a config file' do
279
+ create_file('example_src/example1.rb', [
280
+ '# encoding: utf-8',
281
+ '#' * 90
282
+ ])
283
+ create_file('example_src/.rubocop.yml', [
284
+ 'LineLength:',
285
+ ' Enabled: true',
286
+ ' Max: 100'
287
+ ])
288
+ expect(cli.run(['example_src/example1.rb'])).to eq(0)
289
+ expect($stdout.string).to eq(
290
+ ['', '1 file inspected, no offences detected',
291
+ ''].join("\n"))
292
+ end
293
+
294
+ it 'can have different config files in different directories' do
295
+ %w(src lib).each do |dir|
296
+ create_file("example/#{dir}/example1.rb", [
297
+ '# encoding: utf-8',
298
+ '#' * 90
299
+ ])
209
300
  end
301
+ create_file('example/src/.rubocop.yml', [
302
+ 'LineLength:',
303
+ ' Enabled: true',
304
+ ' Max: 100'
305
+ ])
306
+ expect(cli.run(['example'])).to eq(1)
307
+ expect($stdout.string).to eq(
308
+ ['== example/lib/example1.rb ==',
309
+ 'C: 2: Line is too long. [90/79]',
310
+ '',
311
+ '2 files inspected, 1 offence detected',
312
+ ''].join("\n"))
313
+ end
314
+
315
+ it 'prefers a config file in ancestor directory to another in home' do
316
+ create_file('example_src/example1.rb', [
317
+ '# encoding: utf-8',
318
+ '#' * 90
319
+ ])
320
+ create_file('example_src/.rubocop.yml', [
321
+ 'LineLength:',
322
+ ' Enabled: true',
323
+ ' Max: 100'
324
+ ])
325
+ create_file("#{Dir.home}/.rubocop.yml", [
326
+ 'LineLength:',
327
+ ' Enabled: true',
328
+ ' Max: 80'
329
+ ])
330
+ expect(cli.run(['example_src/example1.rb'])).to eq(0)
331
+ expect($stdout.string).to eq(
332
+ ['', '1 file inspected, no offences detected',
333
+ ''].join("\n"))
334
+ end
210
335
 
211
- it 'finds no violations when checking the rubocop source code' do
212
- cli.run
213
- expect($stdout.string.uncolored).to match(
214
- /files inspected, 0 offences detected\n/
215
- )
336
+ it 'can exclude directories relative to .rubocop.yml' do
337
+ %w(src etc/test etc/spec tmp/test tmp/spec).each do |dir|
338
+ create_file("example/#{dir}/example1.rb", [
339
+ '# encoding: utf-8',
340
+ '#' * 90
341
+ ])
216
342
  end
217
343
 
218
- it 'can process a file with an invalide UTF-8 byte sequence' do
219
- File.open('example.rb', 'w') do |f|
220
- f.puts '# encoding: utf-8'
221
- f.puts "# \xf9\x29"
222
- end
223
- begin
224
- expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
225
- ensure
226
- File.delete 'example.rb'
227
- end
344
+ create_file('example/.rubocop.yml', [
345
+ 'AllCops:',
346
+ ' Excludes:',
347
+ ' - src/**',
348
+ ' - etc/**',
349
+ ' - tmp/spec/**'
350
+ ])
351
+
352
+ expect(cli.run(['example'])).to eq(1)
353
+ expect($stdout.string).to eq(
354
+ ['== example/tmp/test/example1.rb ==',
355
+ 'C: 2: Line is too long. [90/79]',
356
+ '',
357
+ '1 file inspected, 1 offence detected',
358
+ ''].join("\n"))
359
+ end
360
+
361
+ it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
362
+ create_file('example/example1.rb', [
363
+ '# encoding: utf-8',
364
+ '#' * 90
365
+ ])
366
+
367
+ create_file('example/.rubocop.yml', [
368
+ 'LyneLenth:',
369
+ ' Enabled: true',
370
+ ' Max: 100'
371
+ ])
372
+
373
+ expect(cli.run(['example'])).to eq(1)
374
+ expect($stdout.string).to eq(
375
+ ['Warning: unrecognized cop LyneLenth found in ' +
376
+ File.expand_path('example/.rubocop.yml'),
377
+ '== example/example1.rb ==',
378
+ 'C: 2: Line is too long. [90/79]',
379
+ '',
380
+ '1 file inspected, 1 offence detected',
381
+ ''].join("\n"))
382
+ end
383
+
384
+ it 'prints a warning for an unrecognized configuration parameter' do
385
+ create_file('example/example1.rb', [
386
+ '# encoding: utf-8',
387
+ '#' * 90
388
+ ])
389
+
390
+ create_file('example/.rubocop.yml', [
391
+ 'LineLength:',
392
+ ' Enabled: true',
393
+ ' Min: 10'
394
+ ])
395
+
396
+ expect(cli.run(['example'])).to eq(1)
397
+ expect($stdout.string).to eq(
398
+ ['Warning: unrecognized parameter LineLength:Min found in ' +
399
+ File.expand_path('example/.rubocop.yml'),
400
+ '== example/example1.rb ==',
401
+ 'C: 2: Line is too long. [90/79]',
402
+ '',
403
+ '1 file inspected, 1 offence detected',
404
+ ''].join("\n"))
405
+ end
406
+
407
+ it 'registers an offence for a syntax error' do
408
+ pending
409
+ create_file('example.rb', [
410
+ '# encoding: utf-8',
411
+ 'class Test',
412
+ 'en'
413
+ ])
414
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
415
+ unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
416
+ expect($stdout.string).to eq(
417
+ ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
418
+ 'expecting keyword_end',
419
+ '',
420
+ '1 file inspected, 1 offence detected',
421
+ ''].join("\n"))
422
+ end
423
+
424
+ it 'can process a file with an invalid UTF-8 byte sequence' do
425
+ pending
426
+ create_file('example.rb', [
427
+ '# encoding: utf-8',
428
+ "# #{'f9'.hex.chr}#{'29'.hex.chr}"
429
+ ])
430
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
431
+ end
432
+
433
+ it 'can have all cops disabled in a code section' do
434
+ create_file('example.rb', [
435
+ '# encoding: utf-8',
436
+ '# rubocop:disable all',
437
+ '#' * 90,
438
+ 'x(123456)',
439
+ 'y("123")',
440
+ 'def func',
441
+ ' # rubocop: enable LineLength, StringLiterals',
442
+ ' ' + '#' * 93,
443
+ ' x(123456)',
444
+ ' y("123")',
445
+ 'end'
446
+ ])
447
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
448
+ # all cops were disabled, then 2 were enabled again, so we
449
+ # should get 2 offences reported.
450
+ expect($stdout.string).to eq(
451
+ ['example.rb:8: C: Line is too long. [95/79]',
452
+ "example.rb:10: C: Prefer single-quoted strings when you don't " +
453
+ 'need string interpolation or special symbols.',
454
+ '',
455
+ '1 file inspected, 2 offences detected',
456
+ ''].join("\n"))
457
+ end
458
+
459
+ it 'can have selected cops disabled in a code section' do
460
+ create_file('example.rb', [
461
+ '# encoding: utf-8',
462
+ '# rubocop:disable LineLength,NumericLiterals,StringLiterals',
463
+ '#' * 90,
464
+ 'x(123456)',
465
+ 'y("123")',
466
+ 'def func',
467
+ ' # rubocop: enable LineLength, StringLiterals',
468
+ ' ' + '#' * 93,
469
+ ' x(123456)',
470
+ ' y("123")',
471
+ 'end'
472
+ ])
473
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
474
+ # 3 cops were disabled, then 2 were enabled again, so we
475
+ # should get 2 offences reported.
476
+ expect($stdout.string).to eq(
477
+ ['example.rb:8: C: Line is too long. [95/79]',
478
+ "example.rb:10: C: Prefer single-quoted strings when you don't " +
479
+ 'need string interpolation or special symbols.',
480
+ '',
481
+ '1 file inspected, 2 offences detected',
482
+ ''].join("\n"))
483
+ end
484
+
485
+ it 'can have all cops disabled on a single line' do
486
+ create_file('example.rb', [
487
+ '# encoding: utf-8',
488
+ 'y("123", 123456) # rubocop:disable all'
489
+ ])
490
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
491
+ expect($stdout.string).to eq(
492
+ ['',
493
+ '1 file inspected, no offences detected',
494
+ ''].join("\n"))
495
+ end
496
+
497
+ it 'can have selected cops disabled on a single line' do
498
+ create_file('example.rb', [
499
+ '# encoding: utf-8',
500
+ '#' * 90 + ' # rubocop:disable LineLength',
501
+ '#' * 95,
502
+ 'y("123") # rubocop:disable LineLength,StringLiterals'
503
+ ])
504
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
505
+ expect($stdout.string).to eq(
506
+ ['example.rb:3: C: Line is too long. [95/79]',
507
+ '',
508
+ '1 file inspected, 1 offence detected',
509
+ ''].join("\n"))
510
+ end
511
+
512
+ it 'finds a file with no .rb extension but has a shebang line' do
513
+ create_file('example', [
514
+ '#!/usr/bin/env ruby',
515
+ '# encoding: utf-8',
516
+ 'x = 0',
517
+ 'puts x'
518
+ ])
519
+ # Need to pass an empty array explicitly
520
+ # so that the CLI does not refer arguments of `rspec`
521
+ expect(cli.run([])).to eq(0)
522
+ expect($stdout.string).to eq(
523
+ ['', '1 file inspected, no offences detected',
524
+ ''].join("\n"))
525
+ end
526
+
527
+ it 'finds included files' do
528
+ create_file('example', [
529
+ '# encoding: utf-8',
530
+ 'x = 0',
531
+ 'puts x'
532
+ ])
533
+ create_file('regexp', [
534
+ '# encoding: utf-8',
535
+ 'x = 0',
536
+ 'puts x'
537
+ ])
538
+ create_file('.rubocop.yml', [
539
+ 'AllCops:',
540
+ ' Includes:',
541
+ ' - example',
542
+ ' - !ruby/regexp /regexp$/'
543
+ ])
544
+ # Need to pass an empty array explicitly
545
+ # so that the CLI does not refer arguments of `rspec`
546
+ expect(cli.run([])).to eq(0)
547
+ expect($stdout.string).to eq(
548
+ ['', '2 files inspected, no offences detected',
549
+ ''].join("\n"))
550
+ end
551
+
552
+ it 'ignores excluded files' do
553
+ create_file('example.rb', [
554
+ '# encoding: utf-8',
555
+ 'x = 0',
556
+ 'puts x'
557
+ ])
558
+ create_file('regexp.rb', [
559
+ '# encoding: utf-8',
560
+ 'x = 0',
561
+ 'puts x'
562
+ ])
563
+ create_file('exclude_glob.rb', [
564
+ '#!/usr/bin/env ruby',
565
+ '# encoding: utf-8',
566
+ 'x = 0',
567
+ 'puts x'
568
+ ])
569
+ create_file('.rubocop.yml', [
570
+ 'AllCops:',
571
+ ' Excludes:',
572
+ ' - example.rb',
573
+ ' - !ruby/regexp /regexp.rb$/',
574
+ ' - "exclude_*"'
575
+ ])
576
+ # Need to pass an empty array explicitly
577
+ # so that the CLI does not refer arguments of `rspec`
578
+ expect(cli.run([])).to eq(0)
579
+ expect($stdout.string).to eq(
580
+ ['', '0 files inspected, no offences detected',
581
+ ''].join("\n"))
582
+ end
583
+
584
+ describe '#display_summary' do
585
+ it 'handles pluralization correctly' do
586
+ cli.display_summary(0, 0, [])
587
+ expect($stdout.string).to eq(
588
+ "\n0 files inspected, no offences detected\n")
589
+ $stdout = StringIO.new
590
+ cli.display_summary(1, 0, [])
591
+ expect($stdout.string).to eq(
592
+ "\n1 file inspected, no offences detected\n")
593
+ $stdout = StringIO.new
594
+ cli.display_summary(1, 1, [])
595
+ expect($stdout.string).to eq(
596
+ "\n1 file inspected, 1 offence detected\n")
597
+ $stdout = StringIO.new
598
+ cli.display_summary(2, 2, [])
599
+ expect($stdout.string).to eq(
600
+ "\n2 files inspected, 2 offences detected\n")
228
601
  end
229
602
 
230
- it 'has configuration for all cops in .rubocop.yml' do
231
- cop_names = Cop::Cop.all.map do |cop_class|
232
- cop_class.name.split('::').last
233
- end
234
- expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
603
+ it 'displays an error message when errors are present' do
604
+ msg = 'An error occurred while Encoding cop was inspecting file.rb.'
605
+ cli.display_summary(1, 1, [msg])
606
+ expect($stdout.string.lines.to_a[-4..-3])
607
+ .to eq(["1 error occurred:\n", "#{msg}\n"])
235
608
  end
236
609
  end
237
610
  end