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.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -266
- data/CHANGELOG.md +49 -7
- data/README.md +75 -2
- data/Rakefile +2 -2
- data/bin/rubocop +15 -10
- data/lib/rubocop.rb +19 -1
- data/lib/rubocop/cli.rb +113 -116
- data/lib/rubocop/config.rb +202 -0
- data/lib/rubocop/config_store.rb +37 -0
- data/lib/rubocop/cop/alias.rb +2 -5
- data/lib/rubocop/cop/align_parameters.rb +1 -1
- data/lib/rubocop/cop/array_literal.rb +43 -4
- data/lib/rubocop/cop/avoid_for.rb +2 -4
- data/lib/rubocop/cop/avoid_global_vars.rb +49 -0
- data/lib/rubocop/cop/block_comments.rb +17 -0
- data/lib/rubocop/cop/brace_after_percent.rb +9 -5
- data/lib/rubocop/cop/{indentation.rb → case_indentation.rb} +1 -1
- data/lib/rubocop/cop/class_methods.rb +20 -0
- data/lib/rubocop/cop/colon_method_call.rb +44 -0
- data/lib/rubocop/cop/cop.rb +30 -2
- data/lib/rubocop/cop/def_parentheses.rb +1 -1
- data/lib/rubocop/cop/empty_line_between_defs.rb +26 -0
- data/lib/rubocop/cop/empty_lines.rb +10 -13
- data/lib/rubocop/cop/eval.rb +22 -0
- data/lib/rubocop/cop/favor_join.rb +37 -0
- data/lib/rubocop/cop/grammar.rb +2 -2
- data/lib/rubocop/cop/hash_literal.rb +43 -4
- data/lib/rubocop/cop/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/if_then_else.rb +1 -1
- data/lib/rubocop/cop/leading_comment_space.rb +20 -0
- data/lib/rubocop/cop/line_continuation.rb +18 -0
- data/lib/rubocop/cop/line_length.rb +1 -1
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +7 -6
- data/lib/rubocop/cop/method_length.rb +4 -15
- data/lib/rubocop/cop/not.rb +15 -0
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/semicolon.rb +74 -3
- data/lib/rubocop/cop/single_line_methods.rb +60 -0
- data/lib/rubocop/cop/space_after_control_keyword.rb +28 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -9
- data/lib/rubocop/cop/symbol_array.rb +29 -0
- data/lib/rubocop/cop/trivial_accessors.rb +103 -0
- data/lib/rubocop/cop/unless_else.rb +1 -1
- data/lib/rubocop/cop/variable_interpolation.rb +3 -2
- data/lib/rubocop/cop/word_array.rb +38 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +11 -7
- data/spec/project_spec.rb +27 -0
- data/spec/rubocop/cli_spec.rb +549 -487
- data/spec/rubocop/config_spec.rb +399 -0
- data/spec/rubocop/config_store_spec.rb +66 -0
- data/spec/rubocop/cops/alias_spec.rb +7 -0
- data/spec/rubocop/cops/array_literal_spec.rb +8 -1
- data/spec/rubocop/cops/avoid_for_spec.rb +15 -1
- data/spec/rubocop/cops/avoid_global_vars.rb +32 -0
- data/spec/rubocop/cops/block_comments_spec.rb +29 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +19 -13
- data/spec/rubocop/cops/{indentation_spec.rb → case_indentation_spec.rb} +2 -2
- data/spec/rubocop/cops/class_methods_spec.rb +49 -0
- data/spec/rubocop/cops/colon_method_call_spec.rb +47 -0
- data/spec/rubocop/cops/empty_line_between_defs_spec.rb +83 -0
- data/spec/rubocop/cops/empty_lines_spec.rb +6 -63
- data/spec/rubocop/cops/eval_spec.rb +36 -0
- data/spec/rubocop/cops/favor_join_spec.rb +39 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +8 -1
- data/spec/rubocop/cops/leading_comment_space_spec.rb +60 -0
- data/spec/rubocop/cops/line_continuation_spec.rb +24 -0
- data/spec/rubocop/cops/line_length_spec.rb +1 -0
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +20 -0
- data/spec/rubocop/cops/method_length_spec.rb +2 -5
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +2 -3
- data/spec/rubocop/cops/not_spec.rb +34 -0
- data/spec/rubocop/cops/offence_spec.rb +7 -0
- data/spec/rubocop/cops/semicolon_spec.rb +79 -4
- data/spec/rubocop/cops/single_line_methods_spec.rb +50 -0
- data/spec/rubocop/cops/space_after_control_keyword_spec.rb +28 -0
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +11 -1
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +74 -0
- data/spec/rubocop/cops/symbol_array_spec.rb +25 -0
- data/spec/rubocop/cops/trivial_accessors_spec.rb +332 -0
- data/spec/rubocop/cops/variable_interpolation_spec.rb +10 -1
- data/spec/rubocop/cops/word_array_spec.rb +39 -0
- data/spec/spec_helper.rb +16 -9
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +27 -0
- 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.
|
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
|
-
|
9
|
-
|
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
|
data/lib/rubocop/version.rb
CHANGED
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 =
|
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(
|
37
|
-
s.add_development_dependency(
|
38
|
-
s.add_development_dependency(
|
39
|
-
s.add_development_dependency(
|
40
|
-
s.add_development_dependency(
|
41
|
-
s.add_development_dependency(
|
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
|
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -5,537 +5,599 @@ require 'tmpdir'
|
|
5
5
|
require 'spec_helper'
|
6
6
|
|
7
7
|
module Rubocop
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
48
|
+
$stderr = StringIO.new
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
thread.terminate
|
55
|
-
end
|
50
|
+
create_file('example.rb', '# encoding: utf-8')
|
51
|
+
end
|
56
52
|
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
after do
|
54
|
+
$stderr = STDERR
|
55
|
+
@cli_thread.terminate if @cli_thread
|
56
|
+
end
|
60
57
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
expect(exit_status).to eq(1)
|
66
|
-
end
|
58
|
+
def interrupt
|
59
|
+
@interrupt_handlers.each(&:call)
|
60
|
+
end
|
67
61
|
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
77
|
-
|
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
|
-
|
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 '
|
102
|
-
|
103
|
-
|
104
|
-
|
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 '
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
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
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
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
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
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
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
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
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
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
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
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
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
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
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
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
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
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
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
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
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
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
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
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
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
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
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
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
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
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
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
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
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
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
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
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
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
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
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
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
|