Skip to content

Commit

Permalink
Merge pull request #7930 from jonas054/4245_arbitrary_files_on_comman…
Browse files Browse the repository at this point in the history
…d_line

[Fix #4245] Arbitrary files on command line
  • Loading branch information
jonas054 committed May 9, 2020
2 parents db6d1d0 + 75a6984 commit 75542a8
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,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
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
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)
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
10 changes: 6 additions & 4 deletions lib/rubocop/target_finder.rb
Expand Up @@ -27,7 +27,7 @@ def fail_fast?
# (if any). If args is empty, recursively find all Ruby source
# files under the current directory
# @return [Array] array of file paths
def find(args)
def find(args, mode)
return target_files_in_dir if args.empty?

files = []
Expand All @@ -36,7 +36,7 @@ def find(args)
files += if File.directory?(arg)
target_files_in_dir(arg.chomp(File::SEPARATOR))
else
process_explicit_path(arg)
process_explicit_path(arg, mode)
end
end

Expand Down Expand Up @@ -169,10 +169,12 @@ def included_file?(file)
ruby_file?(file) || configured_include?(file)
end

def process_explicit_path(path)
def process_explicit_path(path, mode)
files = path.include?('*') ? Dir[path] : [path]

files.select! { |file| included_file?(file) }
if mode == :only_recognized_file_types || force_exclusion?
files.select! { |file| included_file?(file) }
end

return files unless force_exclusion?

Expand Down
1 change: 1 addition & 0 deletions manual/basic_usage.md
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
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
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 75542a8

Please sign in to comment.