rubocop 0.6.1 → 0.7.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -266
  3. data/CHANGELOG.md +49 -7
  4. data/README.md +75 -2
  5. data/Rakefile +2 -2
  6. data/bin/rubocop +15 -10
  7. data/lib/rubocop.rb +19 -1
  8. data/lib/rubocop/cli.rb +113 -116
  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/avoid_for.rb +2 -4
  15. data/lib/rubocop/cop/avoid_global_vars.rb +49 -0
  16. data/lib/rubocop/cop/block_comments.rb +17 -0
  17. data/lib/rubocop/cop/brace_after_percent.rb +9 -5
  18. data/lib/rubocop/cop/{indentation.rb → case_indentation.rb} +1 -1
  19. data/lib/rubocop/cop/class_methods.rb +20 -0
  20. data/lib/rubocop/cop/colon_method_call.rb +44 -0
  21. data/lib/rubocop/cop/cop.rb +30 -2
  22. data/lib/rubocop/cop/def_parentheses.rb +1 -1
  23. data/lib/rubocop/cop/empty_line_between_defs.rb +26 -0
  24. data/lib/rubocop/cop/empty_lines.rb +10 -13
  25. data/lib/rubocop/cop/eval.rb +22 -0
  26. data/lib/rubocop/cop/favor_join.rb +37 -0
  27. data/lib/rubocop/cop/grammar.rb +2 -2
  28. data/lib/rubocop/cop/hash_literal.rb +43 -4
  29. data/lib/rubocop/cop/hash_syntax.rb +2 -2
  30. data/lib/rubocop/cop/if_then_else.rb +1 -1
  31. data/lib/rubocop/cop/leading_comment_space.rb +20 -0
  32. data/lib/rubocop/cop/line_continuation.rb +18 -0
  33. data/lib/rubocop/cop/line_length.rb +1 -1
  34. data/lib/rubocop/cop/method_and_variable_snake_case.rb +7 -6
  35. data/lib/rubocop/cop/method_length.rb +4 -15
  36. data/lib/rubocop/cop/not.rb +15 -0
  37. data/lib/rubocop/cop/offence.rb +9 -0
  38. data/lib/rubocop/cop/semicolon.rb +74 -3
  39. data/lib/rubocop/cop/single_line_methods.rb +60 -0
  40. data/lib/rubocop/cop/space_after_control_keyword.rb +28 -0
  41. data/lib/rubocop/cop/surrounding_space.rb +48 -9
  42. data/lib/rubocop/cop/symbol_array.rb +29 -0
  43. data/lib/rubocop/cop/trivial_accessors.rb +103 -0
  44. data/lib/rubocop/cop/unless_else.rb +1 -1
  45. data/lib/rubocop/cop/variable_interpolation.rb +3 -2
  46. data/lib/rubocop/cop/word_array.rb +38 -0
  47. data/lib/rubocop/version.rb +1 -1
  48. data/rubocop.gemspec +11 -7
  49. data/spec/project_spec.rb +27 -0
  50. data/spec/rubocop/cli_spec.rb +549 -487
  51. data/spec/rubocop/config_spec.rb +399 -0
  52. data/spec/rubocop/config_store_spec.rb +66 -0
  53. data/spec/rubocop/cops/alias_spec.rb +7 -0
  54. data/spec/rubocop/cops/array_literal_spec.rb +8 -1
  55. data/spec/rubocop/cops/avoid_for_spec.rb +15 -1
  56. data/spec/rubocop/cops/avoid_global_vars.rb +32 -0
  57. data/spec/rubocop/cops/block_comments_spec.rb +29 -0
  58. data/spec/rubocop/cops/brace_after_percent_spec.rb +19 -13
  59. data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +2 -2
  60. data/spec/rubocop/cops/class_methods_spec.rb +49 -0
  61. data/spec/rubocop/cops/colon_method_call_spec.rb +47 -0
  62. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
  63. data/spec/rubocop/cops/empty_lines_spec.rb +6 -63
  64. data/spec/rubocop/cops/eval_spec.rb +36 -0
  65. data/spec/rubocop/cops/favor_join_spec.rb +39 -0
  66. data/spec/rubocop/cops/hash_literal_spec.rb +8 -1
  67. data/spec/rubocop/cops/leading_comment_space_spec.rb +60 -0
  68. data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
  69. data/spec/rubocop/cops/line_length_spec.rb +1 -0
  70. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +20 -0
  71. data/spec/rubocop/cops/method_length_spec.rb +2 -5
  72. data/spec/rubocop/cops/new_lambda_literal_spec.rb +2 -3
  73. data/spec/rubocop/cops/not_spec.rb +34 -0
  74. data/spec/rubocop/cops/offence_spec.rb +7 -0
  75. data/spec/rubocop/cops/semicolon_spec.rb +79 -4
  76. data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
  77. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +28 -0
  78. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +11 -1
  79. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +74 -0
  80. data/spec/rubocop/cops/symbol_array_spec.rb +25 -0
  81. data/spec/rubocop/cops/trivial_accessors_spec.rb +332 -0
  82. data/spec/rubocop/cops/variable_interpolation_spec.rb +10 -1
  83. data/spec/rubocop/cops/word_array_spec.rb +39 -0
  84. data/spec/spec_helper.rb +16 -9
  85. data/spec/support/file_helper.rb +21 -0
  86. data/spec/support/isolated_environment.rb +27 -0
  87. metadata +66 -6
