rubocop 0.5.0 → 0.6.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 (57) hide show
  1. data/.rubocop.yml +59 -1
  2. data/CHANGELOG.md +38 -0
  3. data/Gemfile +1 -10
  4. data/README.md +28 -1
  5. data/Rakefile +1 -15
  6. data/lib/rubocop.rb +14 -0
  7. data/lib/rubocop/cli.rb +70 -7
  8. data/lib/rubocop/cop/alias.rb +5 -8
  9. data/lib/rubocop/cop/array_literal.rb +22 -0
  10. data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
  11. data/lib/rubocop/cop/avoid_perlisms.rb +19 -28
  12. data/lib/rubocop/cop/brace_after_percent.rb +28 -0
  13. data/lib/rubocop/cop/cop.rb +15 -3
  14. data/lib/rubocop/cop/encoding.rb +1 -1
  15. data/lib/rubocop/cop/ensure_return.rb +36 -0
  16. data/lib/rubocop/cop/favor_percent_r.rb +19 -0
  17. data/lib/rubocop/cop/favor_sprintf.rb +2 -10
  18. data/lib/rubocop/cop/grammar.rb +6 -3
  19. data/lib/rubocop/cop/handle_exceptions.rb +21 -0
  20. data/lib/rubocop/cop/hash_literal.rb +22 -0
  21. data/lib/rubocop/cop/method_length.rb +66 -0
  22. data/lib/rubocop/cop/op_method.rb +23 -0
  23. data/lib/rubocop/cop/percent_literals.rb +25 -0
  24. data/lib/rubocop/cop/percent_r.rb +19 -0
  25. data/lib/rubocop/cop/reduce_arguments.rb +67 -0
  26. data/lib/rubocop/cop/rescue_exception.rb +39 -0
  27. data/lib/rubocop/cop/rescue_modifier.rb +20 -0
  28. data/lib/rubocop/cop/symbol_snake_case.rb +5 -5
  29. data/lib/rubocop/cop/syntax.rb +13 -2
  30. data/lib/rubocop/version.rb +3 -1
  31. data/rubocop.gemspec +36 -169
  32. data/spec/rubocop/cli_spec.rb +146 -15
  33. data/spec/rubocop/cops/alias_spec.rb +10 -1
  34. data/spec/rubocop/cops/array_literal_spec.rb +29 -0
  35. data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
  36. data/spec/rubocop/cops/avoid_perlisms_spec.rb +12 -0
  37. data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
  38. data/spec/rubocop/cops/encoding_spec.rb +2 -2
  39. data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
  40. data/spec/rubocop/cops/favor_percent_r.rb +29 -0
  41. data/spec/rubocop/cops/favor_sprintf_spec.rb +8 -1
  42. data/spec/rubocop/cops/grammar_spec.rb +54 -40
  43. data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
  44. data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
  45. data/spec/rubocop/cops/method_length_spec.rb +150 -0
  46. data/spec/rubocop/cops/op_method_spec.rb +58 -0
  47. data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
  48. data/spec/rubocop/cops/percent_r_spec.rb +29 -0
  49. data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
  50. data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
  51. data/spec/rubocop/cops/rescue_modifier.rb +40 -0
  52. data/spec/rubocop/cops/space_around_operators_spec.rb +7 -0
  53. data/spec/rubocop/cops/symbol_snake_case_spec.rb +19 -7
  54. data/spec/rubocop/cops/tab_spec.rb +1 -1
  55. metadata +131 -50
  56. data/Gemfile.lock +0 -41
  57. data/VERSION +0 -1
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class RescueModifier < Cop
6
+ ERROR_MESSAGE = 'Avoid using rescue in its modifier form.'
7
+
8
+ def inspect(file, source, tokens, sexp)
9
+ each(:rescue_mod, sexp) do |s|
10
+ ident = find_first(:@ident, s)
11
+ lineno = ident ? ident[2].lineno : nil
12
+
13
+ add_offence(:convention,
14
+ lineno,
15
+ ERROR_MESSAGE) if lineno
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -5,14 +5,14 @@ module Rubocop
5
5
  class SymbolSnakeCase < Cop
