Skip to content

Commit

Permalink
Fix an error when running --only or --except options
Browse files Browse the repository at this point in the history
This PR fixes an error when running `rubocop --only` or
`rubocop --except` options without cop name argument.

## Before

```consle
% rubocop --only
undefined method `empty?' for nil:NilClass
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.82.0/lib/rubocop/options.rb:87:in
`block in add_cop_selection_csv_option'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.82.0/lib/rubocop/options.rb:206:in
`block in option'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/optparse.rb:1589:in
`block in parse_in_order'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/2.7.0/optparse.rb:1575:in `catch'
```

## After

```console
% rubocop --only
--only argument should be [COP1,COP2,...].
```

The same is true for `rubocop --except`.
  • Loading branch information
koic authored and bbatsov committed Apr 24, 2020
1 parent 172b8a0 commit 9d51e4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@
* [#7790](https://github.com/rubocop-hq/rubocop/issues/7790): Fix `--parallel` and `--ignore-parent-exclusion` combination. ([@jonas054][])
* [#7881](https://github.com/rubocop-hq/rubocop/issues/7881): Fix `--parallel` and `--force-default-config` combination. ([@jonas054][])
* [#7635](https://github.com/rubocop-hq/rubocop/issues/7635): Fix a false positive for `Style/MultilineWhenThen` when `then` required for a body of `when` is used. ([@koic][])
* [#7905](https://github.com/rubocop-hq/rubocop/pull/7905): Fix an error when running `rubocop --only` or `rubocop --except` options without cop name argument. ([@koic][])

### Changes

Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/options.rb
Expand Up @@ -83,6 +83,12 @@ def add_only_options(opts)

def add_cop_selection_csv_option(option, opts)
option(opts, "--#{option} [COP1,COP2,...]") do |list|
unless list
message = "--#{option} argument should be [COP1,COP2,...]."

raise OptionArgumentError, message
end

@options[:"#{option}"] =
if list.empty?
['']
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -575,6 +575,15 @@ class SomeCop < Cop
RESULT
end
end

context 'when a cop name is not specified' do
it 'displays how to use `--only` option' do
expect(cli.run(%w[--except -a Lint/NumberConverion])).to eq(2)
expect($stderr.string).to eq(<<~MESSAGE)
--except argument should be [COP1,COP2,...].
MESSAGE
end
end
end

describe '--except' do
Expand Down Expand Up @@ -694,6 +703,15 @@ class SomeCop < Cop
end
end
end

context 'when a cop name is not specified' do
it 'displays how to use `--except` option' do
expect(cli.run(%w[--except])).to eq(2)
expect($stderr.string).to eq(<<~MESSAGE)
--except argument should be [COP1,COP2,...].
MESSAGE
end
end
end

describe '--lint' do
Expand Down

0 comments on commit 9d51e4f

Please sign in to comment.