@@ -8,7 +8,7 @@ module Rubocop
8
8
 
9
9
  def inspect(file, source, tokens, sexp)
10
10
  each(:unless, sexp) do |unless_sexp|
11
- if unless_sexp.compact.find { |s| s[0] == :else }
11
+ if unless_sexp.compact.any? { |s| s[0] == :else }
12
12
  add_offence(:convention, all_positions(unless_sexp).first.lineno,
13
13
  ERROR_MESSAGE)
14
14
  end
@@ -5,8 +5,9 @@ module Rubocop
5
5
  class VariableInterpolation < Cop
6
6
  def inspect(file, source, tokens, sexp)
7
7
  each(:string_dvar, sexp) do |s|
8
- var = s[1][1][1]
9
- lineno = s[1][1][2].lineno
8
+ interpolation = s[1][0] == :@backref ? s[1] : s[1][1]
9
+ var = interpolation[1]
10
+ lineno = interpolation[2].lineno
10
11
 
11
12
  add_offence(
12
13
  :convention,
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class WordArray < Cop
6
+ ERROR_MESSAGE = 'Use %w or %W for array of words.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:array, sexp) do |s|
10
+ array_elems = s[1]
11
+
12
+ # no need to check empty arrays
13
+ return unless array_elems && array_elems.size > 1
14
+
15
+ string_array = array_elems.all? { |e| e[0] == :string_literal }
16
+
17
+ if string_array && !complex_content?(array_elems)
18
+ add_offence(:convention,
19
+ all_positions(s).first.lineno,
20
+ ERROR_MESSAGE)
21
+ end
22
+ end
23
+ end
24
+
25
+ def complex_content?(arr_sexp)
26
+ non_empty_strings = 0
27
+
28
+ each(:@tstring_content, arr_sexp) do |content|
29
+ non_empty_strings += 1
30
+ return true unless content[1] =~ /\A[\w-]+\z/
31
+ end
32
+
33
+ # check for '' strings in the array
34
+ non_empty_strings == arr_sexp.size ? false : true
35
+ end
36
+ end
37
+ end
38
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Version
5
- STRING = '0.6.1'
5
+ STRING = '0.7.0'
6
6
  end
7
7
  end
data/rubocop.gemspec CHANGED
@@ -9,7 +9,11 @@ Gem::Specification.new do |s|
9
9
  s.required_ruby_version = '>= 1.9.2'
10
10
  s.authors = ['Bozhidar Batsov']
11
11
  s.date = '2013-04-17'
12
- s.description = 'Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide.'
12
+ s.description = <<-EOF
13
+ Automatic Ruby code style checking tool.
14
+ Aims to enforce the community-driven Ruby Style Guide.
15
+ EOF
16
+
13
17
  s.email = 'bozhidar@batsov.com'
14
18
  s.executables = ['rubocop']
15
19
  s.extra_rdoc_files = ['LICENSE.txt', 'README.md']
@@ -33,10 +37,10 @@ Gem::Specification.new do |s|
33
37
  s.rubygems_version = '1.8.23'
34
38
  s.summary = 'Automatic Ruby code style checking tool.'
35
39
 
36
- s.add_runtime_dependency(%q<rainbow>, '>= 1.1.4')
37
- s.add_development_dependency(%q<rake>, '~> 10.0')
38
- s.add_development_dependency(%q<rspec>, '~> 2.13')
39
- s.add_development_dependency(%q<yard>, '~> 0.8')
40
- s.add_development_dependency(%q<bundler>, '~> 1.3')
41
- s.add_development_dependency(%q<simplecov>, '~> 0.7')
40
+ s.add_runtime_dependency('rainbow', '>= 1.1.4')
41
+ s.add_development_dependency('rake', '~> 10.0')
42
+ s.add_development_dependency('rspec', '~> 2.13')
43
+ s.add_development_dependency('yard', '~> 0.8')
44
+ s.add_development_dependency('bundler', '~> 1.3')
45
+ s.add_development_dependency('simplecov', '~> 0.7')
42
46
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'RuboCop Project' do
6
+ describe '.rubocop.yml' do
7
+ it 'has configuration for all cops' do
8
+ cop_names = Rubocop::Cop::Cop.all.map(&:cop_name)
9
+ expect(Rubocop::Config.load_file('.rubocop.yml').keys.sort)
10
+ .to eq((['AllCops'] + cop_names).sort)
11
+ end
12
+ end
13
+
14
+ describe 'source codes' do
15
+ before { $stdout = StringIO.new }
16
+ after { $stdout = STDOUT }
17
+
18
+ it 'has no violations' do
19
+ # Need to pass an empty array explicitly
20
+ # so that the CLI does not refer arguments of `rspec`
21
+ Rubocop::CLI.new.run([])
22
+ expect($stdout.string).to match(
23
+ /files inspected, no offences detected\n/
24
+ )
25
+ end
26
+ end
27
+ end
@@ -5,537 +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, --debug Display debug info',
19
- ' -e, --emacs Emacs style output',
20
- ' -c, --config FILE Configuration file',
21
- ' -s, --silent Silence summary',
22
- ' -n, --no-color Disable color output',
23
- ' -v, --version Display version']
24
- expect($stdout.string).to eq((message * 2).join("\n") + "\n")
25
- 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
26
28
 
