rubocop 0.3.0 → 0.6.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 (139) hide show
  1. data/.rubocop.yml +176 -0
  2. data/.travis.yml +1 -0
  3. data/CHANGELOG.md +60 -0
  4. data/CONTRIBUTING.md +13 -6
  5. data/Gemfile +1 -13
  6. data/README.md +131 -6
  7. data/Rakefile +1 -15
  8. data/bin/rubocop +2 -1
  9. data/lib/rubocop/cli.rb +133 -21
  10. data/lib/rubocop/cop/alias.rb +18 -0
  11. data/lib/rubocop/cop/align_parameters.rb +5 -4
  12. data/lib/rubocop/cop/array_literal.rb +22 -0
  13. data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
  14. data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
  15. data/lib/rubocop/cop/avoid_for.rb +17 -0
  16. data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
  17. data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
  18. data/lib/rubocop/cop/blocks.rb +1 -1
  19. data/lib/rubocop/cop/brace_after_percent.rb +28 -0
  20. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
  21. data/lib/rubocop/cop/collection_methods.rb +29 -0
  22. data/lib/rubocop/cop/cop.rb +16 -4
  23. data/lib/rubocop/cop/def_parentheses.rb +4 -1
  24. data/lib/rubocop/cop/encoding.rb +7 -5
  25. data/lib/rubocop/cop/end_of_line.rb +1 -3
  26. data/lib/rubocop/cop/ensure_return.rb +36 -0
  27. data/lib/rubocop/cop/favor_modifier.rb +82 -0
  28. data/lib/rubocop/cop/favor_percent_r.rb +19 -0
  29. data/lib/rubocop/cop/favor_sprintf.rb +37 -0
  30. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
  31. data/lib/rubocop/cop/grammar.rb +12 -8
  32. data/lib/rubocop/cop/handle_exceptions.rb +21 -0
  33. data/lib/rubocop/cop/hash_literal.rb +22 -0
  34. data/lib/rubocop/cop/indentation.rb +1 -1
  35. data/lib/rubocop/cop/line_length.rb +7 -3
  36. data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
  37. data/lib/rubocop/cop/method_length.rb +66 -0
  38. data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
  39. data/lib/rubocop/cop/offence.rb +3 -1
  40. data/lib/rubocop/cop/op_method.rb +23 -0
  41. data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
  42. data/lib/rubocop/cop/percent_literals.rb +25 -0
  43. data/lib/rubocop/cop/percent_r.rb +19 -0
  44. data/lib/rubocop/cop/reduce_arguments.rb +67 -0
  45. data/lib/rubocop/cop/rescue_exception.rb +39 -0
  46. data/lib/rubocop/cop/rescue_modifier.rb +20 -0
  47. data/lib/rubocop/cop/semicolon.rb +18 -0
  48. data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
  49. data/lib/rubocop/cop/string_literals.rb +12 -0
  50. data/lib/rubocop/cop/surrounding_space.rb +48 -47
  51. data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
  52. data/lib/rubocop/cop/syntax.rb +42 -0
  53. data/lib/rubocop/cop/tab.rb +1 -3
  54. data/lib/rubocop/cop/variable_interpolation.rb +20 -0
  55. data/lib/rubocop/cop/when_then.rb +3 -0
  56. data/lib/rubocop/report/emacs_style.rb +2 -2
  57. data/lib/rubocop/report/plain_text.rb +1 -1
  58. data/lib/rubocop/report/report.rb +5 -2
  59. data/lib/rubocop/version.rb +3 -1
  60. data/lib/rubocop.rb +37 -0
  61. data/rubocop.gemspec +36 -134
  62. data/spec/rubocop/cli_spec.rb +326 -48
  63. data/spec/rubocop/cops/alias_spec.rb +36 -0
  64. data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
  65. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
  66. data/spec/rubocop/cops/array_literal_spec.rb +29 -0
  67. data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
  68. data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
  69. data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
  70. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
  71. data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
  72. data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
  73. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
  74. data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
  75. data/spec/rubocop/cops/cop_spec.rb +5 -5
  76. data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
  77. data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
  78. data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
  79. data/spec/rubocop/cops/encoding_spec.rb +17 -11
  80. data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
  81. data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
  82. data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
  83. data/spec/rubocop/cops/favor_percent_r.rb +29 -0
  84. data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
  85. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
  86. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
  87. data/spec/rubocop/cops/grammar_spec.rb +59 -44
  88. data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
  89. data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
  90. data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
  91. data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
  92. data/spec/rubocop/cops/indentation_spec.rb +7 -7
  93. data/spec/rubocop/cops/line_length_spec.rb +4 -4
  94. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
  95. data/spec/rubocop/cops/method_length_spec.rb +150 -0
  96. data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
  97. data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
  98. data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
  99. data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
  100. data/spec/rubocop/cops/offence_spec.rb +10 -4
  101. data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
  102. data/spec/rubocop/cops/op_method_spec.rb +58 -0
  103. data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
  104. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
  105. data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
  106. data/spec/rubocop/cops/percent_r_spec.rb +29 -0
  107. data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
  108. data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
  109. data/spec/rubocop/cops/rescue_modifier.rb +40 -0
  110. data/spec/rubocop/cops/semicolon_spec.rb +29 -0
  111. data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
  112. data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
  113. data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
  114. data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
  115. data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
  116. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
  117. data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
  118. data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
  119. data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
  120. data/spec/rubocop/cops/string_literals_spec.rb +9 -6
  121. data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
  122. data/spec/rubocop/cops/syntax_spec.rb +34 -0
  123. data/spec/rubocop/cops/tab_spec.rb +4 -4
  124. data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
  125. data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
  126. data/spec/rubocop/cops/unless_else_spec.rb +4 -4
  127. data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
  128. data/spec/rubocop/cops/when_then_spec.rb +12 -5
  129. data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
  130. data/spec/rubocop/reports/report_spec.rb +5 -5
  131. data/spec/spec_helper.rb +6 -0
  132. metadata +165 -36
  133. data/.rbenv-version +0 -1
  134. data/.rvmrc +0 -1
  135. data/Gemfile.lock +0 -39
  136. data/VERSION +0 -1
  137. data/features/rubocop.feature +0 -9
  138. data/features/step_definitions/rubocop_steps.rb +0 -1
  139. data/features/support/env.rb +0 -15
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'fileutils'
4
+ require 'tmpdir'
3
5
  require 'spec_helper'