6
6
  ERROR_MESSAGE = 'Use snake_case for symbols.'
7
7
  SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
8
- OPERATORS = %w(! + - % * ** [])
9
-
10
8
  def inspect(file, source, tokens, sexp)
11
9
  each(:symbol_literal, sexp) do |s|
12
- symbol_ident = s[1][1][1]
10
+ symbol_type = s[1][1][0]
13
11
 
14
- # handle a couple of special cases
15
- next if OPERATORS.include?(symbol_ident)
12
+ # don't check operators
13
+ next if symbol_type == :@op
14
+
15
+ symbol_ident = s[1][1][1]
16
16
 
17
17
  unless symbol_ident =~ SNAKE_CASE
18
18
  line_no = s[1][1][2].lineno
@@ -6,8 +6,19 @@ module Rubocop
6
6
  module Cop
7
7
  class Syntax < Cop
8
8
  def inspect(file, source, tokens, sexp)
9
- _, stderr, _ =
10
- Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
9
+ stderr = nil
10
+
11
+ # it's extremely important to run the syntax check in a
12
+ # clean environment - otherwise it will be extremely slow
13
+ if defined? Bundler
14
+ Bundler.with_clean_env do
15
+ _, stderr, _ =
16
+ Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
17
+ end
18
+ else
19
+ _, stderr, _ =
20
+ Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
21
+ end
11
22
 
12
23
  stderr.each_line do |line|
13
24
  # discard lines that are not containing relevant info
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Rubocop
4
- VERSION = IO.read(File.join(File.dirname(__FILE__), '../../VERSION'))
4
+ module Version
5
+ STRING = '0.6.0'
6
+ end
5
7
  end
