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.

Files changed (149) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG.md +38 -0
  3. data/README.md +34 -0
  4. data/Rakefile +3 -0
  5. data/config/default.yml +14 -1
  6. data/config/enabled.yml +30 -7
  7. data/lib/rubocop.rb +15 -0
  8. data/lib/rubocop/cli.rb +48 -154
  9. data/lib/rubocop/config.rb +19 -22
  10. data/lib/rubocop/config_store.rb +2 -4
  11. data/lib/rubocop/cop/commissioner.rb +90 -0
  12. data/lib/rubocop/cop/cop.rb +38 -31
  13. data/lib/rubocop/cop/corrector.rb +84 -0
  14. data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
  15. data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
  16. data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
  17. data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
  18. data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
  19. data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
  20. data/lib/rubocop/cop/lint/eval.rb +0 -2
  21. data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
  22. data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
  23. data/lib/rubocop/cop/lint/loop.rb +0 -2
  24. data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
  25. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
  26. data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
  27. data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
  28. data/lib/rubocop/cop/lint/void.rb +0 -2
  29. data/lib/rubocop/cop/offence.rb +9 -0
  30. data/lib/rubocop/cop/rails/validation.rb +2 -1
  31. data/lib/rubocop/cop/style/access_control.rb +4 -3
  32. data/lib/rubocop/cop/style/alias.rb +2 -4
  33. data/lib/rubocop/cop/style/align_parameters.rb +0 -2
  34. data/lib/rubocop/cop/style/and_or.rb +4 -6
  35. data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
  36. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
  37. data/lib/rubocop/cop/style/attr.rb +0 -2
  38. data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
  39. data/lib/rubocop/cop/style/avoid_for.rb +0 -2
  40. data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
  41. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
  42. data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
  43. data/lib/rubocop/cop/style/begin_block.rb +0 -2
  44. data/lib/rubocop/cop/style/block_comments.rb +2 -2
  45. data/lib/rubocop/cop/style/block_nesting.rb +3 -3
  46. data/lib/rubocop/cop/style/blocks.rb +0 -2
  47. data/lib/rubocop/cop/style/case_equality.rb +0 -2
  48. data/lib/rubocop/cop/style/case_indentation.rb +0 -2
  49. data/lib/rubocop/cop/style/character_literal.rb +10 -6
  50. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
  51. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  52. data/lib/rubocop/cop/style/collection_methods.rb +3 -5
  53. data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
  54. data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
  55. data/lib/rubocop/cop/style/constant_name.rb +0 -2
  56. data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
  57. data/lib/rubocop/cop/style/documentation.rb +6 -2
  58. data/lib/rubocop/cop/style/dot_position.rb +0 -2
  59. data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
  60. data/lib/rubocop/cop/style/empty_lines.rb +10 -8
  61. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  62. data/lib/rubocop/cop/style/encoding.rb +7 -6
  63. data/lib/rubocop/cop/style/end_block.rb +0 -2
  64. data/lib/rubocop/cop/style/end_of_line.rb +4 -3
  65. data/lib/rubocop/cop/style/favor_join.rb +0 -2
  66. data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
  67. data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
  68. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
  69. data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
  70. data/lib/rubocop/cop/style/if_then_else.rb +0 -2
  71. data/lib/rubocop/cop/style/lambda.rb +0 -2
  72. data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
  73. data/lib/rubocop/cop/style/line_continuation.rb +4 -3
  74. data/lib/rubocop/cop/style/line_length.rb +4 -3
  75. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
  76. data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
  77. data/lib/rubocop/cop/style/method_length.rb +0 -4
  78. data/lib/rubocop/cop/style/not.rb +0 -2
  79. data/lib/rubocop/cop/style/op_method.rb +0 -2
  80. data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
  81. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
  82. data/lib/rubocop/cop/style/proc.rb +0 -2
  83. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
  84. data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
  85. data/lib/rubocop/cop/style/redundant_return.rb +59 -0
  86. data/lib/rubocop/cop/style/redundant_self.rb +83 -0
  87. data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
  88. data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
  89. data/lib/rubocop/cop/style/semicolon.rb +15 -9
  90. data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
  91. data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
  92. data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
  93. data/lib/rubocop/cop/style/string_literals.rb +5 -2
  94. data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
  95. data/lib/rubocop/cop/style/tab.rb +4 -3
  96. data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
  97. data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
  99. data/lib/rubocop/cop/style/unless_else.rb +0 -2
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
  101. data/lib/rubocop/cop/style/when_then.rb +3 -3
  102. data/lib/rubocop/cop/style/while_until_do.rb +3 -5
  103. data/lib/rubocop/cop/style/word_array.rb +0 -2
  104. data/lib/rubocop/cop/util.rb +0 -4
  105. data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
  106. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  107. data/lib/rubocop/processed_source.rb +27 -0
  108. data/lib/rubocop/rake_task.rb +50 -0
  109. data/lib/rubocop/source_parser.rb +105 -0
  110. data/lib/rubocop/target_finder.rb +67 -0
  111. data/lib/rubocop/token.rb +22 -0
  112. data/lib/rubocop/version.rb +1 -1
  113. data/rubocop.gemspec +5 -3
  114. data/spec/project_spec.rb +0 -11
  115. data/spec/rubocop/cli_spec.rb +112 -6
  116. data/spec/rubocop/config_spec.rb +13 -17
  117. data/spec/rubocop/config_store_spec.rb +8 -23
  118. data/spec/rubocop/cops/commissioner_spec.rb +72 -0
  119. data/spec/rubocop/cops/corrector_spec.rb +63 -0
  120. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
  121. data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
  122. data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
  123. data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
  124. data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
  125. data/spec/rubocop/cops/offence_spec.rb +28 -0
  126. data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
  127. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
  128. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
  129. data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
  130. data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
  131. data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
  132. data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
  133. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
  134. data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
  135. data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
  136. data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
  137. data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
  138. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
  139. data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
  140. data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
  141. data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
  142. data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
  143. data/spec/rubocop/processed_source_spec.rb +67 -0
  144. data/spec/rubocop/source_parser_spec.rb +141 -0
  145. data/spec/rubocop/target_finder_spec.rb +180 -0
  146. data/spec/rubocop/token_spec.rb +27 -0
  147. data/spec/spec_helper.rb +24 -4
  148. metadata +108 -18
  149. 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