4
6
 
5
7
  module Rubocop
@@ -10,24 +12,33 @@ module Rubocop
10
12
  after(:each) { $stdout = STDOUT }
11
13
 
12
14
  it 'exits cleanly when -h is used' do
13
- lambda { cli.run ['-h'] }.should exit_with_code(0)
14
- lambda { cli.run ['--help'] }.should exit_with_code(0)
15
+ expect { cli.run ['-h'] }.to exit_with_code(0)
16
+ expect { cli.run ['--help'] }.to exit_with_code(0)
15
17
  message = ['Usage: rubocop [options] [file1, file2, ...]',
16
- ' -v, --[no-]verbose Run verbosely',
18
+ ' -d, --[no-]debug Display debug info',
17
19
  ' -e, --emacs Emacs style output',
18
20
  ' -c, --config FILE Configuration file',
19
- ' -s, --silent Silence summary']
20
- $stdout.string.should == (message * 2).join("\n") + "\n"
21
+ ' -s, --silent Silence summary',
22
+ ' -v, --version Display version']
23
+ expect($stdout.string).to eq((message * 2).join("\n") + "\n")
24
+ end
25
+
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)
21
30
  end
22
31
 
23
32
  it 'checks a given correct file and returns 0' do
24
33
  File.open('example.rb', 'w') do |f|
25
34
  f.puts '# encoding: utf-8'
26
35
  f.puts 'x = 0'