@@ -1,175 +1,42 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'rubocop/version'
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = "rubocop"
8
- s.version = "0.5.0"
6
+ s.name = 'rubocop'
7
+ s.version = Rubocop::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.required_ruby_version = '>= 1.9.2'
10
+ s.authors = ['Bozhidar Batsov']
11
+ s.date = '2013-04-17'
12
+ s.description = 'Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide.'
13
+ s.email = 'bozhidar@batsov.com'
14
+ s.executables = ['rubocop']
15
+ s.extra_rdoc_files = ['LICENSE.txt', 'README.md']
16
+ s.files = ['.document',
17
+ '.rspec',
18
+ '.rubocop.yml',
19
+ '.travis.yml',
20
+ 'CHANGELOG.md',
21
+ 'CONTRIBUTING.md',
22
+ 'Gemfile',
23
+ 'LICENSE.txt',
24
+ 'README.md',
25
+ 'Rakefile',
26
+ 'bin/rubocop',
27
+ 'rubocop.gemspec'] + Dir['lib/**/*.rb']
9
28
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Bozhidar Batsov"]
12
- s.date = "2013-04-17"
13
- s.description = "Automatic Ruby code style checking tool. Aims to enforce the community-driven Ruby Style Guide."
14
- s.email = "bozhidar@batsov.com"
15
- s.executables = ["rubocop"]
16
- s.extra_rdoc_files = [
17
- "LICENSE.txt",
18
- "README.md"
19
- ]
20
- s.files = [
21
- ".document",
22
- ".rspec",
23
- ".rubocop.yml",
24
- ".travis.yml",
25
- "CHANGELOG.md",
26
- "CONTRIBUTING.md",
27
- "Gemfile",
28
- "Gemfile.lock",
29
- "LICENSE.txt",
30
- "README.md",
31
- "Rakefile",
32
- "VERSION",
33
- "bin/rubocop",
34
- "lib/rubocop.rb",
35
- "lib/rubocop/cli.rb",
36
- "lib/rubocop/cop/alias.rb",
37
- "lib/rubocop/cop/align_parameters.rb",
38
- "lib/rubocop/cop/ampersands_pipes_vs_and_or.rb",
39
- "lib/rubocop/cop/avoid_class_vars.rb",
40
- "lib/rubocop/cop/avoid_for.rb",
41
- "lib/rubocop/cop/avoid_perl_backrefs.rb",
42
- "lib/rubocop/cop/avoid_perlisms.rb",
43
- "lib/rubocop/cop/blocks.rb",
44
- "lib/rubocop/cop/class_and_module_camel_case.rb",
45
- "lib/rubocop/cop/collection_methods.rb",
46
- "lib/rubocop/cop/cop.rb",
47
- "lib/rubocop/cop/def_parentheses.rb",
48
- "lib/rubocop/cop/empty_lines.rb",
49
- "lib/rubocop/cop/encoding.rb",
50
- "lib/rubocop/cop/end_of_line.rb",
51
- "lib/rubocop/cop/favor_modifier.rb",
52
- "lib/rubocop/cop/favor_sprintf.rb",
53
- "lib/rubocop/cop/favor_unless_over_negated_if.rb",
54
- "lib/rubocop/cop/grammar.rb",
55
- "lib/rubocop/cop/hash_syntax.rb",
56
- "lib/rubocop/cop/if_then_else.rb",
57
- "lib/rubocop/cop/indentation.rb",
58
- "lib/rubocop/cop/line_length.rb",
59
- "lib/rubocop/cop/method_and_variable_snake_case.rb",
60
- "lib/rubocop/cop/new_lambda_literal.rb",
61
- "lib/rubocop/cop/numeric_literals.rb",
62
- "lib/rubocop/cop/offence.rb",
63
- "lib/rubocop/cop/parameter_lists.rb",
64
- "lib/rubocop/cop/parentheses_around_condition.rb",
65
- "lib/rubocop/cop/semicolon.rb",
66
- "lib/rubocop/cop/space_after_comma_etc.rb",
67
- "lib/rubocop/cop/string_literals.rb",
68
- "lib/rubocop/cop/surrounding_space.rb",
69
- "lib/rubocop/cop/symbol_snake_case.rb",
70
- "lib/rubocop/cop/syntax.rb",
71
- "lib/rubocop/cop/tab.rb",
72
- "lib/rubocop/cop/ternary_operator.rb",
73
- "lib/rubocop/cop/trailing_whitespace.rb",
74
- "lib/rubocop/cop/unless_else.rb",
75
- "lib/rubocop/cop/variable_interpolation.rb",
76
- "lib/rubocop/cop/when_then.rb",
77
- "lib/rubocop/report/emacs_style.rb",
78
- "lib/rubocop/report/plain_text.rb",
79
- "lib/rubocop/report/report.rb",
80
- "lib/rubocop/version.rb",
81
- "rubocop.gemspec",
82
- "spec/rubocop/cli_spec.rb",
83
- "spec/rubocop/cops/alias_spec.rb",
84
- "spec/rubocop/cops/align_parameters_spec.rb",
85
- "spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb",
86
- "spec/rubocop/cops/avoid_class_vars_spec.rb",
87
- "spec/rubocop/cops/avoid_for_spec.rb",
88
- "spec/rubocop/cops/avoid_perl_backrefs_spec.rb",
89
- "spec/rubocop/cops/avoid_perlisms_spec.rb",
90
- "spec/rubocop/cops/class_and_module_camel_case_spec.rb",
91
- "spec/rubocop/cops/collection_methods_spec.rb",
92
- "spec/rubocop/cops/cop_spec.rb",
93
- "spec/rubocop/cops/def_with_parentheses_spec.rb",
94
- "spec/rubocop/cops/def_without_parentheses_spec.rb",
95
- "spec/rubocop/cops/empty_lines_spec.rb",
96
- "spec/rubocop/cops/encoding_spec.rb",
97
- "spec/rubocop/cops/end_of_line_spec.rb",
98
- "spec/rubocop/cops/favor_modifier_spec.rb",
99
- "spec/rubocop/cops/favor_sprintf_spec.rb",
100
- "spec/rubocop/cops/favor_unless_over_negated_if_spec.rb",
101
- "spec/rubocop/cops/favor_until_over_negated_while_spec.rb",
102
- "spec/rubocop/cops/grammar_spec.rb",
103
- "spec/rubocop/cops/hash_syntax_spec.rb",
104
- "spec/rubocop/cops/if_with_semicolon_spec.rb",
105
- "spec/rubocop/cops/indentation_spec.rb",
106
- "spec/rubocop/cops/line_length_spec.rb",
107
- "spec/rubocop/cops/method_and_variable_snake_case_spec.rb",
108
- "spec/rubocop/cops/multiline_blocks_spec.rb",
109
- "spec/rubocop/cops/multiline_if_then_spec.rb",
110
- "spec/rubocop/cops/new_lambda_literal_spec.rb",
111
- "spec/rubocop/cops/numeric_literals_spec.rb",
112
- "spec/rubocop/cops/offence_spec.rb",
113
- "spec/rubocop/cops/one_line_conditional_spec.rb",
114
- "spec/rubocop/cops/parameter_lists_spec.rb",
115
- "spec/rubocop/cops/parentheses_around_condition_spec.rb",
116
- "spec/rubocop/cops/semicolon_spec.rb",
117
- "spec/rubocop/cops/single_line_blocks_spec.rb",
118
- "spec/rubocop/cops/space_after_colon_spec.rb",
119
- "spec/rubocop/cops/space_after_comma_spec.rb",
120
- "spec/rubocop/cops/space_after_semicolon_spec.rb",
121
- "spec/rubocop/cops/space_around_braces_spec.rb",
122
- "spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb",
123
- "spec/rubocop/cops/space_around_operators_spec.rb",
124
- "spec/rubocop/cops/space_inside_brackets_spec.rb",
125
- "spec/rubocop/cops/space_inside_parens_spec.rb",
126
- "spec/rubocop/cops/string_literals_spec.rb",
127
- "spec/rubocop/cops/symbol_snake_case_spec.rb",
128
- "spec/rubocop/cops/syntax_spec.rb",
129
- "spec/rubocop/cops/tab_spec.rb",
130
- "spec/rubocop/cops/ternary_operator_spec.rb",
131
- "spec/rubocop/cops/trailing_whitespace_spec.rb",
132
- "spec/rubocop/cops/unless_else_spec.rb",
133
- "spec/rubocop/cops/variable_interpolation_spec.rb",
134
- "spec/rubocop/cops/when_then_spec.rb",
135
- "spec/rubocop/reports/emacs_style_spec.rb",
136
- "spec/rubocop/reports/report_spec.rb",
137
- "spec/spec_helper.rb"
138
- ]
139
- s.homepage = "http://github.com/bbatsov/rubocop"
140
- s.licenses = ["MIT"]
141
- s.require_paths = ["lib"]
142
- s.rubygems_version = "1.8.23"
143
- s.summary = "Automatic Ruby code style checking tool."
29
+ s.test_files = Dir['spec/**/*.rb']
30
+ s.homepage = 'http://github.com/bbatsov/rubocop'
31
+ s.licenses = ['MIT']
32
+ s.require_paths = ['lib']
33
+ s.rubygems_version = '1.8.23'
34
+ s.summary = 'Automatic Ruby code style checking tool.'
144
35
 
