Skip to content

Commit

Permalink
[Fix #5451] When using --auto-gen-config, do not ouput offenses (#5727)
Browse files Browse the repository at this point in the history
This makes it so that --auto-gen-config will not output offenses to the console by default.
  • Loading branch information
drewpterry authored and bbatsov committed Apr 16, 2018
1 parent ec3bdbf commit 9645bea
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
* [#4517](https://github.com/bbatsov/rubocop/issues/4517): Add option to allow trailing whitespaces inside heredoc strings. ([@Darhazer][])
* [#5652](https://github.com/bbatsov/rubocop/issues/5652): Make `Style/OptionHash` aware of implicit parameter passing to super. ([@Wei-LiangChew][])

### Changes

* [#5451](https://github.com/bbatsov/rubocop/issues/5451): When using --auto-gen-config, do not ouput offenses unless the --output-offenses flag is also passed. ([@drewpterry][])

## 0.54.0 (2018-03-21)

### New features
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@
require_relative 'rubocop/formatter/quiet_formatter'
require_relative 'rubocop/formatter/tap_formatter'
require_relative 'rubocop/formatter/worst_offenders_formatter'
# relies on progress formatter
require_relative 'rubocop/formatter/auto_gen_config_formatter'

require_relative 'rubocop/formatter/formatter_set'

Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ def apply_default_formatter
# This must be done after the options have already been processed,
# because they can affect how ConfigStore behaves
@options[:formatters] ||= begin
cfg = @config_store.for(Dir.pwd).for_all_cops
formatter = cfg['DefaultFormatter'] || 'progress'
if @options[:auto_gen_config]
formatter = 'autogenconf'
else
cfg = @config_store.for(Dir.pwd).for_all_cops
formatter = cfg['DefaultFormatter'] || 'progress'
end
[[formatter, @options[:output_path]]]
end

Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/formatter/auto_gen_config_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module RuboCop
module Formatter
# Does not show individual offenses in the console.
class AutoGenConfigFormatter < ProgressFormatter
def finished(inspected_files)
output.puts

report_summary(inspected_files.size,
@total_offense_count,
@total_correction_count)
end
end
end
end
27 changes: 14 additions & 13 deletions lib/rubocop/formatter/formatter_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ module Formatter
# which invoke same method of each formatters.
class FormatterSet < Array
BUILTIN_FORMATTERS_FOR_KEYS = {
'progress' => ProgressFormatter,
'simple' => SimpleTextFormatter,
'clang' => ClangStyleFormatter,
'fuubar' => FuubarStyleFormatter,
'emacs' => EmacsStyleFormatter,
'json' => JSONFormatter,
'html' => HTMLFormatter,
'files' => FileListFormatter,
'offenses' => OffenseCountFormatter,
'disabled' => DisabledLinesFormatter,
'worst' => WorstOffendersFormatter,
'tap' => TapFormatter,
'quiet' => QuietFormatter
'progress' => ProgressFormatter,
'simple' => SimpleTextFormatter,
'clang' => ClangStyleFormatter,
'fuubar' => FuubarStyleFormatter,
'emacs' => EmacsStyleFormatter,
'json' => JSONFormatter,
'html' => HTMLFormatter,
'files' => FileListFormatter,
'offenses' => OffenseCountFormatter,
'disabled' => DisabledLinesFormatter,
'worst' => WorstOffendersFormatter,
'tap' => TapFormatter,
'quiet' => QuietFormatter,
'autogenconf' => AutoGenConfigFormatter
}.freeze

FORMATTER_APIS = %i[started finished].freeze
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ module OptionsHelp
' [w]orst',
' [t]ap',
' [q]uiet',
' [a]utogenconf',
' custom formatter class name'],
out: ['Write output to a file instead of STDOUT.',
'This option applies to the previously',
Expand Down
12 changes: 12 additions & 0 deletions manual/formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ lib/foo.rb:6:5: C: Style/Documentation: Missing top-level class documentation co
26 files inspected, 46 offenses detected
```

### Auto Gen Formatter

Behaves like Progress Formatter except that it will not show any offenses.

```sh
$ rubocop
Inspecting 26 files
..W.C....C..CWCW.C...WC.CC

26 files inspected, 46 offenses detected
```

### Clang Style Formatter

The `clang` formatter displays the offenses in a manner similar to `clang`:
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,24 @@ def a; end
end
end

describe 'console output' do
before do
create_file('example1.rb', ['$!'])
end

it 'displays report summary but no offenses' do
expect(cli.run(['--auto-gen-config'])).to eq(1)

expect($stdout.string).to include(<<-OUTPUT.strip_indent)
Inspecting 1 file
C
1 file inspected, 1 offense detected
Created .rubocop_todo.yml.
OUTPUT
end
end

it 'can be called when there are no files to inspection' do
expect(cli.run(['--auto-gen-config'])).to eq(0)
end
Expand Down
130 changes: 130 additions & 0 deletions spec/rubocop/formatter/auto_gen_config_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Formatter::AutoGenConfigFormatter do
subject(:formatter) { described_class.new(output) }

let(:output) { StringIO.new }

let(:files) do
%w[lib/rubocop.rb spec/spec_helper.rb bin/rubocop].map do |path|
File.expand_path(path)
end
end

describe '#report_file_as_mark' do
before do
formatter.report_file_as_mark(offenses)
end

def offense_with_severity(severity)
source_buffer = Parser::Source::Buffer.new('test', 1)
source_buffer.source = "a\n"
RuboCop::Cop::Offense.new(severity,
Parser::Source::Range.new(source_buffer, 0, 1),
'message',
'CopName')
end

context 'when no offenses are detected' do
let(:offenses) { [] }

it 'prints "."' do
expect(output.string).to eq('.')
end
end

context 'when a refactor severity offense is detected' do
let(:offenses) { [offense_with_severity(:refactor)] }

it 'prints "R"' do
expect(output.string).to eq('R')
end
end

context 'when a refactor convention offense is detected' do
let(:offenses) { [offense_with_severity(:convention)] }

it 'prints "C"' do
expect(output.string).to eq('C')
end
end

context 'when different severity offenses are detected' do
let(:offenses) do
[
offense_with_severity(:refactor),
offense_with_severity(:error)
]
end

it 'prints highest level mark' do
expect(output.string).to eq('E')
end
end
end

describe '#finished' do
before do
formatter.started(files)
end

context 'when any offenses are detected' do
before do
source_buffer = Parser::Source::Buffer.new('test', 1)
source = Array.new(9) do |index|
"This is line #{index + 1}."
end
source_buffer.source = source.join("\n")
line_length = source[0].length + 1

formatter.file_started(files[0], {})
formatter.file_finished(
files[0],
[
RuboCop::Cop::Offense.new(
:convention,
Parser::Source::Range.new(source_buffer,
line_length + 2,
line_length + 3),
'foo',
'Cop'
)
]
)
end

it 'does not report offenses' do
formatter.finished(files)
expect(output.string).not_to include('Offenses:')
end

it 'outputs report summary' do
formatter.finished(files)
expect(output.string).to include <<-OUTPUT.strip_indent
3 files inspected, 1 offense detected
OUTPUT
end
end

context 'when no offenses are detected' do
before do
files.each do |file|
formatter.file_started(file, {})
formatter.file_finished(file, [])
end
end

it 'does not report offenses' do
formatter.finished(files)
expect(output.string).not_to include('Offenses:')
end
end

it 'calls #report_summary' do
formatter.finished(files)
expect(output.string).to include <<-OUTPUT.strip_indent
3 files inspected, no offenses detected
OUTPUT
end
end
end
1 change: 1 addition & 0 deletions spec/rubocop/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def abs(path)
[w]orst
[t]ap
[q]uiet
[a]utogenconf
custom formatter class name
-o, --out FILE Write output to a file instead of STDOUT.
This option applies to the previously
Expand Down

0 comments on commit 9645bea

Please sign in to comment.