27
- it 'exits cleanly when -v is used' do
28
- expect { cli.run ['-v'] }.to exit_with_code(0)
29
- expect { cli.run ['--version'] }.to exit_with_code(0)
30
- expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
31
- 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
32
34
 
33
- describe '#wants_to_quit?' do
34
- it 'is initially false' do
35
- expect(cli.wants_to_quit?).to be_false
36
- end
35
+ describe '#wants_to_quit?' do
36
+ it 'is initially false' do
37
+ expect(cli.wants_to_quit?).to be_false
37
38
  end
39
+ end
38
40
 
39
- context 'when interrupted with Ctrl-C' do
40
- def execute_rubocop
41
- project_root = File.expand_path('../..', File.dirname(__FILE__))
42
- rubocop_command = File.join(project_root, 'bin', 'rubocop')
43
-
44
- _, stdout, stderr, thread = Open3.popen3(rubocop_command, '--debug')
45
-
46
- unless IO.select([stdout], nil, nil, 10)
47
- fail 'rubocop took too long to start running'
48
- end
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
46
+ end
49
47
 
50
- yield stdout, stderr, thread.pid
48
+ $stderr = StringIO.new
51
49
 
52
- thread.value.exitstatus
53
- ensure
54
- thread.terminate
55
- end
50
+ create_file('example.rb', '# encoding: utf-8')
51
+ end
56
52
 
57
- def wait_for_output(output)
58
- IO.select([output], nil, nil, 10)
59
- end
53
+ after do
54
+ $stderr = STDERR
55
+ @cli_thread.terminate if @cli_thread
56
+ end
60
57
 
