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 an error when running --only or --except options #7905

Merged
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.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 @@ -555,6 +555,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 @@ -674,6 +683,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