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