61
- it 'exits with status 1' do
62
- exit_status = execute_rubocop do |stdout, stderr, pid|
63
- Process.kill('INT', pid)
64
- end
65
- expect(exit_status).to eq(1)
66
- end
58
+ def interrupt
59
+ @interrupt_handlers.each(&:call)
60
+ end
67
61
 
68
- it 'exits gracefully without dumping backtraces' do
69
- execute_rubocop do |stdout, stderr, pid|
70
- Process.kill('INT', pid)
71
- wait_for_output(stderr)
72
- expect(stderr.read).not_to match(/from .+:\d+:in /)
73
- end
62
+ def cli_run_in_thread
63
+ @cli_thread = Thread.new do
64
+ cli.run(['--debug'])
74
65
  end
75
66
 
76
- context 'with Ctrl-C once' do
77
- it 'reports summary' do
78
- execute_rubocop do |stdout, stderr, pid|
79
- Process.kill('INT', pid)
80
- wait_for_output(stdout)
81
- output = stdout.read
82
- expect(output).to match(/files? inspected/)
83
- end
84
- end
85
- end
67
+ # Wait for start.
68
+ loop { break unless $stdout.string.empty? }
86
69
 
87
- context 'with Ctrl-C twice' do
88
- it 'exits immediately without reporting summary' do
89
- execute_rubocop do |stdout, stderr, pid|
90
- Process.kill('INT', pid)
91
- wait_for_output(stderr) # Wait for "Exiting...".
92
- Process.kill('INT', pid)
93
- wait_for_output(stdout)
94
- output = stdout.read
95
- expect(output).not_to match(/files? inspected/)
96
- end
97
- end
98
- end
70
+ @cli_thread
99
71
  end
100
72
 
101
- it 'checks a given correct file and returns 0' do
102
- File.open('example.rb', 'w') do |f|
103
- f.puts '# encoding: utf-8'
104
- f.puts 'x = 0'
105
- f.puts 'puts x'
106
- end
107
- begin
108
- expect(cli.run(['example.rb'])).to eq(0)
109
- expect($stdout.string)
110
- .to eq("\n1 file inspected, no offences detected\n")
111
- ensure
112
- File.delete 'example.rb'
113
- 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)
114
77
  end
115
78
 
116
- it 'checks a given file with faults and returns 1' do
117
- File.open('example.rb', 'w') do |f|
118
- f.puts '# encoding: utf-8'
119
- f.puts 'x = 0 '
120
- f.puts 'puts x'
121
- end
122
- begin
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
- ensure
131
- File.delete 'example.rb'
132
- 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 /)
133
84
  end
134
85
 
135
- it 'can report in emacs style', ruby: 1.9 do
136
- File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
137
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
138
- begin
139
- expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
140
- expect($stdout.string)
141
- .to eq(
142
- ['example1.rb:1: C: Missing utf-8 encoding comment.',
143
- 'example1.rb:1: C: Trailing whitespace detected.',
144
- "example1.rb:1: C: Surrounding space missing for operator '='.",
145
- 'example1.rb:2: C: Trailing whitespace detected.',
146
- 'example2.rb:1: C: Missing utf-8 encoding comment.',
147
- 'example2.rb:1: C: Tab detected.',
148
- '',
149
- '2 files inspected, 6 offences detected',
150
- ''].join("\n"))
151
- ensure
152
- File.delete 'example1.rb'
153
- File.delete 'example2.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/)
154
92
  end
155
93
  end
156
94
 
157
- it 'can report in emacs style', ruby: 2.0 do
158
- File.open('example1.rb', 'w') { |f| f.puts 'x= 0 ', 'y ', 'puts x' }
159
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
160
- begin
161
- expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
162
- expect($stdout.string)
163
- .to eq(
164
- ['example1.rb:1: C: Trailing whitespace detected.',
165
- "example1.rb:1: C: Surrounding space missing for operator '='.",
166
- 'example1.rb:2: C: Trailing whitespace detected.',
167
- 'example2.rb:1: C: Tab detected.',
168
- '',
169
- '2 files inspected, 4 offences detected',
170
- ''].join("\n"))
171
- ensure
172
- File.delete 'example1.rb'
173
- File.delete 'example2.rb'
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
174
102
  end