145
- if s.respond_to? :specification_version then
146
- s.specification_version = 3
147
-
148
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
149
- s.add_runtime_dependency(%q<term-ansicolor>, [">= 0"])
150
- s.add_development_dependency(%q<rake>, [">= 0"])
151
- s.add_development_dependency(%q<rspec>, [">= 0"])
152
- s.add_development_dependency(%q<yard>, [">= 0"])
153
- s.add_development_dependency(%q<bundler>, [">= 0"])
154
- s.add_development_dependency(%q<jeweler>, [">= 0"])
155
- s.add_development_dependency(%q<simplecov>, [">= 0"])
156
- else
157
- s.add_dependency(%q<term-ansicolor>, [">= 0"])
158
- s.add_dependency(%q<rake>, [">= 0"])
159
- s.add_dependency(%q<rspec>, [">= 0"])
160
- s.add_dependency(%q<yard>, [">= 0"])
161
- s.add_dependency(%q<bundler>, [">= 0"])
162
- s.add_dependency(%q<jeweler>, [">= 0"])
163
- s.add_dependency(%q<simplecov>, [">= 0"])
164
- end
165
- else
166
- s.add_dependency(%q<term-ansicolor>, [">= 0"])
167
- s.add_dependency(%q<rake>, [">= 0"])
168
- s.add_dependency(%q<rspec>, [">= 0"])
169
- s.add_dependency(%q<yard>, [">= 0"])
170
- s.add_dependency(%q<bundler>, [">= 0"])
171
- s.add_dependency(%q<jeweler>, [">= 0"])
172
- s.add_dependency(%q<simplecov>, [">= 0"])
173
- end
36
+ s.add_runtime_dependency(%q<term-ansicolor>, '~> 1.1')
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')
174
42
  end
