Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10977] Add option to raise cop errors, --raise-cop-error #11001

Merged
merged 2 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_add_option_to_raise_cop_errors.md
@@ -0,0 +1 @@
* [#11001](https://github.com/rubocop/rubocop/pull/11001): Add option to raise cop errors `--raise-cop-error`. ([@wildmaples][])
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/cops.adoc
Expand Up @@ -10,6 +10,9 @@ enforce different coding conventions.

You can also load xref:extensions.adoc#custom-cops[custom cops].

Cop-related failures are silenced by default but can be turned on using the
`--raise-cop-errors` option.

== Style

Style cops check for stylistic consistency of your code. Many of the them are
Expand Down
3 changes: 3 additions & 0 deletions docs/modules/ROOT/pages/usage/basic_usage.adoc
Expand Up @@ -240,6 +240,9 @@ $ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName
| `--[no-]parallel`
| Use available CPUs to execute inspection in parallel. Default is parallel.

| `--raise-cop-error`
| Raise cop-related errors with cause and location. This is used to prevent cops from failing silently. Default is false.

| `-r/--require`
| Require Ruby file (see xref:extensions.adoc#loading-extensions[Loading Extensions]).

Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/commissioner.rb
Expand Up @@ -159,9 +159,11 @@ def invoke(callback, cops, *args)
def with_cop_error_handling(cop, node = nil)
yield
rescue StandardError => e
raise e if @options[:raise_error]
raise e if @options[:raise_error] # For internal testing

err = ErrorWithAnalyzedFileLocation.new(cause: e, node: node, cop: cop)
raise err if @options[:raise_cop_error] # From user-input option

@errors << err
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/options.rb
Expand Up @@ -65,7 +65,7 @@ def define_options
end

def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
section(opts, 'Basic Options') do
section(opts, 'Basic Options') do # rubocop:disable Metrics/BlockLength
option(opts, '-l', '--lint') do
@options[:only] ||= []
@options[:only] << 'Lint'
Expand All @@ -90,6 +90,7 @@ def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLen
option(opts, '--force-default-config')
option(opts, '-s', '--stdin FILE')
option(opts, '-P', '--[no-]parallel')
option(opts, '--raise-cop-error')
add_severity_option(opts)
end
end
Expand Down Expand Up @@ -589,7 +590,10 @@ module OptionsHelp
restart_server: 'Restart server process.',
start_server: 'Start server process.',
stop_server: 'Stop server process.',
server_status: 'Show server status.'
server_status: 'Show server status.',
raise_cop_error: ['Raise cop-related errors with cause and location.',
'This is used to prevent cops from failing silently.',
'Default is false.']
}.freeze
end
end
10 changes: 10 additions & 0 deletions spec/rubocop/cop/commissioner_spec.rb
Expand Up @@ -114,6 +114,16 @@ def method
end
end

context 'when passed :raise_cop_error option' do
let(:options) { { raise_cop_error: true } }

it 're-raises the exception received while processing' do
allow(cop).to receive(:on_int) { raise RuboCop::ErrorWithAnalyzedFileLocation }

expect { offenses }.to raise_error(RuboCop::ErrorWithAnalyzedFileLocation)
end
end

context 'when given a force' do
let(:force) { instance_double(RuboCop::Cop::Force).as_null_object }
let(:forces) { [force] }
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/options_spec.rb
Expand Up @@ -66,6 +66,9 @@ def abs(path)
reports. This is useful for editor integration.
-P, --[no-]parallel Use available CPUs to execute inspection in
parallel. Default is true.
--raise-cop-error Raise cop-related errors with cause and location.
This is used to prevent cops from failing silently.
Default is false.
--fail-level SEVERITY Minimum severity for exit with error code.
[A] autocorrect
[I] info
Expand Down Expand Up @@ -375,6 +378,13 @@ def abs(path)
end
end

describe '--raise-cop-error' do
it 'raises cop errors' do
results = options.parse %w[--raise-cop-error]
expect(results).to eq([{ raise_cop_error: true }, []])
end
end

describe '--require' do
let(:required_file_path) { './path/to/required_file.rb' }

Expand Down