175
103
  end
104
+ end
176
105
 
177
- it 'ommits summary when --silent passed', ruby: 1.9 do
178
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
179
- File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
180
- begin
181
- expect(cli.run(['--emacs',
182
- '--silent',
183
- 'example1.rb',
184
- 'example2.rb'])).to eq(1)
185
- expect($stdout.string).to eq(
186
- ['example1.rb:1: C: Missing utf-8 encoding comment.',
187
- 'example1.rb:1: C: Trailing whitespace detected.',
188
- 'example2.rb:1: C: Missing utf-8 encoding comment.',
189
- 'example2.rb:1: C: Tab detected.',
190
- ''].join("\n"))
191
- ensure
192
- File.delete 'example1.rb'
193
- File.delete 'example2.rb'
194
- end
195
- 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
196
116
 
197
- it 'ommits summary when --silent passed', ruby: 2.0 do
198
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
199
- File.open('example2.rb', 'w') { |f| f.puts "\tputs 0" }
200
- begin
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: Trailing whitespace detected.',
207
- 'example2.rb:1: C: Tab detected.',
208
- ''].join("\n"))
209
- ensure
210
- File.delete 'example1.rb'
211
- File.delete 'example2.rb'
212
- end
213
- 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
214
131
 
215
- it 'shows cop names when --debug is passed', ruby: 2.0 do
216
- File.open('example1.rb', 'w') { |f| f.puts "\tputs 0" }
217
- begin
218
- expect(cli.run(['--emacs',
219
- '--silent',
220
- '--debug',
221
- 'example1.rb'])).to eq(1)
222
- expect($stdout.string.lines[-1]).to eq(
223
- ['example1.rb:1: C: Tab: Tab detected.',
224
- ''].join("\n"))
225
- ensure
226
- File.delete 'example1.rb'
227
- end
228
- end
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
229
155
 
230
- it 'can be configured with option to disable a certain error' do
231
- File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
232
- File.open('rubocop.yml', 'w') do |f|
233
- f.puts('Encoding:',
234
- ' Enabled: false',
235
- '',
236
- 'Indentation:',
237
- ' Enabled: false')
238
- end
239
- begin
240
- expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
241
- expect($stdout.string).to eq(
242
- ['== example1.rb ==',
243
- 'C: 1: Trailing whitespace detected.',
244
- '',
245
- '1 file inspected, 1 offence detected',
246
- ''].join("\n"))
247
- ensure
248
- File.delete 'example1.rb'
249
- File.delete 'rubocop.yml'
250
- end
251
- end
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
252
177
 
253
- it 'can be configured with project config to disable a certain error' do
254
- FileUtils.mkdir 'example_src'
255
- File.open('example_src/example1.rb', 'w') { |f| f.puts 'puts 0 ' }
256
- File.open('example_src/.rubocop.yml', 'w') do |f|
257
- f.puts('Encoding:',
258
- ' Enabled: false',
259
- '',
260
- 'Indentation:',
261
- ' Enabled: false')
262
- end
263
- begin
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
- ensure
272
- FileUtils.rm_rf 'example_src'
273
- end
274
- end
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
275
192
 
276
- it 'can use an alternative max line length from a config file' do
277
- FileUtils.mkdir 'example_src'
278
- File.open('example_src/example1.rb', 'w') do |f|
279
- f.puts '# encoding: utf-8'
280
- f.puts '#' * 90
281
- end
282
- File.open('example_src/.rubocop.yml', 'w') do |f|
283
- f.puts('LineLength:',
284
- ' Enabled: true',
285
- ' Max: 100')
286
- end
287
- begin
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
- ensure
293
- FileUtils.rm_rf 'example_src'
294
- end
295
- end
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
296
197
 