175
-
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'fileutils'
4
+ require 'tmpdir'
4
5
  require 'spec_helper'
5
6
 
6
7
  module Rubocop
@@ -25,7 +26,7 @@ module Rubocop
25
26
  it 'exits cleanly when -v is used' do
26
27
  expect { cli.run ['-v'] }.to exit_with_code(0)
27
28
  expect { cli.run ['--version'] }.to exit_with_code(0)
28
- expect($stdout.string).to eq((Rubocop::VERSION + "\n") * 2)
29
+ expect($stdout.string).to eq((Rubocop::Version::STRING + "\n") * 2)
29
30
  end
30
31
 
31
32
  it 'checks a given correct file and returns 0' do
@@ -37,7 +38,7 @@ module Rubocop
37
38
  begin
38
39
  expect(cli.run(['example.rb'])).to eq(0)
39
40
  expect($stdout.string.uncolored)
40
- .to eq("\n1 files inspected, 0 offences detected\n")
41
+ .to eq("\n1 file inspected, no offences detected\n")
41
42
  ensure
42
43
  File.delete 'example.rb'
43
44
  end
@@ -55,7 +56,7 @@ module Rubocop
55
56
  .to eq ['== example.rb ==',
56
57
  'C: 2: Trailing whitespace detected.',
57
58
  '',
58
- '1 files inspected, 1 offences detected',
59
+ '1 file inspected, 1 offence detected',
59
60
  ''].join("\n")
60
61
  ensure
61
62
  File.delete 'example.rb'
@@ -69,11 +70,11 @@ module Rubocop
69
70
  expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
70
71
  expect($stdout.string.uncolored)
71
72
  .to eq(
72
- ['example1.rb:1: C: Missing encoding comment.',
73
+ ['example1.rb:1: C: Missing utf-8 encoding comment.',
73
74
  'example1.rb:1: C: Trailing whitespace detected.',
74
75
  "example1.rb:1: C: Surrounding space missing for operator '='.",
75
76
  'example1.rb:2: C: Trailing whitespace detected.',
76
- 'example2.rb:1: C: Missing encoding comment.',
77
+ 'example2.rb:1: C: Missing utf-8 encoding comment.',
77
78
  'example2.rb:1: C: Tab detected.',
78
79
  '',
79
80
  '2 files inspected, 6 offences detected',
@@ -113,9 +114,9 @@ module Rubocop
113
114
  'example1.rb',
114
115
  'example2.rb'])).to eq(1)
