rubocop 0.9.1 → 0.10.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.
- data/.travis.yml +3 -1
- data/CHANGELOG.md +38 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/config/default.yml +14 -1
- data/config/enabled.yml +30 -7
- data/lib/rubocop.rb +15 -0
- data/lib/rubocop/cli.rb +48 -154
- data/lib/rubocop/config.rb +19 -22
- data/lib/rubocop/config_store.rb +2 -4
- data/lib/rubocop/cop/commissioner.rb +90 -0
- data/lib/rubocop/cop/cop.rb +38 -31
- data/lib/rubocop/cop/corrector.rb +84 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
- data/lib/rubocop/cop/lint/eval.rb +0 -2
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
- data/lib/rubocop/cop/lint/loop.rb +0 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
- data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +0 -2
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/rails/validation.rb +2 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -3
- data/lib/rubocop/cop/style/alias.rb +2 -4
- data/lib/rubocop/cop/style/align_parameters.rb +0 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
- data/lib/rubocop/cop/style/attr.rb +0 -2
- data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
- data/lib/rubocop/cop/style/avoid_for.rb +0 -2
- data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
- data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
- data/lib/rubocop/cop/style/begin_block.rb +0 -2
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_nesting.rb +3 -3
- data/lib/rubocop/cop/style/blocks.rb +0 -2
- data/lib/rubocop/cop/style/case_equality.rb +0 -2
- data/lib/rubocop/cop/style/case_indentation.rb +0 -2
- data/lib/rubocop/cop/style/character_literal.rb +10 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +3 -5
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
- data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
- data/lib/rubocop/cop/style/constant_name.rb +0 -2
- data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
- data/lib/rubocop/cop/style/documentation.rb +6 -2
- data/lib/rubocop/cop/style/dot_position.rb +0 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
- data/lib/rubocop/cop/style/empty_lines.rb +10 -8
- data/lib/rubocop/cop/style/empty_literal.rb +3 -1
- data/lib/rubocop/cop/style/encoding.rb +7 -6
- data/lib/rubocop/cop/style/end_block.rb +0 -2
- data/lib/rubocop/cop/style/end_of_line.rb +4 -3
- data/lib/rubocop/cop/style/favor_join.rb +0 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
- data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
- data/lib/rubocop/cop/style/if_then_else.rb +0 -2
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/style/line_continuation.rb +4 -3
- data/lib/rubocop/cop/style/line_length.rb +4 -3
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
- data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/method_length.rb +0 -4
- data/lib/rubocop/cop/style/not.rb +0 -2
- data/lib/rubocop/cop/style/op_method.rb +0 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
- data/lib/rubocop/cop/style/proc.rb +0 -2
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
- data/lib/rubocop/cop/style/redundant_return.rb +59 -0
- data/lib/rubocop/cop/style/redundant_self.rb +83 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
- data/lib/rubocop/cop/style/semicolon.rb +15 -9
- data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
- data/lib/rubocop/cop/style/tab.rb +4 -3
- data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
- data/lib/rubocop/cop/style/unless_else.rb +0 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +0 -2
- data/lib/rubocop/cop/util.rb +0 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/processed_source.rb +27 -0
- data/lib/rubocop/rake_task.rb +50 -0
- data/lib/rubocop/source_parser.rb +105 -0
- data/lib/rubocop/target_finder.rb +67 -0
- data/lib/rubocop/token.rb +22 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +5 -3
- data/spec/project_spec.rb +0 -11
- data/spec/rubocop/cli_spec.rb +112 -6
- data/spec/rubocop/config_spec.rb +13 -17
- data/spec/rubocop/config_store_spec.rb +8 -23
- data/spec/rubocop/cops/commissioner_spec.rb +72 -0
- data/spec/rubocop/cops/corrector_spec.rb +63 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
- data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
- data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
- data/spec/rubocop/cops/offence_spec.rb +28 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
- data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
- data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
- data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
- data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
- data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
- data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
- data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/processed_source_spec.rb +67 -0
- data/spec/rubocop/source_parser_spec.rb +141 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +27 -0
- data/spec/spec_helper.rb +24 -4
- metadata +108 -18
- checksums.yaml +0 -7
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
# A basic wrapper around Parser's tokens.
|
5
|
+
class Token
|
6
|
+
attr_reader :pos, :type, :text
|
7
|
+
|
8
|
+
def self.from_parser_token(parser_token)
|
9
|
+
type, details = *parser_token
|
10
|
+
text, range = *details
|
11
|
+
new(range, type, text)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(pos, type, text)
|
15
|
+
@pos, @type, @text = pos, type, text
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"[[#{@pos.line}, #{@pos.column}], #{@type}, #{@text.inspect}]"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/rubocop/version.rb
CHANGED
data/rubocop.gemspec
CHANGED
@@ -27,9 +27,11 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.summary = 'Automatic Ruby code style checking tool.'
|
28
28
|
|
29
29
|
s.add_runtime_dependency('rainbow', '>= 1.1.4')
|
30
|
-
s.add_runtime_dependency('parser', '2.0.0.
|
31
|
-
s.
|
32
|
-
s.
|
30
|
+
s.add_runtime_dependency('parser', '2.0.0.pre2')
|
31
|
+
s.add_runtime_dependency('backports', '~> 3.3.3')
|
32
|
+
s.add_runtime_dependency('powerpack', '0.0.1')
|
33
|
+
s.add_development_dependency('rake', '~> 10.1')
|
34
|
+
s.add_development_dependency('rspec', '~> 2.14')
|
33
35
|
s.add_development_dependency('yard', '~> 0.8')
|
34
36
|
s.add_development_dependency('bundler', '~> 1.3')
|
35
37
|
s.add_development_dependency('simplecov', '~> 0.7')
|
data/spec/project_spec.rb
CHANGED
@@ -10,15 +10,4 @@ describe 'RuboCop Project' do
|
|
10
10
|
.to eq((['AllCops'] + cop_names).sort)
|
11
11
|
end
|
12
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
|
-
expect(Rubocop::CLI.new.run([])).to eq(0)
|
22
|
-
end
|
23
|
-
end
|
24
13
|
end
|
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -24,10 +24,19 @@ module Rubocop
|
|
24
24
|
File.expand_path(path)
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
describe '-h/--help option' do
|
28
|
+
it 'exits cleanly' do
|
29
|
+
expect { cli.run ['-h'] }.to exit_with_code(0)
|
30
|
+
expect { cli.run ['--help'] }.to exit_with_code(0)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'shows help text' do
|
34
|
+
begin
|
35
|
+
cli.run(['--help'])
|
36
|
+
rescue SystemExit # rubocop:disable HandleExceptions
|
37
|
+
end
|
38
|
+
|
39
|
+
expected_help = <<-END
|
31
40
|
Usage: rubocop [options] [file1, file2, ...]
|
32
41
|
-d, --debug Display debug info.
|
33
42
|
-c, --config FILE Specify configuration file.
|
@@ -40,6 +49,7 @@ Usage: rubocop [options] [file1, file2, ...]
|
|
40
49
|
[c]lang
|
41
50
|
[e]macs
|
42
51
|
[j]son
|
52
|
+
[f]iles
|
43
53
|
custom formatter class name
|
44
54
|
-o, --out FILE Write output to a file instead of STDOUT.
|
45
55
|
This option applies to the previously
|
@@ -53,8 +63,34 @@ Usage: rubocop [options] [file1, file2, ...]
|
|
53
63
|
-n, --no-color Disable color output.
|
54
64
|
-v, --version Display version.
|
55
65
|
-V, --verbose-version Display verbose version.
|
56
|
-
|
57
|
-
|
66
|
+
END
|
67
|
+
|
68
|
+
expect($stdout.string).to eq(expected_help)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'lists all builtin formatters' do
|
72
|
+
begin
|
73
|
+
cli.run(['--help'])
|
74
|
+
rescue SystemExit # rubocop:disable HandleExceptions
|
75
|
+
end
|
76
|
+
|
77
|
+
option_sections = $stdout.string.lines.slice_before(/^\s*-/)
|
78
|
+
|
79
|
+
format_section = option_sections.find do |lines|
|
80
|
+
lines.first =~ /^\s*-f/
|
81
|
+
end
|
82
|
+
|
83
|
+
formatter_keys = format_section.reduce([]) do |keys, line|
|
84
|
+
match = line.match(/^[ ]{39}(\[[a-z\]]+)/)
|
85
|
+
next keys unless match
|
86
|
+
keys << match.captures.first.gsub(/\[|\]/, '')
|
87
|
+
end.sort
|
88
|
+
|
89
|
+
expected_formatter_keys =
|
90
|
+
Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS.keys.sort
|
91
|
+
|
92
|
+
expect(formatter_keys).to eq(expected_formatter_keys)
|
93
|
+
end
|
58
94
|
end
|
59
95
|
|
60
96
|
it 'exits cleanly when -v is used' do
|
@@ -339,6 +375,26 @@ Usage: rubocop [options] [file1, file2, ...]
|
|
339
375
|
''].join("\n"))
|
340
376
|
end
|
341
377
|
|
378
|
+
it 'can be configured to override a parameter that is a hash' do
|
379
|
+
create_file('example1.rb',
|
380
|
+
['# encoding: utf-8',
|
381
|
+
'arr.find_all { |e| e > 0 }.collect { |e| -e }'])
|
382
|
+
# We only care about select over find_all. All other preferred methods
|
383
|
+
# appearing in the default config are gone when we override
|
384
|
+
# PreferredMethods. We get no report about collect.
|
385
|
+
create_file('rubocop.yml',
|
386
|
+
['CollectionMethods:',
|
387
|
+
' PreferredMethods:',
|
388
|
+
' find_all: select'])
|
389
|
+
cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
|
390
|
+
expect($stdout.string).to eq(
|
391
|
+
['== example1.rb ==',
|
392
|
+
'C: 2: 5: Prefer select over find_all.',
|
393
|
+
'',
|
394
|
+
'1 file inspected, 1 offence detected',
|
395
|
+
''].join("\n"))
|
396
|
+
end
|
397
|
+
|
342
398
|
it 'works when a cop that others depend on is disabled' do
|
343
399
|
create_file('example1.rb', ['if a',
|
344
400
|
' b',
|
@@ -467,6 +523,56 @@ Usage: rubocop [options] [file1, file2, ...]
|
|
467
523
|
''].join("\n"))
|
468
524
|
end
|
469
525
|
|
526
|
+
it 'can exclude a typical vendor directory' do
|
527
|
+
create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
|
528
|
+
['AllCops:',
|
529
|
+
' Excludes:',
|
530
|
+
' - lib/parser/lexer.rb'])
|
531
|
+
|
532
|
+
create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
|
533
|
+
['# encoding: utf-8',
|
534
|
+
'#' * 90])
|
535
|
+
|
536
|
+
create_file('.rubocop.yml',
|
537
|
+
['AllCops:',
|
538
|
+
' Excludes:',
|
539
|
+
' - vendor/**'])
|
540
|
+
|
541
|
+
cli.run(%w(--format simple))
|
542
|
+
expect($stdout.string).to eq(
|
543
|
+
['',
|
544
|
+
'0 files inspected, no offences detected',
|
545
|
+
''].join("\n"))
|
546
|
+
end
|
547
|
+
|
548
|
+
# Relative exclude paths in .rubocop.yml files are relative to that file,
|
549
|
+
# but in configuration files with other names they will be relative to
|
550
|
+
# whatever file inherits from them.
|
551
|
+
it 'can exclude a vendor directory indirectly' do
|
552
|
+
create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/.rubocop.yml',
|
553
|
+
['AllCops:',
|
554
|
+
' Excludes:',
|
555
|
+
' - lib/parser/lexer.rb'])
|
556
|
+
|
557
|
+
create_file('vendor/bundle/ruby/1.9.1/gems/parser-2.0.0/lib/ex.rb',
|
558
|
+
['# encoding: utf-8',
|
559
|
+
'#' * 90])
|
560
|
+
|
561
|
+
create_file('.rubocop.yml',
|
562
|
+
['inherit_from: config/default.yml'])
|
563
|
+
|
564
|
+
create_file('config/default.yml',
|
565
|
+
['AllCops:',
|
566
|
+
' Excludes:',
|
567
|
+
' - vendor/**'])
|
568
|
+
|
569
|
+
cli.run(%w(--format simple))
|
570
|
+
expect($stdout.string).to eq(
|
571
|
+
['',
|
572
|
+
'0 files inspected, no offences detected',
|
573
|
+
''].join("\n"))
|
574
|
+
end
|
575
|
+
|
470
576
|
it 'prints a warning for an unrecognized cop name in .rubocop.yml' do
|
471
577
|
create_file('example/example1.rb', [
|
472
578
|
'# encoding: utf-8',
|
data/spec/rubocop/config_spec.rb
CHANGED
@@ -11,8 +11,6 @@ describe Rubocop::Config do
|
|
11
11
|
let(:hash) { {} }
|
12
12
|
let(:loaded_path) { 'example/.rubocop.yml' }
|
13
13
|
|
14
|
-
before { Rubocop::ConfigStore.prepare }
|
15
|
-
|
16
14
|
describe '.configuration_file_for', :isolated_environment do
|
17
15
|
subject(:configuration_file_for) do
|
18
16
|
Rubocop::Config.configuration_file_for(dir_path)
|
@@ -100,9 +98,9 @@ describe Rubocop::Config do
|
|
100
98
|
])
|
101
99
|
end
|
102
100
|
|
103
|
-
it 'gets AllCops/
|
101
|
+
it 'gets AllCops/Excludes from the highest directory level' do
|
104
102
|
excludes = configuration_from_file['AllCops']['Excludes']
|
105
|
-
expect(excludes).to eq(['
|
103
|
+
expect(excludes).to eq([File.expand_path('vendor/**')])
|
106
104
|
end
|
107
105
|
end
|
108
106
|
|
@@ -120,9 +118,9 @@ describe Rubocop::Config do
|
|
120
118
|
create_file(file_path, ['inherit_from: ../.rubocop.yml'])
|
121
119
|
end
|
122
120
|
|
123
|
-
it 'gets AllCops/
|
121
|
+
it 'gets an absolute AllCops/Excludes' do
|
124
122
|
excludes = configuration_from_file['AllCops']['Excludes']
|
125
|
-
expect(excludes).to eq(['
|
123
|
+
expect(excludes).to eq([File.expand_path('vendor/**'), /[A-Z]/])
|
126
124
|
end
|
127
125
|
end
|
128
126
|
|
@@ -139,9 +137,9 @@ describe Rubocop::Config do
|
|
139
137
|
create_file(file_path, ['inherit_from: ../src/.rubocop.yml'])
|
140
138
|
end
|
141
139
|
|
142
|
-
it 'gets AllCops/Exclude
|
140
|
+
it 'gets an absolute AllCops/Exclude' do
|
143
141
|
excludes = configuration_from_file['AllCops']['Excludes']
|
144
|
-
expect(excludes).to eq(['
|
142
|
+
expect(excludes).to eq([File.expand_path('src/vendor/**')])
|
145
143
|
end
|
146
144
|
end
|
147
145
|
|
@@ -350,7 +348,7 @@ describe Rubocop::Config do
|
|
350
348
|
let(:hash) do
|
351
349
|
{
|
352
350
|
'AllCops' => {
|
353
|
-
'Excludes' => ['log/*']
|
351
|
+
'Excludes' => ['/home/foo/project/log/*']
|
354
352
|
}
|
355
353
|
}
|
356
354
|
end
|
@@ -424,14 +422,14 @@ describe Rubocop::Config do
|
|
424
422
|
end
|
425
423
|
|
426
424
|
describe 'configuration for SymbolArray', :isolated_environment do
|
427
|
-
|
428
|
-
|
425
|
+
let(:config) do
|
426
|
+
config_path = Rubocop::Config.configuration_file_for('.')
|
427
|
+
Rubocop::Config.configuration_from_file(config_path)
|
429
428
|
end
|
430
429
|
|
431
430
|
context 'when no config file exists for the target file' do
|
432
431
|
it 'is disabled' do
|
433
|
-
|
434
|
-
expect(configuration.cop_enabled?('SymbolArray')).to be_false
|
432
|
+
expect(config.cop_enabled?('SymbolArray')).to be_false
|
435
433
|
end
|
436
434
|
end
|
437
435
|
|
@@ -441,8 +439,7 @@ describe Rubocop::Config do
|
|
441
439
|
'LineLength:',
|
442
440
|
' Max: 79'
|
443
441
|
])
|
444
|
-
|
445
|
-
expect(configuration.cop_enabled?('SymbolArray')).to be_false
|
442
|
+
expect(config.cop_enabled?('SymbolArray')).to be_false
|
446
443
|
end
|
447
444
|
end
|
448
445
|
|
@@ -452,8 +449,7 @@ describe Rubocop::Config do
|
|
452
449
|
'SymbolArray:',
|
453
450
|
' Enabled: true'
|
454
451
|
])
|
455
|
-
|
456
|
-
expect(configuration.cop_enabled?('SymbolArray')).to be_true
|
452
|
+
expect(config.cop_enabled?('SymbolArray')).to be_true
|
457
453
|
end
|
458
454
|
end
|
459
455
|
end
|
@@ -4,7 +4,8 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Rubocop
|
6
6
|
describe ConfigStore do
|
7
|
-
|
7
|
+
subject(:config_store) { ConfigStore.new }
|
8
|
+
|
8
9
|
before do
|
9
10
|
Config.stub(:configuration_file_for) do |arg|
|
10
11
|
# File tree:
|
@@ -19,26 +20,10 @@ module Rubocop
|
|
19
20
|
Config.stub(:merge_with_default) { |config, file| "merged #{config}" }
|
20
21
|
end
|
21
22
|
|
22
|
-
describe '.prepare' do
|
23
|
-
it 'resets @options_config' do
|
24
|
-
ConfigStore.set_options_config(:options_config)
|
25
|
-
ConfigStore.prepare
|
26
|
-
Config.should_receive(:configuration_file_for)
|
27
|
-
ConfigStore.for('file1')
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'resets @config_cache' do
|
31
|
-
ConfigStore.for('file1')
|
32
|
-
ConfigStore.prepare
|
33
|
-
Config.should_receive(:configuration_file_for)
|
34
|
-
ConfigStore.for('file1')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
23
|
describe '.for' do
|
39
24
|
it 'always uses config specified in command line' do
|
40
|
-
|
41
|
-
expect(
|
25
|
+
config_store.set_options_config(:options_config)
|
26
|
+
expect(config_store.for('file1')).to eq('merged options_config loaded')
|
42
27
|
end
|
43
28
|
|
44
29
|
context 'when no config specified in command line' do
|
@@ -50,15 +35,15 @@ module Rubocop
|
|
50
35
|
Config.should_receive(:configuration_from_file).once
|
51
36
|
.with('dir/.rubocop.yml')
|
52
37
|
|
53
|
-
|
54
|
-
|
55
|
-
|
38
|
+
config_store.for('dir/file2')
|
39
|
+
config_store.for('dir/file2')
|
40
|
+
config_store.for('dir/subdir/file3')
|
56
41
|
end
|
57
42
|
|
58
43
|
it 'searches for config path if not available in cache' do
|
59
44
|
Config.should_receive(:configuration_file_for).once
|
60
45
|
Config.should_receive(:configuration_from_file).once
|
61
|
-
|
46
|
+
config_store.for('file1')
|
62
47
|
end
|
63
48
|
end
|
64
49
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# rubocop:disable LineLength
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
module Rubocop
|
7
|
+
module Cop
|
8
|
+
describe Commissioner do
|
9
|
+
describe '#investigate' do
|
10
|
+
it 'returns all offences found by the cops' do
|
11
|
+
cop = double(Cop, offences: [1])
|
12
|
+
commissioner = Commissioner.new([cop])
|
13
|
+
source = []
|
14
|
+
processed_source = parse_source(source)
|
15
|
+
|
16
|
+
expect(commissioner.investigate(processed_source)).to eq [1]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'traverses the AST and invoke cops specific callbacks' do
|
20
|
+
cop = double(Cop, offences: [])
|
21
|
+
cop.should_receive(:on_def)
|
22
|
+
|
23
|
+
commissioner = Commissioner.new([cop])
|
24
|
+
source = ['def method', '1', 'end']
|
25
|
+
processed_source = parse_source(source)
|
26
|
+
|
27
|
+
commissioner.investigate(processed_source)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'passes the input params to all cops that implement their own #investigate method' do
|
31
|
+
source = []
|
32
|
+
processed_source = parse_source(source)
|
33
|
+
cop = double(Cop, offences: [])
|
34
|
+
cop.should_receive(:investigate).with(processed_source)
|
35
|
+
|
36
|
+
commissioner = Commissioner.new([cop])
|
37
|
+
|
38
|
+
commissioner.investigate(processed_source)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'stores all errors raised by the cops' do
|
42
|
+
cop = double(Cop, offences: [])
|
43
|
+
cop.stub(:on_def) { raise RuntimeError }
|
44
|
+
|
45
|
+
commissioner = Commissioner.new([cop])
|
46
|
+
source = ['def method', '1', 'end']
|
47
|
+
processed_source = parse_source(source)
|
48
|
+
|
49
|
+
commissioner.investigate(processed_source)
|
50
|
+
|
51
|
+
expect(commissioner.errors[cop]).to have(1).item
|
52
|
+
expect(commissioner.errors[cop][0]).to be_instance_of(RuntimeError)
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when passed :raise_error option' do
|
56
|
+
it 're-raises the exception received while processing' do
|
57
|
+
cop = double(Cop, offences: [])
|
58
|
+
cop.stub(:on_def) { raise RuntimeError }
|
59
|
+
|
60
|
+
commissioner = Commissioner.new([cop], raise_error: true)
|
61
|
+
source = ['def method', '1', 'end']
|
62
|
+
processed_source = parse_source(source)
|
63
|
+
|
64
|
+
expect do
|
65
|
+
commissioner.investigate(processed_source)
|
66
|
+
end.to raise_error(RuntimeError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Corrector do
|
8
|
+
describe '#rewrite' do
|
9
|
+
it 'allows removal of a range' do
|
10
|
+
source = 'true and false'
|
11
|
+
processed_source = parse_source(source)
|
12
|
+
|
13
|
+
correction = lambda do |corrector|
|
14
|
+
node = processed_source.ast
|
15
|
+
corrector.remove(node.loc.operator)
|
16
|
+
end
|
17
|
+
|
18
|
+
corrector = Corrector.new(processed_source.buffer, [correction])
|
19
|
+
expect(corrector.rewrite).to eq 'true false'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'allows insertion before a source range' do
|
23
|
+
source = 'true and false'
|
24
|
+
processed_source = parse_source(source)
|
25
|
+
|
26
|
+
correction = lambda do |corrector|
|
27
|
+
node = processed_source.ast
|
28
|
+
corrector.insert_before(node.loc.operator, ';nil ')
|
29
|
+
end
|
30
|
+
|
31
|
+
corrector = Corrector.new(processed_source.buffer, [correction])
|
32
|
+
expect(corrector.rewrite).to eq 'true ;nil and false'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'allows insertion after a source range' do
|
36
|
+
source = 'true and false'
|
37
|
+
processed_source = parse_source(source)
|
38
|
+
|
39
|
+
correction = lambda do |corrector|
|
40
|
+
node = processed_source.ast
|
41
|
+
corrector.insert_after(node.loc.operator, ' nil;')
|
42
|
+
end
|
43
|
+
|
44
|
+
corrector = Corrector.new(processed_source.buffer, [correction])
|
45
|
+
expect(corrector.rewrite).to eq 'true and nil; false'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'allows replacement of a range' do
|
49
|
+
source = 'true and false'
|
50
|
+
processed_source = parse_source(source)
|
51
|
+
|
52
|
+
correction = lambda do |corrector|
|
53
|
+
node = processed_source.ast
|
54
|
+
corrector.replace(node.loc.operator, 'or')
|
55
|
+
end
|
56
|
+
|
57
|
+
corrector = Corrector.new(processed_source.buffer, [correction])
|
58
|
+
expect(corrector.rewrite).to eq 'true or false'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|