297
- it 'can have different config files in different directories' do
298
- %w(src lib).each do |dir|
299
- FileUtils.mkdir_p "example/#{dir}"
300
- File.open("example/#{dir}/example1.rb", 'w') do |f|
301
- f.puts '# encoding: utf-8'
302
- f.puts '#' * 90
303
- end
304
- end
305
- File.open('example/src/.rubocop.yml', 'w') do |f|
306
- f.puts('LineLength:',
307
- ' Enabled: true',
308
- ' Max: 100')
309
- end
310
- begin
311
- expect(cli.run(['example'])).to eq(1)
312
- expect($stdout.string).to eq(
313
- ['== example/lib/example1.rb ==',
314
- 'C: 2: Line is too long. [90/79]',
315
- '',
316
- '2 files inspected, 1 offence detected',
317
- ''].join("\n"))
318
- ensure
319
- FileUtils.rm_rf 'example'
320
- end
321
- end
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
322
212
 
323
- it 'prefers a config file in ancestor directory to another in home' do
324
- FileUtils.mkdir 'example_src'
325
- File.open('example_src/example1.rb', 'w') do |f|
326
- f.puts '# encoding: utf-8'
327
- f.puts '#' * 90
328
- end
329
- File.open('example_src/.rubocop.yml', 'w') do |f|
330
- f.puts('LineLength:',
331
- ' Enabled: true',
332
- ' Max: 100')
333
- end
334
- Dir.mktmpdir do |tmpdir|
335
- @original_home = ENV['HOME']
336
- ENV['HOME'] = tmpdir
337
- File.open("#{Dir.home}/.rubocop.yml", 'w') do |f|
338
- f.puts('LineLength:',
339
- ' Enabled: true',
340
- ' Max: 80')
341
- end
342
- begin
343
- expect(cli.run(['example_src/example1.rb'])).to eq(0)
344
- expect($stdout.string).to eq(
345
- ['', '1 file inspected, no offences detected',
346
- ''].join("\n"))
347
- ensure
348
- FileUtils.rm_rf 'example_src'
349
- ENV['HOME'] = @original_home
350
- end
351
- end
352
- end
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
353
225
 
354
- it 'finds no violations when checking the rubocop source code' do
355
- # Need to pass an empty array explicitly
356
- # so that the CLI does not refer arguments of `rspec`
357
- cli.run([])
358
- expect($stdout.string).to match(
359
- /files inspected, no offences detected\n/
360
- )
361
- end
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
362
236
 
363
- it 'registers an offence for a syntax error' do
364
- File.open('example.rb', 'w') do |f|
365
- f.puts '# encoding: utf-8'
366
- f.puts 'class Test'
367
- f.puts 'en'
368
- end
369
- begin
370
- expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
371
- unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
372
- expect($stdout.string).to eq(
373
- ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
374
- 'expecting keyword_end',
375
- '',
376
- '1 file inspected, 1 offence detected',
377
- ''].join("\n"))
378
- ensure
379
- File.delete 'example.rb'
380
- end
381
- end
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
382
254
 
383
- it 'can process a file with an invalid UTF-8 byte sequence' do
384
- File.open('example.rb', 'w') do |f|
385
- f.puts '# encoding: utf-8'
386
- f.puts "# \xf9\x29"
387
- end
388
- begin
389
- expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
390
- ensure
391
- File.delete 'example.rb'
392
- end
393
- end
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
394
272
 
395
- it 'has configuration for all cops in .rubocop.yml' do
396
- cop_names = Cop::Cop.all.map do |cop_class|
397
- cop_class.name.split('::').last
398
- end
399
- expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
400
- end
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
401
288
 
402
- it 'can have all cops disabled in a code section' do
403
- File.open('example.rb', 'w') do |f|
404
- f.puts '# encoding: utf-8'
405
- f.puts '# rubocop:disable all'
406
- f.puts '#' * 90
407
- f.puts 'x(123456)'
408
- f.puts 'y("123")'
409
- f.puts 'def func'
410
- f.puts ' # rubocop: enable LineLength, StringLiterals'
411
- f.puts ' ' + '#' * 93
412
- f.puts ' x(123456)'
413
- f.puts ' y("123")'
414
- f.puts 'end'
415
- end
416
- begin
417
- expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
418
- # all cops were disabled, then 2 were enabled again, so we
419
- # should get 2 offences reported.
420
- expect($stdout.string).to eq(
421
- ['example.rb:8: C: Line is too long. [95/79]',
422
- "example.rb:10: C: Prefer single-quoted strings when you don't " +
423
- 'need string interpolation or special symbols.',
424
- '',
425
- '1 file inspected, 2 offences detected',
426
- ''].join("\n"))
427
- ensure
428
- File.delete 'example.rb'
429
- end
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
+ ])
430
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
431
309
 
