rubocop 0.27.1 → 0.28.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 (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -4
  3. data/Gemfile +1 -1
  4. data/README.md +16 -1
  5. data/config/default.yml +15 -1
  6. data/config/disabled.yml +5 -0
  7. data/config/enabled.yml +10 -2
  8. data/lib/rubocop.rb +3 -0
  9. data/lib/rubocop/config.rb +1 -1
  10. data/lib/rubocop/cop/mixin/access_modifier_node.rb +2 -2
  11. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +10 -26
  12. data/lib/rubocop/cop/mixin/on_method_def.rb +1 -1
  13. data/lib/rubocop/cop/mixin/string_help.rb +10 -1
  14. data/lib/rubocop/cop/style/align_hash.rb +2 -2
  15. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -1
  16. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +21 -19
  17. data/lib/rubocop/cop/style/else_alignment.rb +29 -16
  18. data/lib/rubocop/cop/style/empty_else.rb +47 -0
  19. data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +38 -0
  20. data/lib/rubocop/cop/style/extra_spacing.rb +35 -0
  21. data/lib/rubocop/cop/style/indentation_width.rb +1 -1
  22. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  23. data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -1
  24. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +20 -4
  25. data/lib/rubocop/cop/style/negated_while.rb +3 -1
  26. data/lib/rubocop/cop/style/perl_backrefs.rb +8 -3
  27. data/lib/rubocop/cop/style/single_line_block_params.rb +7 -1
  28. data/lib/rubocop/cop/style/special_global_vars.rb +8 -3
  29. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  30. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +2 -7
  31. data/lib/rubocop/cop/style/symbol_proc.rb +14 -6
  32. data/lib/rubocop/cop/style/unneeded_capital_w.rb +2 -1
  33. data/lib/rubocop/cop/util.rb +1 -1
  34. data/lib/rubocop/cop/variable_force.rb +3 -3
  35. data/lib/rubocop/cop/variable_force/scope.rb +1 -1
  36. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  37. data/lib/rubocop/formatter/html_formatter.rb +1 -1
  38. data/lib/rubocop/options.rb +13 -6
  39. data/lib/rubocop/rake_task.rb +0 -1
  40. data/lib/rubocop/runner.rb +19 -6
  41. data/lib/rubocop/target_finder.rb +32 -1
  42. data/lib/rubocop/version.rb +1 -1
  43. data/relnotes/v0.28.0.md +90 -0
  44. data/spec/project_spec.rb +1 -0
  45. data/spec/rubocop/cli_spec.rb +115 -24
  46. data/spec/rubocop/comment_config_spec.rb +1 -1
  47. data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +0 -1
  48. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  49. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +1 -1
  50. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +2 -3
  51. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +1 -1
  52. data/spec/rubocop/cop/lint/void_spec.rb +0 -1
  53. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +2 -2
  54. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  55. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +196 -215
  56. data/spec/rubocop/cop/style/case_indentation_spec.rb +4 -4
  57. data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
  58. data/spec/rubocop/cop/style/else_alignment_spec.rb +63 -4
  59. data/spec/rubocop/cop/style/empty_else_spec.rb +100 -0
  60. data/spec/rubocop/cop/style/empty_lines_around_block_body_spec.rb +103 -0
  61. data/spec/rubocop/cop/style/empty_lines_around_method_body_spec.rb +1 -1
  62. data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
  63. data/spec/rubocop/cop/style/extra_spacing_spec.rb +68 -0
  64. data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
  65. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +8 -0
  66. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +35 -2
  67. data/spec/rubocop/cop/style/negated_if_spec.rb +1 -1
  68. data/spec/rubocop/cop/style/negated_while_spec.rb +5 -3
  69. data/spec/rubocop/cop/style/percent_q_literals_spec.rb +1 -1
  70. data/spec/rubocop/cop/style/perl_backrefs_spec.rb +5 -0
  71. data/spec/rubocop/cop/style/signal_exception_spec.rb +0 -1
  72. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +13 -1
  73. data/spec/rubocop/cop/style/special_global_vars_spec.rb +5 -0
  74. data/spec/rubocop/cop/style/string_literals_spec.rb +11 -1
  75. data/spec/rubocop/cop/style/symbol_proc_spec.rb +20 -1
  76. data/spec/rubocop/cop/style/tab_spec.rb +2 -2
  77. data/spec/rubocop/cop/style/trailing_comma_spec.rb +1 -1
  78. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +5 -0
  79. data/spec/rubocop/cop/team_spec.rb +2 -2
  80. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  81. data/spec/rubocop/options_spec.rb +2 -0
  82. data/spec/rubocop/target_finder_spec.rb +23 -2
  83. metadata +12 -2
@@ -181,7 +181,7 @@ module RuboCop
181
181
  node
182
182
  end
183
183
 
184
- # Range#size is not avaialable prior to Ruby 2.0.
184
+ # Range#size is not available prior to Ruby 2.0.
185
185
  def numeric_range_size(range)
186
186
  size = range.end - range.begin
187
187
  size += 1 unless range.exclude_end?
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # This force provides a way to track local variables and scopes of Ruby.
6
- # Cops intertact with this force need to override some of the hook methods.
6
+ # Cops interact with this force need to override some of the hook methods.
7
7
  #
8
8
  # def before_entering_scope(scope, variable_table)
9
9
  # end
@@ -31,7 +31,7 @@ module RuboCop
31
31
  ARGUMENT_DECLARATION_TYPES = [
32
32
  :arg, :optarg, :restarg,
33
33
  :kwarg, :kwoptarg, :kwrestarg,
34
- :blockarg, # This doen't mean block argument, it's block-pass (&block).
34
+ :blockarg, # This doesn't mean block argument, it's block-pass (&block).
35
35
  :shadowarg # This means block local variable (obj.each { |arg; this| }).
36
36
  ].freeze
37
37
 
@@ -290,7 +290,7 @@ module RuboCop
290
290
 
291
291
  referenced_variable_names_in_loop.each do |name|
292
292
  variable = variable_table.find_variable(name)
293
- # Non related references which are catched in the above scan
293
+ # Non related references which are caught in the above scan
294
294
  # would be skipped here.
295
295
  next unless variable
296
296
  variable.assignments.each do |assignment|
@@ -33,10 +33,10 @@ module RuboCop
33
33
  end
34
34
 
35
35
  def name
36
+ # TODO: Add an else clause
36
37
  case @node.type
37
38
  when :def then @node.children[0]
38
39
  when :defs then @node.children[1]
39
- else nil # TODO
40
40
  end
41
41
  end
42
42
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  class VariableForce
6
- # A Variable represents existance of a local variable.
6
+ # A Variable represents existence of a local variable.
7
7
  # This holds a variable declaration node,
8
8
  # and some states of the variable.
9
9
  class Variable
@@ -57,7 +57,7 @@ module RuboCop
57
57
  end
58
58
  end
59
59
 
60
- # This class privides helper methods used in the ERB template.
60
+ # This class provides helper methods used in the ERB template.
61
61
  class ERBContext
62
62
  include PathUtil, TextUtil
63
63
 
@@ -7,6 +7,8 @@ module RuboCop
7
7
  module OptionsHelp
8
8
  TEXT = {
9
9
  only: 'Run only the given cop(s).',
10
+ only_guide_cops: ['Run only cops for rules that link to a',
11
+ 'style guide.'],
10
12
  require: 'Require Ruby file.',
11
13
  config: 'Specify configuration file.',
12
14
  auto_gen_config: ['Generate a configuration file acting as a',
@@ -76,12 +78,7 @@ module RuboCop
76
78
  OptionParser.new do |opts|
77
79
  opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
78
80
 
79
- option(opts, '--only [COP1,COP2,...]') do |list|
80
- @options[:only] = list.split(',').map do |c|
81
- Cop::Cop.qualified_cop_name(c, '--only option')
82
- end
83
- end
84
-
81
+ add_only_options(opts)
85
82
  add_configuration_options(opts, args)
86
83
  add_formatting_options(opts)
87
84
 
@@ -98,6 +95,16 @@ module RuboCop
98
95
  fail ArgumentError, "Incompatible cli options: #{incompat.inspect}"
99
96
  end
100
97
 
98
+ def add_only_options(opts)
99
+ option(opts, '--only [COP1,COP2,...]') do |list|
100
+ @options[:only] = list.split(',').map do |c|
101
+ Cop::Cop.qualified_cop_name(c, '--only option')
102
+ end
103
+ end
104
+
105
+ option(opts, '--only-guide-cops')
106
+ end
107
+
101
108
  def add_configuration_options(opts, args)
102
109
  option(opts, '-c', '--config FILE')
103
110
 
@@ -75,7 +75,6 @@ module RuboCop
75
75
 
76
76
  def setup_subtasks(name)
77
77
  namespace name do
78
-
79
78
  desc 'Auto-correct RuboCop offenses'
80
79
 
81
80
  task :auto_correct do
@@ -106,17 +106,26 @@ module RuboCop
106
106
  @options[:only].include?(c.cop_name) || @options[:lint] && c.lint?
107
107
  end
108
108
  else
109
- # filter out Rails cops unless requested
110
- cop_classes.reject!(&:rails?) unless run_rails_cops?(config)
111
-
112
- # select only lint cops when --lint is passed
113
- cop_classes.select!(&:lint?) if @options[:lint]
109
+ filter_cop_classes(cop_classes, config)
114
110
  end
115
111
 
116
112
  cop_classes
117
113
  end
118
114
  end
119
115
 
116
+ def filter_cop_classes(cop_classes, config)
117
+ # use only cops that link to a style guide if requested
118
+ if style_guide_cops_only?(config)
119
+ cop_classes.select! { |cop| config.for_cop(cop)['StyleGuide'] }
120
+ end
121
+
122
+ # filter out Rails cops unless requested
123
+ cop_classes.reject!(&:rails?) unless run_rails_cops?(config)
124
+
125
+ # select only lint cops when --lint is passed
126
+ cop_classes.select!(&:lint?) if @options[:lint]
127
+ end
128
+
120
129
  def validate_only_option
121
130
  @options[:only].each do |cop_to_run|
122
131
  next unless Cop::Cop.all.none? { |c| c.cop_name == cop_to_run }
@@ -128,6 +137,10 @@ module RuboCop
128
137
  @options[:rails] || config['AllCops']['RunRailsCops']
129
138
  end
130
139
 
140
+ def style_guide_cops_only?(config)
141
+ @options[:only_guide_cops] || config['AllCops']['StyleGuideCopsOnly']
142
+ end
143
+
131
144
  def formatter_set
132
145
  @formatter_set ||= begin
133
146
  set = Formatter::FormatterSet.new
@@ -144,7 +157,7 @@ module RuboCop
144
157
  end
145
158
 
146
159
  def considered_failure?(offense)
147
- offense.severity >= minimum_severity_to_fail
160
+ !offense.corrected? && offense.severity >= minimum_severity_to_fail
148
161
  end
149
162
 
150
163
  def minimum_severity_to_fail
@@ -80,8 +80,39 @@ module RuboCop
80
80
  base_dir_config.file_to_include?(file)
81
81
  end
82
82
 
83
+ # Search for files recursively starting at the given base directory using
84
+ # the given flags that determine how the match is made. Excluded files will
85
+ # be removed later by the caller, but as an optimization find_files removes
86
+ # the top level directories that are excluded in configuration in the
87
+ # normal way (dir/**/*).
83
88
  def find_files(base_dir, flags)
84
- Dir.glob("#{base_dir}/**/*", flags).select { |path| FileTest.file?(path) }
89
+ wanted_toplevel_dirs = toplevel_dirs(base_dir, flags) -
90
+ excluded_dirs(base_dir)
91
+
92
+ pattern = if wanted_toplevel_dirs.empty?
93
+ # We need this special case to avoid creating the pattern
94
+ # /**/* which searches the entire file system.
95
+ "#{base_dir}/**/*"
96
+ else
97
+ # Search the non-excluded top directories, but also add files
98
+ # on the top level, which would otherwise not be found.
99
+ "{#{base_dir}/*,{#{wanted_toplevel_dirs.join(',')}}/**/*}"
100
+ end
101
+ Dir.glob(pattern, flags).select { |path| FileTest.file?(path) }
102
+ end
103
+
104
+ def toplevel_dirs(base_dir, flags)
105
+ Dir.glob(File.join(base_dir, '*'), flags).select do |dir|
106
+ File.directory?(dir) && !(dir.end_with?('/.') || dir.end_with?('/..'))
107
+ end
108
+ end
109
+
110
+ def excluded_dirs(base_dir)
111
+ all_cops_config = @config_store.for(base_dir)['AllCops']
112
+ dir_tree_excludes = all_cops_config['Exclude'].select do |pattern|
113
+ pattern.is_a?(String) && pattern.end_with?('/**/*')
114
+ end
115
+ dir_tree_excludes.map { |pattern| pattern.sub(%r{/\*\*/\*$}, '') }
85
116
  end
86
117
 
87
118
  def ruby_executable?(file)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.27.1'
6
+ STRING = '0.28.0'
7
7
 
8
8
  MSG = '%s (using Parser %s, running on %s %s %s)'
9
9
 
@@ -0,0 +1,90 @@
1
+ ### New features
2
+
3
+ * New cop `ExtraSpacing` points out unnecessary spacing in files. ([@blainesch][])
4
+ * New cop `EmptyLinesAroundBlockBody` provides same functionality as the EmptyLinesAround(Class|Method|Module)Body but for blocks. ([@jcarbo][])
5
+ * New cop `Style/EmptyElse` checks for empty `else`-clauses. ([@Koronen][])
6
+ * [#1454](https://github.com/bbatsov/rubocop/issues/1454): New `--only-guide-cops` and `AllCops/StyleGuideCopsOnly` options that will only enforce cops that link to a style guide. ([@marxarelli][])
7
+
8
+ ### Changes
9
+
10
+ * [#801](https://github.com/bbatsov/rubocop/issues/801): New style `context_dependent` for `Style/BracesAroundHashParameters` looks at preceding parameter to determine if braces should be used for final parameter. ([@jonas054][])
11
+ * [#1427](https://github.com/bbatsov/rubocop/issues/1427): Excluding directories on the top level is now done earlier, so that these file trees are not searched, thus saving time when inspecting projects with many excluded files. ([@jonas054][])
12
+ * [#1325](https://github.com/bbatsov/rubocop/issues/1325): When running with `--auto-correct`, only offenses *that can not be corrected* will result in a non-zero exit code. ([@jonas054][])
13
+ * [#1445](https://github.com/bbatsov/rubocop/issues/1445): Allow sprockets directive comments (starting with `#=`) in `Style/LeadingCommentSpace`. ([@bbatsov][])
14
+
15
+ ### Bugs fixed
16
+
17
+ * Fix `%W[]` auto corrected to `%w(]`. ([@toy][])
18
+ * Fix Style/ElseAlignment Cop to find the right parent on def/rescue/else/ensure/end. ([@oneamtu][])
19
+ * [#1181](https://github.com/bbatsov/rubocop/issues/1181): *(fix again)* `Style/StringLiterals` cop stays away from strings inside interpolated expressions. ([@jonas054][])
20
+ * [#1441](https://github.com/bbatsov/rubocop/issues/1441): Correct the logic used by `Style/Blocks` and other cops to determine if an auto-correction would alter the meaning of the code. ([@jonas054][])
21
+ * [#1449](https://github.com/bbatsov/rubocop/issues/1449): Handle the case in `MultilineOperationIndentation` where instances of both correct style and unrecognized (plain wrong) style are detected during an `--auto-gen-config` run. ([@jonas054][])
22
+ * [#1456](https://github.com/bbatsov/rubocop/pull/1456): Fix autocorrect in `SymbolProc` when there are multiple offenses on the same line. ([@jcarbo][])
23
+ * [#1459](https://github.com/bbatsov/rubocop/issues/1459): Handle parenthesis around the condition in `--auto-correct` for `NegatedWhile`. ([@jonas054][])
24
+ * [#1465](https://github.com/bbatsov/rubocop/issues/1465): Fix autocorrect of code like `#$1` in `PerlBackrefs`. ([@bbatsov][])
25
+ * Fix autocorrect of code like `#$:` in `SpecialGlobalVars`. ([@bbatsov][])
26
+ * [#1466](https://github.com/bbatsov/rubocop/issues/1466): Allow leading underscore for unused parameters in `SingleLineBlockParams`. ([@jonas054][])
27
+ * [#1470](https://github.com/bbatsov/rubocop/issues/1470): Handle `elsif` + `else` in `ElseAlignment`. ([@jonas054][])
28
+ * [#1474](https://github.com/bbatsov/rubocop/issues/1474): Multiline string with both `<<` and `\` caught by `Style/LineEndConcatenation` cop. ([@katieschilling][])
29
+ * [#1485](https://github.com/bbatsov/rubocop/issues/1485): Ignore procs in `SymbolProc`. ([@bbatsov][])
30
+ * [#1473](https://github.com/bbatsov/rubocop/issues/1473): `Style/MultilineOperationIndentation` doesn't recognize assignment to array/hash element. ([@jonas054][])
31
+
32
+ [@bbatsov]: https://github.com/bbatsov
33
+ [@jonas054]: https://github.com/jonas054
34
+ [@yujinakayama]: https://github.com/yujinakayama
35
+ [@dblock]: https://github.com/dblock
36
+ [@nevir]: https://github.com/nevir
37
+ [@daviddavis]: https://github.com/daviddavis
38
+ [@sds]: https://github.com/sds
39
+ [@fancyremarker]: https://github.com/fancyremarker
40
+ [@sinisterchipmunk]: https://github.com/sinisterchipmunk
41
+ [@vonTronje]: https://github.com/vonTronje
42
+ [@agrimm]: https://github.com/agrimm
43
+ [@pmenglund]: https://github.com/pmenglund
44
+ [@chulkilee]: https://github.com/chulkilee
45
+ [@codez]: https://github.com/codez
46
+ [@emou]: https://github.com/emou
47
+ [@skanev]: http://github.com/skanev
48
+ [@claco]: http://github.com/claco
49
+ [@rifraf]: http://github.com/rifraf
50
+ [@scottmatthewman]: https://github.com/scottmatthewman
51
+ [@ma2gedev]: http://github.com/ma2gedev
52
+ [@jeremyolliver]: https://github.com/jeremyolliver
53
+ [@hannestyden]: https://github.com/hannestyden
54
+ [@geniou]: https://github.com/geniou
55
+ [@jkogara]: https://github.com/jkogara
56
+ [@tmorris-fiksu]: https://github.com/tmorris-fiksu
57
+ [@mockdeep]: https://github.com/mockdeep
58
+ [@hiroponz]: https://github.com/hiroponz
59
+ [@tamird]: https://github.com/tamird
60
+ [@fshowalter]: https://github.com/fshowalter
61
+ [@cschramm]: https://github.com/cschramm
62
+ [@bquorning]: https://github.com/bquorning
63
+ [@bcobb]: https://github.com/bcobb
64
+ [@irrationalfab]: https://github.com/irrationalfab
65
+ [@tommeier]: https://github.com/tommeier
66
+ [@sfeldon]: https://github.com/sfeldon
67
+ [@biinari]: https://github.com/biinari
68
+ [@barunio]: https://github.com/barunio
69
+ [@molawson]: https://github.com/molawson
70
+ [@wndhydrnt]: https://github.com/wndhydrnt
71
+ [@ggilder]: https://github.com/ggilder
72
+ [@salbertson]: https://github.com/salbertson
73
+ [@camilleldn]: https://github.com/camilleldn
74
+ [@mcls]: https://github.com/mcls
75
+ [@yous]: https://github.com/yous
76
+ [@vrthra]: https://github.com/vrthra
77
+ [@SkuliOskarsson]: https://github.com/SkuliOskarsson
78
+ [@jspanjers]: https://github.com/jspanjers
79
+ [@sch1zo]: https://github.com/sch1zo
80
+ [@smangelsdorf]: https://github.com/smangelsdorf
81
+ [@mvz]: https://github.com/mvz
82
+ [@jfelchner]: https://github.com/jfelchner
83
+ [@janraasch]: https://github.com/janraasch
84
+ [@jcarbo]: https://github.com/jcarbo
85
+ [@oneamtu]: https://github.com/oneamtu
86
+ [@toy]: https://github.com/toy
87
+ [@Koronen]: https://github.com/Koronen
88
+ [@blainesch]: https://github.com/blainesch
89
+ [@marxarelli]: https://github.com/marxarelli
90
+ [@katieschilling]: https://github.com/katieschilling
@@ -96,6 +96,7 @@ describe 'RuboCop Project' do
96
96
  let(:bodies) do
97
97
  entries.map do |entry|
98
98
  entry
99
+ .gsub(/`[^`]+`/, '``')
99
100
  .sub(/^\*\s*(?:\[.+?\):\s*)?/, '')
100
101
  .sub(/\s*\([^\)]+\)$/, '')
101
102
  end
@@ -35,12 +35,16 @@ describe RuboCop::CLI, :isolated_environment do
35
35
  end
36
36
 
37
37
  describe '--auto-correct' do
38
- it 'corrects SymbolProc and SpaceBeforeBlockBraces offences' do
38
+ it 'corrects SymbolProc and SpaceBeforeBlockBraces offenses' do
39
39
  source = ['foo.map{ |a| a.nil? }']
40
40
  create_file('example.rb', source)
41
- expect(cli.run(['-D', '--auto-correct'])).to eq(1)
41
+ expect(cli.run(['-D', '--auto-correct'])).to eq(0)
42
42
  corrected = "foo.map(&:nil?)\n"
43
43
  expect(IO.read('example.rb')).to eq(corrected)
44
+ uncorrected = $stdout.string.split($RS).select do |line|
45
+ line.include?('example.rb:') && !line.include?('[Corrected]')
46
+ end
47
+ expect(uncorrected).to be_empty # Hence exit code 0.
44
48
  end
45
49
 
46
50
  it 'corrects complicated cases conservatively' do
@@ -48,7 +52,7 @@ describe RuboCop::CLI, :isolated_environment do
48
52
  # Style/AlignHash. Because they make minimal corrections relating only
49
53
  # to their specific areas, and stay away from cleaning up extra
50
54
  # whitespace in the process, the combined changes don't interfere with
51
- # eachother and the result is semantically the same as the starting
55
+ # each other and the result is semantically the same as the starting
52
56
  # point.
53
57
  source = ['# encoding: utf-8',
54
58
  'expect(subject[:address]).to eq({',
@@ -59,7 +63,7 @@ describe RuboCop::CLI, :isolated_environment do
59
63
  " postal_code: '99999-1111'",
60
64
  '})']
61
65
  create_file('example.rb', source)
62
- expect(cli.run(['-D', '--auto-correct'])).to eq(1)
66
+ expect(cli.run(['-D', '--auto-correct'])).to eq(0)
63
67
  corrected =
64
68
  ['# encoding: utf-8',
65
69
  'expect(subject[:address]).to eq(',
@@ -180,7 +184,7 @@ describe RuboCop::CLI, :isolated_environment do
180
184
  '(1..10).each{ |i|',
181
185
  ' puts i',
182
186
  '}'])
183
- expect(cli.run(['--auto-correct'])).to eq(1)
187
+ expect(cli.run(['--auto-correct'])).to eq(0)
184
188
  expect(IO.read('example.rb'))
185
189
  .to eq(['# encoding: utf-8',
186
190
  '(1..10).each do |i|',
@@ -193,7 +197,7 @@ describe RuboCop::CLI, :isolated_environment do
193
197
  create_file('example.rb',
194
198
  ['# encoding: utf-8',
195
199
  "assert_post_status_code 400, 's', {:type => 'bad'}"])
196
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(1)
200
+ expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
197
201
  expect(IO.read('example.rb'))
198
202
  .to eq(['# encoding: utf-8',
199
203
  "assert_post_status_code 400, 's', type: 'bad'",
@@ -292,13 +296,17 @@ describe RuboCop::CLI, :isolated_environment do
292
296
  expect(IO.read('example.rb')).to eq(['module A module B',
293
297
  'end end',
294
298
  ''].join("\n"))
299
+ uncorrected = $stdout.string.split($RS).select do |line|
300
+ line.include?('example.rb:') && !line.include?('[Corrected]')
301
+ end
302
+ expect(uncorrected).not_to be_empty # Hence exit code 1.
295
303
  end
296
304
 
297
305
  it 'can correct single line methods' do
298
306
  create_file('example.rb', ['# encoding: utf-8',
299
307
  'def func1; do_something end # comment',
300
308
  'def func2() do_1; do_2; end'])
301
- expect(cli.run(%w(--auto-correct --format offenses))).to eq(1)
309
+ expect(cli.run(%w(--auto-correct --format offenses))).to eq(0)
302
310
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
303
311
  '# comment',
304
312
  'def func1',
@@ -330,7 +338,7 @@ describe RuboCop::CLI, :isolated_environment do
330
338
  ['# encoding: utf-8',
331
339
  'raise NotImplementedError,',
332
340
  " 'Method should be overridden in child classes'"])
333
- expect(cli.run(['--auto-correct'])).to eq(1)
341
+ expect(cli.run(['--auto-correct'])).to eq(0)
334
342
  expect(IO.read('example.rb'))
335
343
  .to eq(['# encoding: utf-8',
336
344
  'fail NotImplementedError,',
@@ -367,7 +375,7 @@ describe RuboCop::CLI, :isolated_environment do
367
375
  ' def f',
368
376
  ' end',
369
377
  'end'])
370
- expect(cli.run(['--auto-correct'])).to eq(1)
378
+ expect(cli.run(['--auto-correct'])).to eq(0)
371
379
  expect(IO.read('example.rb'))
372
380
  .to eq(['# encoding: utf-8',
373
381
  '# Example class.',
@@ -399,7 +407,7 @@ describe RuboCop::CLI, :isolated_environment do
399
407
  'def primes limit',
400
408
  ' 1.upto(limit).select { |i| i.even? }',
401
409
  'end'])
402
- expect(cli.run(%w(-D --auto-correct))).to eq(1)
410
+ expect(cli.run(%w(-D --auto-correct))).to eq(0)
403
411
  expect($stderr.string).to eq('')
404
412
  expect(IO.read('example.rb'))
405
413
  .to eq(['# encoding: utf-8',
@@ -433,7 +441,7 @@ describe RuboCop::CLI, :isolated_environment do
433
441
  ['# encoding: utf-8',
434
442
  "f(type: ['offline','offline_payment'],",
435
443
  " bar_colors: ['958c12','953579','ff5800','0085cc'])"])
436
- expect(cli.run(%w(-D --auto-correct --format o))).to eq(1)
444
+ expect(cli.run(%w(-D --auto-correct --format o))).to eq(0)
437
445
  expect($stdout.string)
438
446
  .to eq(['',
439
447
  '4 Style/SpaceAfterComma',
@@ -453,7 +461,7 @@ describe RuboCop::CLI, :isolated_environment do
453
461
  create_file('example.rb',
454
462
  ['# encoding: utf-8',
455
463
  "I18n.t('description',:property_name => property.name)"])
456
- expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(1)
464
+ expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(0)
457
465
  expect($stdout.string)
458
466
  .to eq(["#{abs('example.rb')}:2:21: C: [Corrected] " \
459
467
  'Style/SpaceAfterComma: Space missing after comma.',
@@ -470,7 +478,7 @@ describe RuboCop::CLI, :isolated_environment do
470
478
  create_file('example.rb',
471
479
  ['# encoding: utf-8',
472
480
  '{ :b=>1 }'])
473
- expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(1)
481
+ expect(cli.run(%w(-D --auto-correct --format emacs))).to eq(0)
474
482
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
475
483
  '{ b: 1 }',
476
484
  ''].join("\n"))
@@ -488,7 +496,7 @@ describe RuboCop::CLI, :isolated_environment do
488
496
  ['# encoding: utf-8',
489
497
  '{ :b=>1 }'])
490
498
  expect(cli.run(%w(--auto-correct -f emacs
491
- --only Style/HashSyntax))).to eq(1)
499
+ --only Style/HashSyntax))).to eq(0)
492
500
  expect($stderr.string).to eq('')
493
501
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
494
502
  '{ b: 1 }',
@@ -506,7 +514,7 @@ describe RuboCop::CLI, :isolated_environment do
506
514
  ' ',
507
515
  '',
508
516
  ''])
509
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(1)
517
+ expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
510
518
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
511
519
  ''].join("\n"))
512
520
  expect($stdout.string)
@@ -521,7 +529,7 @@ describe RuboCop::CLI, :isolated_environment do
521
529
  create_file('example.rb',
522
530
  ['# encoding: utf-8',
523
531
  'Hash.new()'])
524
- expect(cli.run(%w(--auto-correct --format emacs))).to eq(1)
532
+ expect(cli.run(%w(--auto-correct --format emacs))).to eq(0)
525
533
  expect($stderr.string).to eq('')
526
534
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
527
535
  '{}',
@@ -546,7 +554,7 @@ describe RuboCop::CLI, :isolated_environment do
546
554
  create_file('.rubocop.yml',
547
555
  ['Style/AlignHash:',
548
556
  ' EnforcedColonStyle: separator'])
549
- expect(cli.run(%w(--auto-correct))).to eq(1)
557
+ expect(cli.run(%w(--auto-correct))).to eq(0)
550
558
  expect(IO.read('example.rb'))
551
559
  .to eq(['# encoding: utf-8',
552
560
  'CONVERSION_CORRESPONDENCE = {',
@@ -586,7 +594,7 @@ describe RuboCop::CLI, :isolated_environment do
586
594
  ['# encoding: utf-8',
587
595
  'some_method(a, )'])
588
596
  Timeout.timeout(10) do
589
- expect(cli.run(%w(--auto-correct))).to eq(1)
597
+ expect(cli.run(%w(--auto-correct))).to eq(0)
590
598
  end
591
599
  expect($stderr.string).to eq('')
592
600
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
@@ -599,7 +607,7 @@ describe RuboCop::CLI, :isolated_environment do
599
607
  ['# encoding: utf-8',
600
608
  'puts [1, ]'])
601
609
  Timeout.timeout(10) do
602
- expect(cli.run(%w(--auto-correct))).to eq(1)
610
+ expect(cli.run(%w(--auto-correct))).to eq(0)
603
611
  end
604
612
  expect($stderr.string).to eq('')
605
613
  expect(IO.read('example.rb')).to eq(['# encoding: utf-8',
@@ -1386,7 +1394,6 @@ describe RuboCop::CLI, :isolated_environment do
1386
1394
  create_file('.rubocop.yml', ['AllCops:',
1387
1395
  ' Exclude:',
1388
1396
  " - #{target_file}"])
1389
-
1390
1397
  end
1391
1398
 
1392
1399
  it 'excludes files specified in the configuration Exclude ' \
@@ -1640,6 +1647,91 @@ describe RuboCop::CLI, :isolated_environment do
1640
1647
  .to eq(['', '1 file inspected, no offenses detected', ''].join("\n"))
1641
1648
  end
1642
1649
 
1650
+ describe 'style guide only usage' do
1651
+ context 'via the cli option' do
1652
+ describe '--only-guide-cops' do
1653
+ it 'skips cops that have no link to a style guide' do
1654
+ create_file('example.rb', 'fail')
1655
+ create_file('.rubocop.yml', ['Metrics/LineLength:',
1656
+ ' Enabled: true',
1657
+ ' StyleGuide: ~',
1658
+ ' Max: 2'])
1659
+
1660
+ expect(cli.run(['--format', 'simple', '--only-guide-cops',
1661
+ 'example.rb'])).to eq(0)
1662
+ end
1663
+
1664
+ it 'runs cops for rules that link to a style guide' do
1665
+ create_file('example.rb', 'fail')
1666
+ create_file('.rubocop.yml', ['Metrics/LineLength:',
1667
+ ' Enabled: true',
1668
+ ' StyleGuide: "http://an.example/url"',
1669
+ ' Max: 2'])
1670
+
1671
+ expect(cli.run(['--format', 'simple', '--only-guide-cops',
1672
+ 'example.rb'])).to eq(1)
1673
+
1674
+ expect($stdout.string)
1675
+ .to eq(['== example.rb ==',
1676
+ 'C: 1: 3: Line is too long. [4/2]',
1677
+ '',
1678
+ '1 file inspected, 1 offense detected',
1679
+ ''].join("\n"))
1680
+ end
1681
+
1682
+ it 'overrides configuration of AllCops/StyleGuideCopsOnly' do
1683
+ create_file('example.rb', 'fail')
1684
+ create_file('.rubocop.yml', ['AllCops:',
1685
+ ' StyleGuideCopsOnly: false',
1686
+ 'Metrics/LineLength:',
1687
+ ' Enabled: true',
1688
+ ' StyleGuide: ~',
1689
+ ' Max: 2'])
1690
+
1691
+ expect(cli.run(['--format', 'simple', '--only-guide-cops',
1692
+ 'example.rb'])).to eq(0)
1693
+ end
1694
+ end
1695
+ end
1696
+
1697
+ context 'via the config' do
1698
+ before do
1699
+ create_file('example.rb', 'fail')
1700
+ create_file('.rubocop.yml', ['AllCops:',
1701
+ " StyleGuideCopsOnly: #{guide_cops_only}",
1702
+ 'Metrics/LineLength:',
1703
+ ' Enabled: true',
1704
+ ' StyleGuide: ~',
1705
+ ' Max: 2'])
1706
+ end
1707
+
1708
+ describe 'AllCops/StyleGuideCopsOnly' do
1709
+ context 'when it is true' do
1710
+ let(:guide_cops_only) { 'true' }
1711
+
1712
+ it 'skips cops that have no link to a style guide' do
1713
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(0)
1714
+ end
1715
+ end
1716
+
1717
+ context 'when it is false' do
1718
+ let(:guide_cops_only) { 'false' }
1719
+
1720
+ it 'runs cops for rules regardless of any link to the style guide' do
1721
+ expect(cli.run(['--format', 'simple', 'example.rb'])).to eq(1)
1722
+
1723
+ expect($stdout.string)
1724
+ .to eq(['== example.rb ==',
1725
+ 'C: 1: 3: Line is too long. [4/2]',
1726
+ '',
1727
+ '1 file inspected, 1 offense detected',
1728
+ ''].join("\n"))
1729
+ end
1730
+ end
1731
+ end
1732
+ end
1733
+ end
1734
+
1643
1735
  describe 'rails cops' do
1644
1736
  describe 'enabling/disabling' do
1645
1737
  it 'by default does not run rails cops' do
@@ -1657,7 +1749,7 @@ describe RuboCop::CLI, :isolated_environment do
1657
1749
  expect($stdout.string).to include('Prefer self[:attr]')
1658
1750
  end
1659
1751
 
1660
- it 'with configation option true in one dir runs rails cops there' do
1752
+ it 'with configuration option true in one dir runs rails cops there' do
1661
1753
  source = ['# encoding: utf-8',
1662
1754
  'read_attribute(:test)']
1663
1755
  create_file('dir1/app/models/example1.rb', source)
@@ -1684,7 +1776,7 @@ describe RuboCop::CLI, :isolated_environment do
1684
1776
  ''].join("\n"))
1685
1777
  end
1686
1778
 
1687
- it 'with configation option false but -R given runs rails cops' do
1779
+ it 'with configuration option false but -R given runs rails cops' do
1688
1780
  create_file('app/models/example1.rb', ['# encoding: utf-8',
1689
1781
  'read_attribute(:test)'])
1690
1782
  create_file('.rubocop.yml', ['AllCops:',
@@ -1759,7 +1851,6 @@ describe RuboCop::CLI, :isolated_environment do
1759
1851
  .to eq(['', '4 files inspected, no offenses detected',
1760
1852
  ''].join("\n"))
1761
1853
  end
1762
-
1763
1854
  end
1764
1855
 
1765
1856
  describe 'configuration from file' do
@@ -1864,7 +1955,7 @@ describe RuboCop::CLI, :isolated_environment do
1864
1955
  ''].join("\n"))
1865
1956
  end
1866
1957
 
1867
- it 'matches included/excluded files corectly when . argument is given' do
1958
+ it 'matches included/excluded files correctly when . argument is given' do
1868
1959
  create_file('example.rb', 'x = 0')
1869
1960
  create_file('special.dsl', ['# encoding: utf-8',
1870
1961
  'setup { "stuff" }'