115
116
  expect($stdout.string).to eq(
116
- ['example1.rb:1: C: Missing encoding comment.',
117
+ ['example1.rb:1: C: Missing utf-8 encoding comment.',
117
118
  'example1.rb:1: C: Trailing whitespace detected.',
118
- 'example2.rb:1: C: Missing encoding comment.',
119
+ 'example2.rb:1: C: Missing utf-8 encoding comment.',
119
120
  'example2.rb:1: C: Tab detected.',
120
121
  ''].join("\n"))
121
122
  ensure
@@ -157,7 +158,7 @@ module Rubocop
157
158
  ['== example1.rb ==',
158
159
  'C: 1: Trailing whitespace detected.',
159
160
  '',
160
- '1 files inspected, 1 offences detected',
161
+ '1 file inspected, 1 offence detected',
161
162
  ''].join("\n"))
162
163
  ensure
163
164
  File.delete 'example1.rb'
@@ -181,7 +182,7 @@ module Rubocop
181
182
  ['== example_src/example1.rb ==',
182
183
  'C: 1: Trailing whitespace detected.',
183
184
  '',
184
- '1 files inspected, 1 offences detected',
185
+ '1 file inspected, 1 offence detected',
185
186
  ''].join("\n"))
186
187
  ensure
187
188
  FileUtils.rm_rf 'example_src'
@@ -202,7 +203,7 @@ module Rubocop
202
203
  begin
203
204
  expect(cli.run(['example_src/example1.rb'])).to eq(0)
204
205
  expect($stdout.string.uncolored).to eq(
205
- ['', '1 files inspected, 0 offences detected',
206
+ ['', '1 file inspected, no offences detected',
206
207
  ''].join("\n"))
207
208
  ensure
208
209
  FileUtils.rm_rf 'example_src'
@@ -228,17 +229,50 @@ module Rubocop
228
229
  ['== example/lib/example1.rb ==',
229
230
  'C: 2: Line is too long. [90/79]',
230
231
  '',
231
- '2 files inspected, 1 offences detected',
232
+ '2 files inspected, 1 offence detected',
232
233
  ''].join("\n"))
233
234
  ensure
234
235
  FileUtils.rm_rf 'example'
235
236
  end
236
237
  end
237
238
 
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
268
+ end
269
+
238
270
  it 'finds no violations when checking the rubocop source code' do
239
- cli.run
271
+ # Need to pass an empty array explicitly
272
+ # so that the CLI does not refer arguments of `rspec`
273
+ cli.run([])
240
274
  expect($stdout.string.uncolored).to match(
241
- /files inspected, 0 offences detected\n/
275
+ /files inspected, no offences detected\n/
242
276
  )
243
277
  end
244
278
 
@@ -255,7 +289,7 @@ module Rubocop
255
289
  ["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
256
290
  'expecting keyword_end',
257
291
  '',
258
- '1 files inspected, 1 offences detected',
292
+ '1 file inspected, 1 offence detected',
259
293
  ''].join("\n"))
260
294
  ensure
261
295
  File.delete 'example.rb'
@@ -281,6 +315,101 @@ module Rubocop
281
315
  expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
282
316
  end
283
317
 
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
347
+
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
376
+ end
377
+
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
392
+ end
393
+
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
411
+ end
412
+
284
413
  it 'finds a file with no .rb extension but has a shebang line' do
285
414
  FileUtils::mkdir 'test'
286
415
  File.open('test/example', 'w') do |f|
@@ -291,9 +420,11 @@ module Rubocop
291
420
  end
292
421
  begin
293
422
  FileUtils::cd 'test' do
423
+ # Need to pass an empty array explicitly
424
+ # so that the CLI does not refer arguments of `rspec`
294
425
  expect(cli.run([])).to eq(0)
295
426
  expect($stdout.string.uncolored).to eq(
296
- ['', '1 files inspected, 0 offences detected',
427
+ ['', '1 file inspected, no offences detected',
297
428
  ''].join("\n"))
298
429
  end
299
430
  ensure