432
- it 'can have selected cops disabled in a code section' do
433
- File.open('example.rb', 'w') do |f|
434
- f.puts '# encoding: utf-8'
435
- f.puts '# rubocop:disable LineLength,NumericLiterals,StringLiterals'
436
- f.puts '#' * 90
437
- f.puts 'x(123456)'
438
- f.puts 'y("123")'
439
- f.puts 'def func'
440
- f.puts ' # rubocop: enable LineLength, StringLiterals'
441
- f.puts ' ' + '#' * 93
442
- f.puts ' x(123456)'
443
- f.puts ' y("123")'
444
- f.puts 'end'
445
- end
446
- begin
447
- expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
448
- # 3 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
- ensure
458
- File.delete 'example.rb'
459
- end
460
- 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
461
330
 
462
- it 'can have all cops disabled on a single line' do
463
- File.open('example.rb', 'w') do |f|
464
- f.puts '# encoding: utf-8'
465
- f.puts 'y("123", 123456) # rubocop:disable all'
466
- end
467
- begin
468
- expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
469
- expect($stdout.string).to eq(
470
- ['',
471
- '1 file inspected, no offences detected',
472
- ''].join("\n"))
473
- ensure
474
- File.delete 'example.rb'
475
- end
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
+ ])
476
337
  end
477
338
 
478
- it 'can have selected cops disabled on a single line' do
479
- File.open('example.rb', 'w') do |f|
480
- f.puts '# encoding: utf-8'
481
- f.puts '#' * 90 + ' # rubocop:disable LineLength'
482
- f.puts '#' * 95
483
- f.puts 'y("123") # rubocop:disable LineLength,StringLiterals'
484
- end
485
- begin
486
- expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
487
- expect($stdout.string).to eq(
488
- ['example.rb:3: C: Line is too long. [95/79]',
489
- '',
490
- '1 file inspected, 1 offence detected',
491
- ''].join("\n"))
492
- ensure
493
- File.delete 'example.rb'
494
- end
495
- 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
496
355
 
497
- it 'finds a file with no .rb extension but has a shebang line' do
498
- FileUtils::mkdir 'test'
499
- File.open('test/example', 'w') do |f|
500
- f.puts '#!/usr/bin/env ruby'
501
- f.puts '# encoding: utf-8'
502
- f.puts 'x = 0'
503
- f.puts 'puts x'
504
- end
505
- begin
506
- FileUtils::cd 'test' do
507
- # Need to pass an empty array explicitly
508
- # so that the CLI does not refer arguments of `rspec`
509
- expect(cli.run([])).to eq(0)
510
- expect($stdout.string).to eq(
511
- ['', '1 file inspected, no offences detected',
512
- ''].join("\n"))
513
- end
514
- ensure
515
- FileUtils::rm_rf 'test'
516
- end
517
- end
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
518
378
 
519
- describe '#display_summary' do
520
- it 'handles pluralization correctly' do
521
- cli.display_summary(1, 0, 0)
522
- expect($stdout.string).to eq(
523
- "\n1 file inspected, no offences detected\n")
524
- $stdout = StringIO.new
525
- cli.display_summary(1, 1, 0)
526
- expect($stdout.string).to eq(
527
- "\n1 file inspected, 1 offence detected\n")
528
- $stdout = StringIO.new
529
- cli.display_summary(2, 2, 0)
530
- expect($stdout.string).to eq(
531
- "\n2 files inspected, 2 offences detected\n")
532
- end
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
533
401
 
534
- it 'displays an error message when errors are present' do
535
- cli.display_summary(1, 1, 1)
536
- expect($stdout.string.lines.to_a[-3])
537
- .to eq("1 error occurred.\n")
538
- end
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")
594
+ end
595
+
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"])
539
601
  end
540
602
  end
541
603
  end