36
+ f.puts 'puts x'
27
37
  end
28
38
  begin
29
- cli.run(['example.rb']).should == 0
30
- $stdout.string.should == "\n1 files inspected, 0 offences detected\n"
39
+ expect(cli.run(['example.rb'])).to eq(0)
40
+ expect($stdout.string.uncolored)
41
+ .to eq("\n1 file inspected, no offences detected\n")
31
42
  ensure
32
43
  File.delete 'example.rb'
33
44
  end
@@ -37,52 +48,95 @@ module Rubocop
37
48
  File.open('example.rb', 'w') do |f|
38
49
  f.puts '# encoding: utf-8'
39
50
  f.puts 'x = 0 '
51
+ f.puts 'puts x'
40
52
  end
41
53
  begin
42
- cli.run(['example.rb']).should == 1
43
- $stdout.string.should == ['== example.rb ==',
44
- 'C: 2: Trailing whitespace detected.',
45
- '',
46
- '1 files inspected, 1 offences detected',
47
- ''].join("\n")
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")
48
61
  ensure
49
62
  File.delete 'example.rb'
50
63
  end
51
64
  end
52
65
 
53
- it 'can report in emacs style' do
54
- File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
55
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
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' }
56
69
  begin
57
- cli.run(['--emacs', 'example1.rb', 'example2.rb']).should == 1
58
- $stdout.string.should ==
59
- ['example1.rb:1: C: Missing encoding comment.',
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.',
60
74
  'example1.rb:1: C: Trailing whitespace detected.',
61
- 'example2.rb:1: C: Missing encoding comment.',
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
86
+ end
87
+
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.',
62
98
  'example2.rb:1: C: Tab detected.',
63
99
  '',
64
100
  '2 files inspected, 4 offences detected',
65
- ''].join("\n")
101
+ ''].join("\n"))
66
102
  ensure
67
103
  File.delete 'example1.rb'
68
104
  File.delete 'example2.rb'
69
105
  end
70
106
  end
71
107
 
72
- it 'ommits summary when --silent passed' do
73
- File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
74
- File.open('example2.rb', 'w') { |f| f.puts "\tx = 0" }
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" }
75
111
  begin
76
- cli.run(['--emacs',
77
- '--silent',
78
- 'example1.rb',
79
- 'example2.rb']).should == 1
80
- $stdout.string.should ==
81
- ['example1.rb:1: C: Missing encoding comment.',
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.',
82
118
  'example1.rb:1: C: Trailing whitespace detected.',
83
- 'example2.rb:1: C: Missing encoding comment.',
119
+ 'example2.rb:1: C: Missing utf-8 encoding comment.',
84
120
  'example2.rb:1: C: Tab detected.',
85
- ''].join("\n")
121
+ ''].join("\n"))
122
+ ensure
123
+ File.delete 'example1.rb'
124
+ File.delete 'example2.rb'
125
+ end
126
+ end
127
+
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"))
86
140
  ensure
87
141
  File.delete 'example1.rb'
88
142
  File.delete 'example2.rb'
@@ -90,7 +144,7 @@ module Rubocop
90
144
  end
91
145
 
92
146
  it 'can be configured with option to disable a certain error' do
93
- File.open('example1.rb', 'w') { |f| f.puts 'x = 0 ' }
147
+ File.open('example1.rb', 'w') { |f| f.puts 'puts 0 ' }
94
148
  File.open('rubocop.yml', 'w') do |f|
95
149
  f.puts('Encoding:',
96
150
  ' Enabled: false',
@@ -99,14 +153,13 @@ module Rubocop
99
153
  ' Enabled: false')
100
154
  end
101
155
  begin
