Skip to content

Commit

Permalink
Merge pull request rubocop#2184 from maxjacobson/add-list-target-file…
Browse files Browse the repository at this point in the history
…s-option

Add a --list-target-files option
  • Loading branch information
bbatsov committed Aug 29, 2015
2 parents a584c06 + a638163 commit 6c6a52b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
19 changes: 10 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-05-23 07:26:48 +0200 using RuboCop version 0.31.0.
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2015-08-28 10:37:50 -0400 using RuboCop version 0.33.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 160
# Offense count: 178
Metrics/AbcSize:
Max: 32

# Offense count: 13
# Offense count: 15
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 144
Max: 150

# Offense count: 42
# Offense count: 50
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 160
# Offense count: 184
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 18

# Offense count: 7
# Offense count: 9
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 164

# Offense count: 23
# Offense count: 29
Metrics/PerceivedComplexity:
Max: 11
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* New cop `Lint/DuplicatedKey` checks for duplicated keys in hashes, which Ruby 2.2 warns against. ([@sliuu][])
* [#2106](https://github.com/bbatsov/rubocop/issues/2106): Add `SuspiciousParamNames` option to `Style/OptionHash`. ([@wli][])
* [#2193](https://github.com/bbatsov/rubocop/pull/2193): `Style/Next` supports more `Enumerable` methods. ([@rrosenblum][])
* [#2179](https://github.com/bbatsov/rubocop/issues/2179): Add `--list-target-files` option to CLI, which prints the files which will be inspected. ([@maxjacobson][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Command flag | Description
--------------------------|------------------------------------------------------------
`-v/--version` | Displays the current version and exits.
`-V/--verbose-version` | Displays the current version plus the version of Parser and Ruby.
`-L/--list-target-files` | List all files RuboCop will inspect.
`-F/--fail-fast` | Inspects in modification time order and stops after first file with offenses.
`-d/--debug` | Displays some extra debug output.
`-D/--display-cop-names` | Displays cop names in offense messages.
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def define_options(args)
OptionParser.new do |opts|
opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'

add_list_options(opts)
add_only_options(opts)
add_configuration_options(opts, args)
add_formatting_options(opts)
Expand Down Expand Up @@ -140,6 +141,10 @@ def add_boolean_flags(opts)
option(opts, '-s', '--stdin') { @options[:stdin] = $stdin.read }
end

def add_list_options(opts)
option(opts, '-L', '--list-target-files')
end

# Sets a value in the @options hash, based on the given long option and its
# value, in addition to calling the block if a block is given.
def option(opts, *args)
Expand Down Expand Up @@ -232,6 +237,7 @@ module OptionsHelp
display_style_guide: 'Display style guide URLs in offense messages.',
rails: 'Run extra Rails cops.',
lint: 'Run only lint cops.',
list_target_files: 'List all files RuboCop will inspect.',
auto_correct: 'Auto-correct offenses.',
no_color: 'Disable color output.',
version: 'Display version.',
Expand Down
12 changes: 11 additions & 1 deletion lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def initialize(options, config_store)

def run(paths)
target_files = find_target_files(paths)
inspect_files(target_files)
if @options[:list_target_files]
list_files(target_files)
else
inspect_files(target_files)
end
end

def abort
Expand Down Expand Up @@ -62,6 +66,12 @@ def inspect_files(files)
formatter_set.close_output_files
end

def list_files(paths)
paths.each do |path|
puts PathUtil.relative_path(path)
end
end

def process_file(file)
puts "Scanning #{file}" if @options[:debug]

Expand Down
50 changes: 50 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@ def abs(path)
end

describe 'option' do
describe '--list-target-files' do
context 'when there are no files' do
it 'prints nothing with -L' do
cli.run ['-L']
expect($stdout.string).to be_empty
end

it 'prints nothing with --list-target-files' do
cli.run ['--list-target-files']
expect($stdout.string).to be_empty
end
end

context 'when there are some files' do
before(:each) do
create_file('show.rabl', 'object @user => :person')
create_file('app.rb', 'puts "hello world"')
create_file('Gemfile', ['source "https://rubygems.org"',
'gem "rubocop"'])
create_file('lib/helper.rb', 'puts "helpful"')
end

context 'when there are no includes or excludes' do
it 'prints known ruby files' do
cli.run ['-L']
expect($stdout.string.split("\n")).to match_array ['app.rb',
'Gemfile',
'lib/helper.rb']
end
end

context 'when there is an include and exclude' do
before(:each) do
create_file('.rubocop.yml', ['AllCops:',
' Exclude:',
' - Gemfile',
' Include:',
' - "**/*.rabl"'])
end

it 'prints the included files and not the excluded ones' do
cli.run ['--list-target-files']
expect($stdout.string.split("\n")).to match_array ['app.rb',
'lib/helper.rb',
'show.rabl']
end
end
end
end

describe '--version' do
it 'exits cleanly' do
expect { cli.run ['-v'] }.to exit_with_code(0)
Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def abs(path)
# rubocop:disable Metrics/LineLength
expected_help = <<-END
Usage: rubocop [options] [file1, file2, ...]
-L, --list-target-files List all files RuboCop will inspect.
--except [COP1,COP2,...] Disable the given cop(s).
--only [COP1,COP2,...] Run only the given cop(s).
--only-guide-cops Run only cops for rules that link to a
Expand Down

0 comments on commit 6c6a52b

Please sign in to comment.