Skip to content

Commit

Permalink
[Fix 4245] Add --only-recognized-file-types and change default
Browse files Browse the repository at this point in the history
The default for file names on the command line that are not recognized as a
Ruby file now becomes to inspect the files. With the
--only-recognized-file-types option, the old behavior of skipping the file is
kept.
  • Loading branch information
jonas054 committed May 8, 2020
1 parent b837585 commit 75a6984
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

* [#7860](https://github.com/rubocop-hq/rubocop/issues/7860): Change `AllowInHeredoc` option of `Layout/TrailingWhitespace` to `true` by default. ([@koic][])
* [#7094](https://github.com/rubocop-hq/rubocop/issues/7094): Clarify alignment in `Layout/MultilineOperationIndentation`. ([@jonas054][])
* [#4245](https://github.com/rubocop-hq/rubocop/issues/4245): **(Breaking)** Inspect all files given on command line unless `--only-recognized-file-types` is given. ([@jonas054][])
* [#7390](https://github.com/rubocop-hq/rubocop/issues/7390): **(Breaking)** Enabling a cop overrides disabling its department. ([@jonas054][])

## 0.82.0 (2020-04-16)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def add_cop_selection_csv_option(option, opts)
def add_configuration_options(opts)
option(opts, '-c', '--config FILE')
option(opts, '--force-exclusion')
option(opts, '--only-recognized-file-types')
option(opts, '--ignore-parent-exclusion')
option(opts, '--force-default-config')
add_auto_gen_options(opts)
Expand Down Expand Up @@ -415,6 +416,9 @@ module OptionsHelp
force_exclusion: ['Force excluding files specified in the',
'configuration `Exclude` even if they are',
'explicitly passed as arguments.'],
only_recognized_file_types: ['Inspect files given on the command line only if',
'they are listed in AllCops/Include parameters',
'of user configuration or default configuration.'],
ignore_disable_comments: ['Run cops even when they are disabled locally',
'with a comment.'],
ignore_parent_exclusion: ['Prevent from inheriting AllCops/Exclude from',
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ def warm_cache(target_files)

def find_target_files(paths)
target_finder = TargetFinder.new(@config_store, @options)
target_files = target_finder.find(paths, :only_recognized_file_types)
mode = if @options[:only_recognized_file_types]
:only_recognized_file_types
else
:all_file_types
end
target_files = target_finder.find(paths, mode)
target_files.each(&:freeze).freeze
end

Expand Down
1 change: 1 addition & 0 deletions manual/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Command flag | Description
`-F/--fail-fast` | Inspect files in order of modification time and stops after first file with offenses.
` --fail-level` | Minimum [severity](configuration.md#severity) for exit with error code. Full severity name or upper case initial can be given. Normally, auto-corrected offenses are ignored. Use `A` or `autocorrect` if you'd like them to trigger failure.
` --force-exclusion` | Force excluding files specified in the configuration `Exclude` even if they are explicitly passed as arguments.
` --only-recognized-file-types` | Inspect files given on the command line only if they are listed in `AllCops`/`Include` parameters of user configuration or default configuration.
`-h/--help` | Print usage information.
` --ignore-parent-exclusion` | Ignores all Exclude: settings from all .rubocop.yml files present in parent folders. This is useful when you are importing submodules when you want to test them without being affected by the parent module's rubocop settings.
` --init` | Generate a .rubocop.yml file in the current directory.
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cli/cli_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,41 @@ def f
end
end

describe '--only-recognized-file-types' do
let(:target_file) { 'example.something' }
let(:exit_code) { cli.run(['--only-recognized-file-types', target_file]) }

before do
create_file(target_file, '#' * 90)
end

context 'when explicitly included' do
before do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Include:
- #{target_file}
YAML
end

it 'includes the file given on the command line' do
expect(exit_code).to eq(1)
end
end

context 'when not explicitly included' do
it 'does not include the file given on the command line' do
expect(exit_code).to eq(0)
end

context 'but option is not given' do
it 'includes the file given on the command line' do
expect(cli.run([target_file])).to eq(1)
end
end
end
end

describe '--stdin' do
it 'causes source code to be read from stdin' do
begin
Expand Down
3 changes: 3 additions & 0 deletions spec/rubocop/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def abs(path)
--force-exclusion Force excluding files specified in the
configuration `Exclude` even if they are
explicitly passed as arguments.
--only-recognized-file-types Inspect files given on the command line only if
they are listed in AllCops/Include parameters
of user configuration or default configuration.
--ignore-parent-exclusion Prevent from inheriting AllCops/Exclude from
parent folders.
--force-default-config Use default configuration even if configuration
Expand Down

0 comments on commit 75a6984

Please sign in to comment.