102
- return_code = cli.run(['-c', 'rubocop.yml', 'example1.rb'])
103
- $stdout.string.should ==
156
+ expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
157
+ expect($stdout.string.uncolored).to eq(
104
158
  ['== example1.rb ==',
105
159
  'C: 1: Trailing whitespace detected.',
106
160
  '',
107
- '1 files inspected, 1 offences detected',
108
- ''].join("\n")
109
- return_code.should == 1
161
+ '1 file inspected, 1 offence detected',
162
+ ''].join("\n"))
110
163
  ensure
111
164
  File.delete 'example1.rb'
112
165
  File.delete 'rubocop.yml'
@@ -115,7 +168,7 @@ module Rubocop
115
168
 
116
169
  it 'can be configured with project config to disable a certain error' do
117
170
  FileUtils.mkdir 'example_src'
118
- File.open('example_src/example1.rb', 'w') { |f| f.puts 'x = 0 ' }
171
+ File.open('example_src/example1.rb', 'w') { |f| f.puts 'puts 0 ' }
119
172
  File.open('example_src/.rubocop.yml', 'w') do |f|
120
173
  f.puts('Encoding:',
121
174
  ' Enabled: false',
@@ -124,35 +177,260 @@ module Rubocop
124
177
  ' Enabled: false')
125
178
  end
126
179
  begin
127
- return_code = cli.run(['example_src/example1.rb'])
128
- $stdout.string.should ==
180
+ expect(cli.run(['example_src/example1.rb'])).to eq(1)
181
+ expect($stdout.string.uncolored).to eq(
129
182
  ['== example_src/example1.rb ==',
130
183
  'C: 1: Trailing whitespace detected.',
131
184
  '',
132
- '1 files inspected, 1 offences detected',
133
- ''].join("\n")
134
- return_code.should == 1
185
+ '1 file inspected, 1 offence detected',
186
+ ''].join("\n"))
135
187
  ensure
136
188
  FileUtils.rm_rf 'example_src'
137
189
  end
138
190
  end
139
191
 
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'
210
+ end
211
+ end
212
+
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
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
+
140
270
  it 'finds no violations when checking the rubocop source code' do
141
- cli.run
142
- $stdout.string.should =~ /files inspected, 0 offences detected\n/
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
+ )
277
+ end
278
+
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
143
297
  end
144
298
 
145
- it 'can process a file with an invalide UTF-8 byte sequence' do
299
+ it 'can process a file with an invalid UTF-8 byte sequence' do
146
300
  File.open('example.rb', 'w') do |f|
147
301
  f.puts '# encoding: utf-8'
148
302
  f.puts "# \xf9\x29"
149
303
  end
150
304
  begin
151
- cli.run(['--emacs', 'example.rb']).should == 0
305
+ expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
152
306
  ensure
153
307
  File.delete 'example.rb'
154
308
  end
155
309
  end
310
+
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
314
+ end
315
+ expect(YAML.load_file('.rubocop.yml').keys.sort).to eq(cop_names.sort)
316
+ end
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
+
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
433
+ end
156
434
  end
157
435
  end
158
436
  end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Rubocop
6
+ module Cop
7
+ describe Alias do
8
+ let(:a) { Alias.new }
9
+
10
+ it 'registers an offence for alias with symbol args' do
11
+ inspect_source(a,
12
+ 'file.rb',
13
+ ['alias :ala :bala'])
14
+ expect(a.offences.size).to eq(1)
15
+ expect(a.offences.map(&:message))
16
+ .to eq([Alias::ERROR_MESSAGE])
17
+ end
18
+
19
+ it 'registers an offence for alias with bareword args' do
20
+ inspect_source(a,
21
+ 'file.rb',
22
+ ['alias ala bala'])
23
+ expect(a.offences.size).to eq(1)
24
+ expect(a.offences.map(&:message))
25
+ .to eq([Alias::ERROR_MESSAGE])
26
+ end
27
+
28
+ it 'does not register an offence for alias_method' do
29
+ inspect_source(a,
30
+ 'file.rb',
31
+ ['alias_method :ala, :bala'])
32
+ expect(a.offences).to be_empty
33
+ end
34
+ end
35
+ end
36
+ end