@@ -3,7 +3,7 @@
3
3
  module Rubocop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.9.1'
6
+ STRING = '0.10.0'
7
7
 
8
8
  MSG = '%s (using Parser %s, running on %s %s %s)'
9
9
 
@@ -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.pre1')
31
- s.add_development_dependency('rake', '~> 10.0')
32
- s.add_development_dependency('rspec', '~> 2.13')
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')
@@ -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
@@ -24,10 +24,19 @@ module Rubocop
24
24
  File.expand_path(path)
25
25
  end
26
26
 
27
- it 'exits cleanly when -h is used' do
28
- expect { cli.run ['-h'] }.to exit_with_code(0)
29
- expect { cli.run ['--help'] }.to exit_with_code(0)
30
- message = <<-END
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
- END
57
- expect($stdout.string).to eq(message * 2)
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',
@@ -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/Exclude from the highest directory level' do
101
+ it 'gets AllCops/Excludes from the highest directory level' do
104
102
  excludes = configuration_from_file['AllCops']['Excludes']
105
- expect(excludes).to eq(['../vendor/**'])
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/Exclude relative to its own directory' do
121
+ it 'gets an absolute AllCops/Excludes' do
124
122
  excludes = configuration_from_file['AllCops']['Excludes']
125
- expect(excludes).to eq(['../vendor/**', /[A-Z]/])
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 relative to its own directory' do
140
+ it 'gets an absolute AllCops/Exclude' do
143
141
  excludes = configuration_from_file['AllCops']['Excludes']
144
- expect(excludes).to eq(['../src/vendor/**'])
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
- before do
428
- create_file('example.rb', '# encoding: utf-8')
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
- configuration = Rubocop::ConfigStore.for('example.rb')
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
- configuration = Rubocop::ConfigStore.for('example.rb')
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
- configuration = Rubocop::ConfigStore.for('example.rb')
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
- before(:each) { ConfigStore.prepare }
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
- ConfigStore.set_options_config(:options_config)
41
- expect(ConfigStore.for('file1')).to eq('merged options_config loaded')
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
- ConfigStore.for('dir/file2')
54
- ConfigStore.for('dir/file2')
55
- ConfigStore.for('dir/subdir/file3')
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
- ConfigStore.for('file1')
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